From 6001facc1a96bafed0e414a529b11c1819f0cdbe Mon Sep 17 00:00:00 2001 From: James Wainwright Date: Thu, 22 Feb 2024 07:15:47 +0000 Subject: [PATCH] Add support for bzlmod (#43) * Adopt allowlist attr names Bazel moved to using "allowlist" in Jun 2020 and dropped support for the legacy names in Sep 2023, so newer versions of Bazel don't work. * Update stardoc to v0.6.2 * Run system buildifier in CI * Remove buildifier dependency * Replace buildtools with hello_world in example * Add support for bzlmod modules --- .github/workflows/push.yml | 9 ++++-- .gitignore | 1 + BUILD.bazel | 6 ---- MODULE.bazel | 44 +++++++++++++++++++++++++++ WORKSPACE | 41 ------------------------- WORKSPACE.bzlmod | 16 ++++++++++ docs/MODULE.bazel | 27 ++++++++++++++++ docs/WORKSPACE | 36 ++++++++++++++++------ docs/WORKSPACE.bzlmod | 16 ++++++++++ docs/installer_rule.md | 18 ++++++----- examples/BUILD.bazel | 9 ++++-- examples/MODULE.bazel | 21 +++++++++++++ examples/README.md | 6 ++-- examples/WORKSPACE | 42 ------------------------- examples/WORKSPACE.bzlmod | 16 ++++++++++ examples/hello_world.c | 5 +++ examples/run_all.sh | 4 +-- installer/def.bzl | 4 ++- scripts/run_buildifier.sh | 6 ++-- tests/test_workspace/MODULE.bazel | 15 +++++++++ tests/test_workspace/WORKSPACE.bzlmod | 16 ++++++++++ 21 files changed, 239 insertions(+), 119 deletions(-) create mode 100644 MODULE.bazel create mode 100644 WORKSPACE.bzlmod create mode 100644 docs/MODULE.bazel create mode 100644 docs/WORKSPACE.bzlmod create mode 100644 examples/MODULE.bazel create mode 100644 examples/WORKSPACE.bzlmod create mode 100644 examples/hello_world.c create mode 100644 tests/test_workspace/MODULE.bazel create mode 100644 tests/test_workspace/WORKSPACE.bzlmod diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index ff68c36..c890767 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -12,10 +12,15 @@ jobs: timeout-minutes: 30 steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v4 + + - name: Dependencies + run: | + go install github.com/bazelbuild/buildtools/buildifier@latest + echo "~/go/bin/" >> "$GITHUB_PATH" - name: Bazel cache - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: | ~/.cache/bazel diff --git a/.gitignore b/.gitignore index 360cec3..95ff84e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ **/bazel-* +MODULE.bazel.lock diff --git a/BUILD.bazel b/BUILD.bazel index 0ac0332..5eb4604 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier") - licenses(["notice"]) # Apache 2.0 exports_files([ @@ -21,7 +19,3 @@ exports_files([ "deps.bzl", "setup.bzl", ]) - -buildifier( - name = "buildifier", -) diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 0000000..520fd94 --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,44 @@ +# Copyright 2024 The Bazel Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +module(name = "com_github_google_rules_install") + +bazel_dep(name = "bazel_skylib", version = "1.3.0") + +bazel_dep( + name = "com_github_google_rules_install_examples", + dev_dependency = True, +) +local_path_override( + module_name = "com_github_google_rules_install_examples", + path = "./examples", +) + +bazel_dep( + name = "com_github_google_rules_install_docs", + dev_dependency = True, +) +local_path_override( + module_name = "com_github_google_rules_install_docs", + path = "./docs", +) + +bazel_dep( + name = "com_github_google_rules_install_test_workspace", + dev_dependency = True, +) +local_path_override( + module_name = "com_github_google_rules_install_test_workspace", + path = "./tests/test_workspace", +) diff --git a/WORKSPACE b/WORKSPACE index a419e58..760ab6a 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -31,33 +31,6 @@ local_repository( path = "./tests/test_workspace", ) -http_archive( - name = "com_github_bazelbuild_buildtools", - sha256 = "e3bb0dc8b0274ea1aca75f1f8c0c835adbe589708ea89bf698069d0790701ea3", - strip_prefix = "buildtools-5.1.0", - urls = [ - "https://github.com/bazelbuild/buildtools/archive/refs/tags/5.1.0.tar.gz", - ], -) - -http_archive( - name = "io_bazel_rules_go", - sha256 = "099a9fb96a376ccbbb7d291ed4ecbdfd42f6bc822ab77ae6f1b5cb9e914e94fa", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip", - "https://github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip", - ], -) - -http_archive( - name = "com_google_protobuf", - sha256 = "3bd7828aa5af4b13b99c191e8b1e884ebfa9ad371b0ce264605d347f135d2568", - strip_prefix = "protobuf-3.19.4", - urls = [ - "https://github.com/protocolbuffers/protobuf/archive/v3.19.4.tar.gz", - ], -) - load("@com_github_google_rules_install//:deps.bzl", "install_rules_dependencies") install_rules_dependencies() @@ -65,17 +38,3 @@ install_rules_dependencies() load("@com_github_google_rules_install//:setup.bzl", "install_rules_setup") install_rules_setup() - -load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") - -go_rules_dependencies() - -go_register_toolchains(version = "1.19.2") - -load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") - -protobuf_deps() - -load("@com_github_bazelbuild_buildtools//buildifier:deps.bzl", "buildifier_dependencies") - -buildifier_dependencies() diff --git a/WORKSPACE.bzlmod b/WORKSPACE.bzlmod new file mode 100644 index 0000000..7c64ec1 --- /dev/null +++ b/WORKSPACE.bzlmod @@ -0,0 +1,16 @@ +# Copyright 2024 The Bazel Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This file overrides `WORKSPACE` when bzlmod is active. It is intentionally +# left blank. diff --git a/docs/MODULE.bazel b/docs/MODULE.bazel new file mode 100644 index 0000000..50298f9 --- /dev/null +++ b/docs/MODULE.bazel @@ -0,0 +1,27 @@ +# Copyright 2024 The Bazel Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +module(name = "com_github_google_rules_install_docs") + +bazel_dep(name = "com_github_google_rules_install") +local_path_override( + module_name = "com_github_google_rules_install", + path = "..", +) + +bazel_dep( + name = "stardoc", + version = "0.6.2", + repo_name = "io_bazel_stardoc", +) diff --git a/docs/WORKSPACE b/docs/WORKSPACE index f1c9e0d..f68f432 100644 --- a/docs/WORKSPACE +++ b/docs/WORKSPACE @@ -14,24 +14,42 @@ workspace(name = "com_github_google_rules_install_docs") -load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") local_repository( name = "com_github_google_rules_install", path = "..", ) -git_repository( +http_archive( name = "io_bazel_stardoc", - commit = "4378e9b6bb2831de7143580594782f538f461180", # 0.4.0 - remote = "https://github.com/bazelbuild/stardoc.git", - shallow_since = "1570829166 -0400", + sha256 = "62bd2e60216b7a6fec3ac79341aa201e0956477e7c8f6ccc286f279ad1d96432", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/stardoc/releases/download/0.6.2/stardoc-0.6.2.tar.gz", + "https://github.com/bazelbuild/stardoc/releases/download/0.6.2/stardoc-0.6.2.tar.gz", + ], ) -load("@com_github_google_rules_install//:deps.bzl", "install_rules_dependencies") - -install_rules_dependencies() - load("@io_bazel_stardoc//:setup.bzl", "stardoc_repositories") stardoc_repositories() + +load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps") + +rules_jvm_external_deps() + +load("@rules_jvm_external//:setup.bzl", "rules_jvm_external_setup") + +rules_jvm_external_setup() + +load("@io_bazel_stardoc//:deps.bzl", "stardoc_external_deps") + +stardoc_external_deps() + +load("@stardoc_maven//:defs.bzl", stardoc_pinned_maven_install = "pinned_maven_install") + +stardoc_pinned_maven_install() + +load("@com_github_google_rules_install//:deps.bzl", "install_rules_dependencies") + +install_rules_dependencies() diff --git a/docs/WORKSPACE.bzlmod b/docs/WORKSPACE.bzlmod new file mode 100644 index 0000000..7c64ec1 --- /dev/null +++ b/docs/WORKSPACE.bzlmod @@ -0,0 +1,16 @@ +# Copyright 2024 The Bazel Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This file overrides `WORKSPACE` when bzlmod is active. It is intentionally +# left blank. diff --git a/docs/installer_rule.md b/docs/installer_rule.md index dcf8538..156519c 100755 --- a/docs/installer_rule.md +++ b/docs/installer_rule.md @@ -1,6 +1,10 @@ - +Installer Rules + +Skylark rules for installing files using Bazel. + + ## installer @@ -19,11 +23,11 @@ argument to the installer. | Name | Description | Default Value | -| :-------------: | :-------------: | :-------------: | -| name | A unique name of this rule. | none | -| data | Targets to be installed. File names will not be changed. | none | -| compilation_mode | If not empty, sets compilation_mode of targets in data. | "opt" | -| executable | If True, the copied files will be set as executable. | True | -| target_subdir | Optional subdir under the prefix where the files will be placed. | "" | +| :------------- | :------------- | :------------- | +| name | A unique name of this rule. | none | +| data | Targets to be installed. File names will not be changed. | none | +| compilation_mode | If not empty, sets compilation_mode of targets in data. | `"opt"` | +| executable | If True, the copied files will be set as executable. | `True` | +| target_subdir | Optional subdir under the prefix where the files will be placed. | `""` | diff --git a/examples/BUILD.bazel b/examples/BUILD.bazel index e6dff41..21a7a4c 100644 --- a/examples/BUILD.bazel +++ b/examples/BUILD.bazel @@ -16,7 +16,12 @@ load("@com_github_google_rules_install//installer:def.bzl", "installer") licenses(["notice"]) # Apache 2.0 +cc_binary( + name = "hello_world", + srcs = ["hello_world.c"], +) + installer( - name = "install_buildifier", - data = ["@com_github_bazelbuild_buildtools//buildifier"], + name = "install_hello_world", + data = [":hello_world"], ) diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel new file mode 100644 index 0000000..1606c2f --- /dev/null +++ b/examples/MODULE.bazel @@ -0,0 +1,21 @@ +# Copyright 2024 The Bazel Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +module(name = "com_github_google_rules_install_examples") + +bazel_dep(name = "com_github_google_rules_install") +local_path_override( + module_name = "com_github_google_rules_install", + path = "..", +) diff --git a/examples/README.md b/examples/README.md index 767bd65..8006868 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,9 +1,7 @@ # Example of using install rules for [Bazel](https://bazel.build) -To install -[buildifier from bazelbuild/buildtools](https://github.com/bazelbuild/buildtools/blob/master/buildifier/README.md) -in `~/bin` run: +To install a "hello world" binary in `~/bin` run: ```shell -rules_install/examples$ bazel run -c opt //:install_buildifier ~/bin +rules_install/examples$ bazel run -c opt //:install_hello_world ~/bin ``` diff --git a/examples/WORKSPACE b/examples/WORKSPACE index 0afb898..f209c3d 100644 --- a/examples/WORKSPACE +++ b/examples/WORKSPACE @@ -24,45 +24,3 @@ local_repository( load("@com_github_google_rules_install//:deps.bzl", "install_rules_dependencies") install_rules_dependencies() - -http_archive( - name = "com_github_bazelbuild_buildtools", - strip_prefix = "buildtools-5.1.0", - urls = [ - "https://github.com/bazelbuild/buildtools/archive/refs/tags/5.1.0.tar.gz", - ], -) - -load("@com_github_bazelbuild_buildtools//buildifier:deps.bzl", "buildifier_dependencies") - -buildifier_dependencies() - -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -http_archive( - name = "io_bazel_rules_go", - sha256 = "099a9fb96a376ccbbb7d291ed4ecbdfd42f6bc822ab77ae6f1b5cb9e914e94fa", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip", - "https://github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip", - ], -) - -http_archive( - name = "com_google_protobuf", - sha256 = "3bd7828aa5af4b13b99c191e8b1e884ebfa9ad371b0ce264605d347f135d2568", - strip_prefix = "protobuf-3.19.4", - urls = [ - "https://github.com/protocolbuffers/protobuf/archive/v3.19.4.tar.gz", - ], -) - -load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") - -go_rules_dependencies() - -go_register_toolchains(version = "1.19.2") - -load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") - -protobuf_deps() diff --git a/examples/WORKSPACE.bzlmod b/examples/WORKSPACE.bzlmod new file mode 100644 index 0000000..7c64ec1 --- /dev/null +++ b/examples/WORKSPACE.bzlmod @@ -0,0 +1,16 @@ +# Copyright 2024 The Bazel Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This file overrides `WORKSPACE` when bzlmod is active. It is intentionally +# left blank. diff --git a/examples/hello_world.c b/examples/hello_world.c new file mode 100644 index 0000000..576ad64 --- /dev/null +++ b/examples/hello_world.c @@ -0,0 +1,5 @@ +#include + +int main() { + printf("Hello, world!\n"); +} diff --git a/examples/run_all.sh b/examples/run_all.sh index 60a5b76..920606a 100755 --- a/examples/run_all.sh +++ b/examples/run_all.sh @@ -20,7 +20,7 @@ cd "$(dirname "$0")" declare -r tmpdir="$(mktemp -d)" -bazel run --show_progress_rate_limit=30.0 -c opt :install_buildifier "${tmpdir}" -"${tmpdir}/buildifier" --version +bazel run --show_progress_rate_limit=30.0 -c opt :install_hello_world "${tmpdir}" +"${tmpdir}/hello_world" rm -rf "${tmpdir}" diff --git a/installer/def.bzl b/installer/def.bzl index 381127e..4a7d2be 100644 --- a/installer/def.bzl +++ b/installer/def.bzl @@ -118,7 +118,9 @@ _gen_installer = rule( allow_single_file = True, default = Label(_TEMPLATE_TARGET), ), - "_whitelist_function_transition": attr.label(default = "@bazel_tools//tools/whitelists/function_transition_whitelist"), + "_allowlist_function_transition": attr.label( + default = "@bazel_tools//tools/allowlists/function_transition_allowlist", + ), }, executable = True, outputs = { diff --git a/scripts/run_buildifier.sh b/scripts/run_buildifier.sh index 697b165..d59b8c1 100755 --- a/scripts/run_buildifier.sh +++ b/scripts/run_buildifier.sh @@ -21,12 +21,12 @@ if ! diff -u <(git status --porcelain=v2) /dev/null; then exit 1 fi -if ! bazel run --show_progress_rate_limit=30.0 :buildifier -- --mode=check; then - echo >&2 'Run `bazel run :buildifier`' +if ! buildifier --mode=check -r .; then + echo >&2 'Run `buildifier --mode=fix -r .`' exit 1 fi if ! diff -u <(git status --porcelain=v2) /dev/null; then - echo >&2 'You need to `bazel run //:buildifier`' + echo >&2 'You need to `buildifier --mode=fix -r .`' exit 1 fi diff --git a/tests/test_workspace/MODULE.bazel b/tests/test_workspace/MODULE.bazel new file mode 100644 index 0000000..e952665 --- /dev/null +++ b/tests/test_workspace/MODULE.bazel @@ -0,0 +1,15 @@ +# Copyright 2024 The Bazel Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +module(name = "com_github_google_rules_install_test_workspace") diff --git a/tests/test_workspace/WORKSPACE.bzlmod b/tests/test_workspace/WORKSPACE.bzlmod new file mode 100644 index 0000000..7c64ec1 --- /dev/null +++ b/tests/test_workspace/WORKSPACE.bzlmod @@ -0,0 +1,16 @@ +# Copyright 2024 The Bazel Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This file overrides `WORKSPACE` when bzlmod is active. It is intentionally +# left blank.