Skip to content

Commit ffe4c36

Browse files
authored
Merge pull request #1618 from kimdv/kimdv/migrate-build-script-to-swift
2 parents 4294feb + c148db3 commit ffe4c36

16 files changed

+891
-721
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 SwiftSyntaxDevUtils: 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: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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 buildExample(exampleName: "ExamplePlugin")
33+
34+
try runTests()
35+
36+
logSection("All tests passed")
37+
}
38+
39+
private func runTests() throws {
40+
logSection("Running SwiftSyntax Tests")
41+
42+
if !skipLitTests {
43+
try runLitTests()
44+
}
45+
46+
try runXCTests()
47+
}
48+
49+
private func runLitTests() throws {
50+
logSection("Running lit-based tests")
51+
52+
guard FileManager.default.fileExists(atPath: Paths.litExec.path) else {
53+
throw ScriptExectutionError(
54+
message: """
55+
Error: Could not find lit.py.
56+
Looking at '\(Paths.litExec.path)'.
57+
58+
Make sure you have the llvm repo checked out next to the swift-syntax repo.
59+
Refer to README.md for more information.
60+
"""
61+
)
62+
}
63+
64+
let examplesBinPath = try findExamplesBinPath()
65+
66+
var litCall = [
67+
Paths.litExec.path,
68+
Paths.packageDir.appendingPathComponent("lit_tests").path,
69+
]
70+
71+
if let filecheckExec {
72+
litCall += ["--param", "FILECHECK=" + filecheckExec]
73+
}
74+
75+
litCall += ["--param", "EXAMPLES_BIN_PATH=" + examplesBinPath.path]
76+
litCall += ["--param", "TOOLCHAIN=" + arguments.toolchain.path]
77+
78+
// Print all failures
79+
litCall += ["--verbose"]
80+
// Don't show all commands if verbose is not enabled
81+
if !arguments.verbose {
82+
litCall += ["--succinct"]
83+
}
84+
85+
guard let pythonExec = Paths.python3Exec else {
86+
throw ScriptExectutionError(message: "Didn't find python3 executable")
87+
}
88+
89+
let process = ProcessRunner(
90+
executableURL: pythonExec,
91+
arguments: litCall
92+
)
93+
94+
let processOutput = try process.run(verbose: arguments.verbose)
95+
96+
if !processOutput.stdout.isEmpty {
97+
logSection("lit test stdout")
98+
print(processOutput.stdout)
99+
}
100+
101+
if !processOutput.stderr.isEmpty {
102+
logSection("lit test stderr")
103+
print(processOutput.stderr)
104+
}
105+
}
106+
107+
private func runXCTests() throws {
108+
logSection("Running XCTests")
109+
var swiftpmCallArguments: [String] = []
110+
111+
if arguments.verbose {
112+
swiftpmCallArguments += ["--verbose"]
113+
}
114+
115+
swiftpmCallArguments += ["--test-product", "swift-syntaxPackageTests"]
116+
117+
var additionalEnvironment: [String: String] = [:]
118+
additionalEnvironment["SWIFT_BUILD_SCRIPT_ENVIRONMENT"] = "1"
119+
120+
if arguments.enableRawSyntaxValidation {
121+
additionalEnvironment["SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION"] = "1"
122+
}
123+
124+
if arguments.enableTestFuzzing {
125+
additionalEnvironment["SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION"] = "1"
126+
}
127+
128+
// Tell other projects in the unified build to use local dependencies
129+
additionalEnvironment["SWIFTCI_USE_LOCAL_DEPS"] = "1"
130+
additionalEnvironment["SWIFT_SYNTAX_PARSER_LIB_SEARCH_PATH"] =
131+
arguments.toolchain
132+
.appendingPathComponent("lib")
133+
.appendingPathComponent("swift")
134+
.appendingPathComponent("macosx")
135+
.path
136+
137+
try invokeSwiftPM(
138+
action: "test",
139+
packageDir: Paths.packageDir,
140+
additionalArguments: swiftpmCallArguments,
141+
additionalEnvironment: additionalEnvironment
142+
)
143+
}
144+
145+
private func findSwiftpmBinPath(packageDir: URL) throws -> String {
146+
return try invokeSwiftPM(
147+
action: "build",
148+
packageDir: packageDir,
149+
additionalArguments: ["--show-bin-path"],
150+
additionalEnvironment: [:]
151+
)
152+
}
153+
154+
/// This returns a path to the build examples folder.
155+
/// Example: '<workingDir>/swift-syntax/Examples/.build/arm64-apple-macosx/debug
156+
private func findExamplesBinPath() throws -> URL {
157+
let stdOut = try findSwiftpmBinPath(packageDir: Paths.examplesDir)
158+
return URL(fileURLWithPath: stdOut.trimmingCharacters(in: .whitespacesAndNewlines))
159+
}
160+
}
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(verbose: arguments.verbose)
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)