| Tim Windelschmidt | 98000a5 | 2025-03-06 14:22:15 +0100 | [diff] [blame^] | 1 | # Copyright The Monogon Project Authors. |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | load("@rules_cc//cc:action_names.bzl", "CPP_LINK_EXECUTABLE_ACTION_NAME", "C_COMPILE_ACTION_NAME") |
| 5 | load("@rules_cc//cc/common:cc_common.bzl", "cc_common") |
| 6 | load("//build/toolchain/toolchain-bundle:toolchain.bzl", "TOOLCHAIN_ENV_SETUP", "build_toolchain_env") |
| 7 | |
| 8 | DISABLED_FEATURES = [] |
| 9 | |
| 10 | def build_llvm_compiler_env(ctx, cc_toolchain, prefix = ""): |
| 11 | feature_configuration = cc_common.configure_features( |
| 12 | ctx = ctx, |
| 13 | cc_toolchain = cc_toolchain, |
| 14 | requested_features = ctx.features, |
| 15 | unsupported_features = DISABLED_FEATURES + ctx.disabled_features, |
| 16 | ) |
| 17 | c_compiler_path = cc_common.get_tool_for_action( |
| 18 | feature_configuration = feature_configuration, |
| 19 | action_name = C_COMPILE_ACTION_NAME, |
| 20 | ) |
| 21 | c_compile_variables = cc_common.create_compile_variables( |
| 22 | feature_configuration = feature_configuration, |
| 23 | cc_toolchain = cc_toolchain, |
| 24 | user_compile_flags = ctx.fragments.cpp.copts + ctx.fragments.cpp.conlyopts, |
| 25 | ) |
| 26 | c_compiler_flags = cc_common.get_memory_inefficient_command_line( |
| 27 | feature_configuration = feature_configuration, |
| 28 | action_name = C_COMPILE_ACTION_NAME, |
| 29 | variables = c_compile_variables, |
| 30 | ) |
| 31 | c_linker_flags = cc_common.get_memory_inefficient_command_line( |
| 32 | feature_configuration = feature_configuration, |
| 33 | action_name = CPP_LINK_EXECUTABLE_ACTION_NAME, |
| 34 | variables = c_compile_variables, |
| 35 | ) |
| 36 | |
| 37 | # NOTE: Multicall tool is called as path/to/llvm clang to workaround a bug |
| 38 | # in out-of-process execution where tool name is repeated and parsing breaks. |
| 39 | return { |
| 40 | prefix + "CC_PATH": c_compiler_path.rsplit("/", 1)[0], |
| 41 | prefix + "CC": c_compiler_path.rsplit("/", 1)[0] + "/llvm clang", |
| 42 | prefix + "CXX": c_compiler_path.rsplit("/", 1)[0] + "/llvm clang++", |
| 43 | prefix + "LD": c_compiler_path.rsplit("/", 1)[0] + "/ld.lld", |
| 44 | prefix + "AR": c_compiler_path.rsplit("/", 1)[0] + "/llvm-ar", |
| 45 | prefix + "NM": c_compiler_path.rsplit("/", 1)[0] + "/llvm-nm", |
| 46 | prefix + "STRIP": c_compiler_path.rsplit("/", 1)[0] + "/llvm-strip", |
| 47 | prefix + "OBJCOPY": c_compiler_path.rsplit("/", 1)[0] + "/llvm-objcopy", |
| 48 | prefix + "OBJDUMP": c_compiler_path.rsplit("/", 1)[0] + "/llvm-objdump", |
| 49 | prefix + "READELF": c_compiler_path.rsplit("/", 1)[0] + "/llvm-readelf", |
| 50 | prefix + "CFLAGS": " ".join(c_compiler_flags), |
| 51 | prefix + "LDFLAGS": " ".join(c_linker_flags), |
| 52 | }, cc_toolchain.all_files |
| 53 | |
| 54 | def merge_env(env, extra_env): |
| 55 | for k, v in extra_env.items(): |
| 56 | if k in env: |
| 57 | env[k] += " " + v |
| 58 | else: |
| 59 | env[k] = v |
| 60 | return env |
| 61 | |
| 62 | def generate_foreign_build_env(ctx, target_toolchain, exec_toolchain, toolchain_bundle_tools): |
| 63 | env = {} |
| 64 | |
| 65 | # Figure out cc_toolchains |
| 66 | target_toolchain_env, target_toolchain_inputs = build_llvm_compiler_env(ctx, target_toolchain) |
| 67 | env = merge_env(env, target_toolchain_env) |
| 68 | |
| 69 | exec_toolchain_env, exec_toolchain_inputs = build_llvm_compiler_env(ctx, exec_toolchain, "HOST") |
| 70 | env = merge_env(env, exec_toolchain_env) |
| 71 | |
| 72 | # Setup tools from toolchain-bundle. |
| 73 | toolchain_bundle_env, toolchain_bundle_inputs = build_toolchain_env(ctx, toolchain_bundle_tools) |
| 74 | env = merge_env(env, toolchain_bundle_env) |
| 75 | |
| 76 | inputs = depset( |
| 77 | transitive = [ |
| 78 | target_toolchain_inputs, |
| 79 | exec_toolchain_inputs, |
| 80 | toolchain_bundle_inputs, |
| 81 | ], |
| 82 | ) |
| 83 | |
| 84 | return env, inputs, TOOLCHAIN_ENV_SETUP |