From e61156afaad1ad3ad4c10c2e174b5981221a5501 Mon Sep 17 00:00:00 2001 From: abdelrahman fahmy <104788132+Apdelrahman1911@users.noreply.github.com> Date: Sat, 5 Dec 2026 19:26:15 +0000 Subject: [PATCH 1/2] fix(host,kmp): fix Linux swiftc->clang->ld64.lld platform_version & disable K/N DebugTransparentStepping on Linux Debug --- .../GradleFrameworkProducer.swift | 82 +++++++++++++++++++ Sources/PackLib/OriginalHostCompiler.swift | 5 +- .../XToolTests/OptionAOriginalHostTests.swift | 26 ++++++ Tests/XToolTests/SDKBuilderTests.swift | 26 ++++++ 4 files changed, 138 insertions(+), 1 deletion(-) diff --git a/Sources/KMPBuildSupport/GradleFrameworkProducer.swift b/Sources/KMPBuildSupport/GradleFrameworkProducer.swift index 1b90620b..b6d86cad 100644 --- a/Sources/KMPBuildSupport/GradleFrameworkProducer.swift +++ b/Sources/KMPBuildSupport/GradleFrameworkProducer.swift @@ -136,6 +136,9 @@ public struct GradleFrameworkProducer: Sendable { args.append(contentsOf: extra) } + let defaults = Self.frameworkPropertyDefaults(configuration: configuration, arguments: args, projectRoot: projectRoot) + args.insert(contentsOf: defaults, at: 0) + try await Subprocess.run( .path(FilePath(wrapperURL.path)), arguments: .init(args), @@ -176,6 +179,85 @@ public struct GradleFrameworkProducer: Sendable { ) } + static func frameworkPropertyDefaults( + configuration: BuildConfiguration, arguments: [String], projectRoot: URL + ) -> [String] { + #if os(Linux) + if configuration == .debug { return debugSteppingDefault(arguments: arguments, projectRoot: projectRoot) } + #endif + return [] + } + + // -P overrides gradle.properties, environment and system properties. Supply it only as a default. + static func debugSteppingDefault( + arguments: [String], projectRoot: URL, environment: [String: String] = ProcessInfo.processInfo.environment + ) -> [String] { + let key = "kotlin.native.binary.enableDebugTransparentStepping" + let systemKey = "org.gradle.project." + key + let prefixes = [ + "-P", "--project-prop=", "", "-Dorg.gradle.project.", "--system-prop=org.gradle.project.", "org.gradle.project.", + ] + if arguments.contains(where: { arg in + prefixes.contains { arg == $0 + key || arg.hasPrefix($0 + key + "=") } + }) { return [] } + if environment["ORG_GRADLE_PROJECT_" + key] != nil { return [] } + let jvmOptions = ["GRADLE_OPTS", "JAVA_OPTS", "JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "JDK_JAVA_OPTIONS"] + if jvmOptions.contains(where: { environment[$0]?.contains(systemKey) == true }) { return [] } + + let dirs = propertyDirectories(arguments: arguments, projectRoot: projectRoot, environment: environment) + for dir in dirs { + for filename in ["gradle.properties", "local.properties"] { + if declaresProperty(key, in: dir.appendingPathComponent(filename)) { return [] } + } + } + return ["-P" + key + "=false"] + } + + private static func propertyDirectories(arguments: [String], projectRoot: URL, environment: [String: String]) -> [URL] { + var project = projectRoot + let defaultHome = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".gradle").path + var userHome = environment["GRADLE_USER_HOME"] ?? defaultHome + for (index, arg) in arguments.enumerated() { + func value(short: String, long: String) -> String? { + if arg == short || arg == long { return arguments.dropFirst(index + 1).first } + if arg.hasPrefix(long + "=") { return String(arg.dropFirst(long.count + 1)) } + if arg.hasPrefix(short), arg != short { return String(arg.dropFirst(short.count)) } + return nil + } + if let path = value(short: "-g", long: "--gradle-user-home") { userHome = path } + if let path = value(short: "-p", long: "--project-dir") { + project = URL(fileURLWithPath: path, isDirectory: true, relativeTo: projectRoot).standardizedFileURL + } + } + var dirs = [URL(fileURLWithPath: userHome, isDirectory: true, relativeTo: projectRoot)] + if let installation = environment["GRADLE_HOME"] { dirs.append(URL(fileURLWithPath: installation)) } + while true { + dirs.append(project) + let parent = project.deletingLastPathComponent().standardizedFileURL + if project.path == "/" || parent.path == project.path { break } + if ["settings.gradle", "settings.gradle.kts"].contains(where: { + FileManager.default.fileExists(atPath: project.appendingPathComponent($0).path) + }) { break } + project = parent + } + return dirs + } + + private static func declaresProperty(_ key: String, in file: URL) -> Bool { + guard FileManager.default.fileExists(atPath: file.path) else { return false } + guard let data = try? Data(contentsOf: file) else { return true } + let systemKey = "org.gradle.project." + key + let lines = String(decoding: data, as: UTF8.self).split(whereSeparator: \.isNewline) + return lines.contains { line in + let text = line.drop(while: { $0.isWhitespace }) + if text.hasPrefix("#") || text.hasPrefix("!") { return false } + let name = text.prefix { !$0.isWhitespace && $0 != "=" && $0 != ":" } + // An escaped Java property name may encode or continue the key. Leave its interpretation to Gradle. + if name.contains("\\") { return true } + return name == key || name == "systemProp." + systemKey || (name == "org.gradle.jvmargs" && text.contains(systemKey)) + } + } + private func verifyMaterializedFramework( at frameworkURL: URL, name: String, diff --git a/Sources/PackLib/OriginalHostCompiler.swift b/Sources/PackLib/OriginalHostCompiler.swift index c09c2c95..3373aef5 100644 --- a/Sources/PackLib/OriginalHostCompiler.swift +++ b/Sources/PackLib/OriginalHostCompiler.swift @@ -115,7 +115,10 @@ public struct OriginalHostCompiler: Sendable { if let toolsetBin = request.destination.toolsetBinPath { let lld = URL(fileURLWithPath: toolsetBin).appendingPathComponent("ld64.lld") if FileManager.default.fileExists(atPath: lld.path) { - args += ["-use-ld=\(lld.path)"] + // Clang needs the linker flavor as well as the path to the SDK trampoline. + // Darwin SDK metadata lookup in clang uses -isysroot, while swiftc forwards -sdk as --sysroot. + args += ["-Xclang-linker", "-isysroot", "-Xclang-linker", sdkRoot] + args += ["-use-ld=lld", "-ld-path=\(lld.path)"] } } let sdkSwiftLib = URL(fileURLWithPath: sdkRoot).appendingPathComponent("usr/lib/swift") diff --git a/Tests/XToolTests/OptionAOriginalHostTests.swift b/Tests/XToolTests/OptionAOriginalHostTests.swift index 888c5cc8..c88fcc90 100644 --- a/Tests/XToolTests/OptionAOriginalHostTests.swift +++ b/Tests/XToolTests/OptionAOriginalHostTests.swift @@ -110,6 +110,32 @@ import Yams } } +@Test func linuxDebugSteppingDefaultRespectsGradleOverrides() throws { + let root = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString, directoryHint: .isDirectory) + try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: root) } + try Data().write(to: root.appending(path: "settings.gradle")) + let key = "kotlin.native.binary.enableDebugTransparentStepping" + func flags(_ args: [String] = [], _ env: [String: String] = [:]) -> [String] { + let environment = ["GRADLE_USER_HOME": root.path].merging(env) { _, new in new } + return GradleFrameworkProducer.debugSteppingDefault(arguments: args, projectRoot: root, environment: environment) + } + #expect(flags(["-P" + key + "Extra=true"]) == ["-P" + key + "=false"]) + for args in [["-P" + key + "=true"], ["--project-prop", key + "=false"], ["-Dorg.gradle.project." + key + "=true"]] { + #expect(flags(args).isEmpty) + } + #expect(flags([], ["ORG_GRADLE_PROJECT_" + key: "true"]).isEmpty) + #expect(GradleFrameworkProducer.frameworkPropertyDefaults(configuration: .release, arguments: [], projectRoot: root).isEmpty) + #if os(macOS) + #expect(GradleFrameworkProducer.frameworkPropertyDefaults(configuration: .debug, arguments: [], projectRoot: root).isEmpty) + #endif + let props = root.appending(path: "gradle.properties") + try Data(("# " + key + "=true\n").utf8).write(to: props) + #expect(flags() == ["-P" + key + "=false"]) + try Data((key + " : true\n").utf8).write(to: props) + #expect(flags().isEmpty) +} + @Test func gradleFrameworkProducerResolvesDefaultTasksAndOutputs() throws { let sim = try AppleBuildDestination.resolve(triple: "arm64-apple-ios-simulator", minimumOS: "16.0") let dev = try AppleBuildDestination.resolve(triple: "arm64-apple-ios", minimumOS: "16.0") diff --git a/Tests/XToolTests/SDKBuilderTests.swift b/Tests/XToolTests/SDKBuilderTests.swift index 0d73b591..3f8e110d 100644 --- a/Tests/XToolTests/SDKBuilderTests.swift +++ b/Tests/XToolTests/SDKBuilderTests.swift @@ -30,6 +30,32 @@ struct SDKBuilderTests { #expect(destination == "../../../../../Library/Frameworks/XCTest.framework") } + @Test(arguments: ["arm64-apple-ios", "arm64-apple-ios-simulator"]) + func originalHostSelectsBundledMachOLinker(triple: String) throws { + let fixture = try Fixture() + defer { fixture.remove() } + let bin = fixture.output.appending(path: "toolset/bin") + try FileManager.default.createDirectory(at: bin, withIntermediateDirectories: true) + let sdk = try #require(DarwinSDK(bundle: fixture.output)) + let destination = try AppleBuildDestination.resolve(triple: triple, minimumOS: "16.0", darwinSDK: sdk) + let request = OriginalHostCompiler.Request( + moduleName: "Host", executableName: "Host", destination: destination, + configuration: .release, swiftSources: [fixture.root.appending(path: "Host.swift")], + outputExecutableURL: fixture.root.appending(path: "Host") + ) + func arguments() -> [String] { + OriginalHostCompiler.buildSwiftcArguments(for: request, sdkRoot: fixture.root.path, objcObjects: []) + } + #expect(!arguments().contains { $0.hasPrefix("-use-ld=") || $0.hasPrefix("-ld-path=") || $0 == "-Xclang-linker" }) + let linker = bin.appending(path: "ld64.lld") + try Data().write(to: linker) + let args = arguments() + let selection = args.filter { $0.hasPrefix("-use-ld=") || $0.hasPrefix("-ld-path=") } + #expect(selection == ["-use-ld=lld", "-ld-path=\(linker.path)"]) + let forwarded = zip(args, args.dropFirst()).filter { $0.0 == "-Xclang-linker" }.map { $0.1 } + #expect(forwarded == ["-isysroot", fixture.root.path]) + } + @Test func originalHostUsesSlimSDKSwiftResources() async throws { let fixture = try Fixture() defer { fixture.remove() } From 3eb400c33c4801f8a453157acd1a3fff2966eda4 Mon Sep 17 00:00:00 2001 From: abdelrahman fahmy <104788132+Apdelrahman1911@users.noreply.github.com> Date: Sat, 5 Dec 2026 19:32:42 +0000 Subject: [PATCH 2/2] fix(host,kmp): respect custom linker flags and all Gradle override forms per review --- .../GradleFrameworkProducer.swift | 4 +++- Sources/PackLib/OriginalHostCompiler.swift | 8 ++++++- .../XToolTests/OptionAOriginalHostTests.swift | 13 ++++++++++- Tests/XToolTests/SDKBuilderTests.swift | 22 +++++++++++++------ 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/Sources/KMPBuildSupport/GradleFrameworkProducer.swift b/Sources/KMPBuildSupport/GradleFrameworkProducer.swift index b6d86cad..41008896 100644 --- a/Sources/KMPBuildSupport/GradleFrameworkProducer.swift +++ b/Sources/KMPBuildSupport/GradleFrameworkProducer.swift @@ -195,7 +195,8 @@ public struct GradleFrameworkProducer: Sendable { let key = "kotlin.native.binary.enableDebugTransparentStepping" let systemKey = "org.gradle.project." + key let prefixes = [ - "-P", "--project-prop=", "", "-Dorg.gradle.project.", "--system-prop=org.gradle.project.", "org.gradle.project.", + "-P", "-P=", "--project-prop=", "", + "-Dorg.gradle.project.", "-D=org.gradle.project.", "--system-prop=org.gradle.project.", "org.gradle.project.", ] if arguments.contains(where: { arg in prefixes.contains { arg == $0 + key || arg.hasPrefix($0 + key + "=") } @@ -221,6 +222,7 @@ public struct GradleFrameworkProducer: Sendable { func value(short: String, long: String) -> String? { if arg == short || arg == long { return arguments.dropFirst(index + 1).first } if arg.hasPrefix(long + "=") { return String(arg.dropFirst(long.count + 1)) } + if arg.hasPrefix(short + "=") { return String(arg.dropFirst(short.count + 1)) } if arg.hasPrefix(short), arg != short { return String(arg.dropFirst(short.count)) } return nil } diff --git a/Sources/PackLib/OriginalHostCompiler.swift b/Sources/PackLib/OriginalHostCompiler.swift index 3373aef5..2a4fdab3 100644 --- a/Sources/PackLib/OriginalHostCompiler.swift +++ b/Sources/PackLib/OriginalHostCompiler.swift @@ -118,7 +118,13 @@ public struct OriginalHostCompiler: Sendable { // Clang needs the linker flavor as well as the path to the SDK trampoline. // Darwin SDK metadata lookup in clang uses -isysroot, while swiftc forwards -sdk as --sysroot. args += ["-Xclang-linker", "-isysroot", "-Xclang-linker", sdkRoot] - args += ["-use-ld=lld", "-ld-path=\(lld.path)"] + let selectors = ["-use-ld", "-ld-path", "--ld-path", "-fuse-ld"] + let hasCustomLinker = request.extraSwiftFlags.contains { flag in + selectors.contains { flag == $0 || flag.hasPrefix($0 + "=") } + } + if !hasCustomLinker { + args += ["-use-ld=lld", "-ld-path=\(lld.path)"] + } } } let sdkSwiftLib = URL(fileURLWithPath: sdkRoot).appendingPathComponent("usr/lib/swift") diff --git a/Tests/XToolTests/OptionAOriginalHostTests.swift b/Tests/XToolTests/OptionAOriginalHostTests.swift index c88fcc90..3f2b8c9b 100644 --- a/Tests/XToolTests/OptionAOriginalHostTests.swift +++ b/Tests/XToolTests/OptionAOriginalHostTests.swift @@ -121,7 +121,13 @@ import Yams return GradleFrameworkProducer.debugSteppingDefault(arguments: args, projectRoot: root, environment: environment) } #expect(flags(["-P" + key + "Extra=true"]) == ["-P" + key + "=false"]) - for args in [["-P" + key + "=true"], ["--project-prop", key + "=false"], ["-Dorg.gradle.project." + key + "=true"]] { + let prop = key + "=true" + let systemProp = "org.gradle.project." + prop + for args in [ + ["-P" + prop], ["-P=" + prop], ["-P", prop], ["--project-prop", prop], ["--project-prop=" + prop], + ["-D" + systemProp], ["-D=" + systemProp], ["-D", systemProp], + ["--system-prop", systemProp], ["--system-prop=" + systemProp], + ] { #expect(flags(args).isEmpty) } #expect(flags([], ["ORG_GRADLE_PROJECT_" + key: "true"]).isEmpty) @@ -129,6 +135,11 @@ import Yams #if os(macOS) #expect(GradleFrameworkProducer.frameworkPropertyDefaults(configuration: .debug, arguments: [], projectRoot: root).isEmpty) #endif + let home = root.appending(path: "custom-home", directoryHint: .isDirectory) + try FileManager.default.createDirectory(at: home, withIntermediateDirectories: true) + try Data(prop.utf8).write(to: home.appending(path: "gradle.properties")) + #expect(flags(["-g=" + home.path]).isEmpty) + let props = root.appending(path: "gradle.properties") try Data(("# " + key + "=true\n").utf8).write(to: props) #expect(flags() == ["-P" + key + "=false"]) diff --git a/Tests/XToolTests/SDKBuilderTests.swift b/Tests/XToolTests/SDKBuilderTests.swift index 3f8e110d..8888e2a4 100644 --- a/Tests/XToolTests/SDKBuilderTests.swift +++ b/Tests/XToolTests/SDKBuilderTests.swift @@ -38,13 +38,13 @@ struct SDKBuilderTests { try FileManager.default.createDirectory(at: bin, withIntermediateDirectories: true) let sdk = try #require(DarwinSDK(bundle: fixture.output)) let destination = try AppleBuildDestination.resolve(triple: triple, minimumOS: "16.0", darwinSDK: sdk) - let request = OriginalHostCompiler.Request( - moduleName: "Host", executableName: "Host", destination: destination, - configuration: .release, swiftSources: [fixture.root.appending(path: "Host.swift")], - outputExecutableURL: fixture.root.appending(path: "Host") - ) - func arguments() -> [String] { - OriginalHostCompiler.buildSwiftcArguments(for: request, sdkRoot: fixture.root.path, objcObjects: []) + func arguments(_ flags: [String] = []) -> [String] { + let request = OriginalHostCompiler.Request( + moduleName: "Host", executableName: "Host", destination: destination, + configuration: .release, swiftSources: [fixture.root.appending(path: "Host.swift")], + extraSwiftFlags: flags, outputExecutableURL: fixture.root.appending(path: "Host") + ) + return OriginalHostCompiler.buildSwiftcArguments(for: request, sdkRoot: fixture.root.path, objcObjects: []) } #expect(!arguments().contains { $0.hasPrefix("-use-ld=") || $0.hasPrefix("-ld-path=") || $0 == "-Xclang-linker" }) let linker = bin.appending(path: "ld64.lld") @@ -54,6 +54,14 @@ struct SDKBuilderTests { #expect(selection == ["-use-ld=lld", "-ld-path=\(linker.path)"]) let forwarded = zip(args, args.dropFirst()).filter { $0.0 == "-Xclang-linker" }.map { $0.1 } #expect(forwarded == ["-isysroot", fixture.root.path]) + for flags in [ + ["-use-ld=/custom/ld"], ["-ld-path=/custom/ld"], + ["-Xclang-linker", "--ld-path=/custom/ld"], ["-Xclang-linker", "-fuse-ld=custom"], + ] { + let custom = arguments(flags) + #expect(flags.allSatisfy { custom.contains($0) }) + #expect(selection.allSatisfy { !custom.contains($0) }) + } } @Test func originalHostUsesSlimSDKSwiftResources() async throws {