Skip to content

Commit fc012b1

Browse files
committed
Migrate build script to Swift
1 parent 36b0f23 commit fc012b1

File tree

13 files changed

+573
-2
lines changed

13 files changed

+573
-2
lines changed

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ open class BasicFormat: SyntaxRewriter {
145145

146146
/// Whether a leading newline on `token` should be added.
147147
open func requiresLeadingNewline(_ token: TokenSyntax) -> Bool {
148-
// We don't want to add newlines inside string interpolation
149-
if isInsideStringInterpolation(token) {
148+
// We don't want to add newlines inside string interpolation.
149+
// Only for multiline quotes `"""`.
150+
if isInsideStringInterpolation(token) && token.tokenKind != .multilineStringQuote {
150151
return false
151152
}
152153

SwiftSyntaxCLI/Package.swift

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

SwiftSyntaxCLI/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SwiftSyntaxCLI
2+
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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, Builder {
17+
@OptionGroup
18+
var arguments: BuilderArguments
19+
20+
func run() throws {
21+
// Until rdar://53881101 is implemented, we cannot request a build of multiple
22+
// targets simultaneously. For now, just build one product after the other.
23+
try buildProduct(productName: "SwiftSyntax")
24+
try buildProduct(productName: "SwiftSyntaxBuilder")
25+
26+
// Build examples
27+
try buildExample(exampleName: "AddOneToIntegerLiterals")
28+
try buildExample(exampleName: "CodeGenerationUsingSwiftSyntaxBuilder")
29+
}
30+
}
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, Generator {
17+
@OptionGroup
18+
var arguments: GeneratorArguments
19+
20+
func run() throws {
21+
try self.runCodeGeneration(sourceDir: Paths.sourcesDir)
22+
}
23+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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, Builder {
17+
@OptionGroup
18+
var arguments: BuilderArguments
19+
20+
@Flag(help: "Don't run lit-based tests")
21+
var skipLitTests: Bool = false
22+
23+
func run() throws {
24+
try buildProduct(productName: "lit-test-helper")
25+
try buildExample(exampleName: "ExamplePlugin")
26+
27+
try runTests()
28+
29+
print("** All tests passed **")
30+
}
31+
32+
private func runTests() throws {
33+
print("** Running SwiftSyntax Tests **")
34+
35+
if !skipLitTests {
36+
try runLitTests()
37+
}
38+
39+
// run_xctests(
40+
// toolchain=toolchain,
41+
// build_dir=build_dir,
42+
// multiroot_data_file=multiroot_data_file,
43+
// release=release,
44+
// enable_rawsyntax_validation=enable_rawsyntax_validation,
45+
// enable_test_fuzzing=enable_test_fuzzing,
46+
// verbose=verbose,
47+
// )
48+
}
49+
50+
private func runLitTests() throws {
51+
print("** Running lit-based tests **")
52+
53+
guard FileManager.default.fileExists(atPath: Paths.litExec.relativePath) else {
54+
fatalError(
55+
"""
56+
Error: Could not find lit.py.
57+
Looking at '\(Paths.litExec.relativePath)'.
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+
guard FileManager.default.fileExists(atPath: Paths.incrTransferRoundtripExec.relativePath) else {
66+
fatalError(
67+
"""
68+
Error: Could not find incr_transfer_round_trip.py.
69+
70+
Make sure you have the main swift repo checked out next to the swift-syntax
71+
repo.
72+
Refer to README.md for more information.
73+
"""
74+
)
75+
}
76+
77+
// product_bin_path = find_product_bin_path(
78+
// toolchain=toolchain, build_dir=build_dir, release=release)
79+
// examples_bin_path = find_examples_bin_path(
80+
// toolchain=toolchain, build_dir=build_dir, release=release)
81+
//
82+
// lit_test_helper_exec = os.path.join(product_bin_path, "lit-test-helper")
83+
//
84+
// lit_call = ["python3", LIT_EXEC]
85+
// lit_call.append(os.path.join(PACKAGE_DIR, "lit_tests"))
86+
//
87+
// if filecheck_exec:
88+
// lit_call.extend(["--param", "FILECHECK=" + filecheck_exec])
89+
// if lit_test_helper_exec:
90+
// lit_call.extend(["--param", "LIT_TEST_HELPER=" + lit_test_helper_exec])
91+
// lit_call.extend(
92+
// ["--param", "INCR_TRANSFER_ROUND_TRIP.PY=" + INCR_TRANSFER_ROUNDTRIP_EXEC]
93+
// )
94+
// lit_call.extend(["--param", "EXAMPLES_BIN_PATH=" + examples_bin_path])
95+
// lit_call.extend(["--param", "TOOLCHAIN=" + toolchain])
96+
//
97+
// # Print all failures
98+
// lit_call.extend(["--verbose"])
99+
// # Don't show all commands if verbose is not enabled
100+
// if not verbose:
101+
// lit_call.extend(["--succinct"])
102+
// check_call(lit_call, verbose=verbose)
103+
}
104+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 VerifySourceCode: ParsableCommand, Generator {
17+
@OptionGroup
18+
var arguments: GeneratorArguments
19+
20+
func run() throws {
21+
let tempDir = FileManager.default.temporaryDirectory
22+
23+
try self.runCodeGeneration(sourceDir: tempDir)
24+
25+
print("** Verifing code generated files **")
26+
27+
for module in Paths.modules {
28+
let selfGeneratedDir = tempDir.appendingPathComponent(module).appendingPathComponent("generated")
29+
let userGeneratedDir = Paths.sourcesDir.appendingPathComponent(module).appendingPathComponent("generated")
30+
try checkGeneratedFilesMatch(selfGeneratedDir: selfGeneratedDir, userGeneratedDir: userGeneratedDir)
31+
}
32+
}
33+
34+
private func checkGeneratedFilesMatch(
35+
selfGeneratedDir: URL,
36+
userGeneratedDir: URL
37+
) throws {
38+
39+
let selfGeneratedFiles = try FileManager.default.contentsOfDirectory(at: selfGeneratedDir, includingPropertiesForKeys: nil)
40+
let userGeneratedFiles = try FileManager.default.contentsOfDirectory(at: userGeneratedDir, includingPropertiesForKeys: nil)
41+
42+
zip(selfGeneratedFiles, userGeneratedFiles)
43+
.forEach {
44+
if arguments.verbose {
45+
print("Comparing \($0.relativePath) with \($1.relativePath) ...")
46+
}
47+
48+
if !FileManager.default.contentsEqual(atPath: $0.relativePath, andPath: $1.relativePath) {
49+
fatalError(
50+
"""
51+
FAIL: code-generated files committed to repository do
52+
not match generated ones. Please re-generate the
53+
code-generated-files using the following command, open a PR to the
54+
SwiftSyntax project and merge it alongside the main PR.
55+
$ swift-syntax/build-script.py generate-source-code
56+
--toolchain /path/to/toolchain.xctoolchain/usr
57+
"""
58+
)
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)