Skip to content

Commit e68d010

Browse files
committed
Migrate build script to Swift
1 parent f790929 commit e68d010

16 files changed

+872
-726
lines changed

SwiftSyntaxDevUtils/Package.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// swift-tools-version:5.7
2+
3+
import PackageDescription
4+
import Foundation
5+
6+
let package = Package(
7+
name: "swift-syntax-dev-utils",
8+
platforms: [
9+
.macOS(.v10_15)
10+
],
11+
products: [
12+
.executable(name: "swift-syntax-dev-utils", targets: ["swift-syntax-dev-utils"])
13+
],
14+
targets: [
15+
.executableTarget(
16+
name: "swift-syntax-dev-utils",
17+
dependencies: [
18+
.product(name: "ArgumentParser", package: "swift-argument-parser")
19+
]
20+
)
21+
]
22+
)
23+
24+
if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
25+
// Building standalone.
26+
package.dependencies += [
27+
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2")
28+
]
29+
} else {
30+
package.dependencies += [
31+
.package(path: "../../swift-argument-parser")
32+
]
33+
}

SwiftSyntaxDevUtils/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# swift-syntax-dev-utils
2+
3+
Scripts to help build swift-syntax in CI. In most cases, you should not need to run these scripts yourself. The [Contributing Guide](../Contributing.md) contains information of how to build swift-syntax locally.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import ArgumentParser
14+
15+
@main
16+
struct SwiftSyntaxCLI: ParsableCommand {
17+
18+
static var configuration: CommandConfiguration = CommandConfiguration(
19+
abstract: """
20+
Build and test script for SwiftSyntax.
21+
22+
The build script can also drive the test suite included in the SwiftSyntax
23+
repo. This requires a custom build of the compiler project since it accesses
24+
test utilities that are not shipped as part of the toolchains. See the Testing
25+
section for arguments that need to be specified for this.
26+
""",
27+
subcommands: [
28+
Build.self,
29+
GenerateSourceCode.self,
30+
Test.self,
31+
VerifySourceCode.self,
32+
]
33+
)
34+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import ArgumentParser
14+
import Foundation
15+
16+
struct Build: ParsableCommand, BuildCommand {
17+
@OptionGroup
18+
var arguments: BuildArguments
19+
20+
func run() throws {
21+
try buildTarget(packageDir: Paths.packageDir, targetName: "SwiftSyntax-all")
22+
try buildTarget(packageDir: Paths.examplesDir, targetName: "Examples-all")
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import ArgumentParser
14+
import Foundation
15+
16+
struct GenerateSourceCode: ParsableCommand, SourceCodeGeneratorCommand {
17+
@OptionGroup
18+
var arguments: SourceCodeGeneratorArguments
19+
20+
func run() throws {
21+
try self.runCodeGeneration(sourceDir: Paths.sourcesDir)
22+
}
23+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import ArgumentParser
14+
import Foundation
15+
16+
struct Test: ParsableCommand, BuildCommand {
17+
@OptionGroup
18+
var arguments: BuildArguments
19+
20+
@Flag(help: "Don't run lit-based tests")
21+
var skipLitTests: Bool = false
22+
23+
@Option(
24+
help: """
25+
Path to the FileCheck executable that was built as part of the LLVM repository.
26+
If not specified, it will be looked up from PATH.
27+
"""
28+
)
29+
var filecheckExec: String?
30+
31+
func run() throws {
32+
try buildProduct(productName: "lit-test-helper")
33+
try buildExample(exampleName: "ExamplePlugin")
34+
35+
try runTests()
36+
37+
logSection("All tests passed")
38+
}
39+
40+
private func runTests() throws {
41+
logSection("Running SwiftSyntax Tests")
42+
43+
if !skipLitTests {
44+
try runLitTests()
45+
}
46+
47+
try runXCTests()
48+
}
49+
50+
private func runLitTests() throws {
51+
logSection("Running lit-based tests")
52+
53+
guard FileManager.default.fileExists(atPath: Paths.litExec.path) else {
54+
throw ScriptExectutionError(
55+
message: """
56+
Error: Could not find lit.py.
57+
Looking at '\(Paths.litExec.path)'.
58+
59+
Make sure you have the llvm repo checked out next to the swift-syntax repo.
60+
Refer to README.md for more information.
61+
"""
62+
)
63+
}
64+
65+
let productBinPath = try findProductBinPath()
66+
let examplesBinPath = try findExamplesBinPath()
67+
68+
let litTestHelperExec = productBinPath.appendingPathComponent("lit-test-helper")
69+
var litCall = [
70+
Paths.litExec.path,
71+
Paths.packageDir.path,
72+
"lit_tests",
73+
]
74+
75+
if let filecheckExec {
76+
litCall += ["--param", "FILECHECK=" + filecheckExec]
77+
}
78+
79+
litCall += ["--param", "LIT_TEST_HELPER=" + litTestHelperExec.path]
80+
81+
litCall += ["--param", "EXAMPLES_BIN_PATH=" + examplesBinPath.path]
82+
litCall += ["--param", "TOOLCHAIN=" + arguments.toolchain.path]
83+
84+
// Print all failures
85+
litCall += ["--verbose"]
86+
// Don't show all commands if verbose is not enabled
87+
if !arguments.verbose {
88+
litCall += ["--succinct"]
89+
}
90+
91+
guard let pythonExec = Paths.python3Exec else {
92+
throw ScriptExectutionError(message: "Didn't find python3 executable")
93+
}
94+
95+
let process = ProcessRunner(
96+
executableURL: pythonExec,
97+
arguments: litCall
98+
)
99+
100+
try process.run()
101+
}
102+
103+
private func runXCTests() throws {
104+
logSection("Running XCTests")
105+
var swiftpmCallArguments: [String] = []
106+
107+
if arguments.verbose {
108+
swiftpmCallArguments += ["--verbose"]
109+
}
110+
111+
swiftpmCallArguments += ["--test-product", "SwiftSyntaxPackageTests"]
112+
113+
var additionalEnvironment: [String: String] = [:]
114+
additionalEnvironment["SWIFT_BUILD_SCRIPT_ENVIRONMENT"] = "1"
115+
116+
if arguments.enableRawSyntaxValidation {
117+
additionalEnvironment["SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION"] = "1"
118+
}
119+
120+
if arguments.enableTestFuzzing {
121+
additionalEnvironment["SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION"] = "1"
122+
}
123+
124+
// Tell other projects in the unified build to use local dependencies
125+
additionalEnvironment["SWIFTCI_USE_LOCAL_DEPS"] = "1"
126+
additionalEnvironment["SWIFT_SYNTAX_PARSER_LIB_SEARCH_PATH"] =
127+
arguments.toolchain
128+
.appendingPathComponent("lib")
129+
.appendingPathComponent("swift")
130+
.appendingPathComponent("macosx")
131+
.path
132+
133+
try invokeSwiftPM(
134+
action: "test",
135+
packageDir: Paths.packageDir,
136+
additionalArguments: swiftpmCallArguments,
137+
additionalEnvironment: additionalEnvironment
138+
)
139+
}
140+
141+
private func findSwiftpmBinPath(packageDir: URL) throws -> String {
142+
return try invokeSwiftPM(
143+
action: "build",
144+
packageDir: packageDir,
145+
additionalArguments: ["--show-bin-path"],
146+
additionalEnvironment: [:]
147+
)
148+
}
149+
150+
/// This returns a path to the build product folder.
151+
/// Example: '<workingDir>/swift-syntax/.build/arm64-apple-macosx/debug/'
152+
private func findProductBinPath() throws -> URL {
153+
let stdOut = try findSwiftpmBinPath(packageDir: Paths.packageDir)
154+
return URL(fileURLWithPath: stdOut.trimmingCharacters(in: .whitespacesAndNewlines))
155+
}
156+
157+
/// This returns a path to the build examples folder.
158+
/// Example: '<workingDir>/swift-syntax/Examples/.build/arm64-apple-macosx/debug
159+
private func findExamplesBinPath() throws -> URL {
160+
let stdOut = try findSwiftpmBinPath(packageDir: Paths.examplesDir)
161+
return URL(fileURLWithPath: stdOut.trimmingCharacters(in: .whitespacesAndNewlines))
162+
}
163+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import ArgumentParser
14+
import Foundation
15+
16+
fileprivate var modules: [String] {
17+
["SwiftParser", "SwiftParserDiagnostics", "SwiftSyntax", "SwiftSyntaxBuilder"]
18+
}
19+
20+
struct VerifySourceCode: ParsableCommand, SourceCodeGeneratorCommand {
21+
@OptionGroup
22+
var arguments: SourceCodeGeneratorArguments
23+
24+
func run() throws {
25+
let tempDir = FileManager.default.temporaryDirectory
26+
27+
try self.runCodeGeneration(sourceDir: tempDir)
28+
29+
logSection("Verifing code generated files")
30+
31+
guard let diffExec = Paths.diffExec else {
32+
throw ScriptExectutionError(message: "Didn't find a diff execution path")
33+
}
34+
35+
for module in modules {
36+
let selfGeneratedDir = tempDir.appendingPathComponent(module).appendingPathComponent("generated")
37+
let userGeneratedDir = Paths.sourcesDir.appendingPathComponent(module).appendingPathComponent("generated")
38+
39+
let process = ProcessRunner(
40+
executableURL: diffExec,
41+
arguments: [
42+
"--recursive",
43+
"--exclude",
44+
".*", // Exclude dot files like .DS_Store
45+
"--context=0",
46+
selfGeneratedDir.path,
47+
userGeneratedDir.path,
48+
]
49+
)
50+
51+
let result = try process.run()
52+
53+
if !result.stderr.isEmpty {
54+
throw ScriptExectutionError(
55+
message: """
56+
FAIL: code-generated files committed to repository do
57+
not match generated ones. Please re-generate the
58+
code-generated-files using the following command, open a PR to the
59+
SwiftSyntax project and merge it alongside the main PR.
60+
$ swift run swift-syntax-dev-utils generate-source-code
61+
/path/to/toolchain.xctoolchain/usr
62+
"""
63+
)
64+
}
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)