Skip to content

Commit 3264bfd

Browse files
authored
Merge pull request #322 from tayloraswift/simplify-toolchain-spec
simplify toolchain path specification interface
2 parents 7d7975a + 0791f28 commit 3264bfd

File tree

12 files changed

+248
-225
lines changed

12 files changed

+248
-225
lines changed

Sources/SymbolGraphBuilder/Builds/SSGC.PackageBuild.swift

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -188,21 +188,21 @@ extension SSGC.PackageBuild:SSGC.DocumentationBuild
188188
{
189189
func compile(updating status:SSGC.StatusStream?,
190190
cache:FilePath.Directory,
191-
with swift:SSGC.Toolchain,
191+
with toolchain:SSGC.Toolchain,
192192
clean:Bool = true) throws -> (SymbolGraphMetadata, any SSGC.DocumentationSources)
193193
{
194194
switch self.type
195195
{
196196
case .package:
197197
try self.compileSwiftPM(updating: status,
198198
cache: cache,
199-
with: swift,
199+
with: toolchain,
200200
clean: clean)
201201

202202
case .book:
203203
try self.compileBook(updating: status,
204204
cache: cache,
205-
with: swift)
205+
with: toolchain)
206206
}
207207
}
208208
}
@@ -212,7 +212,7 @@ extension SSGC.PackageBuild
212212
@_spi(testable) public
213213
func compileBook(updating status:SSGC.StatusStream? = nil,
214214
cache _:FilePath.Directory,
215-
with swift:SSGC.Toolchain) throws -> (SymbolGraphMetadata, SSGC.BookSources)
215+
with toolchain:SSGC.Toolchain) throws -> (SymbolGraphMetadata, SSGC.BookSources)
216216
{
217217
switch self.id
218218
{
@@ -239,8 +239,8 @@ extension SSGC.PackageBuild
239239
scope: self.id.pin?.location.owner,
240240
name: self.id.package),
241241
commit: self.id.commit,
242-
triple: swift.triple,
243-
swift: swift.id,
242+
triple: toolchain.splash.triple,
243+
swift: toolchain.splash.swift,
244244
tools: nil,
245245
manifests: [],
246246
requirements: [],
@@ -255,7 +255,7 @@ extension SSGC.PackageBuild
255255
@_spi(testable) public
256256
func compileSwiftPM(updating status:SSGC.StatusStream? = nil,
257257
cache:FilePath.Directory,
258-
with swift:SSGC.Toolchain,
258+
with toolchain:SSGC.Toolchain,
259259
clean:Bool = true) throws -> (SymbolGraphMetadata, SSGC.PackageSources)
260260
{
261261
if clean
@@ -277,7 +277,7 @@ extension SSGC.PackageBuild
277277
}
278278

279279
let manifestVersions:[MinorVersion] = try self.listExtraManifests()
280-
var manifest:SPM.Manifest = try swift.manifest(package: self.root,
280+
var manifest:SPM.Manifest = try toolchain.manifest(package: self.root,
281281
json: artifacts / "\(self.id.package).package.json",
282282
leaf: true)
283283

@@ -289,7 +289,7 @@ extension SSGC.PackageBuild
289289
let pins:[SPM.DependencyPin]
290290
do
291291
{
292-
pins = try swift.resolve(package: self.root)
292+
pins = try toolchain.resolve(package: self.root)
293293
}
294294
catch SystemProcessError.exit(let code, let invocation)
295295
{
@@ -300,7 +300,7 @@ extension SSGC.PackageBuild
300300

301301
do
302302
{
303-
try swift.build(package: self.root,
303+
try toolchain.build(package: self.root,
304304
using: self.scratch,
305305
flags: self.flags.dumping(symbols: .default, to: artifacts))
306306
}
@@ -309,14 +309,14 @@ extension SSGC.PackageBuild
309309
throw SSGC.PackageBuildError.swift_build(code, invocation)
310310
}
311311

312-
let platform:SymbolGraphMetadata.Platform = try swift.platform()
312+
let platform:SymbolGraphMetadata.Platform = try toolchain.platform()
313313
var packages:SSGC.PackageGraph = .init(platform: platform)
314314

315315
for pin:SPM.DependencyPin in pins
316316
{
317317
print("Dumping manifest for package '\(pin.identity)' at \(pin.state)")
318318

319-
let manifest:SPM.Manifest = try swift.manifest(
319+
let manifest:SPM.Manifest = try toolchain.manifest(
320320
package: self.scratch.location / "checkouts" / "\(pin.location.name)",
321321
json: artifacts / "\(pin.identity).package.json",
322322
leaf: false)
@@ -329,14 +329,17 @@ extension SSGC.PackageBuild
329329
as: self.id.package)
330330

331331
// Dump the standard library’s symbols, unless they’re already cached.
332-
let artifactsCached:FilePath.Directory = try swift.dump(
332+
let artifactsCached:FilePath.Directory = try toolchain.dump(
333333
standardLibrary: .init(platform: platform),
334334
options: .default,
335335
cache: cache)
336336
for (module, include):(Symbol.Module, [FilePath.Directory]) in try self.modulesToDump(
337337
among: modules)
338338
{
339-
try swift.dump(module: module, to: artifacts, options: .default, include: include)
339+
try toolchain.dump(module: module,
340+
to: artifacts,
341+
options: .default,
342+
include: include)
340343
}
341344

342345
// This step is considered part of documentation building.
@@ -374,8 +377,8 @@ extension SSGC.PackageBuild
374377
scope: self.id.pin?.location.owner,
375378
name: self.id.package),
376379
commit: self.id.commit,
377-
triple: swift.triple,
378-
swift: swift.id,
380+
triple: toolchain.splash.triple,
381+
swift: toolchain.splash.swift,
379382
tools: manifest.format,
380383
manifests: manifestVersions,
381384
requirements: manifest.requirements,

Sources/SymbolGraphBuilder/SSGC.Compile.swift

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,21 @@ extension SSGC
4646
var outputLog:FilePath? = nil
4747

4848
@Option(
49-
name: [.customLong("swift-runtime")],
50-
help: "The path to the Swift runtime directory, usually ending in /usr/lib",
49+
name: [.customLong("swift-toolchain"), .customShort("u")],
50+
help: "The path to a Swift toolchain directory, usually ending in 'usr'",
5151
completion: .directory)
52-
var swiftRuntime:FilePath.Directory? = nil
52+
var swiftTools:FilePath.Directory?
5353

5454
@Option(
5555
name: [.customLong("swiftpm-cache")],
5656
help: "The path to the SwiftPM cache directory to use",
5757
completion: .directory)
5858
var swiftCache:FilePath.Directory?
5959

60-
@Option(
61-
name: [.customLong("swift"), .customShort("s")],
62-
help: "The path to the Swift toolchain",
63-
completion: .file(extensions: []))
64-
var swiftPath:FilePath? = nil
65-
6660
@Option(
6761
name: [.customLong("sdk"), .customShort("k")],
68-
help: "The Swift SDK to use")
69-
var swiftSDK:AppleSDK? = nil
62+
help: "The Apple SDK to use")
63+
var appleSDK:AppleSDK? = nil
7064

7165
@Option(
7266
name: [.customLong("package-name"), .customShort("n")],
@@ -221,10 +215,8 @@ extension SSGC.Compile
221215
status:SSGC.StatusStream?,
222216
logger:SSGC.Logger) throws
223217
{
224-
let toolchain:SSGC.Toolchain = try .detect(swiftRuntime: self.swiftRuntime,
225-
swiftCache: self.swiftCache,
226-
swiftPath: self.swiftPath,
227-
swiftSDK: self.swiftSDK,
218+
let toolchain:SSGC.Toolchain = try .detect(appleSDK: self.appleSDK,
219+
paths: .init(swiftPM: self.swiftCache, usr: self.swiftTools),
228220
pretty: self.pretty)
229221

230222
let object:SymbolGraphObject<Void>

Sources/SymbolGraphBuilder/Standard library/SSGC.StandardLibraryBuild.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ extension SSGC.StandardLibraryBuild:SSGC.DocumentationBuild
2121
{
2222
func compile(updating _:SSGC.StatusStream?,
2323
cache:FilePath.Directory,
24-
with swift:SSGC.Toolchain,
24+
with toolchain:SSGC.Toolchain,
2525
clean _:Bool) throws -> (SymbolGraphMetadata, any SSGC.DocumentationSources)
2626
{
27-
let standardLibrary:SSGC.StandardLibrary = .init(platform: try swift.platform())
27+
let standardLibrary:SSGC.StandardLibrary = .init(platform: try toolchain.platform())
2828

29-
let artifacts:FilePath.Directory = try swift.dump(standardLibrary: standardLibrary,
29+
let artifacts:FilePath.Directory = try toolchain.dump(standardLibrary: standardLibrary,
3030
options: .default,
3131
cache: cache)
3232

33-
let metadata:SymbolGraphMetadata = .swift(swift.id,
34-
commit: swift.commit,
35-
triple: swift.triple,
33+
let metadata:SymbolGraphMetadata = .swift(toolchain.splash.swift,
34+
commit: toolchain.splash.commit,
35+
triple: toolchain.splash.triple,
3636
products: standardLibrary.products)
3737

3838
let sources:SSGC.StandardLibrarySources = .init(modules: standardLibrary.modules,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import System
2+
3+
extension SSGC.Toolchain
4+
{
5+
@frozen public
6+
struct Paths
7+
{
8+
/// A path to the SwiftPM cache directory to use.
9+
@usableFromInline
10+
let swiftPM:FilePath.Directory?
11+
/// A path to a Swift toolchain to use, usually ending in `usr`.
12+
@usableFromInline
13+
let usr:FilePath.Directory?
14+
15+
@inlinable public
16+
init(swiftPM:FilePath.Directory?, usr:FilePath.Directory?)
17+
{
18+
self.swiftPM = swiftPM
19+
self.usr = usr
20+
}
21+
}
22+
}
23+
extension SSGC.Toolchain.Paths
24+
{
25+
/// Returns the path to the `swift` executable, or just the string `swift`.
26+
var swiftCommand:String
27+
{
28+
guard
29+
let usr:FilePath.Directory = self.usr
30+
else
31+
{
32+
return "swift"
33+
}
34+
35+
let swift:FilePath = usr / "bin" / "swift"
36+
return swift.string
37+
}
38+
39+
var libIndexStore:FilePath
40+
{
41+
let name:FilePath.Component
42+
let usr:FilePath.Directory
43+
44+
#if os(macOS)
45+
46+
name = "libIndexStore.dylib"
47+
usr = self.usr ?? """
48+
/Applications/Xcode.app/Contents/Developer/Toolchains\
49+
/XcodeDefault.xctoolchain/usr
50+
"""
51+
52+
#else
53+
54+
name = "libIndexStore.so"
55+
usr = self.usr ?? "/usr"
56+
57+
#endif
58+
59+
return usr / "lib" / name
60+
}
61+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import SemanticVersions
2+
import SymbolGraphs
3+
4+
extension SSGC.Toolchain
5+
{
6+
@frozen public
7+
struct Splash
8+
{
9+
public
10+
let commit:SymbolGraphMetadata.Commit?
11+
public
12+
let triple:Triple
13+
public
14+
let swift:SwiftVersion
15+
16+
private
17+
init(commit:SymbolGraphMetadata.Commit?,
18+
triple:Triple,
19+
swift:SwiftVersion)
20+
{
21+
self.commit = commit
22+
self.triple = triple
23+
self.swift = swift
24+
}
25+
}
26+
}
27+
extension SSGC.Toolchain.Splash
28+
{
29+
public
30+
init(parsing splash:String) throws
31+
{
32+
// Splash should consist of two complete lines and a final newline. If the final
33+
// newline isn’t present, the output was clipped.
34+
let lines:[Substring] = splash.split(separator: "\n", omittingEmptySubsequences: false)
35+
guard lines.count == 3
36+
else
37+
{
38+
throw SSGC.ToolchainError.malformedSplash
39+
}
40+
41+
let toolchain:[Substring] = lines[0].split(separator: " ")
42+
let triple:[Substring] = lines[1].split(separator: " ")
43+
44+
guard
45+
triple.count == 2,
46+
triple[0] == "Target:",
47+
let triple:Triple = .init(triple[1])
48+
else
49+
{
50+
throw SSGC.ToolchainError.malformedSplash
51+
}
52+
53+
var k:Int = toolchain.endIndex
54+
for (i, j):(Int, Int) in zip(toolchain.indices, toolchain.indices.dropFirst())
55+
{
56+
if toolchain[i ... j] == ["Swift", "version"]
57+
{
58+
k = toolchain.index(after: j)
59+
break
60+
}
61+
}
62+
if k == toolchain.endIndex
63+
{
64+
throw SSGC.ToolchainError.malformedSplash
65+
}
66+
67+
let id:SwiftVersion
68+
if let version:NumericVersion = .init(toolchain[k])
69+
{
70+
id = .init(version: PatchVersion.init(padding: version))
71+
}
72+
73+
else if
74+
let version:MinorVersion = .init(toolchain[k].prefix { $0 != "-" })
75+
{
76+
id = .init(
77+
version: .v(version.components.major, version.components.minor, 0),
78+
nightly: .DEVELOPMENT_SNAPSHOT)
79+
}
80+
else
81+
{
82+
throw SSGC.ToolchainError.malformedSwiftVersion
83+
}
84+
85+
let commit:SymbolGraphMetadata.Commit?
86+
if case nil = id.nightly,
87+
let word:Substring = toolchain[toolchain.index(after: k)...].first
88+
{
89+
commit = .parenthesizedSwiftRelease(word)
90+
}
91+
else
92+
{
93+
commit = nil
94+
}
95+
96+
self.init(commit: commit, triple: triple, swift: id)
97+
}
98+
}

0 commit comments

Comments
 (0)