gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/bazeldefs/defs.bzl (about) 1 """Meta and miscellaneous rules.""" 2 3 load("@bazel_skylib//:bzl_library.bzl", _bzl_library = "bzl_library") 4 load("@bazel_skylib//rules:build_test.bzl", _build_test = "build_test") 5 load("@bazel_skylib//rules:common_settings.bzl", _BuildSettingInfo = "BuildSettingInfo", _bool_flag = "bool_flag") 6 7 build_test = _build_test 8 bzl_library = _bzl_library 9 bool_flag = _bool_flag 10 BuildSettingInfo = _BuildSettingInfo 11 more_shards = 4 12 most_shards = 8 13 version = "//tools/bazeldefs:version" 14 15 def short_path(path): 16 return path 17 18 def proto_library(name, has_services = None, **kwargs): 19 native.proto_library( 20 name = name, 21 **kwargs 22 ) 23 24 def select_arch(amd64 = None, arm64 = None, riscv64 = None, default = None, **kwargs): 25 """Select an option against standard architectures. 26 27 Args: 28 amd64: the option if the architecture is amd64. 29 arm64: the option if the architecture is arm64. 30 default: the option if no matching architecture is provided. 31 **kwargs: extra select arguments. 32 33 Returns: 34 An appropriate select.""" 35 values = dict() 36 if amd64 != None: 37 values["//tools/bazeldefs:amd64"] = amd64 38 if arm64 != None: 39 values["//tools/bazeldefs:arm64"] = arm64 40 if riscv64 != None: 41 values["//tools/bazeldefs:riscv64"] = riscv64 42 if default != None: 43 values["//conditions:default"] = default 44 return select(values, **kwargs) 45 46 def select_system(linux = ["__linux__"], darwin = [], **kwargs): 47 return select({ 48 "@bazel_tools//src/conditions:darwin": darwin, 49 "//conditions:default": linux, 50 }) 51 52 arch_config = [ 53 "@io_bazel_rules_go//go/config:race", 54 "//command_line_option:cpu", 55 "//command_line_option:crosstool_top", 56 "//command_line_option:platforms", 57 ] 58 59 def arm64_config(settings, attr): 60 return { 61 # Race builds are always disabled for cross-architecture generation. We 62 # can't run it locally anyways, what value can this provide? 63 "@io_bazel_rules_go//go/config:race": False, 64 "//command_line_option:cpu": "aarch64", 65 "//command_line_option:crosstool_top": "@crosstool//:toolchains", 66 "//command_line_option:platforms": "@io_bazel_rules_go//go/toolchain:linux_arm64", 67 } 68 69 def amd64_config(settings, attr): 70 return { 71 # See above. 72 "@io_bazel_rules_go//go/config:race": False, 73 "//command_line_option:cpu": "k8", 74 "//command_line_option:crosstool_top": "@crosstool//:toolchains", 75 "//command_line_option:platforms": "@io_bazel_rules_go//go/toolchain:linux_amd64", 76 } 77 78 transition_allowlist = "@bazel_tools//tools/allowlists/function_transition_allowlist" 79 80 def default_installer(): 81 return None 82 83 def default_net_util(): 84 return [] # Nothing needed. 85 86 def coreutil(): 87 return [] # Nothing needed. 88 89 def bpf_program(name, src, bpf_object, visibility, hdrs): 90 """Generates BPF object files from .c source code. 91 92 Args: 93 name: target name for BPF program. 94 src: BPF program souce code in C. 95 bpf_object: name of generated bpf object code. 96 visibility: target visibility. 97 hdrs: header files, but currently unsupported. 98 """ 99 if hdrs != []: 100 fail("hdrs attribute is unsupported") 101 102 native.genrule( 103 name = name, 104 srcs = [src], 105 visibility = visibility, 106 outs = [bpf_object], 107 cmd = "clang -O2 -Wall -Werror -target bpf -c $< -o $@ -I/usr/include/$$(uname -m)-linux-gnu", 108 )