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

Allows to provide Lipo tool location via custom toolchain. #336

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 33 additions & 5 deletions rules/universal_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,48 @@
"""Implementation for macOS universal binary rule."""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_skylib//lib:shell.bzl", "shell")
load("//lib:apple_support.bzl", "apple_support")
load("//lib:lipo.bzl", "lipo")
load("//lib:transitions.bzl", "macos_universal_transition")

def _universal_binary_impl(ctx):
inputs = [
binary.files.to_list()[0]
for binary in ctx.split_attr.binary.values()
]
inputs = []
for binary in ctx.split_attr.binary.values():
inputs += binary.files.to_list()

if not inputs:
fail("Target (%s) `binary` label ('%s') does not provide any " +
"file for universal binary" % (ctx.attr.name, ctx.attr.binary))

output = ctx.actions.declare_file(ctx.label.name)

if len(inputs) > 1:
lipo_toolchain = ctx.toolchains["//toolchain/lipo:toolchain_type"]

if len(inputs) > 1 and lipo_toolchain:
lipo_tool = lipo_toolchain.info.lipo

cmd = [
"mkdir -p {} &&".format(shell.quote(output.dirname)),
lipo_tool.path,
"-create",
]
cmd.extend([
shell.quote(file.path)
for file in inputs
])
cmd.extend([
"-output",
shell.quote(output.path),
])

ctx.actions.run_shell(
command = " ".join(cmd),
mnemonic = "AppleLipo",
inputs = inputs + [lipo_tool],
outputs = [output],
)
elif len(inputs) > 1:
lipo.create(
actions = ctx.actions,
apple_fragment = ctx.fragments.apple,
Expand Down Expand Up @@ -84,4 +109,7 @@ binary.
executable = True,
fragments = ["apple"],
implementation = _universal_binary_impl,
toolchains = [
config_common.toolchain_type("//toolchain/lipo:toolchain_type", mandatory = False),
],
)
4 changes: 4 additions & 0 deletions toolchain/lipo/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)
48 changes: 48 additions & 0 deletions toolchain/lipo/lipo_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Toolchain definition to provide Lipo tool location.

Usage:

load("@build_bazel_apple_support//toolchain/lipo:lipo_toolchain.bzl", "lipo_toolchain")

lipo_toolchain(
name = "_lipo_toolchain",
lipo = ":lipo",
)

toolchain(
name = "lipo_toolchain",
toolchain = ":_lipo_toolchain",
toolchain_type = "@build_bazel_apple_support//toolchain/lipo:toolchain_type",
exec_compatible_with = [
"@platforms//os:macos",
],
target_compatible_with = [
"@platforms//os:macos",
],
)

"""

Info = provider(
fields = ["lipo"],
doc = "Tools info to locate xcode tools",
)

def _lipo_toolchain_impl(ctx):
toolchain_info = platform_common.ToolchainInfo(
info = Info(
lipo = ctx.file.lipo,
),
)
return [toolchain_info]

lipo_toolchain = rule(
doc = "Toolchain rule to provide Lipo tool location.",
implementation = _lipo_toolchain_impl,
attrs = {
"lipo": attr.label(
doc = "a label to lipo tool.",
allow_single_file = True,
),
},
)