-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathManifestLoader.swift
868 lines (777 loc) · 36.8 KB
/
ManifestLoader.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics
import Foundation
import PackageModel
import TSCBasic
import enum TSCUtility.Diagnostics
public enum ManifestParseError: Swift.Error, Equatable {
/// The manifest contains invalid format.
case invalidManifestFormat(String, diagnosticFile: AbsolutePath?)
/// The manifest was successfully loaded by swift interpreter but there were runtime issues.
case runtimeManifestErrors([String])
}
// used to output the errors via the observability system
extension ManifestParseError: CustomStringConvertible {
public var description: String {
switch self {
case .invalidManifestFormat(let error, _):
return "Invalid manifest\n\(error)"
case .runtimeManifestErrors(let errors):
return "Invalid manifest (evaluation failed)\n\(errors.joined(separator: "\n"))"
}
}
}
// MARK: - ManifestLoaderProtocol
/// Protocol for the manifest loader interface.
public protocol ManifestLoaderProtocol {
/// Load the manifest for the package at `path`.
///
/// - Parameters:
/// - manifestPath: The root path of the package.
/// - manifestToolsVersion: The version of the tools the manifest supports.
/// - packageIdentity: the identity of the package
/// - packageKind: The kind of package the manifest is from.
/// - packageLocation: The location the package the manifest was loaded from.
/// - packageVersion: Optional. The version and revision of the package.
/// - identityResolver: A helper to resolve identities based on configuration
/// - fileSystem: File system to load from.
/// - observabilityScope: Observability scope to emit diagnostics.
/// - delegateQueue: The dispatch queue to call delegate handlers on.
/// - callbackQueue: The dispatch queue to perform completion handler on.
/// - completion: The completion handler .
func load(
manifestPath: AbsolutePath,
manifestToolsVersion: ToolsVersion,
packageIdentity: PackageIdentity,
packageKind: PackageReference.Kind,
packageLocation: String,
packageVersion: (version: Version?, revision: String?)?,
identityResolver: IdentityResolver,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope,
delegateQueue: DispatchQueue,
callbackQueue: DispatchQueue,
completion: @escaping (Result<Manifest, Error>) -> Void
)
/// Reset any internal cache held by the manifest loader.
func resetCache() throws
/// Reset any internal cache held by the manifest loader and purge any entries in a shared cache
func purgeCache() throws
}
public protocol ManifestLoaderDelegate {
func willLoad(manifest: AbsolutePath)
func willParse(manifest: AbsolutePath)
}
// loads a manifest given a package root path
// this will first find the most appropriate manifest file in the package directory
// bases on the toolchain's tools-version and proceed to load that manifest
extension ManifestLoaderProtocol {
public func load(
packagePath: AbsolutePath,
packageIdentity: PackageIdentity,
packageKind: PackageReference.Kind,
packageLocation: String,
packageVersion: (version: Version?, revision: String?)?,
currentToolsVersion: ToolsVersion,
identityResolver: IdentityResolver,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope,
delegateQueue: DispatchQueue,
callbackQueue: DispatchQueue,
completion: @escaping (Result<Manifest, Error>) -> Void
) {
do {
// find the manifest path and parse it's tools-version
let manifestPath = try ManifestLoader.findManifest(packagePath: packagePath, fileSystem: fileSystem, currentToolsVersion: currentToolsVersion)
let manifestToolsVersion = try ToolsVersionParser.parse(manifestPath: manifestPath, fileSystem: fileSystem)
// validate the manifest tools-version against the toolchain tools-version
try manifestToolsVersion.validateToolsVersion(currentToolsVersion, packageIdentity: packageIdentity, packageVersion: packageVersion?.version?.description ?? packageVersion?.revision)
self.load(
manifestPath: manifestPath,
manifestToolsVersion: manifestToolsVersion,
packageIdentity: packageIdentity,
packageKind: packageKind,
packageLocation: packageLocation,
packageVersion: packageVersion,
identityResolver: identityResolver,
fileSystem: fileSystem,
observabilityScope: observabilityScope,
delegateQueue: delegateQueue,
callbackQueue: callbackQueue,
completion: completion
)
} catch {
callbackQueue.async {
completion(.failure(error))
}
}
}
}
// MARK: - ManifestLoader
/// Utility class for loading manifest files.
///
/// This class is responsible for reading the manifest data and produce a
/// properly formed `PackageModel.Manifest` object. It currently does so by
/// interpreting the manifest source using Swift -- that produces a JSON
/// serialized form of the manifest (as implemented by `PackageDescription`'s
/// `atexit()` handler) which is then deserialized and loaded.
public final class ManifestLoader: ManifestLoaderProtocol {
private let toolchain: UserToolchain
private let serializedDiagnostics: Bool
private let isManifestSandboxEnabled: Bool
private let delegate: ManifestLoaderDelegate?
private let extraManifestFlags: [String]
private let databaseCacheDir: AbsolutePath?
private let sdkRootCache = ThreadSafeBox<AbsolutePath>()
/// DispatchSemaphore to restrict concurrent manifest evaluations
private let concurrencySemaphore: DispatchSemaphore
/// OperationQueue to park pending lookups
private let evaluationQueue: OperationQueue
public init(
toolchain: UserToolchain,
serializedDiagnostics: Bool = false,
isManifestSandboxEnabled: Bool = true,
cacheDir: AbsolutePath? = nil,
delegate: ManifestLoaderDelegate? = nil,
extraManifestFlags: [String] = []
) {
self.toolchain = toolchain
self.serializedDiagnostics = serializedDiagnostics
self.isManifestSandboxEnabled = isManifestSandboxEnabled
self.delegate = delegate
self.extraManifestFlags = extraManifestFlags
self.databaseCacheDir = cacheDir.map(resolveSymlinks)
// this queue and semaphore is used to limit the amount of concurrent manifest loading taking place
self.evaluationQueue = OperationQueue()
self.evaluationQueue.name = "org.swift.swiftpm.manifest-loader"
self.evaluationQueue.maxConcurrentOperationCount = Concurrency.maxOperations
self.concurrencySemaphore = DispatchSemaphore(value: Concurrency.maxOperations)
}
public func load(
manifestPath: AbsolutePath,
manifestToolsVersion: ToolsVersion,
packageIdentity: PackageIdentity,
packageKind: PackageReference.Kind,
packageLocation: String,
packageVersion: (version: Version?, revision: String?)?,
identityResolver: IdentityResolver,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope,
delegateQueue: DispatchQueue,
callbackQueue: DispatchQueue,
completion: @escaping (Result<Manifest, Error>) -> Void
) {
// Inform the delegate.
delegateQueue.async {
self.delegate?.willLoad(manifest: manifestPath)
}
// Validate that the file exists.
guard fileSystem.isFile(manifestPath) else {
return callbackQueue.async {
completion(.failure(PackageModel.Package.Error.noManifest(at: manifestPath, version: packageVersion?.version)))
}
}
self.loadAndCacheManifest(
at: manifestPath,
toolsVersion: manifestToolsVersion,
packageIdentity: packageIdentity,
packageKind: packageKind,
packageVersion: packageVersion?.version,
identityResolver: identityResolver,
fileSystem: fileSystem,
observabilityScope: observabilityScope,
delegateQueue: delegateQueue,
callbackQueue: callbackQueue
) { parseResult in
do {
dispatchPrecondition(condition: .onQueue(callbackQueue))
let parsedManifest = try parseResult.get()
// Convert legacy system packages to the current target‐based model.
var products = parsedManifest.products
var targets = parsedManifest.targets
if products.isEmpty, targets.isEmpty,
fileSystem.isFile(manifestPath.parentDirectory.appending(component: moduleMapFilename)) {
try products.append(ProductDescription(
name: parsedManifest.name,
type: .library(.automatic),
targets: [parsedManifest.name])
)
targets.append(try TargetDescription(
name: parsedManifest.name,
path: "",
type: .system,
pkgConfig: parsedManifest.pkgConfig,
providers: parsedManifest.providers
))
}
let manifest = Manifest(
displayName: parsedManifest.name,
path: manifestPath,
packageKind: packageKind,
packageLocation: packageLocation,
defaultLocalization: parsedManifest.defaultLocalization,
platforms: parsedManifest.platforms,
version: packageVersion?.version,
revision: packageVersion?.revision,
toolsVersion: manifestToolsVersion,
pkgConfig: parsedManifest.pkgConfig,
providers: parsedManifest.providers,
cLanguageStandard: parsedManifest.cLanguageStandard,
cxxLanguageStandard: parsedManifest.cxxLanguageStandard,
swiftLanguageVersions: parsedManifest.swiftLanguageVersions,
dependencies: parsedManifest.dependencies,
products: products,
targets: targets
)
completion(.success(manifest))
} catch {
callbackQueue.async {
completion(.failure(error))
}
}
}
}
/// Load the JSON string for the given manifest.
private func parseManifest(
_ result: EvaluationResult,
packageIdentity: PackageIdentity,
packageKind: PackageReference.Kind,
toolsVersion: ToolsVersion,
identityResolver: IdentityResolver,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope
) throws -> ManifestJSONParser.Result {
// Throw now if we weren't able to parse the manifest.
guard let manifestJSON = result.manifestJSON, !manifestJSON.isEmpty else {
let errors = result.errorOutput ?? result.compilerOutput ?? "Missing or empty JSON output from manifest compilation for \(packageIdentity)"
throw ManifestParseError.invalidManifestFormat(errors, diagnosticFile: result.diagnosticFile)
}
// We should not have any fatal error at this point.
guard result.errorOutput == nil else {
throw InternalError("unexpected error output: \(result.errorOutput!)")
}
// We might have some non-fatal output (warnings/notes) from the compiler even when
// we were able to parse the manifest successfully.
if let compilerOutput = result.compilerOutput {
// FIXME: We shouldn't assume the compiler output to be a single piece. There could be combined
// output from different stages of compiling the manifest, but it's hard to distinguish them.
// A better approach might be teaching the driver to emit a structured log with context and severity.
var outputSeverity: Basics.Diagnostic.Severity = .warning
#if os(Windows)
// Filter out `LINK` note for creating manifest executable.
if let compiledManifestName = result.compiledManifestFile?.basenameWithoutExt,
!compilerOutput.contains(where: \.isNewline) {
if compilerOutput.contains("\(compiledManifestName).lib") && compilerOutput.contains("\(compiledManifestName).exp") {
outputSeverity = .debug
}
}
#endif
let metadata = result.diagnosticFile.map { diagnosticFile -> ObservabilityMetadata in
var metadata = ObservabilityMetadata()
metadata.manifestLoadingDiagnosticFile = diagnosticFile
return metadata
}
observabilityScope.emit(severity: outputSeverity, message: compilerOutput, metadata: metadata)
}
return try ManifestJSONParser.parse(
v4: manifestJSON,
toolsVersion: toolsVersion,
packageKind: packageKind,
identityResolver: identityResolver,
fileSystem: fileSystem
)
}
private func loadAndCacheManifest(
at path: AbsolutePath,
toolsVersion: ToolsVersion,
packageIdentity: PackageIdentity,
packageKind: PackageReference.Kind,
packageVersion: Version?,
identityResolver: IdentityResolver,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope,
delegateQueue: DispatchQueue,
callbackQueue: DispatchQueue,
completion: @escaping (Result<ManifestJSONParser.Result, Error>) -> Void
) {
let cache = self.databaseCacheDir.map { cacheDir -> SQLiteBackedCache<EvaluationResult> in
let path = Self.manifestCacheDBPath(cacheDir)
var configuration = SQLiteBackedCacheConfiguration()
// FIXME: expose as user-facing configuration
configuration.maxSizeInMegabytes = 100
configuration.truncateWhenFull = true
return SQLiteBackedCache<EvaluationResult>(
tableName: "MANIFEST_CACHE",
location: .path(path),
configuration: configuration
)
}
var closeAfterRead = DelayableAction(target: cache) { cache in
do {
try cache.close()
} catch {
observabilityScope.emit(warning: "failed closing cache: \(error)")
}
}
defer { closeAfterRead.perform() }
let key : CacheKey
do {
key = try CacheKey(
packageIdentity: packageIdentity,
manifestPath: path,
toolsVersion: toolsVersion,
env: ProcessEnv.vars,
swiftpmVersion: SwiftVersion.current.displayString,
fileSystem: fileSystem
)
} catch {
return callbackQueue.async {
completion(.failure(error))
}
}
do {
// try to get it from the cache
if let result = try cache?.get(key: key.sha256Checksum), let manifestJSON = result.manifestJSON, !manifestJSON.isEmpty {
observabilityScope.emit(debug: "loading manifest for '\(packageIdentity)' v. \(packageVersion?.description ?? "unknown") from cache")
let parsedManifest = try self.parseManifest(
result,
packageIdentity: packageIdentity,
packageKind: packageKind,
toolsVersion: toolsVersion,
identityResolver: identityResolver,
fileSystem: fileSystem,
observabilityScope: observabilityScope
)
return callbackQueue.async {
completion(.success(parsedManifest))
}
}
} catch {
observabilityScope.emit(warning: "failed loading cached manifest for '\(key.packageIdentity)': \(error)")
}
// delay closing cache until after write.
let closeAfterWrite = closeAfterRead.delay()
// shells out and compiles the manifest, finally output a JSON
observabilityScope.emit(debug: "evaluating manifest for '\(packageIdentity)' v. \(packageVersion?.description ?? "unknown")")
self.evaluateManifest(
packageIdentity: key.packageIdentity,
manifestPath: key.manifestPath,
manifestContents: key.manifestContents,
toolsVersion: key.toolsVersion,
delegateQueue: delegateQueue,
callbackQueue: callbackQueue
) { result in
dispatchPrecondition(condition: .onQueue(callbackQueue))
do {
defer { closeAfterWrite.perform() }
let evaluationResult = try result.get()
// only cache successfully parsed manifests
let parseManifest = try self.parseManifest(
evaluationResult,
packageIdentity: packageIdentity,
packageKind: packageKind,
toolsVersion: toolsVersion,
identityResolver: identityResolver,
fileSystem: fileSystem,
observabilityScope: observabilityScope
)
do {
// FIXME: (diagnostics) pass in observability scope when we have one
try cache?.put(key: key.sha256Checksum, value: evaluationResult)
} catch {
observabilityScope.emit(warning: "failed storing manifest for '\(key.packageIdentity)' in cache: \(error)")
}
completion(.success(parseManifest))
} catch {
callbackQueue.async {
completion(.failure(error))
}
}
}
}
/// Compiler the manifest at the given path and retrieve the JSON.
fileprivate func evaluateManifest(
packageIdentity: PackageIdentity,
manifestPath: AbsolutePath,
manifestContents: [UInt8],
toolsVersion: ToolsVersion,
delegateQueue: DispatchQueue,
callbackQueue: DispatchQueue,
completion: @escaping (Result<EvaluationResult, Error>) -> Void
) {
do {
if localFileSystem.isFile(manifestPath) {
self.evaluateManifest(
at: manifestPath,
packageIdentity: packageIdentity,
toolsVersion: toolsVersion,
delegateQueue: delegateQueue,
callbackQueue: callbackQueue,
completion: completion
)
} else {
try withTemporaryFile(suffix: ".swift") { tempFile, cleanupTempFile in
try localFileSystem.writeFileContents(tempFile.path, bytes: ByteString(manifestContents))
self.evaluateManifest(
at: tempFile.path,
packageIdentity: packageIdentity,
toolsVersion: toolsVersion,
delegateQueue: delegateQueue,
callbackQueue: callbackQueue
) { result in
dispatchPrecondition(condition: .onQueue(callbackQueue))
cleanupTempFile(tempFile)
completion(result)
}
}
}
} catch {
callbackQueue.async {
completion(.failure(error))
}
}
}
/// Helper method for evaluating the manifest.
func evaluateManifest(
at manifestPath: AbsolutePath,
packageIdentity: PackageIdentity,
toolsVersion: ToolsVersion,
delegateQueue: DispatchQueue,
callbackQueue: DispatchQueue,
completion: @escaping (Result<EvaluationResult, Error>) -> Void
) {
// The compiler has special meaning for files with extensions like .ll, .bc etc.
// Assert that we only try to load files with extension .swift to avoid unexpected loading behavior.
guard manifestPath.extension == "swift" else {
return callbackQueue.async {
completion(.failure(InternalError("Manifest files must contain .swift suffix in their name, given: \(manifestPath).")))
}
}
var evaluationResult = EvaluationResult()
delegateQueue.async {
self.delegate?.willParse(manifest: manifestPath)
}
// For now, we load the manifest by having Swift interpret it directly.
// Eventually, we should have two loading processes, one that loads only
// the declarative package specification using the Swift compiler directly
// and validates it.
// Compute the path to runtime we need to load.
let runtimePath = self.toolchain.swiftPMLibrariesLocation.manifestLibraryPath
// FIXME: Workaround for the module cache bug that's been haunting Swift CI
// <rdar://problem/48443680>
let moduleCachePath = (ProcessEnv.vars["SWIFTPM_MODULECACHE_OVERRIDE"] ?? ProcessEnv.vars["SWIFTPM_TESTS_MODULECACHE"]).flatMap{ AbsolutePath.init($0) }
var cmd: [String] = []
cmd += [self.toolchain.swiftCompilerPathForManifests.pathString]
// if runtimePath is set to "PackageFrameworks" that means we could be developing SwiftPM in Xcode
// which produces a framework for dynamic package products.
if runtimePath.extension == "framework" {
cmd += [
"-F", runtimePath.parentDirectory.pathString,
"-framework", "PackageDescription",
"-Xlinker", "-rpath", "-Xlinker", runtimePath.parentDirectory.pathString,
]
} else {
cmd += [
"-L", runtimePath.pathString,
"-lPackageDescription",
]
#if !os(Windows)
// -rpath argument is not supported on Windows,
// so we add runtimePath to PATH when executing the manifest instead
cmd += ["-Xlinker", "-rpath", "-Xlinker", runtimePath.pathString]
#endif
}
// Use the same minimum deployment target as the PackageDescription library (with a fallback of 10.15).
#if os(macOS)
let version = self.toolchain.swiftPMLibrariesLocation.manifestLibraryMinimumDeploymentTarget.versionString
cmd += ["-target", "\(self.toolchain.triple.tripleString(forPlatformVersion: version))"]
#endif
// Add any extra flags required as indicated by the ManifestLoader.
cmd += self.toolchain.swiftCompilerFlags
cmd += self.interpreterFlags(for: toolsVersion)
if let moduleCachePath = moduleCachePath {
cmd += ["-module-cache-path", moduleCachePath.pathString]
}
// Add the arguments for emitting serialized diagnostics, if requested.
if self.serializedDiagnostics, let databaseCacheDir = self.databaseCacheDir {
let diaDir = databaseCacheDir.appending(component: "ManifestLoading")
let diagnosticFile = diaDir.appending(component: "\(packageIdentity).dia")
do {
try localFileSystem.createDirectory(diaDir, recursive: true)
cmd += ["-Xfrontend", "-serialize-diagnostics-path", "-Xfrontend", diagnosticFile.pathString]
evaluationResult.diagnosticFile = diagnosticFile
} catch {
return callbackQueue.async {
completion(.failure(error))
}
}
}
cmd += [manifestPath.pathString]
cmd += self.extraManifestFlags
// wrap the completion to free concurrency control semaphore
let completion: (Result<EvaluationResult, Error>) -> Void = { result in
self.concurrencySemaphore.signal()
completion(result)
}
// we must not block the calling thread (for concurrency control) so nesting this in a queue
self.evaluationQueue.addOperation {
do {
// park the evaluation thread based on the max concurrency allowed
self.concurrencySemaphore.wait()
// run the evaluation
try withTemporaryDirectory { tmpDir, cleanupTmpDir in
// Set path to compiled manifest executable.
#if os(Windows)
let executableSuffix = ".exe"
#else
let executableSuffix = ""
#endif
let compiledManifestFile = tmpDir.appending(component: "\(packageIdentity)-manifest\(executableSuffix)")
cmd += ["-o", compiledManifestFile.pathString]
evaluationResult.compiledManifestFile = compiledManifestFile
// Compile the manifest.
TSCBasic.Process.popen(arguments: cmd, environment: self.toolchain.swiftCompilerEnvironment, queue: callbackQueue) { result in
dispatchPrecondition(condition: .onQueue(callbackQueue))
var cleanupIfError = DelayableAction(target: tmpDir, action: cleanupTmpDir)
defer { cleanupIfError.perform() }
let compilerResult : ProcessResult
do {
compilerResult = try result.get()
evaluationResult.compilerOutput = try (compilerResult.utf8Output() + compilerResult.utf8stderrOutput()).spm_chuzzle()
} catch {
return completion(.failure(error))
}
// Return now if there was an error.
if compilerResult.exitStatus != .terminated(code: 0) {
return completion(.success(evaluationResult))
}
// Pass an open file descriptor of a file to which the JSON representation of the manifest will be written.
let jsonOutputFile = tmpDir.appending(component: "\(packageIdentity)-output.json")
guard let jsonOutputFileDesc = fopen(jsonOutputFile.pathString, "w") else {
return completion(.failure(StringError("couldn't create the manifest's JSON output file")))
}
cmd = [compiledManifestFile.pathString]
#if os(Windows)
// NOTE: `_get_osfhandle` returns a non-owning, unsafe,
// unretained HANDLE. DO NOT invoke `CloseHandle` on `hFile`.
let hFile: Int = _get_osfhandle(_fileno(jsonOutputFileDesc))
cmd += ["-handle", "\(String(hFile, radix: 16))"]
#else
cmd += ["-fileno", "\(fileno(jsonOutputFileDesc))"]
#endif
do {
let packageDirectory = manifestPath.parentDirectory.pathString
let contextModel = ContextModel(packageDirectory: packageDirectory)
cmd += ["-context", try contextModel.encode()]
} catch {
return completion(.failure(error))
}
// If enabled, run command in a sandbox.
// This provides some safety against arbitrary code execution when parsing manifest files.
// We only allow the permissions which are absolutely necessary.
if self.isManifestSandboxEnabled {
let cacheDirectories = [self.databaseCacheDir, moduleCachePath].compactMap{ $0 }
let strictness: Sandbox.Strictness = toolsVersion < .v5_3 ? .manifest_pre_53 : .default
cmd = Sandbox.apply(command: cmd, strictness: strictness, writableDirectories: cacheDirectories)
}
// Run the compiled manifest.
var environment = ProcessEnv.vars
#if os(Windows)
let windowsPathComponent = runtimePath.pathString.replacingOccurrences(of: "/", with: "\\")
environment["Path"] = "\(windowsPathComponent);\(environment["Path"] ?? "")"
#endif
let cleanupAfterRunning = cleanupIfError.delay()
TSCBasic.Process.popen(arguments: cmd, environment: environment, queue: callbackQueue) { result in
dispatchPrecondition(condition: .onQueue(callbackQueue))
defer { cleanupAfterRunning.perform() }
fclose(jsonOutputFileDesc)
do {
let runResult = try result.get()
if let runOutput = try (runResult.utf8Output() + runResult.utf8stderrOutput()).spm_chuzzle() {
// Append the runtime output to any compiler output we've received.
evaluationResult.compilerOutput = (evaluationResult.compilerOutput ?? "") + runOutput
}
// Return now if there was an error.
if runResult.exitStatus != .terminated(code: 0) {
// TODO: should this simply be an error?
// return completion(.failure(ProcessResult.Error.nonZeroExit(runResult)))
evaluationResult.errorOutput = evaluationResult.compilerOutput
return completion(.success(evaluationResult))
}
// Read the JSON output that was emitted by libPackageDescription.
let jsonOutput: String = try localFileSystem.readFileContents(jsonOutputFile)
evaluationResult.manifestJSON = jsonOutput
completion(.success(evaluationResult))
} catch {
completion(.failure(error))
}
}
}
}
} catch {
return callbackQueue.async {
completion(.failure(error))
}
}
}
}
/// Returns path to the sdk, if possible.
private func sdkRoot() -> AbsolutePath? {
if let sdkRoot = self.sdkRootCache.get() {
return sdkRoot
}
var sdkRootPath: AbsolutePath? = nil
// Find SDKROOT on macOS using xcrun.
#if os(macOS)
let foundPath = try? TSCBasic.Process.checkNonZeroExit(
args: "/usr/bin/xcrun", "--sdk", "macosx", "--show-sdk-path")
guard let sdkRoot = foundPath?.spm_chomp(), !sdkRoot.isEmpty else {
return nil
}
let path = AbsolutePath(sdkRoot)
sdkRootPath = path
self.sdkRootCache.put(path)
#endif
return sdkRootPath
}
/// Returns the interpreter flags for a manifest.
public func interpreterFlags(
for toolsVersion: ToolsVersion
) -> [String] {
var cmd = [String]()
let runtimePath = self.toolchain.swiftPMLibrariesLocation.manifestLibraryPath
cmd += ["-swift-version", toolsVersion.swiftLanguageVersion.rawValue]
// if runtimePath is set to "PackageFrameworks" that means we could be developing SwiftPM in Xcode
// which produces a framework for dynamic package products.
if runtimePath.extension == "framework" {
cmd += ["-I", runtimePath.parentDirectory.parentDirectory.pathString]
} else {
cmd += ["-I", runtimePath.pathString]
}
#if os(macOS)
if let sdkRoot = self.toolchain.sdkRootPath ?? self.sdkRoot() {
cmd += ["-sdk", sdkRoot.pathString]
}
#endif
cmd += ["-package-description-version", toolsVersion.description]
return cmd
}
/// Returns path to the manifest database inside the given cache directory.
private static func manifestCacheDBPath(_ cacheDir: AbsolutePath) -> AbsolutePath {
return cacheDir.appending(component: "manifest.db")
}
/// reset internal cache
public func resetCache() throws {
// nothing needed at this point
}
/// reset internal state and purge shared cache
public func purgeCache() throws {
try self.resetCache()
if let manifestCacheDBPath = self.databaseCacheDir.flatMap({ Self.manifestCacheDBPath($0) }) {
try localFileSystem.removeFileTree(manifestCacheDBPath)
}
}
}
extension ManifestLoader {
struct CacheKey: Hashable {
let packageIdentity: PackageIdentity
let manifestPath: AbsolutePath
let manifestContents: [UInt8]
let toolsVersion: ToolsVersion
let env: EnvironmentVariables
let swiftpmVersion: String
let sha256Checksum: String
init (packageIdentity: PackageIdentity,
manifestPath: AbsolutePath,
toolsVersion: ToolsVersion,
env: EnvironmentVariables,
swiftpmVersion: String,
fileSystem: FileSystem
) throws {
let manifestContents = try fileSystem.readFileContents(manifestPath).contents
let sha256Checksum = try Self.computeSHA256Checksum(packageIdentity: packageIdentity, manifestContents: manifestContents, toolsVersion: toolsVersion, env: env, swiftpmVersion: swiftpmVersion)
self.packageIdentity = packageIdentity
self.manifestPath = manifestPath
self.manifestContents = manifestContents
self.toolsVersion = toolsVersion
self.env = env
self.swiftpmVersion = swiftpmVersion
self.sha256Checksum = sha256Checksum
}
func hash(into hasher: inout Hasher) {
hasher.combine(self.sha256Checksum)
}
private static func computeSHA256Checksum(
packageIdentity: PackageIdentity,
manifestContents: [UInt8],
toolsVersion: ToolsVersion,
env: EnvironmentVariables,
swiftpmVersion: String
) throws -> String {
let stream = BufferedOutputByteStream()
stream <<< packageIdentity
stream <<< manifestContents
stream <<< toolsVersion.description
for (key, value) in env.sorted(by: { $0.key > $1.key }) {
stream <<< key <<< value
}
stream <<< swiftpmVersion
return stream.bytes.sha256Checksum
}
}
}
extension ManifestLoader {
struct EvaluationResult: Codable {
/// The path to the compiled manifest.
var compiledManifestFile: AbsolutePath?
/// The path to the diagnostics file (.dia).
///
/// This is only present if serialized diagnostics are enabled.
var diagnosticFile: AbsolutePath?
/// The output from compiler, if any.
///
/// This would contain the errors and warnings produced when loading the manifest file.
var compilerOutput: String?
/// The manifest in JSON format.
var manifestJSON: String?
/// Any non-compiler error that might have occurred during manifest loading.
///
/// For e.g., we could have failed to spawn the process or create temporary file.
var errorOutput: String? {
didSet {
assert(self.manifestJSON == nil)
}
}
var hasErrors: Bool {
return self.manifestJSON == nil
}
}
}
extension ManifestLoader {
/// Represents behavior that can be deferred until a more appropriate time.
struct DelayableAction<T> {
var target: T?
var action: ((T) -> Void)?
func perform() {
if let value = target, let cleanup = action {
cleanup(value)
}
}
mutating func delay() -> DelayableAction {
let next = DelayableAction(target: target, action: action)
target = nil
action = nil
return next
}
}
}