diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 00000000000..9c386031258 --- /dev/null +++ b/.bazelrc @@ -0,0 +1,3 @@ +common --enable_bzlmod + +test --test_output=errors diff --git a/.bazelversion b/.bazelversion new file mode 100644 index 00000000000..09b254e90c6 --- /dev/null +++ b/.bazelversion @@ -0,0 +1 @@ +6.0.0 diff --git a/.gitignore b/.gitignore index 020d21cbcf7..057fece8514 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 00000000000..810a092581e --- /dev/null +++ b/BUILD.bazel @@ -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", + ], +) diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 00000000000..b91985a8795 --- /dev/null +++ b/MODULE.bazel @@ -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") diff --git a/README.md b/README.md index a03bdd425f6..42cad1bd756 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/WORKSPACE b/WORKSPACE new file mode 100644 index 00000000000..e69de29bb2d diff --git a/utils/bazel/BUILD.bazel b/utils/bazel/BUILD.bazel new file mode 100644 index 00000000000..e69de29bb2d diff --git a/utils/bazel/opt_wrapper.bzl b/utils/bazel/opt_wrapper.bzl new file mode 100644 index 00000000000..b100390ef1e --- /dev/null +++ b/utils/bazel/opt_wrapper.bzl @@ -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", + ), + }, +) diff --git a/utils/bazel/swift_syntax_library.bzl b/utils/bazel/swift_syntax_library.bzl new file mode 100644 index 00000000000..b3014c76e54 --- /dev/null +++ b/utils/bazel/swift_syntax_library.bzl @@ -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, + )