Skip to content

Commit 9298147

Browse files
authored
Replace setup with new Swift-only library (#14)
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 f49eb43 commit 9298147

File tree

10 files changed

+163
-94
lines changed

10 files changed

+163
-94
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@ jobs:
1212
run: sudo xcode-select -s /Applications/Xcode_13.2.app
1313
- name: test
1414
run: bazelisk test //...
15-
# TODO: Re-enable when 5.7.1 is out
16-
# linux-test:
17-
# name: Linux test
18-
# runs-on: ubuntu-20.04
19-
# steps:
20-
# - uses: actions/checkout@v1
21-
# - uses: fwal/setup-swift@286618643423cd7921459c230b7cd8a96c9c7a10
22-
# with:
23-
# swift-version: "5.7"
24-
# - name: Get swift version
25-
# run: swift --version
26-
# - name: test
27-
# run: CC=clang bazelisk test --build_tests_only //test:swift_binary_test //test:swift_test
15+
linux-test:
16+
name: Linux test
17+
runs-on: ubuntu-20.04
18+
steps:
19+
- uses: actions/checkout@v1
20+
- uses: fwal/setup-swift@286618643423cd7921459c230b7cd8a96c9c7a10
21+
with:
22+
swift-version: "5.7"
23+
- name: Get swift version
24+
run: swift --version
25+
- name: test
26+
run: CC=clang bazelisk test --build_tests_only //test:swift_binary_test //test:swift_test

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.1/lib_InternalSwiftSyntaxParser.xcframework.zip",
18-
sha256 = "feb332ba0a027812b1ee7f552321d6069a46207e5cd0f64fa9bb78e2a261b366",
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 = "6a748d2118a5865116ccd1b099b566f6754514d7ecb83413e1cc2c46b5faa619",
3512
build_file = "@com_github_keith_swift_syntax_bazel//:SwiftSyntax.BUILD",
36-
sha256 = "ea96dcd129ed4a05ea0efd7dbe39d929d47c55b1b5b8c2c7a8fce5c7de0bc4d8",
37-
strip_prefix = "swift-syntax-0.50700.1",
38-
url = "https://github.com/apple/swift-syntax/archive/refs/tags/0.50700.1.tar.gz",
13+
strip_prefix = "swift-syntax-1bf320abf560142ce29967328033ae04d447774f",
14+
url = "https://github.com/apple/swift-syntax/archive/1bf320abf560142ce29967328033ae04d447774f.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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ swift_test(
55
name = "swift_test",
66
size = "small",
77
srcs = ["swift_test.swift"],
8-
deps = ["@com_github_keith_swift_syntax//:SwiftSyntaxParser"],
8+
copts = ["-parse-as-library"],
9+
deps = ["@com_github_keith_swift_syntax//:SwiftParser"],
910
)
1011

1112
swift_binary(
1213
name = "swift_binary",
1314
srcs = ["main.swift"],
1415
visibility = ["//visibility:public"],
15-
deps = ["@com_github_keith_swift_syntax//:SwiftSyntaxParser"],
16+
deps = ["@com_github_keith_swift_syntax//:SwiftParser"],
1617
)
1718

1819
sh_test(
@@ -24,9 +25,10 @@ sh_test(
2425

2526
swift_library(
2627
name = "macos_test_library",
28+
testonly = True,
2729
srcs = ["macos_test_library.swift"],
2830
tags = ["manual"],
29-
deps = ["@com_github_keith_swift_syntax//:SwiftSyntaxParser"],
31+
deps = ["@com_github_keith_swift_syntax//:SwiftParser"],
3032
)
3133

3234
macos_unit_test(
@@ -40,7 +42,7 @@ swift_library(
4042
name = "macos_binary_main",
4143
srcs = ["main.swift"],
4244
tags = ["manual"],
43-
deps = ["@com_github_keith_swift_syntax//:SwiftSyntaxParser"],
45+
deps = ["@com_github_keith_swift_syntax//:SwiftParser"],
4446
)
4547

4648
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)