summaryrefslogtreecommitdiffhomepage
path: root/tools/bazeldefs
diff options
context:
space:
mode:
authorAndrei Vagin <avagin@google.com>2020-12-09 00:42:53 -0800
committerShentubot <shentubot@google.com>2020-12-09 15:51:33 -0800
commit658f874b94ad83d9b4abed47d9aba856fe5f617c (patch)
treece2ea322df840b882d41f02bc27a1e288decefc9 /tools/bazeldefs
parent96d14de0fa51d6c7fd5a34c69f27caf76a26f7aa (diff)
Prepare for supporting cross compilation.
PiperOrigin-RevId: 346496532
Diffstat (limited to 'tools/bazeldefs')
-rw-r--r--tools/bazeldefs/BUILD18
-rw-r--r--tools/bazeldefs/defs.bzl41
-rw-r--r--tools/bazeldefs/go.bzl4
3 files changed, 61 insertions, 2 deletions
diff --git a/tools/bazeldefs/BUILD b/tools/bazeldefs/BUILD
index 27e85a75e..97c7cb45f 100644
--- a/tools/bazeldefs/BUILD
+++ b/tools/bazeldefs/BUILD
@@ -58,3 +58,21 @@ bzl_library(
srcs = ["defs.bzl"],
visibility = ["//visibility:private"],
)
+
+config_setting(
+ name = "linux_arm64_cross",
+ values = {
+ "cpu": "aarch64",
+ "host_cpu": "k8",
+ },
+ visibility = ["//visibility:private"],
+)
+
+config_setting(
+ name = "linux_amd64_cross",
+ values = {
+ "cpu": "k8",
+ "host_cpu": "aarch64",
+ },
+ visibility = ["//visibility:private"],
+)
diff --git a/tools/bazeldefs/defs.bzl b/tools/bazeldefs/defs.bzl
index 51e17a79a..279a38fed 100644
--- a/tools/bazeldefs/defs.bzl
+++ b/tools/bazeldefs/defs.bzl
@@ -39,3 +39,44 @@ def default_net_util():
def coreutil():
return [] # Nothing needed.
+
+def select_native_vs_cross(native = [], amd64 = [], arm64 = [], cross = []):
+ values = {
+ "//tools/bazeldefs:linux_arm64_cross": arm64 + cross,
+ "//tools/bazeldefs:linux_amd64_cross": amd64 + cross,
+ "//conditions:default": native,
+ }
+ return select(values)
+
+def arch_genrule(name, srcs, outs, cmd, tools):
+ """Runs a gen command on the target architecture.
+
+ If the target architecture isn't match the host architecture, it will build
+ a command for the target architecture and run it via qemu.
+
+ The native genrule runs the command on the host architecture.
+
+ Args:
+ name: name of generated target.
+ srcs: A list of inputs for this rule.
+ cmd: The command to run. It has to contain " QEMU " before executed binaries.
+ outs: A list of files generated by this rule.
+ tools: A list of tool dependencies for this rule.
+ """
+ qemu_arm64 = "qemu-aarch64-static"
+ qemu_amd64 = "qemu-x86_64-static"
+ srcs = select_native_vs_cross(
+ cross = srcs + tools,
+ native = srcs,
+ )
+ tools = select_native_vs_cross(
+ cross = [],
+ native = tools,
+ )
+ cmd = select_native_vs_cross(
+ arm64 = cmd.replace("QEMU", qemu_arm64),
+ amd64 = cmd.replace("QEMU", qemu_amd64),
+ native = cmd.replace("QEMU", ""),
+ cross = "",
+ )
+ native.genrule(name = name, srcs = srcs, outs = outs, cmd = cmd, tools = tools)
diff --git a/tools/bazeldefs/go.bzl b/tools/bazeldefs/go.bzl
index 661c9727e..bcd8cffe7 100644
--- a/tools/bazeldefs/go.bzl
+++ b/tools/bazeldefs/go.bzl
@@ -28,7 +28,7 @@ def go_proto_library(name, **kwargs):
def go_grpc_and_proto_libraries(name, **kwargs):
_go_proto_or_grpc_library(_go_grpc_library, name, **kwargs)
-def go_binary(name, static = False, pure = False, x_defs = None, **kwargs):
+def go_binary(name, static = False, pure = False, x_defs = None, system_malloc = False, **kwargs):
"""Build a go binary.
Args:
@@ -52,7 +52,7 @@ def go_importpath(target):
"""Returns the importpath for the target."""
return target[GoLibrary].importpath
-def go_library(name, **kwargs):
+def go_library(name, arch_deps = [], **kwargs):
_go_library(
name = name,
importpath = "gvisor.dev/gvisor/" + native.package_name(),