Skip to content

Commit

Permalink
Make universal binary for darwin java_tools
Browse files Browse the repository at this point in the history
  • Loading branch information
hvadehra committed Dec 13, 2022
1 parent cd10d50 commit ae0e759
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,8 @@ filegroup(
for version in ("17", "18")
],
)

exports_files(
["darwin_universal_binary.bzl"],
visibility = ["//third_party/ijar:__pkg__"],
)
54 changes: 54 additions & 0 deletions src/darwin_universal_binary.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
def _universal_split_transition_impl(ctx, attr):
return {
"x86_64" : {
"//command_line_option:cpu": "darwin_x86_64",
},
"arm64" : {
"//command_line_option:cpu": "darwin_arm64",
}
}

_universal_split_transition = transition(
implementation = _universal_split_transition_impl,
inputs = [],
outputs = ["//command_line_option:cpu"],
)

def _impl(ctx):
binaries = [
attr.files.to_list()[0] for attr in ctx.split_attr.binary.values()
]
out = ctx.actions.declare_file(ctx.label.name + "/" + ctx.attr.output_name)
args = ctx.actions.args()
args.add("-create")
args.add_all(binaries)
args.add("-output", out)
apple_env = {}
xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
apple_env.update(apple_common.apple_host_system_env(xcode_config))
apple_env.update(
apple_common.target_apple_env(
xcode_config,
ctx.fragments.apple.multi_arch_platform(apple_common.platform_type.macos),
),
)
ctx.actions.run(
executable = "/usr/bin/lipo",
arguments = [args],
inputs = binaries,
outputs = [out],
execution_requirements = xcode_config.execution_info(),
env = apple_env,
)
return DefaultInfo(executable = out)

darwin_universal_binary = rule(
implementation = _impl,
attrs = {
"output_name" : attr.string(),
"binary": attr.label(cfg = _universal_split_transition),
"_xcode_config": attr.label(default = "@bazel_tools//tools/osx:current_xcode_config"),
"_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
},
fragments = ["apple"],
)
17 changes: 16 additions & 1 deletion src/tools/singlejar/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@rules_java//java:defs.bzl", "java_library")
load("//src:release_archive.bzl", "release_archive")
load("//src:darwin_universal_binary.bzl", "darwin_universal_binary")

# Description:
# singlejar C++ implementation.
Expand Down Expand Up @@ -64,7 +65,7 @@ release_archive(

release_archive(
name = "singlejar_deploy_zip",
srcs = [":singlejar_local"],
srcs = [":singlejar_local_binary_for_deploy"],
package_dir = "java_tools/src/tools/singlejar",
visibility = ["//src:__pkg__"],
)
Expand All @@ -88,6 +89,20 @@ cc_binary(
],
)

alias(
name = "singlejar_local_binary_for_deploy",
actual = select({
"//src/conditions:darwin": ":singlejar_local_darwin",
"//conditions:default": ":singlejar_local",
})
)

darwin_universal_binary(
name = "singlejar_local_darwin",
binary = ":singlejar_local",
output_name = "singlejar_local",
)

cc_binary(
name = "singlejar_local",
srcs = [
Expand Down
23 changes: 19 additions & 4 deletions third_party/ijar/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("//src:darwin_universal_binary.bzl", "darwin_universal_binary")

package(
default_visibility = [
"//src:__subpackages__",
Expand Down Expand Up @@ -76,6 +78,12 @@ cc_binary(
deps = [":zip"],
)

darwin_universal_binary(
name = "zipper_darwin",
binary = ":zipper",
output_name = "zipper",
)

cc_binary(
name = "ijar",
srcs = [
Expand All @@ -86,6 +94,12 @@ cc_binary(
deps = [":zip"],
)

darwin_universal_binary(
name = "ijar_darwin",
binary = ":ijar",
output_name = "ijar",
)

filegroup(
name = "srcs",
srcs = glob(["**"]) + ["//third_party/ijar/test:srcs"],
Expand All @@ -105,6 +119,7 @@ filegroup(
"zlib_client.cc",
"zlib_client.h",
"BUILD",
"//src:darwin_universal_binary.bzl",
] + select({
"//src:windows": [
"mapped_file_windows.cc",
Expand Down Expand Up @@ -137,10 +152,10 @@ genrule(

genrule(
name = "ijar_deploy_zip",
srcs = [
":ijar",
":zipper",
],
srcs = select({
"//src/conditions:darwin": [":ijar_darwin", ":zipper_darwin"],
"//conditions:default": [":ijar",":zipper"],
}),
outs = ["ijar_deploy.zip"],
cmd = "$(location //src:zip_files) java_tools/ijar $@ $(SRCS)",
tools = ["//src:zip_files"],
Expand Down

0 comments on commit ae0e759

Please sign in to comment.