2022-06-02 08:46:16 +03:00
|
|
|
def _vars_script(env, run_under, cmd):
|
|
|
|
ret = ["#!/bin/sh"]
|
|
|
|
for k, v in env.items():
|
|
|
|
ret += ['export {}="{}"'.format(k, v)]
|
|
|
|
ret += ['exec {} {} "$@"'.format(run_under, cmd)]
|
|
|
|
return "\n".join(ret) + "\n" # trailing newline is easier on the eyes
|
|
|
|
|
2022-04-13 15:58:11 +03:00
|
|
|
def _platform_transition_impl(settings, attr):
|
|
|
|
_ignore = settings
|
|
|
|
return {
|
2022-04-13 17:52:25 +03:00
|
|
|
"//command_line_option:platforms": "@zig_sdk{}".format(attr.platform),
|
2022-04-13 15:58:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_platform_transition = transition(
|
|
|
|
implementation = _platform_transition_impl,
|
|
|
|
inputs = [],
|
|
|
|
outputs = [
|
|
|
|
"//command_line_option:platforms",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def _platform_binary_impl(ctx):
|
|
|
|
source_info = ctx.attr.src[DefaultInfo]
|
|
|
|
|
|
|
|
executable = None
|
|
|
|
if source_info.files_to_run and source_info.files_to_run.executable:
|
2022-06-02 08:46:16 +03:00
|
|
|
command = _vars_script(ctx.attr.env, ctx.attr.run_under, source_info.files_to_run.executable.short_path)
|
2022-04-13 15:58:11 +03:00
|
|
|
executable = ctx.actions.declare_file("{}_{}".format(ctx.file.src.basename, ctx.attr.platform))
|
2022-06-02 08:46:16 +03:00
|
|
|
ctx.actions.write(
|
|
|
|
output = executable,
|
|
|
|
content = command,
|
|
|
|
is_executable = True,
|
2022-04-13 15:58:11 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
return [DefaultInfo(
|
|
|
|
executable = executable,
|
2022-06-02 08:46:16 +03:00
|
|
|
files = depset([executable]),
|
|
|
|
runfiles = ctx.runfiles(files = ctx.files.src),
|
2022-04-13 15:58:11 +03:00
|
|
|
)]
|
|
|
|
|
|
|
|
_attrs = {
|
|
|
|
"src": attr.label(
|
|
|
|
allow_single_file = True,
|
|
|
|
mandatory = True,
|
|
|
|
doc = "Target to build.",
|
|
|
|
),
|
|
|
|
"platform": attr.string(
|
|
|
|
doc = "The platform to build the target for.",
|
|
|
|
),
|
2022-06-02 08:46:16 +03:00
|
|
|
"run_under": attr.string(
|
|
|
|
doc = "wrapper executable",
|
|
|
|
),
|
|
|
|
"env": attr.string_dict(
|
|
|
|
doc = "Environment variables for the test",
|
|
|
|
),
|
2022-04-13 15:58:11 +03:00
|
|
|
"_allowlist_function_transition": attr.label(
|
|
|
|
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2022-04-18 12:17:03 +03:00
|
|
|
# wrap a single exectable and build it for the specified platform.
|
2022-04-13 15:58:11 +03:00
|
|
|
platform_binary = rule(
|
|
|
|
implementation = _platform_binary_impl,
|
|
|
|
cfg = _platform_transition,
|
|
|
|
attrs = _attrs,
|
|
|
|
executable = True,
|
|
|
|
)
|
|
|
|
|
2022-04-18 12:17:03 +03:00
|
|
|
# wrap a single test target and build it for the specified platform.
|
2022-04-13 15:58:11 +03:00
|
|
|
platform_test = rule(
|
|
|
|
implementation = _platform_binary_impl,
|
|
|
|
cfg = _platform_transition,
|
|
|
|
attrs = _attrs,
|
|
|
|
test = True,
|
|
|
|
)
|