33import Foundation
44import PackageDescription
55
6+ // MARK: - Parse build arguments
7+
8+ func hasEnvironmentVariable( _ name: String ) -> Bool {
9+ return ProcessInfo . processInfo. environment [ name] != nil
10+ }
11+
12+ /// Set when building swift-syntax using swift-syntax-dev-utils or in Swift CI in general.
13+ ///
14+ /// Modifies the build in the following ways
15+ /// - Enables assertions even in release builds
16+ /// - Removes the dependency of swift-syntax on os_log
17+ /// - Enables raw syntax validation (ie. implies `SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION`)
18+ /// - Enables alternate token introspection (ie. implies `SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION`)
19+ let buildScriptEnvironment = hasEnvironmentVariable ( " SWIFT_BUILD_SCRIPT_ENVIRONMENT " )
20+
21+ /// Check that the layout of the syntax tree is correct.
22+ ///
23+ /// See CONTRIBUTING.md for more information
24+ let rawSyntaxValidation = hasEnvironmentVariable ( " SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION " )
25+
26+ /// Mutate the input of `assertParse` test cases.
27+ ///
28+ /// See CONTRIBUTING.md for more information
29+ let alternateTokenIntrospection = hasEnvironmentVariable ( " SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION " )
30+
31+ /// Assume that swift-argument-parser is checked out next to swift-syntax and use that instead of fetching a remote dependency.
32+ let useLocalDependencies = hasEnvironmentVariable ( " SWIFTCI_USE_LOCAL_DEPS " )
33+
34+ // MARK: - Compute custom build settings
35+
36+ // These build settings apply to the target and the corresponding test target.
637var swiftSyntaxSwiftSettings : [ SwiftSetting ] = [ ]
738var swiftSyntaxBuilderSwiftSettings : [ SwiftSetting ] = [ ]
839var swiftParserSwiftSettings : [ SwiftSetting ] = [ ]
940
10- /// If we are in a controlled CI environment, we can use internal compiler flags
11- /// to speed up the build or improve it.
12- if ProcessInfo . processInfo. environment [ " SWIFT_BUILD_SCRIPT_ENVIRONMENT " ] != nil {
41+ if buildScriptEnvironment {
1342 swiftSyntaxSwiftSettings += [
1443 . define( " SWIFTSYNTAX_ENABLE_ASSERTIONS " )
1544 ]
1645 swiftSyntaxBuilderSwiftSettings += [
1746 . define( " SWIFTSYNTAX_NO_OSLOG_DEPENDENCY " )
1847 ]
19- swiftParserSwiftSettings += [
20- . define( " SWIFTSYNTAX_NO_OSLOG_DEPENDENCY " )
21- ]
2248}
2349
24- if ProcessInfo . processInfo . environment [ " SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION " ] != nil {
50+ if rawSyntaxValidation {
2551 swiftSyntaxSwiftSettings += [
2652 . define( " SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION " )
2753 ]
2854}
2955
30- if ProcessInfo . processInfo . environment [ " SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION " ] != nil {
56+ if alternateTokenIntrospection {
3157 swiftParserSwiftSettings += [
3258 . define( " SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION " )
3359 ]
3460}
3561
62+ // MARK: - Build the package
63+
3664let package = Package (
3765 name: " swift-syntax " ,
3866 platforms: [
@@ -153,10 +181,11 @@ let package = Package(
153181
154182 . testTarget(
155183 name: " SwiftSyntaxTest " ,
156- dependencies: [ " _SwiftSyntaxTestSupport " , " SwiftSyntax " , " SwiftSyntaxBuilder " ]
184+ dependencies: [ " _SwiftSyntaxTestSupport " , " SwiftSyntax " , " SwiftSyntaxBuilder " ] ,
185+ swiftSettings: swiftSyntaxSwiftSettings
157186 ) ,
158187
159- // MARK: Verison marker modules
188+ // MARK: Version marker modules
160189
161190 . target(
162191 name: " SwiftSyntax509 " ,
@@ -174,7 +203,8 @@ let package = Package(
174203
175204 . testTarget(
176205 name: " SwiftSyntaxBuilderTest " ,
177- dependencies: [ " _SwiftSyntaxTestSupport " , " SwiftSyntaxBuilder " ]
206+ dependencies: [ " _SwiftSyntaxTestSupport " , " SwiftSyntaxBuilder " ] ,
207+ swiftSettings: swiftSyntaxBuilderSwiftSettings
178208 ) ,
179209
180210 // MARK: SwiftSyntaxMacros
@@ -303,7 +333,7 @@ package.targets.append(
303333 )
304334)
305335
306- if ProcessInfo . processInfo . environment [ " SWIFTCI_USE_LOCAL_DEPS " ] == nil {
336+ if useLocalDependencies {
307337 // Building standalone.
308338 package . dependencies += [
309339 . package ( url: " https://github.com/apple/swift-argument-parser.git " , from: " 1.2.2 " )
0 commit comments