From 97d11834de2031ede9310f85ca0fc82c2a66604e Mon Sep 17 00:00:00 2001 From: Brad Corso Date: Sat, 20 Jul 2024 13:55:14 -0700 Subject: [PATCH] Clean up Bazel workspace rules * Migrated most WORKSPACE deps to MODULE.bazel (will be required in Bazel 9.x). * Use `.bazelversion` to declare Bazel version instead of `USE_BAZEL_VERSION` env variable (makes it easier to run locally with same bazel version). * Explicitly set `source`/`target` for `dagger.android` api, and use `.bazelrc` to set default `source`/`target` for annotation processors. * Removed unused bazel common dependencies in WORKSPACE * Removed `bazel_skylib` * Removed `google_bazel_common` * Removed `rules_python` * Removed `zlib` * Removed obsolete `tools/bazel.rc` config RELNOTES=N/A PiperOrigin-RevId: 654352167 --- .bazelrc | 24 ++++++ .bazelversion | 1 + .github/workflows/ci.yml | 3 - .github/workflows/release.yml | 3 - MODULE.bazel | 40 ++++++++++ WORKSPACE | 122 +----------------------------- build_defs.bzl | 2 +- java/dagger/android/BUILD | 3 + java/dagger/android/support/BUILD | 3 + test_defs.bzl | 1 + tools/bazel.rc | 11 --- tools/maven/maven.bzl | 5 +- 12 files changed, 79 insertions(+), 139 deletions(-) create mode 100644 .bazelversion create mode 100644 MODULE.bazel delete mode 100644 tools/bazel.rc diff --git a/.bazelrc b/.bazelrc index ecd54a58a52..552f211b6da 100644 --- a/.bazelrc +++ b/.bazelrc @@ -6,3 +6,27 @@ build --host_javacopt=-g # TODO(ronshapiro): explore how much work it would be to reenable this build --javacopt="-Xep:BetaApi:OFF" build --host_javacopt="-Xep:BetaApi:OFF" + +# Enable Bzlmod for every Bazel command. This flag will be enabled by default in +# Bazel 7.0.0, but for now we enable it manually. +common --enable_bzlmod + +# Note: This flag is required to prevent actions from clashing with each when +# reading/writing tmp files. Without this flag we get errors like: +# +# Error: Cannot use file /tmp/hsperfdata_runner/12 because it is locked by +# another process +# +# This flag will be enabled by default in Bazel 7.0.0, but for now we enable it +# manually. For more details: https://github.com/bazelbuild/bazel/issues/3236. +build --incompatible_sandbox_hermetic_tmp + +# Sets the JDK for compiling sources and executing tests. +build --java_language_version=11 +build --tool_java_language_version=11 +build --java_runtime_version=remotejdk_11 +build --tool_java_runtime_version=remotejdk_11 + +# Sets the default source/target for Dagger's processors. Note that Dagger's api +# libraries can target JDK 8, so we set it explicitly on the Bazel target. +build --javacopt="-source 8 -target 11" \ No newline at end of file diff --git a/.bazelversion b/.bazelversion new file mode 100644 index 00000000000..c0be8a7992a --- /dev/null +++ b/.bazelversion @@ -0,0 +1 @@ +6.4.0 \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4a18be6594d..f25c9a4160b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,9 +13,6 @@ env: USE_JAVA_VERSION: '11' # This is required by AGP 8.3+. USE_JAVA_VERSION_FOR_PLUGIN: '17' - # Our Bazel builds currently rely on 6.4.0. The version is set via - # baselisk by USE_BAZEL_VERSION: https://github.com/bazelbuild/bazelisk. - USE_BAZEL_VERSION: '6.4.0' # The default Maven 3.9.0 has a regression so we manually install 3.8.7. # https://issues.apache.org/jira/browse/MNG-7679 USE_MAVEN_VERSION: '3.8.7' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 31ecc66bb78..3bb86d8e447 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,9 +12,6 @@ env: USE_JAVA_VERSION: '11' # This is required by AGP 8.3+. USE_JAVA_VERSION_FOR_PLUGIN: '17' - # Our Bazel builds currently rely on 6.4.0. The version is set via - # baselisk by USE_BAZEL_VERSION: https://github.com/bazelbuild/bazelisk. - USE_BAZEL_VERSION: '6.4.0' DAGGER_RELEASE_VERSION: "${{ github.event.inputs.dagger_release_version }}" # The default Maven 3.9.0 has a regression so we manually install 3.8.7. # https://issues.apache.org/jira/browse/MNG-7679 diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 00000000000..2cfed343ad8 --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,40 @@ +# Copyright (C) 2024 The Dagger 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 +# +# http://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. + +bazel_dep( + name = "bazel_skylib", + version = "1.7.1", +) + +bazel_dep( + name = "rules_java", + version = "7.0.6", +) + +bazel_dep( + name = "rules_kotlin", + version = "1.9.0", + repo_name = "io_bazel_rules_kotlin", +) + +bazel_dep( + name = "rules_jvm_external", + version = "5.3", +) + +bazel_dep( + name = "rules_robolectric", + version = "4.11.1", + repo_name = "robolectric", +) diff --git a/WORKSPACE b/WORKSPACE index ec2774bc7ed..27a43bc1a18 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -13,8 +13,6 @@ # limitations under the License. -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - ############################# # Load nested repository ############################# @@ -26,37 +24,6 @@ local_repository( path = "examples/bazel", ) -############################# -# Load Bazel Skylib rules -############################# - -BAZEL_SKYLIB_VERSION = "1.5.0" - -BAZEL_SKYLIB_SHA = "cd55a062e763b9349921f0f5db8c3933288dc8ba4f76dd9416aac68acee3cb94" - -http_archive( - name = "bazel_skylib", - sha256 = BAZEL_SKYLIB_SHA, - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/%s/bazel-skylib-%s.tar.gz" % (BAZEL_SKYLIB_VERSION, BAZEL_SKYLIB_VERSION), - "https://github.com/bazelbuild/bazel-skylib/releases/download/%s/bazel-skylib-%s.tar.gz" % (BAZEL_SKYLIB_VERSION, BAZEL_SKYLIB_VERSION), - ], -) - -load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") - -bazel_skylib_workspace() - -############################# -# Load rules_java repository -############################# - -http_archive( - name = "rules_java", - sha256 = "c73336802d0b4882e40770666ad055212df4ea62cfa6edf9cb0f9d29828a0934", - url = "https://github.com/bazelbuild/rules_java/releases/download/5.3.5/rules_java-5.3.5.tar.gz", -) - ############################# # Load Android Sdk ############################# @@ -67,84 +34,10 @@ android_sdk_repository( build_tools_version = "32.0.0", ) -#################################################### -# Load Protobuf repository (needed by bazel-common) -#################################################### - -http_archive( - name = "rules_proto", - # output from `sha256sum` on the downloaded tar.gz file - sha256 = "66bfdf8782796239d3875d37e7de19b1d94301e8972b3cbd2446b332429b4df1", - strip_prefix = "rules_proto-4.0.0", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz", - "https://github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz", - ], -) - -load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains") - -rules_proto_dependencies() - -rules_proto_toolchains() - -############################# -# Load Protobuf dependencies -############################# - -# rules_python and zlib are required by protobuf. -# TODO(ronshapiro): Figure out if zlib is in fact necessary, or if proto can depend on the -# @bazel_tools library directly. See discussion in -# https://github.com/protocolbuffers/protobuf/pull/5389#issuecomment-481785716 -# TODO(cpovirk): Should we eventually get rules_python from "Bazel Federation?" -# https://github.com/bazelbuild/rules_python#getting-started - -http_archive( - name = "rules_python", - sha256 = "e5470e92a18aa51830db99a4d9c492cc613761d5bdb7131c04bd92b9834380f6", - strip_prefix = "rules_python-4b84ad270387a7c439ebdccfd530e2339601ef27", - urls = ["https://github.com/bazelbuild/rules_python/archive/4b84ad270387a7c439ebdccfd530e2339601ef27.tar.gz"], -) - -http_archive( - name = "zlib", - build_file = "@com_google_protobuf//:third_party/zlib.BUILD", - sha256 = "629380c90a77b964d896ed37163f5c3a34f6e6d897311f1df2a7016355c45eff", - strip_prefix = "zlib-1.2.11", - urls = ["https://github.com/madler/zlib/archive/v1.2.11.tar.gz"], -) - -############################# -# Load Robolectric repository -############################# - -ROBOLECTRIC_VERSION = "4.4" - -http_archive( - name = "robolectric", - sha256 = "d4f2eb078a51f4e534ebf5e18b6cd4646d05eae9b362ac40b93831bdf46112c7", - strip_prefix = "robolectric-bazel-%s" % ROBOLECTRIC_VERSION, - urls = ["https://github.com/robolectric/robolectric-bazel/archive/%s.tar.gz" % ROBOLECTRIC_VERSION], -) - -load("@robolectric//bazel:robolectric.bzl", "robolectric_repositories") - -robolectric_repositories() - ############################# # Load Kotlin repository ############################# -RULES_KOTLIN_TAG = "v1.8" - -RULES_KOTLIN_SHA = "01293740a16e474669aba5b5a1fe3d368de5832442f164e4fbfc566815a8bc3a" - -http_archive( - name = "io_bazel_rules_kotlin", - sha256 = RULES_KOTLIN_SHA, - urls = ["https://github.com/bazelbuild/rules_kotlin/releases/download/%s/rules_kotlin_release.tgz" % RULES_KOTLIN_TAG], -) - load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories", "kotlinc_version") KOTLIN_VERSION = "1.9.23" @@ -167,17 +60,6 @@ kt_register_toolchains() # Load Maven dependencies ############################# -RULES_JVM_EXTERNAL_TAG = "4.5" - -RULES_JVM_EXTERNAL_SHA = "b17d7388feb9bfa7f2fa09031b32707df529f26c91ab9e5d909eb1676badd9a6" - -http_archive( - name = "rules_jvm_external", - sha256 = RULES_JVM_EXTERNAL_SHA, - strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG, - url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG, -) - load("@rules_jvm_external//:defs.bzl", "maven_install") ANDROID_LINT_VERSION = "30.1.0" @@ -301,8 +183,8 @@ maven_install( "org.ow2.asm:asm:%s" % ASM_VERSION, "org.ow2.asm:asm-tree:%s" % ASM_VERSION, "org.ow2.asm:asm-commons:%s" % ASM_VERSION, - "org.robolectric:robolectric:4.4", - "org.robolectric:shadows-framework:4.4", # For ActivityController + "org.robolectric:robolectric:%s" % ROBOLECTRIC_VERSION, + "org.robolectric:shadows-framework:%s" % ROBOLECTRIC_VERSION, # For ActivityController ], repositories = [ "https://repo1.maven.org/maven2", diff --git a/build_defs.bzl b/build_defs.bzl index 70afaf0bfb1..6c2aa65b7da 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -19,7 +19,7 @@ DOCLINT_HTML_AND_SYNTAX = ["-Xdoclint:html,syntax"] DOCLINT_REFERENCES = ["-Xdoclint:reference"] JAVA_RELEASE_MIN = [ - "-source 7 -target 7", + "-source 8 -target 8", ] POM_VERSION = "${project.version}" diff --git a/java/dagger/android/BUILD b/java/dagger/android/BUILD index e8e0824ba22..e445d2e21fb 100644 --- a/java/dagger/android/BUILD +++ b/java/dagger/android/BUILD @@ -17,6 +17,8 @@ load( "//:build_defs.bzl", + "DOCLINT_HTML_AND_SYNTAX", + "JAVA_RELEASE_MIN", "POM_VERSION", ) load("//tools:dejetify.bzl", "dejetified_library") @@ -43,6 +45,7 @@ filegroup( android_library( name = "android", srcs = SRCS, + javacopts = JAVA_RELEASE_MIN + DOCLINT_HTML_AND_SYNTAX, plugins = [ "//java/dagger/android/internal/proguard:plugin", ], diff --git a/java/dagger/android/support/BUILD b/java/dagger/android/support/BUILD index f404f1ae969..bedf1ffeb65 100644 --- a/java/dagger/android/support/BUILD +++ b/java/dagger/android/support/BUILD @@ -17,6 +17,8 @@ load( "//:build_defs.bzl", + "DOCLINT_HTML_AND_SYNTAX", + "JAVA_RELEASE_MIN", "POM_VERSION", ) load("//tools:dejetify.bzl", "dejetified_library") @@ -36,6 +38,7 @@ filegroup( android_library( name = "support", srcs = glob(["*.java"]), + javacopts = JAVA_RELEASE_MIN + DOCLINT_HTML_AND_SYNTAX, tags = ["maven_coordinates=com.google.dagger:dagger-android-support:" + POM_VERSION], deps = [ "//:dagger_with_compiler", diff --git a/test_defs.bzl b/test_defs.bzl index 3b136261ba6..45db3cf76c6 100644 --- a/test_defs.bzl +++ b/test_defs.bzl @@ -16,6 +16,7 @@ load("@rules_java//java:defs.bzl", "java_library", "java_test") load("//:build_defs.bzl", "JAVA_RELEASE_MIN") + load( "@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library", diff --git a/tools/bazel.rc b/tools/bazel.rc deleted file mode 100644 index 2707c1c3ac6..00000000000 --- a/tools/bazel.rc +++ /dev/null @@ -1,11 +0,0 @@ -# Global bazelrc file (see https://bazel.build/run/bazelrc#global-bazelrc) - -# Note: This flag is required to prevent actions from clashing with each when -# reading/writing tmp files. Without this flag we get errors like: -# -# Error: Cannot use file /tmp/hsperfdata_runner/12 because it is locked by -# another process -# -# This flag will be enabled by default in Bazel 7.0.0, but for now we enable it -# manually. For more details: https://github.com/bazelbuild/bazel/issues/3236. -build --incompatible_sandbox_hermetic_tmp \ No newline at end of file diff --git a/tools/maven/maven.bzl b/tools/maven/maven.bzl index 29036550e48..98069f9ae61 100644 --- a/tools/maven/maven.bzl +++ b/tools/maven/maven.bzl @@ -356,7 +356,10 @@ def _validate_list(name, actual_list, expected_list, banned_list = []): fail("\t[Error]: Found banned {}: \n\t\t".format(name) + "\n\t\t".join(banned)) def _strip_artifact_version(artifact): - return artifact.rsplit(":", 1)[0] + artifact = artifact.rsplit(":", 1)[0] + if artifact.endswith(":aar"): + return artifact.rsplit(":aar", 1)[0] + return artifact _validate_maven_deps = rule( implementation = _validate_maven_deps_impl,