Skip to content

Commit a8c689e

Browse files
committed
Replace setup with new Swift-only library
This eliminates the need for handling the internal library, but is potentially still valuable to share this build file, and always force these targets to be built in the opt configuration to avoid performance issues when they're built in dbg
1 parent f40484d commit a8c689e

File tree

9 files changed

+150
-81
lines changed

9 files changed

+150
-81
lines changed

README.md

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
# swift-syntax-bazel
22

33
This repo provides a bazel target for
4-
[`SwiftSyntax`](https://github.com/apple/swift-syntax). Most importantly
5-
it handles vendoring `lib_InternalSwiftSyntaxParser` as a static library
6-
so your tool doesn't depend on a specific Xcode.app path or version.
4+
[`SwiftSyntax`](https://github.com/apple/swift-syntax).
75

86
## Usage
97

108
1. Make sure you've setup
11-
[`rules_apple`](https://github.com/bazelbuild/rules_apple)
9+
[`rules_swift`](https://github.com/bazelbuild/rules_swift)
1210
2. Go to the [releases
1311
page](https://github.com/keith/swift-syntax-bazel/releases) to grab
1412
the WORKSPACE snippet for the Xcode version you're using
@@ -19,18 +17,3 @@ so your tool doesn't depend on a specific Xcode.app path or version.
1917
"@com_github_keith_swift_syntax//:SwiftSyntax",
2018
]
2119
```
22-
23-
## Details
24-
25-
Previously if you built `SwiftSyntax` in your bazel build, the final
26-
binary would end up with a `rpath` like this:
27-
28-
```
29-
/Applications/Xcode-12.4.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx
30-
```
31-
32-
This meant if you used a remote bazel cache in your builds, everyone's
33-
Xcode path would have to match for this to work correctly. This repo
34-
links [a static
35-
binary](https://github.com/keith/StaticInternalSwiftSyntaxParser) for
36-
`lib_InternalSwiftSyntaxParser` instead.

SwiftSyntax.BUILD

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,96 @@
11
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
2+
load("@com_github_keith_swift_syntax_bazel//:opt_wrapper.bzl", "opt_wrapper")
23

34
cc_library(
45
name = "_CSwiftSyntax",
56
srcs = glob(["Sources/_CSwiftSyntax/src/*.c"]),
67
hdrs = glob(["Sources/_CSwiftSyntax/include/*.h"]),
7-
copts = ["-Iexternal/com_github_keith_swift_syntax/Sources/_CSwiftSyntax/include"],
88
linkstatic = True,
99
tags = ["swift_module"],
1010
)
1111

1212
swift_library(
13-
name = "SwiftSyntax",
13+
name = "SwiftSyntax_lib",
1414
srcs = glob(["Sources/SwiftSyntax/**/*.swift"]),
1515
module_name = "SwiftSyntax",
16-
private_deps = ["_CSwiftSyntax"] + select({
17-
"@platforms//os:macos": [
18-
"@StaticInternalSwiftSyntaxParser//:lib_InternalSwiftSyntaxParser",
19-
],
20-
"//conditions:default": [],
21-
}),
16+
private_deps = ["_CSwiftSyntax"],
17+
)
18+
19+
opt_wrapper(
20+
name = "SwiftSyntax",
2221
visibility = ["//visibility:public"],
22+
deps = [
23+
":SwiftSyntax_lib",
24+
],
2325
)
2426

2527
swift_library(
26-
name = "SwiftSyntaxParser",
27-
srcs = glob(["Sources/SwiftSyntaxParser/**/*.swift"]),
28-
module_name = "SwiftSyntaxParser",
29-
private_deps = select({
30-
"@platforms//os:macos": [
31-
"@StaticInternalSwiftSyntaxParser//:lib_InternalSwiftSyntaxParser",
32-
],
33-
"//conditions:default": [],
34-
}),
28+
name = "SwiftBasicFormat",
29+
srcs = glob(["Sources/SwiftBasicFormat/**/*.swift"]),
30+
module_name = "SwiftBasicFormat",
31+
deps = [":SwiftSyntax_lib"],
32+
)
33+
34+
swift_library(
35+
name = "SwiftDiagnostics",
36+
srcs = glob(["Sources/SwiftDiagnostics/**/*.swift"]),
37+
module_name = "SwiftDiagnostics",
38+
deps = [":SwiftSyntax_lib"],
39+
)
40+
41+
swift_library(
42+
name = "SwiftParser_lib",
43+
srcs = glob(["Sources/SwiftParser/**/*.swift"]),
44+
module_name = "SwiftParser",
45+
deps = [
46+
":SwiftBasicFormat",
47+
":SwiftDiagnostics",
48+
":SwiftSyntax_lib",
49+
],
50+
)
51+
52+
opt_wrapper(
53+
name = "SwiftParser",
3554
visibility = ["//visibility:public"],
36-
deps = [":SwiftSyntax"],
55+
deps = [
56+
":SwiftParser_lib",
57+
],
3758
)
3859

3960
swift_library(
40-
name = "SwiftSyntaxBuilder",
61+
name = "SwiftSyntaxBuilder_lib",
4162
srcs = glob(["Sources/SwiftSyntaxBuilder/**/*.swift"]),
4263
module_name = "SwiftSyntaxBuilder",
64+
deps = [
65+
":SwiftBasicFormat",
66+
":SwiftParser_lib",
67+
":SwiftSyntax_lib",
68+
],
69+
)
70+
71+
opt_wrapper(
72+
name = "SwiftSyntaxBuilder",
73+
visibility = ["//visibility:public"],
74+
deps = [
75+
":SwiftSyntaxBuilder_libSwiftParser_lib",
76+
],
77+
)
78+
79+
swift_library(
80+
name = "SwiftOperators_lib",
81+
srcs = glob(["Sources/SwiftOperators/**/*.swift"]),
82+
module_name = "SwiftOperators",
83+
deps = [
84+
":SwiftDiagnostics",
85+
":SwiftParser_lib",
86+
":SwiftSyntax_lib",
87+
],
88+
)
89+
90+
opt_wrapper(
91+
name = "SwiftOperators",
4392
visibility = ["//visibility:public"],
44-
deps = [":SwiftSyntax"],
93+
deps = [
94+
":SwiftOperators_lib",
95+
],
4596
)

WORKSPACE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
44

55
http_archive(
66
name = "build_bazel_rules_apple",
7-
sha256 = "36072d4f3614d309d6a703da0dfe48684ec4c65a89611aeb9590b45af7a3e592",
8-
url = "https://github.com/bazelbuild/rules_apple/releases/download/1.0.1/rules_apple.1.0.1.tar.gz",
7+
sha256 = "90e3b5e8ff942be134e64a83499974203ea64797fd620eddeb71b3a8e1bff681",
8+
url = "https://github.com/bazelbuild/rules_apple/releases/download/1.1.2/rules_apple.1.1.2.tar.gz",
99
)
1010

1111
load(

deps.bzl

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,13 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
33

44
def swift_syntax_deps():
55
"""Fetches dependencies of SwiftSyntax"""
6-
if not native.existing_rule("build_bazel_rules_apple"):
7-
fail("error: this depends on rules_apple but that wasn't setup")
8-
96
if not native.existing_rule("build_bazel_rules_swift"):
107
fail("error: this depends on rules_swift but that wasn't setup")
118

12-
if not native.existing_rule("build_bazel_rules_apple"):
13-
fail("error: this depends on rules_apple but that wasn't setup")
14-
15-
http_archive(
16-
name = "StaticInternalSwiftSyntaxParser",
17-
url = "https://github.com/keith/StaticInternalSwiftSyntaxParser/releases/download/5.7/lib_InternalSwiftSyntaxParser.xcframework.zip",
18-
sha256 = "99803975d10b2664fc37cc223a39b4e37fe3c79d3d6a2c44432007206d49db15",
19-
build_file_content = """
20-
load("@build_bazel_rules_apple//apple:apple.bzl", "apple_static_framework_import")
21-
22-
apple_static_framework_import(
23-
name = "lib_InternalSwiftSyntaxParser",
24-
framework_imports = glob(
25-
["lib_InternalSwiftSyntaxParser.xcframework/macos-arm64_x86_64/lib_InternalSwiftSyntaxParser.framework/**"],
26-
allow_empty = False,
27-
),
28-
visibility = ["//visibility:public"],
29-
)
30-
""",
31-
)
32-
339
http_archive(
3410
name = "com_github_keith_swift_syntax",
11+
sha256 = "7ee32f23e5ee3c72ab71939ed027d973583a6529644790666684a618c605197e",
3512
build_file = "@com_github_keith_swift_syntax_bazel//:SwiftSyntax.BUILD",
36-
sha256 = "d652af27b8a986d8fe4967292839374e06e3a962382318eaf00e0278746b6a41",
37-
strip_prefix = "swift-syntax-swift-5.7-DEVELOPMENT-SNAPSHOT-2022-08-30-a",
38-
url = "https://github.com/apple/swift-syntax/archive/refs/tags/swift-5.7-DEVELOPMENT-SNAPSHOT-2022-08-30-a.tar.gz",
13+
strip_prefix = "swift-syntax-26bf2172cc5b7d54f28b10d18e8b5de351b5c4a3",
14+
url = "https://github.com/apple/swift-syntax/archive/26bf2172cc5b7d54f28b10d18e8b5de351b5c4a3.tar.gz",
3915
)

opt_wrapper.bzl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
A rule for forcing all dependent targets to be built in the opt configuration
3+
4+
This is useful when you're using 'bazel run' with a target, but still want the
5+
benefits of compiler optimizations.
6+
"""
7+
8+
load("@build_bazel_rules_swift//swift:swift.bzl", "SwiftInfo", "swift_common")
9+
10+
def _force_opt_impl(settings, _attr):
11+
return {
12+
"//command_line_option:compilation_mode": "opt",
13+
"//command_line_option:features": settings["//command_line_option:features"] + [
14+
"-swift.opt_uses_osize",
15+
"swift.opt_uses_wmo",
16+
],
17+
}
18+
19+
_force_opt = transition(
20+
implementation = _force_opt_impl,
21+
inputs = [
22+
"//command_line_option:features",
23+
],
24+
outputs = [
25+
"//command_line_option:compilation_mode",
26+
"//command_line_option:features",
27+
],
28+
)
29+
30+
def _impl(ctx):
31+
ccinfos = []
32+
swiftinfos = []
33+
34+
for dep in ctx.attr.deps:
35+
ccinfos.append(dep[CcInfo])
36+
swiftinfos.append(dep[SwiftInfo])
37+
38+
return [
39+
cc_common.merge_cc_infos(direct_cc_infos = ccinfos),
40+
swift_common.create_swift_info(swift_infos = swiftinfos),
41+
]
42+
43+
opt_wrapper = rule(
44+
implementation = _impl,
45+
attrs = {
46+
"deps": attr.label_list(
47+
cfg = _force_opt,
48+
),
49+
"_allowlist_function_transition": attr.label(
50+
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
51+
),
52+
},
53+
)

test/BUILD

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ swift_test(
55
name = "swift_test",
66
size = "small",
77
srcs = ["swift_test.swift"],
8-
deps = ["@com_github_keith_swift_syntax//:SwiftSyntaxParser"],
8+
deps = ["@com_github_keith_swift_syntax//:SwiftParser"],
99
)
1010

1111
swift_binary(
1212
name = "swift_binary",
1313
srcs = ["main.swift"],
1414
visibility = ["//visibility:public"],
15-
deps = ["@com_github_keith_swift_syntax//:SwiftSyntaxParser"],
15+
deps = ["@com_github_keith_swift_syntax//:SwiftParser"],
1616
)
1717

1818
sh_test(
@@ -24,9 +24,10 @@ sh_test(
2424

2525
swift_library(
2626
name = "macos_test_library",
27+
testonly = True,
2728
srcs = ["macos_test_library.swift"],
2829
tags = ["manual"],
29-
deps = ["@com_github_keith_swift_syntax//:SwiftSyntaxParser"],
30+
deps = ["@com_github_keith_swift_syntax//:SwiftParser"],
3031
)
3132

3233
macos_unit_test(
@@ -40,7 +41,7 @@ swift_library(
4041
name = "macos_binary_main",
4142
srcs = ["main.swift"],
4243
tags = ["manual"],
43-
deps = ["@com_github_keith_swift_syntax//:SwiftSyntaxParser"],
44+
deps = ["@com_github_keith_swift_syntax//:SwiftParser"],
4445
)
4546

4647
macos_command_line_application(

test/macos_test_library.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import XCTest
2-
import SwiftSyntaxParser
2+
import SwiftParser
33

44
final class TestLoad: XCTestCase {
55
func testNoThrows() {
6-
_ = try! SyntaxParser.parse(source: "/dev/null")
6+
_ = try! Parser.parse(source: "/dev/null")
77
}
88
}

test/main.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import SwiftSyntaxParser
1+
import SwiftParser
22

3-
_ = try! SyntaxParser.parse(source: "/dev/null")
3+
_ = try! Parser.parse(source: "/dev/null")

test/swift_test.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import XCTest
2-
import SwiftSyntaxParser
2+
import SwiftParser
33

44
final class TestLoad: XCTestCase {
55
func testNoThrows() {
6-
_ = try! SyntaxParser.parse(source: "/dev/null")
6+
_ = try! Parser.parse(source: "/dev/null")
77
}
88
}
99

1010
#if os(Linux)
11-
XCTMain([
12-
testCase([("testNoThrows", TestLoad.testNoThrows)])
13-
])
11+
@main
12+
struct MainWrapper {
13+
static func main() {
14+
XCTMain([
15+
testCase([("testNoThrows", TestLoad.testNoThrows)])
16+
])
17+
}
18+
}
1419
#endif

0 commit comments

Comments
 (0)