From 7f0601624ceb55c84690ba9fc86a097a4c7cf6b8 Mon Sep 17 00:00:00 2001 From: lberki Date: Fri, 21 Jun 2024 17:41:03 +0200 Subject: [PATCH] Make Stardoc work with --incompatible_enable_proto_toolchain_resolution. (#237) --- BUILD | 1 - MODULE.bazel | 7 +- WORKSPACE | 37 +++++++--- deps.bzl | 1 + maven_install.json | 15 ++++- rules_jvm_external.patch | 142 --------------------------------------- setup.bzl | 22 +++--- toolchains/BUILD.bazel | 12 ++++ 8 files changed, 72 insertions(+), 165 deletions(-) delete mode 100644 rules_jvm_external.patch create mode 100644 toolchains/BUILD.bazel diff --git a/BUILD b/BUILD index ad160379..836e7de6 100644 --- a/BUILD +++ b/BUILD @@ -44,7 +44,6 @@ filegroup( "CONTRIBUTORS", "LICENSE", "maven_install.json", - "rules_jvm_external.patch", "//src/main/java/com/google/devtools/build/stardoc/renderer:srcs", "//src/main/java/com/google/devtools/build/stardoc/rendering:srcs", "//stardoc:distro_srcs", diff --git a/MODULE.bazel b/MODULE.bazel index ef4ebed7..772f5834 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -7,8 +7,10 @@ module( bazel_dep(name = "bazel_skylib", version = "1.6.1") bazel_dep(name = "rules_java", version = "7.6.1") -bazel_dep(name = "rules_jvm_external", version = "5.2") +bazel_dep(name = "rules_jvm_external", version = "6.1") bazel_dep(name = "rules_license", version = "0.0.7") +bazel_dep(name = "rules_proto", version = "6.0.2") +bazel_dep(name = "toolchains_protoc", version = "0.3.1") # Maven artifacts required by Stardoc; keep consistent with deps.bzl STARDOC_MAVEN_ARTIFACTS = [ @@ -17,6 +19,7 @@ STARDOC_MAVEN_ARTIFACTS = [ "com.google.guava:guava:31.1-jre", "com.google.truth:truth:1.1.3", "junit:junit:4.13.2", + "com.google.protobuf:protobuf-java:4.27.1", ] maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven") @@ -36,3 +39,5 @@ use_repo(maven, "stardoc_maven") # # Dev-only and test-only dependencies bazel_dep(name = "rules_pkg", version = "0.10.1", dev_dependency = True) + +register_toolchains("//toolchains:all") diff --git a/WORKSPACE b/WORKSPACE index bc2138fd..d7e67acd 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -70,11 +70,32 @@ rules_pkg_dependencies() # Needed as a transitive dependency of @io_bazel http_archive( name = "rules_proto", - sha256 = "dc3fb206a2cb3441b485eb1e423165b231235a1ea9b031b4433cf7bc1fa460dd", - strip_prefix = "rules_proto-5.3.0-21.7", - urls = [ - "https://github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.7.tar.gz", - ], + sha256 = "6fb6767d1bef535310547e03247f7518b03487740c11b6c6adb7952033fe1295", + strip_prefix = "rules_proto-6.0.2", + url = "https://github.com/bazelbuild/rules_proto/releases/download/6.0.2/rules_proto-6.0.2.tar.gz", +) + +load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies") + +rules_proto_dependencies() + +load("@rules_proto//proto:setup.bzl", "rules_proto_setup") + +rules_proto_setup() + +load("@rules_proto//proto:toolchains.bzl", "rules_proto_toolchains") + +rules_proto_toolchains() + +load("@bazel_features//:deps.bzl", "bazel_features_deps") + +bazel_features_deps() + +load("@toolchains_protoc//protoc:toolchain.bzl", "protoc_toolchains") + +protoc_toolchains( + name = "protoc_toolchains", + version = "v27.1", ) # Needed only for testing stardoc across local-repository bounds. @@ -88,8 +109,4 @@ local_repository( path = "test/testdata/local_repository_test", ) -load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains") - -rules_proto_dependencies() - -rules_proto_toolchains() +register_toolchains("//toolchains:all") diff --git a/deps.bzl b/deps.bzl index 65bf7b9a..35574b79 100644 --- a/deps.bzl +++ b/deps.bzl @@ -24,6 +24,7 @@ STARDOC_MAVEN_ARTIFACTS = [ "com.google.guava:guava:31.1-jre", "com.google.truth:truth:1.1.3", "junit:junit:4.13.2", + "com.google.protobuf:protobuf-java:4.27.1", ] def stardoc_external_deps(): diff --git a/maven_install.json b/maven_install.json index d5755ea0..a3a69aea 100644 --- a/maven_install.json +++ b/maven_install.json @@ -1,7 +1,7 @@ { "__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL", - "__INPUT_ARTIFACTS_HASH": -1307148388, - "__RESOLVED_ARTIFACTS_HASH": 1764595048, + "__INPUT_ARTIFACTS_HASH": 1794655755, + "__RESOLVED_ARTIFACTS_HASH": 2130017368, "artifacts": { "com.beust:jcommander": { "shasums": { @@ -57,6 +57,12 @@ }, "version": "1.3" }, + "com.google.protobuf:protobuf-java": { + "shasums": { + "jar": "798c79f6c9fc6859cc76aa5474228aad250821438e1004dad34880d53aa745f9" + }, + "version": "4.27.1" + }, "com.google.truth:truth": { "shasums": { "jar": "fc0b67782289a2aabfddfdf99eff1dcd5edc890d49143fcd489214b107b8f4f3" @@ -166,6 +172,10 @@ "com.google.j2objc:j2objc-annotations": [ "com.google.j2objc.annotations" ], + "com.google.protobuf:protobuf-java": [ + "com.google.protobuf", + "com.google.protobuf.compiler" + ], "com.google.truth:truth": [ "com.google.common.truth" ], @@ -255,6 +265,7 @@ "com.google.guava:guava", "com.google.guava:listenablefuture", "com.google.j2objc:j2objc-annotations", + "com.google.protobuf:protobuf-java", "com.google.truth:truth", "junit:junit", "org.checkerframework:checker-qual", diff --git a/rules_jvm_external.patch b/rules_jvm_external.patch deleted file mode 100644 index 8344889e..00000000 --- a/rules_jvm_external.patch +++ /dev/null @@ -1,142 +0,0 @@ -commit 569fe4da2530d7b1356265d7cc62ca74f93ec7e5 -Author: Yun Peng -Date: Thu Jan 5 16:37:29 2023 +0800 - - Add targets to make it easier to vendor the `@maven` repository - - This change is required to support Bazel's offline bootstrap build. - More context in https://github.com/bazelbuild/bazel/pull/17112 - - Instead of checking in jar files in Bazel's source tree, Bazel wants to use rules_jvm_external - to fetch jars dependencies. However, to support Bazel's bootstrap build, - we need to patch rules_jvm_external for vendoring the @maven repository. - - - Generate a BUILD.vendor file to be used in the vendored `@maven` repository. - Added a jvm_import and a filegroup rule for each downloaded jar artifact. - The filegroup rule is required by the bootstrap Java toolchain used in Bazel's - bootstrap build. The bootstrap Java toolchain cannot depend on a jvm_import target. - Because the jvm_import rule depends on a java_binary tool "AddJarManifestEntry", - which requires a Java toolchain. Depending on the jar via a filegroup rule helps - avoid this cyclic dependency. - - Added a filegroup rule to collect all sources needed for vendoring `@maven`, - including BUILD.vendor, WORKSPACE and jar files. - -diff --git a/coursier.bzl b/coursier.bzl -index a71507a..90b4cb0 100644 ---- a/coursier.bzl -+++ b/coursier.bzl -@@ -49,6 +49,12 @@ bzl_library( - ) - """ - -+_BUILD_VENDOR = """ -+load("@rules_jvm_external//private/rules:jvm_import.bzl", "jvm_import") -+ -+{vendor_targets} -+""" -+ - DEFAULT_AAR_IMPORT_LABEL = "@build_bazel_rules_android//android:rules.bzl" - - _AAR_IMPORT_STATEMENT = """\ -@@ -473,7 +479,7 @@ def _pinned_coursier_fetch_impl(repository_ctx): - ) - - repository_ctx.report_progress("Generating BUILD targets..") -- (generated_imports, jar_versionless_target_labels) = parser.generate_imports( -+ (generated_imports, jar_versionless_target_labels, generated_vendor_targets) = parser.generate_imports( - repository_ctx = repository_ctx, - dependencies = importer.get_artifacts(maven_install_json_content), - explicit_artifacts = { -@@ -512,6 +518,14 @@ def _pinned_coursier_fetch_impl(repository_ctx): - executable = False, - ) - -+ repository_ctx.file( -+ "BUILD.vendor", -+ (_BUILD_VENDOR).format( -+ vendor_targets = generated_vendor_targets, -+ ), -+ executable = False, -+ ) -+ - _add_outdated_files(repository_ctx, artifacts, repositories) - - # Generate a compatibility layer of external repositories for all jar artifacts. -@@ -1036,7 +1050,7 @@ def _coursier_fetch_impl(repository_ctx): - ) - - repository_ctx.report_progress("Generating BUILD targets..") -- (generated_imports, jar_versionless_target_labels) = parser.generate_imports( -+ (generated_imports, jar_versionless_target_labels, _) = parser.generate_imports( - repository_ctx = repository_ctx, - dependencies = v2_lock_file.get_artifacts(lock_file_contents), - explicit_artifacts = { -diff --git a/private/dependency_tree_parser.bzl b/private/dependency_tree_parser.bzl -index 8eea757..0a53528 100644 ---- a/private/dependency_tree_parser.bzl -+++ b/private/dependency_tree_parser.bzl -@@ -107,6 +107,9 @@ def _generate_imports(repository_ctx, dependencies, explicit_artifacts, neverlin - for jetify_include_artifact in repository_ctx.attr.jetify_include_list: - jetify_include_dict[jetify_include_artifact] = None - -+ artifact_paths = [] -+ vendor_targets = [] -+ - # Iterate through the list of artifacts, and generate the target declaration strings. - for artifact in dependencies: - artifact_path = artifact["file"] -@@ -347,6 +350,7 @@ def _generate_imports(repository_ctx, dependencies, explicit_artifacts, neverlin - target_import_string.append(")") - - all_imports.append("\n".join(target_import_string)) -+ vendor_targets.append("\n".join(target_import_string)) - - # 10. Create a versionless alias target - # -@@ -357,6 +361,9 @@ def _generate_imports(repository_ctx, dependencies, explicit_artifacts, neverlin - versioned_target_alias_label = escape(strip_packaging_and_classifier(artifact["coordinates"])) - all_imports.append("alias(\n\tname = \"%s\",\n\tactual = \"%s\",\n%s)" % - (versioned_target_alias_label, target_label, alias_visibility)) -+ file_group_target_string = "filegroup(\n\tname = \"%s\",\n\tsrcs = [\"%s\"],\n%s)" % (target_label + "_file", artifact_path, alias_visibility) -+ all_imports.append(file_group_target_string) -+ vendor_targets.append(file_group_target_string) - - # 11. If using maven_install.json, use a genrule to copy the file from the http_file - # repository into this repository. -@@ -370,6 +377,9 @@ def _generate_imports(repository_ctx, dependencies, explicit_artifacts, neverlin - if repository_ctx.attr.maven_install_json: - all_imports.append(_genrule_copy_artifact_from_http_file(artifact, default_visibilities)) - -+ # 12. collect the artifact_path for the filegroup rule collecting all necessary sources for vendoring -+ artifact_paths.append("\"%s\"" % artifact_path) -+ - else: # artifact_path == None: - # Special case for certain artifacts that only come with a POM file. - # Such artifacts "aggregate" their dependencies, so they don't have -@@ -421,7 +431,10 @@ def _generate_imports(repository_ctx, dependencies, explicit_artifacts, neverlin - all_imports.append("alias(\n\tname = \"%s\",\n\tactual = \"%s\",\n%s)" % - (versioned_target_alias_label, target_label, alias_visibility)) - -- return ("\n".join(all_imports), jar_versionless_target_labels) -+ all_imports.append("filegroup(\n\tname = \"srcs\",\n\tsrcs = [\n\t\t%s,\n\t],\n\tvisibility = [\"//visibility:public\"],\n)" % -+ (",\n\t\t".join(["\"BUILD.vendor\"", "\"defs.bzl\"", "\"WORKSPACE\""] + artifact_paths))) -+ -+ return ("\n".join(all_imports), jar_versionless_target_labels, "\n".join(vendor_targets)) - - parser = struct( - generate_imports = _generate_imports, -diff --git a/private/tools/java/com/github/bazelbuild/rules_jvm_external/BUILD b/private/tools/java/com/github/bazelbuild/rules_jvm_external/BUILD -index 63ce118..84e0417 100644 ---- a/private/tools/java/com/github/bazelbuild/rules_jvm_external/BUILD -+++ b/private/tools/java/com/github/bazelbuild/rules_jvm_external/BUILD -@@ -1,10 +1,6 @@ - java_library( - name = "rules_jvm_external", - srcs = glob(["*.java"]), -- javacopts = [ -- "--release", -- "8", -- ], - visibility = [ - "//private/tools/java:__subpackages__", - "//tests/com:__subpackages__", diff --git a/setup.bzl b/setup.bzl index 2b14744b..985c2b36 100644 --- a/setup.bzl +++ b/setup.bzl @@ -50,13 +50,11 @@ def stardoc_repositories(): sha256 = "f8ae9ed3887df02f40de9f4f7ac3873e6dd7a471f9cddf63952538b94b59aeb3", ) - RULES_JVM_EXTERNAL_TAG = "5.2" - RULES_JVM_EXTERNAL_SHA = "f86fd42a809e1871ca0aabe89db0d440451219c3ce46c58da240c7dcdc00125f" + RULES_JVM_EXTERNAL_TAG = "6.1" + RULES_JVM_EXTERNAL_SHA = "08ea921df02ffe9924123b0686dc04fd0ff875710bfadb7ad42badb931b0fd50" maybe( http_archive, name = "rules_jvm_external", - patch_args = ["-p1"], - patches = ["@io_bazel_stardoc//:rules_jvm_external.patch"], strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG, sha256 = RULES_JVM_EXTERNAL_SHA, url = "https://github.com/bazelbuild/rules_jvm_external/releases/download/%s/rules_jvm_external-%s.tar.gz" % (RULES_JVM_EXTERNAL_TAG, RULES_JVM_EXTERNAL_TAG), @@ -72,16 +70,22 @@ def stardoc_repositories(): sha256 = "4531deccb913639c30e5c7512a054d5d875698daeb75d8cf90f284375fe7c360", ) + maybe( + http_archive, + name = "toolchains_protoc", + sha256 = "3898f8e621ca5b3c7b94300c5ae19075d5bb28349d6e6f407490a466ba1e2544", + strip_prefix = "toolchains_protoc-0.3.1", + url = "https://github.com/aspect-build/toolchains_protoc/releases/download/v0.3.1/toolchains_protoc-v0.3.1.tar.gz", + ) + # Transitive dep of com_google_protobuf. Unfortunately, protobuf_deps() # pulls in a dep that's too old. maybe( http_archive, name = "rules_proto", - sha256 = "dc3fb206a2cb3441b485eb1e423165b231235a1ea9b031b4433cf7bc1fa460dd", - strip_prefix = "rules_proto-5.3.0-21.7", - urls = [ - "https://github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.7.tar.gz", - ], + sha256 = "6fb6767d1bef535310547e03247f7518b03487740c11b6c6adb7952033fe1295", + strip_prefix = "rules_proto-6.0.2", + url = "https://github.com/bazelbuild/rules_proto/releases/download/6.0.2/rules_proto-6.0.2.tar.gz", ) # Transitive dep of com_google_protobuf. Unfortunately, protobuf_deps() diff --git a/toolchains/BUILD.bazel b/toolchains/BUILD.bazel new file mode 100644 index 00000000..3e020659 --- /dev/null +++ b/toolchains/BUILD.bazel @@ -0,0 +1,12 @@ +proto_lang_toolchain( + name = "protoc_java_toolchain", + command_line = "--java_out=%s", + progress_message = "Generating Java proto_library %{label}", + runtime = "@stardoc_maven//:com_google_protobuf_protobuf_java", +) + +toolchain( + name = "protoc_java_toolchain.registration", + toolchain = ":protoc_java_toolchain", + toolchain_type = "@rules_java//java/proto:toolchain_type", +)