Skip to content

Commit

Permalink
Add darwin_arm64 java_tools
Browse files Browse the repository at this point in the history
Build a fat universal binary for java_tools_prebuilt on darwin

Work towards: bazelbuild/java_tools#57 and bazelbuild#13944

Closes bazelbuild#16960.

PiperOrigin-RevId: 512683379
Change-Id: Ie9db26c729a301fbb22f17dd15065861f3198f57
  • Loading branch information
hvadehra authored and copybara-github committed Feb 27, 2023
1 parent 7793f7b commit 570e25f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 5 deletions.
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("//third_party/ijar: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
34 changes: 30 additions & 4 deletions third_party/ijar/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load(":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",
"darwin_universal_binary.bzl",
] + select({
"//src:windows": [
"mapped_file_windows.cc",
Expand Down Expand Up @@ -137,10 +152,16 @@ 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 All @@ -158,3 +179,8 @@ genrule(
tools = ["//src:zip_files"],
visibility = ["//visibility:private"],
)

exports_files(
["darwin_universal_binary.bzl"],
visibility = ["//src/tools/singlejar:__pkg__"],
)
61 changes: 61 additions & 0 deletions third_party/ijar/darwin_universal_binary.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
""" Defines a rule to package multi-arch binaries for darwin in a single fat binary
Uses the lipo tool as described here: https://developer.apple.com/documentation/apple-silicon/building-a-universal-macos-binary#Update-the-Architecture-List-of-Custom-Makefiles
"""

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"],
exec_compatible_with = ["@platforms//os:macos"],
)

0 comments on commit 570e25f

Please sign in to comment.