Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate rules/cpp/cc_configure.WORKSPACE from distdir_deps.bzl rather than keeping it in sync by hand #12743

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5f8de55
More progress on #12081.
aiuto Dec 18, 2020
2f17277
Merge remote-tracking branch 'upstream/master'
aiuto Dec 21, 2020
8dbb4fa
fix my main to match upstream
aiuto Dec 21, 2020
671f883
generate rules/cpp/cc_configure.WORKSPACE from distdir_deps.bzl rathe…
aiuto Dec 22, 2020
a9696ad
include cc_configure.WORKSPACE in distdir srcs
aiuto Dec 23, 2020
e2404d0
Merge remote-tracking branch 'upstream/master'
aiuto Dec 23, 2020
96a4dc7
Merge branch 'master' into gen_ws
aiuto Dec 23, 2020
d563318
Merge remote-tracking branch 'upstream/master'
aiuto Dec 23, 2020
094a386
Merge branch 'master' into gen_ws
aiuto Dec 23, 2020
ca2cde0
Merge remote-tracking branch 'upstream/master'
aiuto Dec 31, 2020
d21c3c7
Merge branch 'master' into gen_ws
aiuto Dec 31, 2020
7d67fed
grasping at straws
aiuto Jan 5, 2021
56bb0f5
Merge remote-tracking branch 'upstream/master'
aiuto Jan 5, 2021
69d8ef3
Merge branch 'master' into gen_ws
aiuto Jan 5, 2021
a0e3ac2
Merge remote-tracking branch 'upstream/master'
aiuto Jan 5, 2021
2f5d93c
Merge branch 'master' into gen_ws
aiuto Jan 5, 2021
74a20a5
account for the generated resource
aiuto Jan 5, 2021
c9e144f
make cc_configur.WORKSPACEW visbile
aiuto Jan 5, 2021
6ca2a9e
make cc_configure.WORKSPACE visbile to //
aiuto Jan 5, 2021
2fa47ef
fix fixibilty and remove crufty bits
aiuto Jan 5, 2021
b749857
Merge remote-tracking branch 'upstream/master'
aiuto Jan 5, 2021
142e63a
Merge branch 'master' into gen_ws
aiuto Jan 5, 2021
47f4a46
Merge branch 'gen_ws' of github.com:aiuto/bazel into gen_ws
aiuto Jan 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ filegroup(
# be included in //src:derived_java_sources).
filegroup(
name = "generated_resources",
srcs = ["//src/main/java/com/google/devtools/build/lib/bazel/rules:builtins_bzl.zip"],
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",
],
)

pkg_tar(
Expand Down
26 changes: 26 additions & 0 deletions distdir.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
# limitations under the License.
"""Defines a repository rule that generates an archive consisting of the specified files to fetch"""

load("//:distdir_deps.bzl", "DIST_DEPS")
load("//tools/build_defs/repo:http.bzl", "http_archive")

_BUILD = """
load("@rules_pkg//:pkg.bzl", "pkg_tar")

Expand Down Expand Up @@ -70,3 +73,26 @@ def distdir_tar(name, archives, sha256, urls, dirname, dist_deps = None):
urls = urls,
dirname = dirname,
)

def dist_http_archive(name, **kwargs):
"""Wraps http_archive but takes sha and urls from DIST_DEPS.

dist_http_archive wraps an http_archive invocation, but looks up relevant
information from DIST_DEPS so the user does not have to specify it. It
always strips sha256 and urls from kwargs.

Args:
name: repo name
**kwargs: see http_archive for allowed args.
"""
info = DIST_DEPS[name]
if "patches" not in kwargs:
kwargs["patches"] = info.get("patches")
if "strip_prefix" not in kwargs:
kwargs["strip_prefix"] = info.get("strip_prefix")
http_archive(
name = name,
sha256 = info["sha256"],
urls = info["urls"],
**kwargs
)
63 changes: 63 additions & 0 deletions distdir_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,66 @@ DIST_DEPS = {
],
},
}

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 = """
maybe(
http_archive,
"{repo}",
sha256 = "{sha256}",
strip_prefix = {strip_prefix},
urls = {urls},
)
"""
repo_stanzas = {}
for repo in ctx.attr.repos:
info = DIST_DEPS[repo]
aiuto marked this conversation as resolved.
Show resolved Hide resolved
strip_prefix = info.get("strip_prefix")
if strip_prefix:
strip_prefix = "\"%s\"" % strip_prefix
else:
strip_prefix = "None"

repo_stanzas["{%s}" % repo] = repo_clause.format(
repo = repo,
archive = info["archive"],
sha256 = str(info["sha256"]),
strip_prefix = strip_prefix,
urls = info["urls"],
)

if ctx.attr.template:
ctx.actions.expand_template(
output = ctx.outputs.out,
template = ctx.file.template,
substitutions = repo_stanzas,
)
else:
content = "\n".join([l.strip() for l in ctx.attr.preamble.strip().split("\n")])
content += "\n"
content += "".join(repo_stanzas.values())
content += "\n"
content += "\n".join([l.strip() for l in ctx.attr.postamble.strip().split("\n")])
content += "\n"
ctx.actions.write(ctx.outputs.out, content)

return [DefaultInfo(files = depset([ctx.outputs.out]))]

gen_workspace_stanza = rule(
implementation = _gen_workspace_stanza_impl,
attrs = {
"repos": attr.string_list(doc = "Set of repos to inlcude"),
"out": attr.output(mandatory = True),
"preamble": attr.string(doc = "Preamble."),
"postamble": attr.string(doc = "setup rules to follow repos."),
"template": attr.label(
aiuto marked this conversation as resolved.
Show resolved Hide resolved
doc = "Template WORKSPACE file. May not be used with preable or postamble." +
"Repo stanzas can be include with the syntax '{repo name}'.",
allow_single_file = True,
mandatory = False,
),
},
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//:distdir_deps.bzl", "gen_workspace_stanza")
load("@rules_java//java:defs.bzl", "java_library")

package(
Expand All @@ -18,7 +19,7 @@ java_library(
["*.java"],
exclude = ["BazelCppSemantics.java"],
),
resources = glob(["*.WORKSPACE"]),
resources = [":cc_configure.WORKSPACE"],
deps = [
":bazel_cpp_semantics",
"//src/main/java/com/google/devtools/build/lib/actions:artifacts",
Expand Down Expand Up @@ -52,3 +53,18 @@ java_library(
"//third_party:guava",
],
)

gen_workspace_stanza(
name = "workspace_with_rules_cc",
out = "cc_configure.WORKSPACE",
postamble = """
cc_configure()
""",
preamble = """
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("@bazel_tools//tools/cpp:cc_configure.bzl", "cc_configure")
""",
repos = ["rules_cc"],
visibility = ["//:__pkg__"],
)

This file was deleted.