Skip to content

Commit

Permalink
Modernize Bazel setup (#20)
Browse files Browse the repository at this point in the history
* No longer rely on `native.bind` and the `external` package
* Replace host with exec cfg
* Use `ctx.actions.args()` instead of string lists
* Use `http_jar` instead of `jvm_maven_import_external`
  • Loading branch information
fmeum authored Oct 15, 2022
1 parent 670d3c8 commit e8b1334
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
56 changes: 26 additions & 30 deletions jar_jar.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load(
"@bazel_tools//tools/build_defs/repo:jvm.bzl",
"jvm_maven_import_external",
"@bazel_tools//tools/build_defs/repo:http.bzl",
"http_jar",
)

def _jar_jar_impl(ctx):
Expand All @@ -16,12 +16,18 @@ def _jar_jar_impl(ctx):
content = "\n".join(ctx.attr.inline_rules)
)

args = ctx.actions.args()
args.add("process")
args.add(rule_file)
args.add(ctx.file.input_jar)
args.add(ctx.outputs.jar)

ctx.actions.run(
inputs = [rule_file, ctx.file.input_jar],
outputs = [ctx.outputs.jar],
executable = ctx.executable._jarjar_runner,
progress_message = "jarjar %s" % ctx.label,
arguments = ["process", rule_file.path, ctx.file.input_jar.path, ctx.outputs.jar.path],
arguments = [args],
)

return [
Expand All @@ -38,41 +44,31 @@ jar_jar = rule(
"input_jar": attr.label(allow_single_file = True),
"rules": attr.label(allow_single_file = True),
"inline_rules" : attr.string_list(),
"_jarjar_runner": attr.label(executable = True, cfg = "host", default = Label("@com_github_johnynek_bazel_jar_jar//src/main/java/com/github/johnynek/jarjar:app")),
"_jarjar_runner": attr.label(executable = True, cfg = "exec", default = "//src/main/java/com/github/johnynek/jarjar:app"),
},
outputs = {
"jar": "%{name}.jar",
},
provides = [JavaInfo],
)

def _mvn_name(coord):
nocolon = "_".join(coord.split(":"))
nodot = "_".join(nocolon.split("."))
nodash = "_".join(nodot.split("-"))
return nodash

def _mvn_jar(coord, sha, bname, servers):
nm = "jar_jar_" + _mvn_name(coord)
jvm_maven_import_external(
name = nm,
artifact = coord,
server_urls = servers,
artifact_sha256 = sha,
licenses = [],
def _http_jar_with_servers(name, path, sha256, servers):
http_jar(
name = name,
urls = [server + path for server in servers],
sha256 = sha256,
)
native.bind(name = ("com_github_johnynek_bazel_jar_jar/%s" % bname), actual = "@%s//jar" % nm)

def jar_jar_repositories(servers = ["https://repo1.maven.org/maven2"]):
_mvn_jar(
"org.ow2.asm:asm:7.0",
"b88ef66468b3c978ad0c97fd6e90979e56155b4ac69089ba7a44e9aa7ffe9acf",
"asm",
servers,
def jar_jar_repositories(servers=["https://repo1.maven.org/maven2"]):
_http_jar_with_servers(
name = "bazel_jar_jar_asm",
path = "/org/ow2/asm/asm/7.0/asm-7.0.jar",
sha256 = "b88ef66468b3c978ad0c97fd6e90979e56155b4ac69089ba7a44e9aa7ffe9acf",
servers = servers,
)
_mvn_jar(
"org.ow2.asm:asm-commons:7.0",
"fed348ef05958e3e846a3ac074a12af5f7936ef3d21ce44a62c4fa08a771927d",
"asm_commons",
servers,
_http_jar_with_servers(
name = "bazel_jar_jar_asm_commons",
path = "/org/ow2/asm/asm-commons/7.0/asm-commons-7.0.jar",
sha256 = "fed348ef05958e3e846a3ac074a12af5f7936ef3d21ce44a62c4fa08a771927d",
servers = servers,
)
2 changes: 1 addition & 1 deletion jar_jar_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jar_jar_toolchain = rule(
attrs = {
"rules": attr.label(allow_single_file = True),
"duplicate_class_to_warn": attr.bool(mandatory = False, default = False),
"jar_jar_runner": attr.label(executable = True, cfg = "host", default = Label("@com_github_johnynek_bazel_jar_jar//src/main/java/com/github/johnynek/jarjar:app")),
"jar_jar_runner": attr.label(executable = True, cfg = "exec", default = "//src/main/java/com/github/johnynek/jarjar:app"),
"jar_jar_is_native_image": attr.bool(default = False)
},
)
4 changes: 2 additions & 2 deletions src/main/java/com/github/johnynek/jarjar/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ java_library(
],
visibility = ["//visibility:public"],
deps = [
"//external:com_github_johnynek_bazel_jar_jar/asm",
"//external:com_github_johnynek_bazel_jar_jar/asm_commons",
"@bazel_jar_jar_asm//jar",
"@bazel_jar_jar_asm_commons//jar",
],
)

Expand Down

0 comments on commit e8b1334

Please # to comment.