Skip to content

Commit de61b85

Browse files
committed
SWBBuild SDK support
1 parent ce94b9e commit de61b85

1 file changed

Lines changed: 61 additions & 11 deletions

File tree

Sources/XToolSupport/SDKBuilder.swift

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ struct SDKBuilder {
136136
"linker": {
137137
"path": "ld64.lld"
138138
},
139+
"librarian": {
140+
"path": "llvm-lib"
141+
},
139142
"swiftCompiler": {
140143
"extraCLIOptions": [
141144
"-Xfrontend", "-enable-cross-import-overlays",
@@ -149,6 +152,24 @@ struct SDKBuilder {
149152
encoding: .utf8
150153
)
151154

155+
// this toolset works with the swiftbuild system
156+
try """
157+
{
158+
"schemaVersion": "1.0",
159+
"rootPath": "toolset/bin",
160+
"linker": {
161+
"path": "ld"
162+
},
163+
"librarian": {
164+
"path": "llvm-lib"
165+
}
166+
}
167+
""".write(
168+
to: output.appendingPathComponent("toolset-swb.json"),
169+
atomically: false,
170+
encoding: .utf8
171+
)
172+
152173
let sdkDefinition = SDKDefinition(
153174
schemaVersion: "4.0",
154175
targetTriples: [
@@ -215,6 +236,16 @@ struct SDKBuilder {
215236
try await input.finish()
216237
}
217238
.checkSuccess()
239+
240+
// swift-build assumes we have an Apple-flavored librarian for Apple platforms
241+
// (https://github.com/swiftlang/swift-build/blob/b2433e74e/Sources/SWBCore/SpecImplementations/Tools/LinkerTools.swift#L1735)
242+
// but allows us to override this assumption if we explicitly provide the name llvm-lib (look a few lines above in that file)
243+
try FileManager.default.moveItem(
244+
at: toolsetDir.appending(path: "bin/libtool"),
245+
to: toolsetDir.appending(path: "bin/llvm-lib")
246+
)
247+
248+
// TODO: install cctools ld as well
218249
}
219250

220251
// swiftlint:disable:next cyclomatic_complexity
@@ -321,10 +352,11 @@ struct SDKBuilder {
321352

322353
for platform in ["iPhoneOS", "MacOSX", "iPhoneSimulator"] {
323354
let lib = "../../../../../Library"
324-
let dest = dev.appendingPathComponent("""
325-
Platforms/\(platform).platform/Developer/SDKs/\(platform).sdk\
326-
/System/Library/Frameworks
327-
""").path
355+
let sdkDir = dev.appending(path: "Platforms/\(platform).platform/Developer/SDKs/\(platform).sdk")
356+
357+
try patchSDKSettings(path: sdkDir.appending(path: "SDKSettings.plist"))
358+
359+
let dest = sdkDir.appending(path: "System/Library/Frameworks").path
328360

329361
try FileManager.default.createSymbolicLink(
330362
atPath: "\(dest)/Testing.framework",
@@ -354,6 +386,21 @@ struct SDKBuilder {
354386
return dev
355387
}
356388

389+
private func patchSDKSettings(path: URL) throws {
390+
let data = try Data(contentsOf: path)
391+
guard var plist = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any] else {
392+
throw Console.Error("Could not read SDKSettings.plist at '\(path.path)'")
393+
}
394+
395+
var customProperties = (plist["CustomProperties"] as? [String: Any]) ?? [:]
396+
customProperties["SWIFT_RESOURCE_DIR"] = "$(PLATFORM_DIR)/../../Toolchains/XcodeDefault.xctoolchain/usr/lib/swift"
397+
customProperties["CLANG_RESOURCE_DIR"] = "$(SYSTEM_DEVELOPER_USR_DIR)/lib/swift/clang"
398+
plist["CustomProperties"] = customProperties
399+
400+
let newData = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0)
401+
try newData.write(to: path)
402+
}
403+
357404
// returns the number of files we actually want to keep,
358405
// useful for computing progress % during fs traversal
359406
private func extractXIP(inputPath: String, outDir: String) async throws -> Int {
@@ -497,13 +544,16 @@ struct SDKEntry {
497544
E("clang"),
498545
]),
499546
E("Platforms", ["iPhoneOS", "MacOSX", "iPhoneSimulator"].map {
500-
E("\($0).platform/Developer", [
501-
E("SDKs"),
502-
E("Library", [
503-
E("Frameworks"),
504-
E("PrivateFrameworks"),
505-
]),
506-
E("usr/lib"),
547+
E("\($0).platform", [
548+
E("Info.plist"),
549+
E("Developer", [
550+
E("SDKs"),
551+
E("Library", [
552+
E("Frameworks"),
553+
E("PrivateFrameworks"),
554+
]),
555+
E("usr/lib"),
556+
])
507557
])
508558
}),
509559
])

0 commit comments

Comments
 (0)