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
|
// swift-tools-version:5.3
import PackageDescription
import Foundation
/// Return the value of the configuration parameter with the given `name` either
/// from the environment variables or, if that doesn't exist, from a
/// `Package-config.json` file located next to `Package.swift`, like the following:
/// ```
/// {
/// "SWIFT_STRESS_TESTER_SOURCEKIT_SEARCHPATH": "/path/to/lib/with/sourcekitd.framework",
/// "SWIFT_STRESS_TESTER_SWIFTSYNTAX_SEARCHPATH": "/path/to/folder/with/SwiftSyntax.framework"
/// }
/// ```
/// If none of these exist, return `nil`.
func getConfigParam(_ name: String) -> String? {
if let envValue = ProcessInfo.processInfo.environment[name] {
return envValue
}
let configFile = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent()
.appendingPathComponent("Package-config.json")
let configData: Data
do {
configData = try Data(contentsOf: configFile)
} catch {
// Config file not found. That's fine. Return `nil` without complaining.
return nil
}
do {
let config = try JSONDecoder().decode([String: String].self, from: configData)
return config[name]
} catch {
// We couldn't parse the Package-config.json, probably malformatted JSON.
// Print the error, which shows up as a warning in Xcode.
print("Loading Package-config.json failed with error: \(error)")
return nil
}
}
// MARK: - Configuration options variables
/// Path to the directory containing sourcekitd.framework.
/// Required for a successful build.
/// We can't fatalError if the environment variable is not set because
/// SwiftSyntax parses this package manifest while not specifying the
/// `SWIFT_STRESS_TESTER_SOURCEKIT_SEARCHPATH` enviornment variable in the
/// unified build.
/// The environment variable is only specified once we build the stress tester.
let sourceKitSearchPath: String? = getConfigParam("SWIFT_STRESS_TESTER_SOURCEKIT_SEARCHPATH")
/// Path to a directory containing SwiftSyntax.framework and SwiftParser.framework.
/// Optional. If not specified, SwiftSyntax will be built from source.
let swiftSyntaxSearchPath: String? = getConfigParam("SWIFT_STRESS_TESTER_SWIFTSYNTAX_SEARCHPATH")
/// If specified expect swift-tools-support-core, swift-argument-parser and swift-syntax
/// to be checked out next to swift-stresss-tester.
let useLocalDependencies = getConfigParam("SWIFTCI_USE_LOCAL_DEPS") != nil
// MARK: - Conditional build settings
var stressTesterTargetDependencies: [Target.Dependency] = [
"Common",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"SwiftSourceKit",
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
]
// Unsafe Swift/Linker settings that all targets linking against sourcekitd need to include
var sourceKitSwiftSettings: [String] = []
var sourceKitLinkerSettings: [String] = []
if let sourceKitSearchPath = sourceKitSearchPath {
sourceKitSwiftSettings = ["-Fsystem", sourceKitSearchPath]
sourceKitLinkerSettings = ["-Xlinker", "-F", "-Xlinker", sourceKitSearchPath,
"-Xlinker", "-rpath", "-Xlinker", sourceKitSearchPath]
}
// Unsafe Swift/Linker settings that all targets linking against SwiftSyntax need to include
var swiftSyntaxSwiftSettings: [String] = []
var swiftSyntaxLinkerSettings: [String] = []
// If we have a SwiftSyntax search path look for it in that directory.
// Otherwise, add SwiftSyntax as a SwiftPM dependency.
if let swiftSyntaxSearchPath = swiftSyntaxSearchPath {
swiftSyntaxSwiftSettings = ["-F", swiftSyntaxSearchPath]
swiftSyntaxLinkerSettings = ["-Xlinker", "-F", "-Xlinker", swiftSyntaxSearchPath,
"-Xlinker", "-rpath", "-Xlinker", swiftSyntaxSearchPath]
} else {
stressTesterTargetDependencies += [
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SwiftParserDiagnostics", package: "swift-syntax"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftOperators", package: "swift-syntax"),
]
}
// MARK: - Package description
let package = Package(
name: "SourceKitStressTester",
platforms: [.macOS(.v11)],
products: [
.executable(name: "sk-stress-test", targets: ["sk-stress-test"]),
.executable(name: "sk-swiftc-wrapper", targets: ["sk-swiftc-wrapper"]),
],
dependencies: [
// See dependencies added below.
],
targets: [
.target(
name: "SwiftSourceKit",
dependencies: [],
exclude: [
"UIDs.swift.gyb"
],
swiftSettings: [.unsafeFlags(sourceKitSwiftSettings)],
linkerSettings: [.unsafeFlags(sourceKitLinkerSettings)]
),
.target(
name: "Common"
),
.target(
name: "InstructionCount"
),
.target(
name: "StressTester",
dependencies: ["InstructionCount"] + stressTesterTargetDependencies,
swiftSettings: [.unsafeFlags(sourceKitSwiftSettings + swiftSyntaxSwiftSettings)],
linkerSettings: [.unsafeFlags(sourceKitLinkerSettings + swiftSyntaxLinkerSettings)]
),
.target(
name: "SwiftCWrapper",
dependencies: [
"Common",
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core")
]
),
.target(
name: "sk-stress-test",
dependencies: ["StressTester"],
swiftSettings: [.unsafeFlags(sourceKitSwiftSettings + swiftSyntaxSwiftSettings)],
linkerSettings: [.unsafeFlags(sourceKitLinkerSettings + swiftSyntaxLinkerSettings)]
),
.target(
name: "sk-swiftc-wrapper",
dependencies: ["SwiftCWrapper"]
),
.target(
name: "TestHelpers"
),
.testTarget(
name: "StressTesterToolTests",
dependencies: ["StressTester", "TestHelpers"],
swiftSettings: [.unsafeFlags(sourceKitSwiftSettings)],
linkerSettings: [.unsafeFlags(sourceKitLinkerSettings)]
),
.testTarget(
name: "SwiftCWrapperToolTests",
dependencies: ["SwiftCWrapper", "TestHelpers"]
)
]
)
if !useLocalDependencies {
// Building standalone.
package.dependencies += [
.package(url: "https://github.com/apple/swift-tools-support-core.git", .branch("release/6.0")),
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "1.0.1")),
]
if swiftSyntaxSearchPath == nil {
package.dependencies.append(.package(url: "https://github.com/swiftlang/swift-syntax.git", .branch("release/6.0")))
}
} else {
package.dependencies += [
.package(path: "../../swift-tools-support-core"),
.package(path: "../../swift-argument-parser"),
]
if swiftSyntaxSearchPath == nil {
package.dependencies.append(.package(path: "../../swift-syntax"))
}
}
|