-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rule executable_jar generates an executable jar for a set of libraries.
- Loading branch information
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
load("//java/private:executable_jar.bzl", _executable_jar = "executable_jar") | ||
load("//java/private:javadoc.bzl", _javadoc = "javadoc") | ||
|
||
executable_jar = _executable_jar | ||
javadoc = _javadoc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
visibility("private") | ||
|
||
def set_difference(a, b): | ||
b = {element: True for element in b.to_list()} | ||
return depset([element for element in a.to_list() if element not in b]) | ||
|
||
def to_paths(depset): | ||
return [element.path for element in depset.to_list()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
load(":depsets.bzl", "set_difference", "to_paths") | ||
load(":files.bzl", "copy_files", "write_file") | ||
load(":jars.bzl", "extract_jars") | ||
|
||
def _manifest(ctx): | ||
return """\ | ||
Main-Class: extrarulesjava.loader.Loader | ||
Start-Class: {}\ | ||
""".format(ctx.attr.main_class) | ||
|
||
def _executable_jar_impl(ctx): | ||
app = depset(transitive = [depset(lib[JavaInfo].runtime_output_jars) for lib in ctx.attr.libs]) | ||
libs = depset(transitive = [lib[JavaInfo].transitive_runtime_jars for lib in ctx.attr.libs]) | ||
libs = set_difference(libs, app) | ||
output = ctx.actions.declare_file("{}.jar".format(ctx.label.name)) | ||
|
||
java_home = ctx.attr._jdk[java_common.JavaRuntimeInfo].java_home | ||
tmp_dir = "{}/_{}/".format(output.dirname, output.basename) | ||
app_dir = "{}/app/".format(tmp_dir) | ||
lib_dir = "{}/lib/".format(tmp_dir) | ||
manifest_file = "{}/manifest".format(tmp_dir) | ||
|
||
commands = [ | ||
"rm -rf {}".format(tmp_dir), | ||
"mkdir {} {}".format(tmp_dir, lib_dir), | ||
extract_jars(to_paths(app), app_dir), | ||
extract_jars(to_paths(ctx.attr._loader.files), tmp_dir), | ||
write_file(manifest_file, _manifest(ctx)), | ||
"{0}/bin/jar -c -f {1} -m {2} -C {3} app/ -C {3} extrarulesjava/".format(java_home, output.path, manifest_file, tmp_dir), | ||
copy_files(to_paths(libs), lib_dir), | ||
"{}/bin/jar -u -0 -f {} -C {} lib/".format(java_home, output.path, tmp_dir), | ||
] | ||
|
||
ctx.actions.run_shell( | ||
command = " && ".join(commands), | ||
inputs = ctx.files._jdk + ctx.files._loader + app.to_list() + libs.to_list(), | ||
outputs = [output], | ||
) | ||
|
||
return [DefaultInfo(files = depset([output]))] | ||
|
||
executable_jar = rule( | ||
doc = "Generates an executable jar for a set of libraries.", | ||
implementation = _executable_jar_impl, | ||
attrs = { | ||
"libs": attr.label_list( | ||
allow_empty = False, | ||
mandatory = True, | ||
providers = [JavaInfo], | ||
), | ||
"main_class": attr.string( | ||
mandatory = True, | ||
), | ||
"_jdk": attr.label( | ||
default = Label("@bazel_tools//tools/jdk:current_java_runtime"), | ||
providers = [java_common.JavaRuntimeInfo], | ||
), | ||
"_loader": attr.label( | ||
default = Label("//loader"), | ||
providers = [JavaInfo], | ||
), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
visibility("private") | ||
|
||
def copy_files(files, dir): | ||
return "cp {} {}".format(" ".join(files), dir) | ||
|
||
def write_file(file, content): | ||
return "echo '{}' > {}".format(content, file) |