Skip to content

Add bazel configuration #1251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
common --enable_bzlmod

test --test_output=errors
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.0.0
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
UserInterfaceState.xcuserstate
xcuserdata/

# Ignore bazel symlinks
/bazel-*

# We always build swiftSyntax of trunk dependencies. Ignore any fixed
# dependency versions.
Package.resolved
Expand Down
60 changes: 60 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
load("//utils/bazel:swift_syntax_library.bzl", "swift_syntax_library")

package(default_visibility = ["//visibility:public"])

swift_syntax_library(
name = "SwiftSyntax",
deps = [],
)

swift_syntax_library(
name = "SwiftBasicFormat",
deps = [
":SwiftSyntax",
],
)

swift_syntax_library(
name = "SwiftDiagnostics",
deps = [
":SwiftSyntax",
],
)

swift_syntax_library(
name = "SwiftParser",
deps = [
":SwiftBasicFormat",
":SwiftDiagnostics",
":SwiftSyntax",
],
)

swift_syntax_library(
name = "SwiftParserDiagnostics",
deps = [
":SwiftBasicFormat",
":SwiftDiagnostics",
":SwiftParser",
":SwiftSyntax",
],
)

swift_syntax_library(
name = "SwiftSyntaxBuilder",
deps = [
":SwiftBasicFormat",
":SwiftParser",
":SwiftParserDiagnostics",
":SwiftSyntax",
],
)

swift_syntax_library(
name = "SwiftOperators",
deps = [
":SwiftDiagnostics",
":SwiftParser",
":SwiftSyntax",
],
)
7 changes: 7 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module(
name = "swift-syntax",
version = "0.50800.0",
compatibility_level = 1,
)

bazel_dep(name = "rules_swift", version = "1.5.1", repo_name = "build_bazel_rules_swift")
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ Start contributing to SwiftSyntax see [this guide](CONTRIBUTING.md) for more inf

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

## Bazel

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

## License

Please see [LICENSE](LICENSE.txt) for more information.
Empty file added WORKSPACE
Empty file.
Empty file added utils/bazel/BUILD.bazel
Empty file.
46 changes: 46 additions & 0 deletions utils/bazel/opt_wrapper.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
A rule for forcing all dependent targets to be built in the opt configuration

This is useful when you're using 'bazel run' with a target, but still want the
benefits of compiler optimizations.
"""

load("@build_bazel_rules_swift//swift:swift.bzl", "SwiftInfo")

def _force_opt_impl(settings, _attr):
return {
"//command_line_option:compilation_mode": "opt",
"//command_line_option:features": settings["//command_line_option:features"] + [
"-swift.opt_uses_osize",
"swift.opt_uses_wmo",
],
}

_force_opt = transition(
implementation = _force_opt_impl,
inputs = [
"//command_line_option:features",
],
outputs = [
"//command_line_option:compilation_mode",
"//command_line_option:features",
],
)

def _impl(ctx):
dep = ctx.attr.dep[0]
return [
dep[CcInfo],
dep[DefaultInfo],
dep[SwiftInfo],
]

opt_wrapper = rule(
implementation = _impl,
attrs = {
"dep": attr.label(cfg = _force_opt, mandatory = True),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
)
21 changes: 21 additions & 0 deletions utils/bazel/swift_syntax_library.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Convenience wrapper for swift_library targets using this repo's conventions"""

load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
load(":opt_wrapper.bzl", "opt_wrapper")

def swift_syntax_library(name, deps):
swift_library(
name = name,
srcs = native.glob(
["Sources/{}/**/*.swift".format(name)],
exclude = ["**/*.docc/**"],
allow_empty = False,
),
module_name = name,
deps = deps,
)

opt_wrapper(
name = name + "_opt",
dep = name,
)