Enable Bzlmod for Bazel
Bazel can now be built with Bzlmod with dependencies fetched from [the Bazel Central Registry](https://github.com/bazelbuild/bazel-central-registry) (https://github.com/bazelbuild/bazel-central-registry/issues/7).
Changes in this PR:
- Added MODULE.bazel file for Bazel
- Added MODULE.bazel file for third_party/remoteapis and third_party/googleapis to make them work as Bazel modules
- Updated gson to 2.8.6, because we build with protobuf 3.19.0 with Bzlmod which needs a newer version of gson.
- Added WORKSPACE.bzlmod, this eventually should be empty, but for now we still need some bind rules to make some jar dependencies available for protobuf.
- package-bazel.sh: use wildcard to locate the directory of the platforms repo, whose canonical repo name is `platform.<version>` when building with Bzlmod.
To build Bazel with Bzlmod:
- Copy WORKSPACE.bzlmod to WORKSPACE
- Run `USE_BAZEL_VERSION=last_green bazelisk build --experimental_enable_bzlmod //src:bazel_nojdk`
Closes #14171.
PiperOrigin-RevId: 406097216
diff --git a/MODULE.bazel b/MODULE.bazel
new file mode 100644
index 0000000..ffd8703
--- /dev/null
+++ b/MODULE.bazel
@@ -0,0 +1,36 @@
+# To build Bazel with Bzlmod
+# 1. Copy WORKSPACE.bzlmod to replace the original WORKSPACE file.
+# 2. Run `bazel build --experimental_enable_bzlmod //src:bazel_nojdk`.
+
+module(
+ name = "bazel",
+ version = "6.0.0-pre",
+)
+
+bazel_dep(name = "bazel_skylib", version = "1.0.3")
+bazel_dep(name = "com_google_protobuf", version = "3.19.0")
+bazel_dep(name = "grpc", version = "1.41.0", repo_name = "com_github_grpc_grpc")
+bazel_dep(name = "platforms", version = "0.0.4")
+bazel_dep(name = "rules_pkg", version = "0.5.1")
+bazel_dep(name = "stardoc", version = "0.5.0", repo_name = "io_bazel_skydoc")
+
+# TODO(pcloudy): Add remoteapis and googleapis as Bazel modules in the BCR.
+bazel_dep(name = "remoteapis", version = "")
+bazel_dep(name = "googleapis", version = "")
+local_path_override(
+ module_name = "remoteapis",
+ path = "./third_party/remoteapis",
+)
+local_path_override(
+ module_name = "googleapis",
+ path = "./third_party/googleapis",
+)
+
+# The following are required when building without WORKSPACE SUFFIX
+# TODO(pcloudy): Make building Bazel work without WORKSPACE SUFFIX.
+# This requires migrating all dependencies and toolchain registration defined in
+# WORKSPACE to their corresponding Bazel modules. Eg. remote jdks, java tools
+# and java toolchain registrations all should be moved to rules_java.
+bazel_dep(name = "rules_cc", version = "0.0.1")
+bazel_dep(name = "rules_java", version = "4.0.0")
+bazel_dep(name = "rules_proto", version = "4.0.0")
diff --git a/WORKSPACE.oss.bzlmod b/WORKSPACE.oss.bzlmod
new file mode 100644
index 0000000..5bfcf18
--- /dev/null
+++ b/WORKSPACE.oss.bzlmod
@@ -0,0 +1,31 @@
+# Replace the original WORKSPACE file with this file to build Bazel with bzlmod.
+# We still need the bind rules to make protobuf work.
+workspace(name = "io_bazel")
+
+# Required by @com_google_protobuf//java/util:util in protobuf 3.19.0
+# TODO(pcloudy): Remove those bind rules, when com_google_protobuf can
+# fetch jar dependencies via rules_jvm_external with module extension.
+bind(
+ name = "error_prone_annotations",
+ actual = "//third_party:error_prone_annotations",
+)
+
+bind(
+ name = "j2objc_annotations",
+ actual = "//third_party/java/j2objc-annotations:j2objc-annotations",
+)
+
+bind(
+ name = "gson",
+ actual = "//third_party:gson",
+)
+
+bind(
+ name = "jsr305",
+ actual = "//third_party:jsr305",
+)
+
+bind(
+ name = "guava",
+ actual = "//third_party:guava",
+)
diff --git a/src/package-bazel.sh b/src/package-bazel.sh
index 024b1ec..c7fc3e3 100755
--- a/src/package-bazel.sh
+++ b/src/package-bazel.sh
@@ -78,14 +78,16 @@
cd platforms
# Platform files may be located under external/platform or platform depending
# on the external repository source layout. Take them out if it's the case.
- if [ -d "external/platforms" ]; then
+ # Note that, when enabling Bzlmod, the canonical repo name for platforms is platforms.<version>,
+ # therefore, we use wildcard (platform*) to make sure it always work.
+ if ls external/platforms*/ >/dev/null 2>&1; then
# --experimental_sibling_repository_layout=false
- mv external/platforms/* .
- rmdir -p external/platforms
+ mv external/platforms*/* .
+ rmdir -p external/platforms*
else
# --experimental_sibling_repository_layout=true
- mv platforms/* .
- rmdir -p platforms
+ mv platforms*/* .
+ rmdir -p platforms*
fi
>> WORKSPACE
)