blob: 443c6ad25ae04f6454f2b341b55ea2072ce0e791 [file] [log] [blame]
commit 569fe4da2530d7b1356265d7cc62ca74f93ec7e5
Author: Yun Peng <pcloudy@google.com>
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.
- Fix `testonly` attribute handling in Bzlmod.
- Set `fetch_sources` to false by default in Bzlmod.
- Fix `excluded_artifacts` in Bzlmod.
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/private/extensions/maven.bzl b/private/extensions/maven.bzl
index bfba8b8..7f5be73 100644
--- a/private/extensions/maven.bzl
+++ b/private/extensions/maven.bzl
@@ -206,7 +206,7 @@ def _maven_impl(mctx):
to_add.update({"neverlink": artifact.neverlink})
if artifact.testonly:
- to_add.update({"version": artifact.testonly})
+ to_add.update({"testonly": artifact.testonly})
if artifact.exclusions:
artifact_exclusions = []
diff --git a/private/extensions/maven.bzl b/private/extensions/maven.bzl
index 7f5be73..07551f2 100644
--- a/private/extensions/maven.bzl
+++ b/private/extensions/maven.bzl
@@ -38,7 +38,7 @@ _install = tag_class(
# What do we fetch?
"fetch_javadoc": attr.bool(default = False),
- "fetch_sources": attr.bool(default = True),
+ "fetch_sources": attr.bool(default = False),
# Controlling visibility
"strict_visibility": attr.bool(
diff --git a/private/extensions/maven.bzl b/private/extensions/maven.bzl
index 5b37174..bfba8b8 100644
--- a/private/extensions/maven.bzl
+++ b/private/extensions/maven.bzl
@@ -242,7 +242,7 @@ def _maven_impl(mctx):
))
repo["lock_file"] = install.lock_file
- repo["excluded_artifacts"] = _add_exclusions(exclusions, install.excluded_artifacts)
+ repo["excluded_artifacts"] = repo.get("excluded_artifacts", []) + _add_exclusions(exclusions, install.excluded_artifacts)
_logical_or(repo, "fail_if_repin_required", False, install.fail_if_repin_required)
_logical_or(repo, "fail_on_missing_checksum", False, install.fail_on_missing_checksum)