Skip to content

Commit 0a73615

Browse files
committed
Add bazel configuration
This allows bazel users to depend on these libraries directly through the SwiftSyntax repo instead of everyone having to provide their own duplicate BUILD files. Since swift-syntax is pretty uniform this is setup using a simple bazel macro that uses the conventions of swiftpm to setup each library. This also includes a bazel target for always building a library with optimizations which can be useful if you want the improved performance and aren't trying to debug swift-syntax itself.
1 parent cb3f64d commit 0a73615

10 files changed

+152
-0
lines changed

.bazelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
common --enable_bzlmod
2+
3+
test --test_output=errors

.bazelversion

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.0.0

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
UserInterfaceState.xcuserstate
1010
xcuserdata/
1111

12+
# Ignore bazel symlinks
13+
/bazel-*
14+
1215
# We always build swiftSyntax of trunk dependencies. Ignore any fixed
1316
# dependency versions.
1417
Package.resolved

BUILD.bazel

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
load("//utils/bazel:swift_syntax_library.bzl", "swift_syntax_library")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
swift_syntax_library(
6+
name = "SwiftSyntax",
7+
deps = [],
8+
)
9+
10+
swift_syntax_library(
11+
name = "SwiftBasicFormat",
12+
deps = [
13+
":SwiftSyntax",
14+
],
15+
)
16+
17+
swift_syntax_library(
18+
name = "SwiftDiagnostics",
19+
deps = [
20+
":SwiftSyntax",
21+
],
22+
)
23+
24+
swift_syntax_library(
25+
name = "SwiftParser",
26+
deps = [
27+
":SwiftBasicFormat",
28+
":SwiftDiagnostics",
29+
":SwiftSyntax",
30+
],
31+
)
32+
33+
swift_syntax_library(
34+
name = "SwiftParserDiagnostics",
35+
deps = [
36+
":SwiftBasicFormat",
37+
":SwiftDiagnostics",
38+
":SwiftParser",
39+
":SwiftSyntax",
40+
],
41+
)
42+
43+
swift_syntax_library(
44+
name = "SwiftSyntaxBuilder",
45+
deps = [
46+
":SwiftBasicFormat",
47+
":SwiftParser",
48+
":SwiftParserDiagnostics",
49+
":SwiftSyntax",
50+
],
51+
)
52+
53+
swift_syntax_library(
54+
name = "SwiftOperators",
55+
deps = [
56+
":SwiftDiagnostics",
57+
":SwiftParser",
58+
":SwiftSyntax",
59+
],
60+
)

MODULE.bazel

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module(
2+
name = "swift-syntax",
3+
version = "0.50800.0",
4+
compatibility_level = 1,
5+
)
6+
7+
bazel_dep(name = "rules_swift", version = "1.5.1", repo_name = "build_bazel_rules_swift")

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ Start contributing to SwiftSyntax see [this guide](CONTRIBUTING.md) for more inf
9595

9696
If you should hit any issues while using SwiftSyntax, we appreciate bug reports on [GitHub Issue](https://github.com/apple/swift-syntax/issues).
9797

98+
## Bazel
99+
100+
SwiftSyntax provides an experimental [Bazel](https://bazel.build) build configuration, maintained by Keith Smiley.
101+
To use it you can pull the source archive from the relevant release tag
102+
into your `WORKSPACE` and depend on the libraries you need from the
103+
[`BUILD.bazel`](BUILD.bazel) file. Each library also has an associated
104+
`Library_opt` target (such as `SwiftSyntax_opt`) which forces
105+
SwiftSyntax to always build with optimizations enabled. This may help
106+
local runtime performance at the cost of debuggability, and initial
107+
build time. Please tag any [issues](https://github.com/apple/swift-syntax/issues) related to the Bazel configuration with the label "Bazel".
108+
98109
## License
99110

100111
Please see [LICENSE](LICENSE.txt) for more information.

WORKSPACE

Whitespace-only changes.

utils/bazel/BUILD.bazel

Whitespace-only changes.

utils/bazel/opt_wrapper.bzl

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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")
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+
dep = ctx.attr.dep[0]
32+
return [
33+
dep[CcInfo],
34+
dep[DefaultInfo],
35+
dep[SwiftInfo],
36+
]
37+
38+
opt_wrapper = rule(
39+
implementation = _impl,
40+
attrs = {
41+
"dep": attr.label(cfg = _force_opt, mandatory = True),
42+
"_allowlist_function_transition": attr.label(
43+
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
44+
),
45+
},
46+
)

utils/bazel/swift_syntax_library.bzl

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Convenience wrapper for swift_library targets using this repo's conventions"""
2+
3+
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
4+
load(":opt_wrapper.bzl", "opt_wrapper")
5+
6+
def swift_syntax_library(name, deps):
7+
swift_library(
8+
name = name,
9+
srcs = native.glob(
10+
["Sources/{}/**/*.swift".format(name)],
11+
exclude = ["**/*.docc/**"],
12+
allow_empty = False,
13+
),
14+
module_name = name,
15+
deps = deps,
16+
)
17+
18+
opt_wrapper(
19+
name = name + "_opt",
20+
dep = name,
21+
)

0 commit comments

Comments
 (0)