Skip to content

Add support for a Package-config.json to specify defaults for environment variables #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SourceKitStressTester/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/DerivedData
/Package.resolved
/.swiftpm
/Package-config.json
51 changes: 38 additions & 13 deletions SourceKitStressTester/Package.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
// swift-tools-version:5.3

import PackageDescription
#if os(Linux)
import Glibc
#else
import Darwin.C
#endif

func readEnv(_ name: String) -> String? {
if let pointer = getenv(name) {
return String(cString: pointer)
} else {
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
}
}
Expand All @@ -24,15 +49,15 @@ func readEnv(_ name: String) -> String? {
/// `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? = readEnv("SWIFT_STRESS_TESTER_SOURCEKIT_SEARCHPATH")
let sourceKitSearchPath: String? = getConfigParam("SWIFT_STRESS_TESTER_SOURCEKIT_SEARCHPATH")

/// Path to a directory containing SwiftSyntax.framework and SwiftSyntaxParser.framework.
/// Optional. If not specified, SwiftSyntax will be built from source.
let swiftSyntaxSearchPath: String? = readEnv("SWIFT_STRESS_TESTER_SWIFTSYNTAX_SEARCHPATH")
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 = readEnv("SWIFTCI_USE_LOCAL_DEPS") != nil
let useLocalDependencies = getConfigParam("SWIFTCI_USE_LOCAL_DEPS") != nil

// MARK: - Conditional build settings

Expand Down