blob: bedbd0fe6bd18da5008671841cfb6adac2da1980 [file] [log] [blame]
Lorenz Brun605efbe2021-09-28 14:01:01 +02001load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "feature", "flag_group", "flag_set", "tool", "tool_path", "with_feature_set")
2load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
3
4all_compile_actions = [
5 ACTION_NAMES.c_compile,
6 ACTION_NAMES.cpp_compile,
7 ACTION_NAMES.linkstamp_compile,
8 ACTION_NAMES.assemble,
9 ACTION_NAMES.preprocess_assemble,
10 ACTION_NAMES.cpp_header_parsing,
11 ACTION_NAMES.cpp_module_compile,
12 ACTION_NAMES.cpp_module_codegen,
13 ACTION_NAMES.clif_match,
14 ACTION_NAMES.lto_backend,
15]
16
17all_cpp_compile_actions = [
18 ACTION_NAMES.cpp_compile,
19 ACTION_NAMES.linkstamp_compile,
20 ACTION_NAMES.cpp_header_parsing,
21 ACTION_NAMES.cpp_module_compile,
22 ACTION_NAMES.cpp_module_codegen,
23 ACTION_NAMES.clif_match,
24]
25
26preprocessor_compile_actions = [
27 ACTION_NAMES.c_compile,
28 ACTION_NAMES.cpp_compile,
29 ACTION_NAMES.linkstamp_compile,
30 ACTION_NAMES.preprocess_assemble,
31 ACTION_NAMES.cpp_header_parsing,
32 ACTION_NAMES.cpp_module_compile,
33 ACTION_NAMES.clif_match,
34]
35
36codegen_compile_actions = [
37 ACTION_NAMES.c_compile,
38 ACTION_NAMES.cpp_compile,
39 ACTION_NAMES.linkstamp_compile,
40 ACTION_NAMES.assemble,
41 ACTION_NAMES.preprocess_assemble,
42 ACTION_NAMES.cpp_module_codegen,
43 ACTION_NAMES.lto_backend,
44]
45
46all_link_actions = [
47 ACTION_NAMES.cpp_link_executable,
48 ACTION_NAMES.cpp_link_dynamic_library,
49 ACTION_NAMES.cpp_link_nodeps_dynamic_library,
50]
51
52lto_index_actions = [
53 ACTION_NAMES.lto_index_for_executable,
54 ACTION_NAMES.lto_index_for_dynamic_library,
55 ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
56]
57
58# This defines a relatively minimal EFI toolchain based on host LLVM and no standard library or headers.
59def _efi_k8_cc_toolchain_impl(ctx):
60 default_compile_flags_feature = feature(
61 name = "default_compile_flags",
62 enabled = True,
63 flag_sets = [
64 flag_set(
65 actions = all_compile_actions,
66 flag_groups = ([
67 flag_group(
68 flags = ["-target", "x86_64-unknown-windows"],
69 ),
70 ]),
71 ),
72 flag_set(
73 actions = all_compile_actions,
74 flag_groups = ([
75 flag_group(
76 flags = ["-g"],
77 ),
78 ]),
79 with_features = [with_feature_set(features = ["dbg"])],
80 ),
81 flag_set(
82 actions = all_compile_actions,
83 flag_groups = ([
84 flag_group(
85 # Don't bother with O3, this is an EFI toolchain. It's unlikely to gain much performance here
86 # and increases the risk of dangerous optimizations.
87 flags = ["-O2", "-DNDEBUG"],
88 ),
89 ]),
90 with_features = [with_feature_set(features = ["opt"])],
91 ),
92 ],
93 )
94
95 # "Hybrid" mode disables some MSVC C extensions (but keeps its ABI), but still identifies as MSVC.
96 # This is useful if code requires GNU extensions to work which are silently ignored in full MSVC mode.
97 # As EFI does not include Windows headers which depend on nonstandard C behavior this should be fine for most code.
98 # If this feature is disabled, the toolchain runs with MSVC C extensions fully enabled.
99 hybrid_gnu_msvc_feature = feature(
100 name = "hybrid_gnu_msvc",
101 enabled = True,
102 flag_sets = [
103 flag_set(
104 actions = all_compile_actions,
105 flag_groups = [
106 flag_group(
107 flags = ["-D_MSC_VER=1920", "-fno-ms-compatibility", "-fno-ms-extensions"],
108 ),
109 ],
110 ),
111 ],
112 )
113
114 default_link_flags_feature = feature(
115 name = "default_link_flags",
116 enabled = True,
117 flag_sets = [
118 flag_set(
119 actions = all_link_actions + lto_index_actions,
120 flag_groups = ([
121 flag_group(
122 flags = [
123 "-target",
124 "x86_64-unknown-windows",
125 "-fuse-ld=lld",
126 "-Wl,-entry:efi_main",
127 "-Wl,-subsystem:efi_application",
128 "-Wl,/BASE:0x0",
Serge Bazanski4f00f902023-12-19 13:54:04 +0100129 "-Wl,/Brepro",
Lorenz Brun605efbe2021-09-28 14:01:01 +0200130 "-nostdlib",
131 "build/toolchain/llvm-efi/fltused.o",
132 ],
133 ),
134 ]),
135 ),
136 ],
137 )
138
139 lto_feature = feature(
140 name = "lto",
141 enabled = False,
142 flag_sets = [
143 flag_set(
144 actions = all_compile_actions + all_link_actions,
145 flag_groups = ([
146 flag_group(
147 flags = [
148 "-flto",
149 ],
150 ),
151 ]),
152 ),
153 ],
154 )
155
156 tool_paths = [
157 tool_path(
158 name = "gcc",
159 path = "/usr/bin/clang",
160 ),
161 tool_path(
162 name = "ld",
163 path = "/usr/bin/lld-link",
164 ),
165 tool_path(
166 name = "ar",
167 path = "/usr/bin/llvm-ar",
168 ),
169 tool_path(
170 name = "cpp",
171 path = "/bin/false",
172 ),
173 tool_path(
174 name = "gcov",
175 path = "/bin/false",
176 ),
177 tool_path(
178 name = "nm",
179 path = "/usr/bin/llvm-nm",
180 ),
181 tool_path(
182 name = "objcopy",
183 # We can't use LLVM's objcopy until we pick up https://reviews.llvm.org/D106942
184 path = "/usr/bin/objcopy",
185 ),
186 tool_path(
187 name = "objdump",
188 path = "/usr/bin/llvm-objdump",
189 ),
190 tool_path(
191 name = "strip",
192 path = "/usr/bin/llvm-strip",
193 ),
194 ]
195
196 return cc_common.create_cc_toolchain_config_info(
197 ctx = ctx,
198 features = [default_link_flags_feature, default_compile_flags_feature, hybrid_gnu_msvc_feature, lto_feature],
199 # Needed for various compiler built-in headers and auxiliary data. No system libraries are being used.
200 cxx_builtin_include_directories = [
Tim Windelschmidtf69d84b2024-07-03 20:32:19 +0200201 "/usr/lib/clang/18/include/"
Lorenz Brun605efbe2021-09-28 14:01:01 +0200202 ],
203 toolchain_identifier = "k8-toolchain",
204 host_system_name = "local",
205 target_system_name = "x86_64-efi",
206 target_cpu = "k8",
207 target_libc = "none",
208 compiler = "clang",
209 abi_version = "none",
210 abi_libc_version = "none",
211 tool_paths = tool_paths,
212 )
213
214efi_k8_cc_toolchain_config = rule(
215 implementation = _efi_k8_cc_toolchain_impl,
216 attrs = {},
217 provides = [CcToolchainConfigInfo],
218)