Skip to content

Clean ups to the swift-syntax-dev-utils and Package.swift #2200

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 3 commits into from
Sep 19, 2023
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
52 changes: 40 additions & 12 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,62 @@
import Foundation
import PackageDescription

// MARK: - Parse build arguments

func hasEnvironmentVariable(_ name: String) -> Bool {
return ProcessInfo.processInfo.environment[name] != nil
}

/// Set when building swift-syntax using swift-syntax-dev-utils or in Swift CI in general.
///
/// Modifies the build in the following ways
/// - Enables assertions even in release builds
/// - Removes the dependency of swift-syntax on os_log
let buildScriptEnvironment = hasEnvironmentVariable("SWIFT_BUILD_SCRIPT_ENVIRONMENT")

/// Check that the layout of the syntax tree is correct.
///
/// See CONTRIBUTING.md for more information
let rawSyntaxValidation = hasEnvironmentVariable("SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION")

/// Mutate the input of `assertParse` test cases.
///
/// See CONTRIBUTING.md for more information
let alternateTokenIntrospection = hasEnvironmentVariable("SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION")

/// Assume that swift-argument-parser is checked out next to swift-syntax and use that instead of fetching a remote dependency.
let useLocalDependencies = hasEnvironmentVariable("SWIFTCI_USE_LOCAL_DEPS")

// MARK: - Compute custom build settings

// These build settings apply to the target and the corresponding test target.
var swiftSyntaxSwiftSettings: [SwiftSetting] = []
var swiftSyntaxBuilderSwiftSettings: [SwiftSetting] = []
var swiftParserSwiftSettings: [SwiftSetting] = []

/// If we are in a controlled CI environment, we can use internal compiler flags
/// to speed up the build or improve it.
if ProcessInfo.processInfo.environment["SWIFT_BUILD_SCRIPT_ENVIRONMENT"] != nil {
if buildScriptEnvironment {
swiftSyntaxSwiftSettings += [
.define("SWIFTSYNTAX_ENABLE_ASSERTIONS")
]
swiftSyntaxBuilderSwiftSettings += [
.define("SWIFTSYNTAX_NO_OSLOG_DEPENDENCY")
]
swiftParserSwiftSettings += [
.define("SWIFTSYNTAX_NO_OSLOG_DEPENDENCY")
]
}

if ProcessInfo.processInfo.environment["SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION"] != nil {
if rawSyntaxValidation {
swiftSyntaxSwiftSettings += [
.define("SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION")
]
}

if ProcessInfo.processInfo.environment["SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION"] != nil {
if alternateTokenIntrospection {
swiftParserSwiftSettings += [
.define("SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION")
]
}

// MARK: - Build the package

let package = Package(
name: "swift-syntax",
platforms: [
Expand Down Expand Up @@ -153,10 +179,11 @@ let package = Package(

.testTarget(
name: "SwiftSyntaxTest",
dependencies: ["_SwiftSyntaxTestSupport", "SwiftSyntax", "SwiftSyntaxBuilder"]
dependencies: ["_SwiftSyntaxTestSupport", "SwiftSyntax", "SwiftSyntaxBuilder"],
swiftSettings: swiftSyntaxSwiftSettings
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of interest, was this causing rebuilds of the other targets?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

),

// MARK: Verison marker modules
// MARK: Version marker modules

.target(
name: "SwiftSyntax509",
Expand All @@ -174,7 +201,8 @@ let package = Package(

.testTarget(
name: "SwiftSyntaxBuilderTest",
dependencies: ["_SwiftSyntaxTestSupport", "SwiftSyntaxBuilder"]
dependencies: ["_SwiftSyntaxTestSupport", "SwiftSyntaxBuilder"],
swiftSettings: swiftSyntaxBuilderSwiftSettings
),

// MARK: SwiftSyntaxMacros
Expand Down Expand Up @@ -303,7 +331,7 @@ package.targets.append(
)
)

if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
if useLocalDependencies {
// Building standalone.
package.dependencies += [
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,11 @@ struct Test: ParsableCommand, BuildCommand {

private func runTests() throws {
logSection("Running SwiftSyntax Tests")
var swiftpmCallArguments = [
"--test-product", "swift-syntaxPackageTests",
]

if arguments.verbose {
swiftpmCallArguments += ["--verbose"]
}

try invokeSwiftPM(
action: "test",
packageDir: Paths.packageDir,
additionalArguments: swiftpmCallArguments,
additionalArguments: ["--test-product", "swift-syntaxPackageTests"],
additionalEnvironment: swiftPMEnvironmentVariables,
captureStdout: false,
captureStderr: false
Expand All @@ -52,18 +45,10 @@ struct Test: ParsableCommand, BuildCommand {

private func runCodeGenerationTests() throws {
logSection("Running CodeGeneration Tests")
var swiftpmCallArguments = [
"--test-product", "CodeGenerationPackageTests",
]

if arguments.verbose {
swiftpmCallArguments += ["--verbose"]
}

try invokeSwiftPM(
action: "test",
packageDir: Paths.codeGenerationDir,
additionalArguments: swiftpmCallArguments,
additionalArguments: ["--test-product", "CodeGenerationPackageTests"],
additionalEnvironment: swiftPMEnvironmentVariables,
captureStdout: false,
captureStderr: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ struct BuildArguments: ParsableArguments {
)
var multirootDataFile: URL?

@Flag(help: "Disable sandboxes when building with SwiftPM")
var disableSandbox: Bool = false

@Flag(help: "Build in release mode.")
var release: Bool = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ extension BuildCommand {
args += ["--multiroot-data-file", multirootDataFile]
}

if arguments.disableSandbox {
args += ["--disable-sandbox"]
}

if arguments.verbose {
args += ["--verbose"]
}
Expand Down