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