| Tim Windelschmidt | 98000a5 | 2025-03-06 14:22:15 +0100 | [diff] [blame] | 1 | load("@rules_foreign_cc//toolchains/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain") |
| 2 | |
| Tim Windelschmidt | 03dadba | 2025-07-09 06:58:03 +0200 | [diff] [blame^] | 3 | SUPPORTED_TARGETS = [ |
| 4 | struct( |
| 5 | tuple = "linux_x86_64", |
| 6 | triple = "x86_64-unknown-linux-musl", |
| 7 | constrain = ["@platforms//os:linux", "@platforms//cpu:x86_64"], |
| 8 | ), |
| 9 | struct( |
| 10 | tuple = "linux_aarch64", |
| 11 | triple = "aarch64-unknown-linux-musl", |
| 12 | constrain = ["@platforms//os:linux", "@platforms//cpu:aarch64"], |
| 13 | ), |
| 14 | ] |
| 15 | |
| Tim Windelschmidt | 98000a5 | 2025-03-06 14:22:15 +0100 | [diff] [blame] | 16 | # Copied from bazel-contrib/rules_foreign_cc licensed under Apache-2.0 |
| 17 | def _current_toolchain_impl(ctx): |
| 18 | toolchain = ctx.toolchains[ctx.attr._toolchain] |
| 19 | |
| 20 | if toolchain.data.target: |
| 21 | return [ |
| 22 | toolchain, |
| 23 | platform_common.TemplateVariableInfo(toolchain.data.env), |
| 24 | DefaultInfo( |
| 25 | files = toolchain.data.target.files, |
| 26 | runfiles = toolchain.data.target.default_runfiles, |
| 27 | ), |
| 28 | ] |
| 29 | return [ |
| 30 | toolchain, |
| 31 | platform_common.TemplateVariableInfo(toolchain.data.env), |
| 32 | DefaultInfo(), |
| 33 | ] |
| 34 | |
| 35 | def current_toolchain(name): |
| 36 | return rule( |
| 37 | implementation = _current_toolchain_impl, |
| 38 | attrs = { |
| 39 | "_toolchain": attr.string(default = "//build/toolchain/toolchain-bundle:%s_toolchain" % name), |
| 40 | }, |
| 41 | toolchains = [ |
| 42 | "//build/toolchain/toolchain-bundle:%s_toolchain" % name, |
| 43 | ], |
| 44 | ) |
| 45 | |
| 46 | def toolchain_for(name, config): |
| 47 | native.toolchain_type( |
| 48 | name = "%s_toolchain" % name, |
| 49 | ) |
| 50 | |
| 51 | config.current_toolchain_func( |
| 52 | name = name, |
| 53 | ) |
| 54 | |
| Tim Windelschmidt | 03dadba | 2025-07-09 06:58:03 +0200 | [diff] [blame^] | 55 | for target in SUPPORTED_TARGETS: |
| 56 | native.toolchain( |
| 57 | name = "%s_%s_toolchain" % (name, target.tuple), |
| 58 | exec_compatible_with = target.constrain, |
| 59 | toolchain = ":%s_%s" % (name, target.tuple), |
| 60 | toolchain_type = ":%s_toolchain" % name, |
| 61 | ) |
| Tim Windelschmidt | 98000a5 | 2025-03-06 14:22:15 +0100 | [diff] [blame] | 62 | |
| Tim Windelschmidt | 03dadba | 2025-07-09 06:58:03 +0200 | [diff] [blame^] | 63 | native_tool_toolchain( |
| 64 | name = "%s_%s" % (name, target.tuple), |
| 65 | env = { |
| 66 | name.upper(): "$(execpath @toolchain-bundle-%s//:%s)" % (target.triple, config.target), |
| 67 | }, |
| 68 | target = "@toolchain-bundle-%s//:%s" % (target.triple, config.target), |
| 69 | ) |
| Tim Windelschmidt | 98000a5 | 2025-03-06 14:22:15 +0100 | [diff] [blame] | 70 | |
| 71 | current_qemu_img_toolchain = current_toolchain("qemu-img") |
| 72 | current_qemu_kvm_toolchain = current_toolchain("qemu-kvm") |
| 73 | current_make_toolchain = current_toolchain("make") |
| 74 | current_strace_toolchain = current_toolchain("strace") |
| 75 | current_nasm_toolchain = current_toolchain("nasm") |
| 76 | current_bison_toolchain = current_toolchain("bison") |
| 77 | current_flex_toolchain = current_toolchain("flex") |
| 78 | current_m4_toolchain = current_toolchain("m4") |
| 79 | current_bc_toolchain = current_toolchain("bc") |
| 80 | current_busybox_toolchain = current_toolchain("busybox") |
| 81 | current_diff_toolchain = current_toolchain("diff") |
| 82 | current_perl_toolchain = current_toolchain("perl") |
| 83 | current_iasl_toolchain = current_toolchain("iasl") |
| 84 | current_lz4_toolchain = current_toolchain("lz4") |
| 85 | |
| 86 | TOOLCHAINS = { |
| 87 | "qemu-img": struct( |
| 88 | target = "bin/qemu-img", |
| 89 | current_toolchain_func = current_qemu_img_toolchain, |
| 90 | ), |
| 91 | "qemu-kvm": struct( |
| 92 | target = "qemu-kvm", |
| 93 | current_toolchain_func = current_qemu_kvm_toolchain, |
| 94 | ), |
| 95 | "make": struct( |
| 96 | target = "bin/make", |
| 97 | current_toolchain_func = current_make_toolchain, |
| 98 | ), |
| 99 | "strace": struct( |
| 100 | target = "bin/strace", |
| 101 | current_toolchain_func = current_strace_toolchain, |
| 102 | ), |
| 103 | "nasm": struct( |
| 104 | target = "bin/nasm", |
| 105 | current_toolchain_func = current_nasm_toolchain, |
| 106 | ), |
| 107 | "bison": struct( |
| 108 | target = "bison", |
| 109 | current_toolchain_func = current_bison_toolchain, |
| 110 | ), |
| 111 | "flex": struct( |
| 112 | target = "bin/flex", |
| 113 | current_toolchain_func = current_flex_toolchain, |
| 114 | ), |
| 115 | "m4": struct( |
| 116 | target = "bin/m4", |
| 117 | current_toolchain_func = current_m4_toolchain, |
| 118 | ), |
| 119 | "bc": struct( |
| 120 | target = "bin/bc", |
| 121 | current_toolchain_func = current_bc_toolchain, |
| 122 | ), |
| 123 | "diff": struct( |
| 124 | target = "bin/diff", |
| 125 | current_toolchain_func = current_diff_toolchain, |
| 126 | ), |
| 127 | "iasl": struct( |
| 128 | target = "bin/iasl", |
| 129 | current_toolchain_func = current_iasl_toolchain, |
| 130 | ), |
| 131 | "busybox": struct( |
| 132 | target = "busybox", |
| 133 | current_toolchain_func = current_busybox_toolchain, |
| 134 | ), |
| 135 | "perl": struct( |
| 136 | target = "perl", |
| 137 | current_toolchain_func = current_perl_toolchain, |
| 138 | ), |
| 139 | "lz4": struct( |
| 140 | target = "bin/lz4", |
| 141 | current_toolchain_func = current_lz4_toolchain, |
| 142 | ), |
| 143 | } |
| 144 | |
| 145 | def build_toolchain_env(ctx, toolchains): |
| 146 | toolchain_info = [ctx.toolchains[t] for t in toolchains] |
| 147 | env = dict([(k, v) for t in toolchain_info for k, v in t.data.env.items()]) |
| 148 | env = env | {"TOOL_PATH": ":".join([t.data.target.files.to_list()[0].path.rsplit("/", 1)[0] for t in toolchain_info])} |
| 149 | |
| 150 | inputs = depset(transitive = [ |
| 151 | depset(transitive = [t.data.target.files, t.data.target.default_runfiles.files]) |
| 152 | for t in toolchain_info |
| 153 | ]) |
| 154 | |
| 155 | return env, inputs |
| 156 | |
| 157 | TOOLCHAIN_ENV_SETUP = """ |
| 158 | set -e |
| 159 | |
| 160 | # Iterate over all environment variables and expand paths that are |
| 161 | # either external or bazel-out. |
| 162 | for name in $(env | cut -d= -f1); do |
| 163 | val="${!name}" |
| 164 | [[ "$val" != *external/* && "$val" != *bazel-out/* ]] && continue # Quick skip |
| 165 | |
| 166 | sep=' '; [[ $name == "TOOL_PATH" ]] && sep=':' # Set separator: : for PATH, space otherwise |
| 167 | IFS=$sep read -r -a items <<< "$val" # Split value into array using correct separator |
| 168 | |
| 169 | for i in "${!items[@]}"; do |
| 170 | key="${items[i]%%=*}"; v="${items[i]#*=}" # Handle 'key=val' and standalone paths |
| 171 | if [[ ( $v == external/* || $v == bazel-out/* ) && -e "$v" ]]; then |
| 172 | [ "$key" = "$v" ] && items[i]=$(realpath -s "$v") || items[i]="$key=$(realpath -s "$v")" |
| 173 | fi |
| 174 | done |
| 175 | export "$name=$(IFS=$sep; echo "${items[*]}")" # Re-export with correct separator |
| 176 | done |
| 177 | |
| 178 | # Add our now expanded TOOL_PATH to PATH |
| 179 | PATH="$PATH:$TOOL_PATH" |
| 180 | |
| 181 | """ |