Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 61 additions & 11 deletions Sources/XToolSupport/SDKBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
let output: URL
let arch: Arch

func buildSDK() async throws {

Check warning on line 59 in Sources/XToolSupport/SDKBuilder.swift

View workflow job for this annotation

GitHub Actions / swiftlint

Function body should span 100 lines or less excluding comments and whitespace: currently spans 113 lines (function_body_length)
// TODO: store relevant info for staleness check
let sdkVersion = "develop"

Expand Down Expand Up @@ -136,6 +136,9 @@
"linker": {
"path": "ld64.lld"
},
"librarian": {
"path": "llvm-lib"
},
"swiftCompiler": {
"extraCLIOptions": [
"-Xfrontend", "-enable-cross-import-overlays",
Expand All @@ -149,6 +152,24 @@
encoding: .utf8
)

// this toolset works with the swiftbuild system
try """
{
"schemaVersion": "1.0",
"rootPath": "toolset/bin",
"linker": {
"path": "ld"
},
"librarian": {
"path": "llvm-lib"
}
}
""".write(
to: output.appendingPathComponent("toolset-swb.json"),
atomically: false,
encoding: .utf8
)

let sdkDefinition = SDKDefinition(
schemaVersion: "4.0",
targetTriples: [
Expand Down Expand Up @@ -215,6 +236,16 @@
try await input.finish()
}
.checkSuccess()

// swift-build assumes we have an Apple-flavored librarian for Apple platforms
// (https://github.com/swiftlang/swift-build/blob/b2433e74e/Sources/SWBCore/SpecImplementations/Tools/LinkerTools.swift#L1735)
// but allows us to override this assumption if we explicitly provide the name llvm-lib (look a few lines above in that file)
try FileManager.default.moveItem(
at: toolsetDir.appending(path: "bin/libtool"),
to: toolsetDir.appending(path: "bin/llvm-lib")
)

// TODO: install cctools ld as well
}

// swiftlint:disable:next cyclomatic_complexity
Expand Down Expand Up @@ -321,10 +352,11 @@

for platform in ["iPhoneOS", "MacOSX", "iPhoneSimulator"] {
let lib = "../../../../../Library"
let dest = dev.appendingPathComponent("""
Platforms/\(platform).platform/Developer/SDKs/\(platform).sdk\
/System/Library/Frameworks
""").path
let sdkDir = dev.appending(path: "Platforms/\(platform).platform/Developer/SDKs/\(platform).sdk")

try patchSDKSettings(path: sdkDir.appending(path: "SDKSettings.plist"))

let dest = sdkDir.appending(path: "System/Library/Frameworks").path

try FileManager.default.createSymbolicLink(
atPath: "\(dest)/Testing.framework",
Expand Down Expand Up @@ -354,6 +386,21 @@
return dev
}

private func patchSDKSettings(path: URL) throws {
let data = try Data(contentsOf: path)
guard var plist = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any] else {
throw Console.Error("Could not read SDKSettings.plist at '\(path.path)'")
}

var customProperties = (plist["CustomProperties"] as? [String: Any]) ?? [:]
customProperties["SWIFT_RESOURCE_DIR"] = "$(PLATFORM_DIR)/../../Toolchains/XcodeDefault.xctoolchain/usr/lib/swift"
customProperties["CLANG_RESOURCE_DIR"] = "$(SYSTEM_DEVELOPER_USR_DIR)/lib/swift/clang"
plist["CustomProperties"] = customProperties

let newData = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0)
try newData.write(to: path)
}

// returns the number of files we actually want to keep,
// useful for computing progress % during fs traversal
private func extractXIP(inputPath: String, outDir: String) async throws -> Int {
Expand Down Expand Up @@ -497,13 +544,16 @@
E("clang"),
]),
E("Platforms", ["iPhoneOS", "MacOSX", "iPhoneSimulator"].map {
E("\($0).platform/Developer", [
E("SDKs"),
E("Library", [
E("Frameworks"),
E("PrivateFrameworks"),
]),
E("usr/lib"),
E("\($0).platform", [
E("Info.plist"),
E("Developer", [
E("SDKs"),
E("Library", [
E("Frameworks"),
E("PrivateFrameworks"),
]),
E("usr/lib"),
])
])
}),
])
Expand Down
Loading