1
1
// swift-tools-version:5.3
2
2
3
3
import PackageDescription
4
- #if os(Linux)
5
- import Glibc
6
- #else
7
- import Darwin. C
8
- #endif
9
-
10
- func readEnv( _ name: String ) -> String ? {
11
- if let pointer = getenv ( name) {
12
- return String ( cString: pointer)
13
- } else {
4
+ import Foundation
5
+
6
+ /// Return the value of the configuration parameter with the given `name` either
7
+ /// from the environment variables or, if that doesn't exist, from a
8
+ /// `Package-config.json` file located next to `Package.swift`, like the following:
9
+ /// ```
10
+ /// {
11
+ /// "SWIFT_STRESS_TESTER_SOURCEKIT_SEARCHPATH": "/path/to/lib/with/sourcekitd.framework",
12
+ /// "SWIFT_STRESS_TESTER_SWIFTSYNTAX_SEARCHPATH": "/path/to/folder/with/SwiftSyntax.framework"
13
+ /// }
14
+ /// ```
15
+ /// If none of these exist, return `nil`.
16
+ func getConfigParam( _ name: String ) -> String ? {
17
+ if let envValue = ProcessInfo . processInfo. environment [ name] {
18
+ return envValue
19
+ }
20
+
21
+ let configFile = URL ( fileURLWithPath: #filePath)
22
+ . deletingLastPathComponent ( )
23
+ . appendingPathComponent ( " Package-config.json " )
24
+
25
+ let configData : Data
26
+ do {
27
+ configData = try Data ( contentsOf: configFile)
28
+ } catch {
29
+ // Config file not found. That's fine. Return `nil` without complaining.
30
+ return nil
31
+ }
32
+ do {
33
+ let config = try JSONDecoder ( ) . decode ( [ String : String ] . self, from: configData)
34
+ return config [ name]
35
+ } catch {
36
+ // We couldn't parse the Package-config.json, probably malformatted JSON.
37
+ // Print the error, which shows up as a warning in Xcode.
38
+ print ( " Loading Package-config.json failed with error: \( error) " )
14
39
return nil
15
40
}
16
41
}
@@ -24,15 +49,15 @@ func readEnv(_ name: String) -> String? {
24
49
/// `SWIFT_STRESS_TESTER_SOURCEKIT_SEARCHPATH` enviornment variable in the
25
50
/// unified build.
26
51
/// The environment variable is only specified once we build the stress tester.
27
- let sourceKitSearchPath : String ? = readEnv ( " SWIFT_STRESS_TESTER_SOURCEKIT_SEARCHPATH " )
52
+ let sourceKitSearchPath : String ? = getConfigParam ( " SWIFT_STRESS_TESTER_SOURCEKIT_SEARCHPATH " )
28
53
29
54
/// Path to a directory containing SwiftSyntax.framework and SwiftSyntaxParser.framework.
30
55
/// Optional. If not specified, SwiftSyntax will be built from source.
31
- let swiftSyntaxSearchPath : String ? = readEnv ( " SWIFT_STRESS_TESTER_SWIFTSYNTAX_SEARCHPATH " )
56
+ let swiftSyntaxSearchPath : String ? = getConfigParam ( " SWIFT_STRESS_TESTER_SWIFTSYNTAX_SEARCHPATH " )
32
57
33
58
/// If specified expect swift-tools-support-core, swift-argument-parser and swift-syntax
34
59
/// to be checked out next to swift-stresss-tester.
35
- let useLocalDependencies = readEnv ( " SWIFTCI_USE_LOCAL_DEPS " ) != nil
60
+ let useLocalDependencies = getConfigParam ( " SWIFTCI_USE_LOCAL_DEPS " ) != nil
36
61
37
62
// MARK: - Conditional build settings
38
63
0 commit comments