Skip to content

Commit

Permalink
Generate rules_java http_archive() instances from distdir_deps.bzl.
Browse files Browse the repository at this point in the history
This consolidates the specification of the rules_java we depend on into a single place. We generate `http_archive()` stanzas for the main WORKSPACE as well as test harnesses from that, rather than keeping 3 instances in sync by hand. The old practice was error prone because you had to know about all the locations, and an update to one might not break the others immediately, but could be a lurking alignment problem.

This continues the work on #12081.

Closes #12787.

PiperOrigin-RevId: 350761327
  • Loading branch information
aiuto authored and copybara-github committed Jan 8, 2021
1 parent 7e48642 commit b15a8b4
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 71 deletions.
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ filegroup(
srcs = [
"//src/main/java/com/google/devtools/build/lib/bazel/rules:builtins_bzl.zip",
"//src/main/java/com/google/devtools/build/lib/bazel/rules/cpp:cc_configure.WORKSPACE",
"//src/main/java/com/google/devtools/build/lib/bazel/rules/java:jdk.WORKSPACE",
],
)

Expand Down
26 changes: 1 addition & 25 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ distdir_tar(
"bazel-skylib-1.0.3.tar.gz",
# bazelbuild/platforms
"platforms-0.0.2.tar.gz",
# bazelbuild/rules_java
"7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
# bazelbuild/bazel-toolchains
"bazel-toolchains-3.1.0.tar.gz",
# bazelbuild/rules_proto
Expand Down Expand Up @@ -193,8 +191,6 @@ distdir_tar(
"bazel-skylib-1.0.3.tar.gz": "1c531376ac7e5a180e0237938a2536de0c54d93f5c278634818e0efc952dd56c",
# bazelbuild/platforms
"platforms-0.0.2.tar.gz": "48a2d8d343863989c232843e01afc8a986eb8738766bfd8611420a7db8f6f0c3",
# bazelbuild/rules_java
"7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip": "bc81f1ba47ef5cc68ad32225c3d0e70b8c6f6077663835438da8d5733f917598",
# bazelbuild/bazel-toolchains
"bazel-toolchains-3.1.0.tar.gz": "726b5423e1c7a3866a3a6d68e7123b4a955e9fcbe912a51e0f737e6dab1d0af2",
# bazelbuild/rules_proto
Expand Down Expand Up @@ -249,11 +245,6 @@ distdir_tar(
"https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.2/platforms-0.0.2.tar.gz",
"https://github.com/bazelbuild/platforms/releases/download/0.0.2/platforms-0.0.2.tar.gz",
],
# bazelbuild/rules_java
"7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip": [
"https://mirror.bazel.build/github.com/bazelbuild/rules_java/archive/7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
"https://github.com/bazelbuild/rules_java/archive/7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
],
# bazelbuild/bazel-toolchains
"bazel-toolchains-3.1.0.tar.gz": [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-toolchains/releases/download/3.1.0/bazel-toolchains-3.1.0.tar.gz",
Expand Down Expand Up @@ -480,16 +471,10 @@ dist_http_archive(
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_FILE_WIN,
)

http_archive(
dist_http_archive(
name = "rules_java",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_FILE_WIN,
sha256 = "bc81f1ba47ef5cc68ad32225c3d0e70b8c6f6077663835438da8d5733f917598",
strip_prefix = "rules_java-7cf3cefd652008d0a64a419c34c13bdca6c8f178",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_java/archive/7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
"https://github.com/bazelbuild/rules_java/archive/7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
],
)

http_archive(
Expand Down Expand Up @@ -524,8 +509,6 @@ distdir_tar(
"bazel-skylib-1.0.3.tar.gz",
# bazelbuild/platforms
"platforms-0.0.2.tar.gz",
# bazelbuild/rules_java
"7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
# bazelbuild/rules_proto
"7e4afce6fe62dbff0a4a03450143146f9f2d7488.tar.gz",
# protocolbuffers/protobuf
Expand Down Expand Up @@ -558,8 +541,6 @@ distdir_tar(
"bazel-skylib-1.0.3.tar.gz": "1c531376ac7e5a180e0237938a2536de0c54d93f5c278634818e0efc952dd56c",
# bazelbuild/platforms
"platforms-0.0.2.tar.gz": "48a2d8d343863989c232843e01afc8a986eb8738766bfd8611420a7db8f6f0c3",
# bazelbuild/rules_java
"7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip": "bc81f1ba47ef5cc68ad32225c3d0e70b8c6f6077663835438da8d5733f917598",
# bazelbuild/rules_proto
"7e4afce6fe62dbff0a4a03450143146f9f2d7488.tar.gz": "8e7d59a5b12b233be5652e3d29f42fba01c7cbab09f6b3a8d0a57ed6d1e9a0da",
# protocolbuffers/protobuf
Expand Down Expand Up @@ -598,11 +579,6 @@ distdir_tar(
"https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.2/platforms-0.0.2.tar.gz",
"https://github.com/bazelbuild/platforms/releases/download/0.0.2/platforms-0.0.2.tar.gz",
],
# bazelbuild/rules_java
"7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip": [
"https://mirror.bazel.build/github.com/bazelbuild/rules_java/archive/7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
"https://github.com/bazelbuild/rules_java/archive/7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
],
# bazelbuild/rules_proto
"7e4afce6fe62dbff0a4a03450143146f9f2d7488.tar.gz": [
"https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/7e4afce6fe62dbff0a4a03450143146f9f2d7488.tar.gz",
Expand Down
31 changes: 30 additions & 1 deletion distdir_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ DIST_DEPS = {
],
"need_in_test_WORKSPACE": True,
},
"rules_java": {
"archive": "7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
"sha256": "bc81f1ba47ef5cc68ad32225c3d0e70b8c6f6077663835438da8d5733f917598",
"strip_prefix": "rules_java-7cf3cefd652008d0a64a419c34c13bdca6c8f178",
"urls": [
"https://mirror.bazel.build/github.com/bazelbuild/rules_java/archive/7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
"https://github.com/bazelbuild/rules_java/archive/7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
],
"used_in": [
"additional_distfiles",
"test_WORKSPACE_files",
],
"need_in_test_WORKSPACE": True,
},

########################################
#
# Build time dependencies
Expand Down Expand Up @@ -71,7 +86,8 @@ def _gen_workspace_stanza_impl(ctx):
if ctx.attr.template and (ctx.attr.preamble or ctx.attr.postamble):
fail("Can not use template with either preamble or postamble")

repo_clause = """
if ctx.attr.use_maybe:
repo_clause = """
maybe(
http_archive,
"{repo}",
Expand All @@ -80,6 +96,16 @@ maybe(
urls = {urls},
)
"""
else:
repo_clause = """
http_archive(
name = "{repo}",
sha256 = "{sha256}",
strip_prefix = {strip_prefix},
urls = {urls},
)
"""

repo_stanzas = {}
for repo in ctx.attr.repos:
info = DIST_DEPS[repo]
Expand Down Expand Up @@ -116,6 +142,8 @@ maybe(

gen_workspace_stanza = rule(
implementation = _gen_workspace_stanza_impl,
doc = "Use specifications from DIST_DEPS to generate WORKSPACE http_archive stanzas or to fill" +
"drop them into a template.",
attrs = {
"repos": attr.string_list(doc = "Set of repos to inlcude"),
"out": attr.output(mandatory = True),
Expand All @@ -127,5 +155,6 @@ gen_workspace_stanza = rule(
allow_single_file = True,
mandatory = False,
),
"use_maybe": attr.bool(doc = "Use maybe() invocation instead of http_archive"),
},
)
18 changes: 16 additions & 2 deletions src/main/java/com/google/devtools/build/lib/bazel/rules/java/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_java//java:defs.bzl", "java_library")
load("//:distdir_deps.bzl", "gen_workspace_stanza")

package(
default_visibility = ["//src:__subpackages__"],
Expand All @@ -21,9 +22,10 @@ java_library(
resources = glob(
[
"*.txt",
"*.WORKSPACE",
],
),
) + [
":jdk.WORKSPACE",
],
deps = [
":bazel_java_semantics",
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
Expand Down Expand Up @@ -71,3 +73,15 @@ java_library(
"//third_party:jsr305",
],
)

gen_workspace_stanza(
name = "workspace_with_rules_java",
out = "jdk.WORKSPACE",
repos = [
"rules_cc",
"rules_java",
],
template = "jdk.WORKSPACE.tmpl",
use_maybe = True,
visibility = ["//:__pkg__"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -254,28 +254,10 @@ maybe(
],
)

maybe(
http_archive,
"rules_java",
sha256 = "bc81f1ba47ef5cc68ad32225c3d0e70b8c6f6077663835438da8d5733f917598",
strip_prefix = "rules_java-7cf3cefd652008d0a64a419c34c13bdca6c8f178",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_java/archive/7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
"https://github.com/bazelbuild/rules_java/archive/7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
],
)
{rules_java}

# Needed only because of java_tools.
maybe(
http_archive,
"rules_cc",
sha256 = "d0c573b94a6ef20ef6ff20154a23d0efcb409fb0e1ff0979cec318dfe42f0cdd",
strip_prefix = "rules_cc-b1c40e1de81913a3c40e5948f78719c28152486d",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_cc/archive/b1c40e1de81913a3c40e5948f78719c28152486d.zip",
"https://github.com/bazelbuild/rules_cc/archive/b1c40e1de81913a3c40e5948f78719c28152486d.zip",
],
)
{rules_cc}

# Needed only because of java_tools.
maybe(
Expand Down
19 changes: 15 additions & 4 deletions src/test/shell/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//:distdir_deps.bzl", "gen_workspace_stanza")
load("//tools/python:private/defs.bzl", "py_test")

package(default_visibility = ["//visibility:private"])
Expand All @@ -6,7 +7,6 @@ exports_files([
"bin/bazel",
"bin/bazel_jdk_minimal",
"bin/bazel_nojdk",
"testenv.sh",
"integration_test_setup.sh",
"sandboxing_test_utils.sh",
"unittest.bash",
Expand All @@ -22,13 +22,24 @@ filegroup(
visibility = ["//src:__pkg__"],
)

gen_workspace_stanza(
name = "testenv_sh",
out = "testenv.sh",
repos = [
"rules_cc",
"rules_java",
],
template = "testenv.sh.tmpl",
visibility = ["//visibility:public"],
)

sh_library(
name = "bashunit",
srcs = [
"unittest.bash",
"unittest_utils.sh",
],
data = ["testenv.sh"],
data = [":testenv.sh"],
visibility = ["//visibility:public"],
)

Expand Down Expand Up @@ -59,9 +70,9 @@ sh_test(
name = "shell_utils_test",
srcs = ["shell_utils_test.sh"],
data = [
"testenv.sh",
":bashunit",
":shell_utils",
":testenv.sh",
"@bazel_tools//tools/bash/runfiles",
],
)
Expand All @@ -70,9 +81,9 @@ sh_test(
name = "shell_utils_symlinks_test",
srcs = ["shell_utils_symlinks_test.sh"],
data = [
"testenv.sh",
":bashunit",
":shell_utils",
":testenv.sh",
"@bazel_tools//tools/bash/runfiles",
],
tags = [
Expand Down
2 changes: 1 addition & 1 deletion src/test/shell/bazel/external_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2385,7 +2385,7 @@ EOF
# Also verify that the repository class and its definition is reported, to
# help finding out where the implict dependency comes from.
expect_log "Repository data instantiated at:"
expect_log ".../WORKSPACE:47"
expect_log ".../WORKSPACE:[0-9]*"
expect_log "Repository rule data_repo defined at:"
expect_log ".../withimplicit.bzl:6"
}
Expand Down
20 changes: 2 additions & 18 deletions src/test/shell/testenv.sh → src/test/shell/testenv.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -526,31 +526,15 @@ function add_rules_cc_to_workspace() {
cat >> "$1"<<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_cc",
sha256 = "1d4dbbd1e1e9b57d40bb0ade51c9e882da7658d5bfbf22bbd15b68e7879d761f",
strip_prefix = "rules_cc-8bd6cd75d03c01bb82561a96d9c1f9f7157b13d0",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_cc/archive/8bd6cd75d03c01bb82561a96d9c1f9f7157b13d0.zip",
"https://github.com/bazelbuild/rules_cc/archive/8bd6cd75d03c01bb82561a96d9c1f9f7157b13d0.zip",
],
)
{rules_cc}
EOF
}

function add_rules_java_to_workspace() {
cat >> "$1"<<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_java",
sha256 = "bc81f1ba47ef5cc68ad32225c3d0e70b8c6f6077663835438da8d5733f917598",
strip_prefix = "rules_java-7cf3cefd652008d0a64a419c34c13bdca6c8f178",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_java/archive/7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
"https://github.com/bazelbuild/rules_java/archive/7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
],
)
{rules_java}
EOF
}

Expand Down

0 comments on commit b15a8b4

Please sign in to comment.