Skip to content

Commit 7d364e4

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 fcc2b00 commit 7d364e4

File tree

9 files changed

+155
-0
lines changed

9 files changed

+155
-0
lines changed

.bazelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
common --enable_bzlmod
2+
3+
test --test_output=errors

.bazelversion

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.0.0

.gitignore

Lines changed: 3 additions & 0 deletions
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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
load("//utils/bazel:swift_syntax_library.bzl", "swift_syntax_library")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
cc_library(
6+
name = "_CSwiftSyntax",
7+
srcs = glob(["Sources/_CSwiftSyntax/src/*.c"]),
8+
hdrs = glob(["Sources/_CSwiftSyntax/include/*.h"]),
9+
linkstatic = True,
10+
tags = ["swift_module"],
11+
visibility = ["//visibility:private"],
12+
)
13+
14+
swift_syntax_library(
15+
name = "SwiftSyntax",
16+
private_deps = ["_CSwiftSyntax"],
17+
deps = [],
18+
)
19+
20+
swift_syntax_library(
21+
name = "SwiftBasicFormat",
22+
deps = [
23+
":SwiftSyntax",
24+
],
25+
)
26+
27+
swift_syntax_library(
28+
name = "SwiftDiagnostics",
29+
deps = [
30+
":SwiftSyntax",
31+
],
32+
)
33+
34+
swift_syntax_library(
35+
name = "SwiftParser",
36+
deps = [
37+
":SwiftBasicFormat",
38+
":SwiftDiagnostics",
39+
":SwiftSyntax",
40+
],
41+
)
42+
43+
swift_syntax_library(
44+
name = "SwiftParserDiagnostics",
45+
deps = [
46+
":SwiftBasicFormat",
47+
":SwiftDiagnostics",
48+
":SwiftParser",
49+
":SwiftSyntax",
50+
],
51+
)
52+
53+
swift_syntax_library(
54+
name = "SwiftSyntaxBuilder",
55+
deps = [
56+
":SwiftBasicFormat",
57+
":SwiftParser",
58+
":SwiftParserDiagnostics",
59+
":SwiftSyntax",
60+
],
61+
)
62+
63+
swift_syntax_library(
64+
name = "SwiftOperators",
65+
deps = [
66+
":SwiftDiagnostics",
67+
":SwiftParser",
68+
":SwiftSyntax",
69+
],
70+
)

MODULE.bazel

Lines changed: 7 additions & 0 deletions
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")

WORKSPACE

Whitespace-only changes.

utils/bazel/BUILD.bazel

Whitespace-only changes.

utils/bazel/opt_wrapper.bzl

Lines changed: 46 additions & 0 deletions
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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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(
7+
name,
8+
deps,
9+
private_deps = []):
10+
swift_library(
11+
name = name,
12+
srcs = native.glob(
13+
["Sources/{}/**/*.swift".format(name)],
14+
exclude = ["**/*.docc/**"],
15+
allow_empty = False,
16+
),
17+
module_name = name,
18+
deps = deps,
19+
private_deps = private_deps,
20+
)
21+
22+
opt_wrapper(
23+
name = name + "_opt",
24+
dep = name,
25+
)

0 commit comments

Comments
 (0)