blob: 8f6030eb4640280676ff8ca275aa4e8d2fb96d0e [file] [log] [blame]
Tim Windelschmidt98000a52025-03-06 14:22:15 +01001# Copyright The Monogon Project Authors.
2# SPDX-License-Identifier: Apache-2.0
3
4load("@rules_cc//cc:action_names.bzl", "CPP_LINK_EXECUTABLE_ACTION_NAME", "C_COMPILE_ACTION_NAME")
5load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
6load("//build/toolchain/toolchain-bundle:toolchain.bzl", "TOOLCHAIN_ENV_SETUP", "build_toolchain_env")
7
8DISABLED_FEATURES = []
9
10def 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
54def 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
62def 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