2023-03-02 23:42:35 +02:00
|
|
|
# Copyright 2023 Uber Technologies, Inc.
|
2023-04-20 03:23:25 +03:00
|
|
|
# Licensed under the MIT License
|
2023-03-02 23:42:35 +02:00
|
|
|
|
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):
|
2023-03-21 10:13:57 +02:00
|
|
|
platform_sanitized = ctx.attr.platform.replace("/", "_").replace(":", "_")
|
2023-04-24 10:18:34 +03:00
|
|
|
dstname = "{}-{}".format(
|
|
|
|
_paths_basename(ctx.file.src.path),
|
|
|
|
platform_sanitized,
|
|
|
|
)
|
|
|
|
dst = ctx.actions.declare_file(dstname)
|
2023-03-21 10:13:57 +02:00
|
|
|
src = ctx.file.src
|
2023-03-22 10:43:29 +02:00
|
|
|
ctx.actions.run(
|
2023-03-21 10:13:57 +02:00
|
|
|
outputs = [dst],
|
|
|
|
inputs = [src],
|
2023-03-22 10:43:29 +02:00
|
|
|
executable = "cp",
|
2023-03-21 10:13:57 +02:00
|
|
|
arguments = [src.path, dst.path],
|
|
|
|
)
|
2022-04-13 15:58:11 +03:00
|
|
|
return [DefaultInfo(
|
2023-03-21 10:13:57 +02:00
|
|
|
files = depset([dst]),
|
|
|
|
executable = dst,
|
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.",
|
|
|
|
),
|
|
|
|
"_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,
|
|
|
|
)
|
2023-04-24 10:18:34 +03:00
|
|
|
|
|
|
|
## Copied from https://github.com/bazelbuild/bazel-skylib/blob/1.4.1/lib/paths.bzl#L22
|
|
|
|
def _paths_basename(p):
|
|
|
|
return p.rpartition("/")[-1]
|