Skip to content

Commit f6304a0

Browse files
authored
Merge pull request #78194 from hamishknight/xcodegen-runtimes
[xcodegen] Add support for new 'Runtimes' build
2 parents b64c019 + 5408b16 commit f6304a0

File tree

6 files changed

+76
-15
lines changed

6 files changed

+76
-15
lines changed

utils/swift-xcodegen/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ PROJECT CONFIGURATION:
105105
unfortunately means some Clang targes such as 'lib/Basic' and 'stdlib'
106106
cannot currently use buildable folders. (default: --buildable-folders)
107107
108+
--runtimes-build-dir <runtimes-build-dir>
109+
Experimental: The path to a build directory for the new 'Runtimes/'
110+
stdlib CMake build. This creates a separate 'SwiftRuntimes' project, along
111+
with a 'Swift+Runtimes' workspace.
112+
113+
Note: This requires passing '-DCMAKE_EXPORT_COMPILE_COMMANDS=YES' to
114+
CMake.
115+
108116
MISC:
109117
--project-root-dir <project-root-dir>
110118
The project root directory, which is the parent directory of the Swift repo.

utils/swift-xcodegen/Sources/SwiftXcodeGen/Ninja/NinjaBuildDir.swift

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,30 @@
1313
public final class NinjaBuildDir: Sendable {
1414
public let path: AbsolutePath
1515
public let projectRootDir: AbsolutePath
16-
public let tripleSuffix: String
16+
private let _tripleSuffix: Result<String, Error>
1717

1818
private let repoBuildDirs = MutexBox<[Repo: RepoBuildDir]>()
1919

2020
private static func detectTripleSuffix(
2121
buildDir: AbsolutePath
22-
) throws -> String {
23-
for dir in try buildDir.getDirContents() {
24-
guard buildDir.appending(dir).isDirectory,
25-
let triple = dir.fileName.tryDropPrefix("swift-") else {
26-
continue
22+
) -> Result<String, Error> {
23+
Result {
24+
for dir in try buildDir.getDirContents() {
25+
guard buildDir.appending(dir).isDirectory,
26+
let triple = dir.fileName.tryDropPrefix("swift-") else {
27+
continue
28+
}
29+
return triple
2730
}
28-
return triple
31+
let couldBeParent = buildDir.fileName.hasPrefix("swift-")
32+
throw XcodeGenError.noSwiftBuildDir(buildDir, couldBeParent: couldBeParent)
33+
}
34+
}
35+
36+
public var tripleSuffix: String {
37+
get throws {
38+
try _tripleSuffix.get()
2939
}
30-
let couldBeParent = buildDir.fileName.hasPrefix("swift-")
31-
throw XcodeGenError.noSwiftBuildDir(buildDir, couldBeParent: couldBeParent)
3240
}
3341

3442
// We can infer the project root from the location of swift-xcodegen itself.
@@ -51,7 +59,7 @@ public final class NinjaBuildDir: Sendable {
5159
throw XcodeGenError.pathNotFound(path)
5260
}
5361
self.path = path
54-
self.tripleSuffix = try Self.detectTripleSuffix(buildDir: path)
62+
self._tripleSuffix = Self.detectTripleSuffix(buildDir: path)
5563
self.projectRootDir = try projectRootDir ?? Self.detectProjectRoot()
5664
}
5765

utils/swift-xcodegen/Sources/SwiftXcodeGen/Ninja/RepoBuildDir.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public final class RepoBuildDir: Sendable {
2727
init(_ repo: Repo, for parent: NinjaBuildDir) throws {
2828
self.projectRootDir = parent.projectRootDir
2929
self.repo = repo
30-
self.path = parent.path.appending(
31-
"\(repo.buildDirPrefix)-\(parent.tripleSuffix)"
32-
)
30+
self.path = try repo.buildDirPrefix.map { prefix in
31+
parent.path.appending("\(prefix)-\(try parent.tripleSuffix)")
32+
} ?? parent.path
3333
self.repoRelativePath = repo.relativePath
3434
self.repoPath = projectRootDir.appending(repo.relativePath)
3535
self.repoDirCache = DirectoryCache(root: repoPath)

utils/swift-xcodegen/Sources/SwiftXcodeGen/Repo.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,25 @@
1313
// TODO: This really ought to be defined in swift-xcodegen
1414
public enum Repo: CaseIterable, Sendable {
1515
case swift
16+
case swiftRuntimes
1617
case lldb
1718
case llvm
1819
case cmark
1920

2021
public var relativePath: RelativePath {
2122
switch self {
2223
case .swift: "swift"
24+
case .swiftRuntimes: "swift/Runtimes"
2325
case .cmark: "cmark"
2426
case .lldb: "llvm-project/lldb"
2527
case .llvm: "llvm-project"
2628
}
2729
}
2830

29-
public var buildDirPrefix: String {
31+
public var buildDirPrefix: String? {
3032
switch self {
3133
case .swift: "swift"
34+
case .swiftRuntimes: nil
3235
case .cmark: "cmark"
3336
case .lldb: "lldb"
3437
case .llvm: "llvm"

utils/swift-xcodegen/Sources/swift-xcodegen/Options.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,19 @@ struct ProjectOptions: ParsableArguments {
222222
)
223223
var useBuildableFolders: Bool = true
224224

225+
@Option(
226+
name: .customLong("runtimes-build-dir"),
227+
help: """
228+
Experimental: The path to a build directory for the new 'Runtimes/'
229+
stdlib CMake build. This creates a separate 'SwiftRuntimes' project, along
230+
with a 'Swift+Runtimes' workspace.
231+
232+
Note: This requires passing '-DCMAKE_EXPORT_COMPILE_COMMANDS=YES' to
233+
CMake.
234+
"""
235+
)
236+
var runtimesBuildDir: AnyPath?
237+
225238
@Option(help: .hidden)
226239
var blueFolders: String = ""
227240
}

utils/swift-xcodegen/Sources/swift-xcodegen/SwiftXcodegen.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ struct SwiftXcodegen: AsyncParsableCommand, Sendable {
167167
return try spec.generateAndWrite(into: outputDir)
168168
}
169169

170+
func writeSwiftRuntimesXcodeProject(
171+
for ninja: NinjaBuildDir, into outputDir: AbsolutePath
172+
) throws -> GeneratedProject {
173+
let buildDir = try ninja.buildDir(for: .swiftRuntimes)
174+
var spec = newProjectSpec("SwiftRuntimes", for: buildDir)
175+
176+
spec.addClangTarget(at: "core", mayHaveUnbuildableFiles: true)
177+
spec.addSwiftTargets(below: "core")
178+
179+
if self.addDocs {
180+
spec.addTopLevelDocs()
181+
}
182+
return try spec.generateAndWrite(into: outputDir)
183+
}
184+
170185
@discardableResult
171186
func writeClangXcodeProject(
172187
for ninja: NinjaBuildDir, into outputDir: AbsolutePath
@@ -327,6 +342,15 @@ struct SwiftXcodegen: AsyncParsableCommand, Sendable {
327342
let swiftProj = try await runTask {
328343
try writeSwiftXcodeProject(for: buildDir, into: outputDir)
329344
}
345+
let runtimesProj = try await runTask { () -> GeneratedProject? in
346+
guard let runtimesBuildDir = self.runtimesBuildDir?.absoluteInWorkingDir else {
347+
return nil
348+
}
349+
let buildDir = try NinjaBuildDir(
350+
at: runtimesBuildDir, projectRootDir: projectRootDir
351+
)
352+
return try writeSwiftRuntimesXcodeProject(for: buildDir, into: outputDir)
353+
}
330354
let llvmProj = try await runTask {
331355
self.addLLVM ? try writeLLVMXcodeProject(for: buildDir, into: outputDir) : nil
332356
}
@@ -337,7 +361,12 @@ struct SwiftXcodegen: AsyncParsableCommand, Sendable {
337361
self.addLLDB ? try writeLLDBXcodeProject(for: buildDir, into: outputDir) : nil
338362
}
339363

340-
let swiftWorkspace = try await getWorkspace(for: swiftProj.value)
364+
var swiftWorkspace = try await getWorkspace(for: swiftProj.value)
365+
366+
if let runtimesProj = try await runtimesProj.value {
367+
swiftWorkspace.addProject(runtimesProj)
368+
try swiftWorkspace.write("Swift+Runtimes", into: outputDir)
369+
}
341370

342371
if let llvmProj = try await llvmProj.value {
343372
var swiftLLVMWorkspace = swiftWorkspace

0 commit comments

Comments
 (0)