blob: 697b6750d53845d814ae1434c03d7e42f3e4ba15 [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",
129 "-nostdlib",
130 "build/toolchain/llvm-efi/fltused.o",
131 ],
132 ),
133 ]),
134 ),
135 ],
136 )
137
138 lto_feature = feature(
139 name = "lto",
140 enabled = False,
141 flag_sets = [
142 flag_set(
143 actions = all_compile_actions + all_link_actions,
144 flag_groups = ([
145 flag_group(
146 flags = [
147 "-flto",
148 ],
149 ),
150 ]),
151 ),
152 ],
153 )
154
155 tool_paths = [
156 tool_path(
157 name = "gcc",
158 path = "/usr/bin/clang",
159 ),
160 tool_path(
161 name = "ld",
162 path = "/usr/bin/lld-link",
163 ),
164 tool_path(
165 name = "ar",
166 path = "/usr/bin/llvm-ar",
167 ),
168 tool_path(
169 name = "cpp",
170 path = "/bin/false",
171 ),
172 tool_path(
173 name = "gcov",
174 path = "/bin/false",
175 ),
176 tool_path(
177 name = "nm",
178 path = "/usr/bin/llvm-nm",
179 ),
180 tool_path(
181 name = "objcopy",
182 # We can't use LLVM's objcopy until we pick up https://reviews.llvm.org/D106942
183 path = "/usr/bin/objcopy",
184 ),
185 tool_path(
186 name = "objdump",
187 path = "/usr/bin/llvm-objdump",
188 ),
189 tool_path(
190 name = "strip",
191 path = "/usr/bin/llvm-strip",
192 ),
193 ]
194
195 return cc_common.create_cc_toolchain_config_info(
196 ctx = ctx,
197 features = [default_link_flags_feature, default_compile_flags_feature, hybrid_gnu_msvc_feature, lto_feature],
198 # Needed for various compiler built-in headers and auxiliary data. No system libraries are being used.
199 cxx_builtin_include_directories = [
200 "/usr/lib64/clang",
201 ],
202 toolchain_identifier = "k8-toolchain",
203 host_system_name = "local",
204 target_system_name = "x86_64-efi",
205 target_cpu = "k8",
206 target_libc = "none",
207 compiler = "clang",
208 abi_version = "none",
209 abi_libc_version = "none",
210 tool_paths = tool_paths,
211 )
212
213efi_k8_cc_toolchain_config = rule(
214 implementation = _efi_k8_cc_toolchain_impl,
215 attrs = {},
216 provides = [CcToolchainConfigInfo],
217)