blob: 42c4acfcfed363c8d6c56d8edff734180f591789 [file] [log] [blame] [edit]
# SPDX-License-Identifier: GPL-2.0 OR Apache-2.0
# Copyright (C) 2023 The Android Open Source Project
"""
ABI aware build rules.
"""
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
load(
"@rules_pkg//pkg:mappings.bzl",
"pkg_files",
"strip_prefix",
)
load(
"//build/kernel/kleaf:kernel.bzl",
"android_filegroup",
)
visibility("private")
_ALL_ABIS = ["arm", "arm64", "x86", "x86_64"]
def _copy_with_abi(
name,
visibility = None,
path_prefix = None,
abis = None,
out = None):
if not path_prefix:
path_prefix = ""
if not abis:
abis = _ALL_ABIS
if not out:
out = name
for abi in abis:
cpu = abi
if abi == "x86":
cpu = "i386"
android_filegroup(
name = "{name}_{abi}_bin".format(name = name, abi = abi),
srcs = [":{name}".format(name = name)],
cpu = cpu,
visibility = visibility,
)
pkg_files(
name = "{name}_{abi}".format(name = name, abi = abi),
srcs = [":{name}_{abi}_bin".format(name = name, abi = abi)],
renames = {
":{name}_{abi}_bin".format(name = name, abi = abi): paths.join(path_prefix, abi, out),
},
strip_prefix = strip_prefix.from_pkg(),
visibility = visibility,
)
def cc_binary_with_abi(
name,
path_prefix = None,
abis = None,
visibility = None,
out = None,
**kwargs):
"""A cc_binary replacement that generates output in each subdirectory named by abi.
For example:
```
cc_binary_with_abi(
name = "a_binary",
abis = ["x86_64", "arm64"],
path_prefix = "my/path",
)
```
generates 2 rules:
* Rule a_binary_x86_64: Builds the cc_binary and put output in my/path/x86_64/a_binary.
* Rule a_binary_arm64: Builds the cc_binary and put output in my/path/arm64/a_binary.
Args:
name: the name of the build rule.
path_prefix: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The path prefix to attach to output.
abis: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The intended abis to generate. Default is arm64 & x86_64.
visibility: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The visibility attribute on a target controls whether the target can be used in other packages.
out: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The output filename. Default is `name`.
**kwargs: the rest args that cc_binary uses.
"""
native.cc_binary(
name = name,
visibility = visibility,
**kwargs
)
_copy_with_abi(
name = name,
path_prefix = path_prefix,
abis = abis,
visibility = visibility,
out = out,
)
def sh_binary_with_abi(
name,
path_prefix = None,
abis = None,
visibility = None,
out = None,
**kwargs):
"""A sh_binary replacement that generates output in each subdirectory named by abi.
For example:
```
sh_binary_with_abi(
name = "a_binary",
abis = ["x86_64", "arm64"],
path_prefix = "my/path",
)
```
generates 2 rules:
* Rule a_binary_x86_64: Copies a_binary and put output in my/path/x86_64/a_binary.
* Rule a_binary_arm64: Copies a_binary and put output in my/path/arm64/a_binary.
Args:
name: the name of the build rule.
path_prefix: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The path prefix to attach to output.
abis: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The intended abis to generate. Default is arm64 & x86_64.
visibility: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The visibility attribute on a target controls whether the target can be used in other packages.
out: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The output filename. Default is `name`.
**kwargs: the rest args that native_binary uses.
"""
if not out:
out = name
# Uses native_binary instead of sh_binary because sh_binary is not
# compatible with copy_file (sh_binary generates more than 1 outs).
native_binary(
name = name,
visibility = visibility,
out = out,
**kwargs
)
_copy_with_abi(
name = name,
path_prefix = path_prefix,
abis = abis,
visibility = visibility,
out = out,
)