blob: 0fdb23e2b45983a2e9e0d26eef5a2419833f2ace [file] [log] [blame]
Serge Bazanski385c12f2020-06-17 12:12:42 +02001# Copyright 2020 The Monogon Project Authors.
2#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Lorenz Brun735119f2021-03-11 00:30:01 +010017load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "feature", "flag_group", "flag_set", "tool", "tool_path")
18load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
19
20all_compile_actions = [
21 ACTION_NAMES.c_compile,
22 ACTION_NAMES.cpp_compile,
23 ACTION_NAMES.linkstamp_compile,
24 ACTION_NAMES.assemble,
25 ACTION_NAMES.preprocess_assemble,
26 ACTION_NAMES.cpp_header_parsing,
27 ACTION_NAMES.cpp_module_compile,
28 ACTION_NAMES.cpp_module_codegen,
29 ACTION_NAMES.clif_match,
30 ACTION_NAMES.lto_backend,
31]
32
33all_cpp_compile_actions = [
34 ACTION_NAMES.cpp_compile,
35 ACTION_NAMES.linkstamp_compile,
36 ACTION_NAMES.cpp_header_parsing,
37 ACTION_NAMES.cpp_module_compile,
38 ACTION_NAMES.cpp_module_codegen,
39 ACTION_NAMES.clif_match,
40]
41
42preprocessor_compile_actions = [
43 ACTION_NAMES.c_compile,
44 ACTION_NAMES.cpp_compile,
45 ACTION_NAMES.linkstamp_compile,
46 ACTION_NAMES.preprocess_assemble,
47 ACTION_NAMES.cpp_header_parsing,
48 ACTION_NAMES.cpp_module_compile,
49 ACTION_NAMES.clif_match,
50]
51
52codegen_compile_actions = [
53 ACTION_NAMES.c_compile,
54 ACTION_NAMES.cpp_compile,
55 ACTION_NAMES.linkstamp_compile,
56 ACTION_NAMES.assemble,
57 ACTION_NAMES.preprocess_assemble,
58 ACTION_NAMES.cpp_module_codegen,
59 ACTION_NAMES.lto_backend,
60]
61
Serge Bazanski70e5a972023-06-20 13:19:20 +020062executable_link_actions = [
63 ACTION_NAMES.cpp_link_executable,
64 ACTION_NAMES.lto_index_for_executable,
65]
66
Lorenz Brun735119f2021-03-11 00:30:01 +010067all_link_actions = [
68 ACTION_NAMES.cpp_link_executable,
69 ACTION_NAMES.cpp_link_dynamic_library,
70 ACTION_NAMES.cpp_link_nodeps_dynamic_library,
71]
72
73lto_index_actions = [
74 ACTION_NAMES.lto_index_for_executable,
75 ACTION_NAMES.lto_index_for_dynamic_library,
76 ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
77]
Serge Bazanski385c12f2020-06-17 12:12:42 +020078
Serge Bazanski9e861a82020-09-16 13:46:41 +020079# This defines a minimal, barely parametrized toolchain configuration rule that
80# uses the host GCC with some possible overrides.
Serge Bazanski385c12f2020-06-17 12:12:42 +020081
82def _host_cc_toolchain_impl(ctx):
Lorenz Brun735119f2021-03-11 00:30:01 +010083 cpp_feature = feature(
84 name = "cpp",
85 enabled = ctx.attr.has_cpp,
86 flag_sets = [
87 flag_set(
88 actions = all_cpp_compile_actions + [ACTION_NAMES.lto_backend],
89 flag_groups = ([
90 flag_group(
91 flags = ["-std=c++17"],
92 ),
93 ]),
94 ),
95 flag_set(
96 actions = all_link_actions + lto_index_actions,
97 flag_groups = ([
98 flag_group(
99 flags = [
100 "-lstdc++",
101 ],
102 ),
103 ]),
104 ),
105 ],
106 )
107 link_full_libc_feature = feature(
108 name = "link_full_libc",
109 enabled = True,
110 flag_sets = [
111 flag_set(
112 actions = all_link_actions + lto_index_actions,
113 flag_groups = ([
114 flag_group(
115 flags = [
116 "-lm",
117 "-lutil",
118 "-lpthread",
119 ],
120 ),
121 ] if ctx.attr.is_glibc else []), # musl just works
122 ),
123 ],
124 )
Serge Bazanski70e5a972023-06-20 13:19:20 +0200125 pie_feature = feature(
126 name = "pie",
127 enabled = False,
128 flag_sets = [
129 flag_set(
130 actions = executable_link_actions,
131 flag_groups = ([
132 flag_group(
133 flags = [
134 "-static-pie",
135 ],
136 ),
137 ]),
138 ),
139 flag_set(
140 actions = all_compile_actions,
141 flag_groups = ([
142 flag_group(
143 flags = [
144 "-fPIE",
145 ],
146 ),
147 ]),
148 ),
149 ],
150 )
Lorenz Brun735119f2021-03-11 00:30:01 +0100151 default_link_flags_feature = feature(
152 name = "default_link_flags",
153 enabled = True,
154 flag_sets = [
155 flag_set(
156 actions = all_link_actions + lto_index_actions,
157 flag_groups = ([
158 flag_group(
159 flags = [
160 "-Wl,-z,relro,-z,now",
161 "-pass-exit-codes",
162 ],
163 ),
164 ]),
165 ),
166 ],
167 )
Serge Bazanski385c12f2020-06-17 12:12:42 +0200168 tool_paths = [
169 tool_path(
170 name = "gcc",
Serge Bazanski9e861a82020-09-16 13:46:41 +0200171 path = ctx.attr.gcc,
Serge Bazanski385c12f2020-06-17 12:12:42 +0200172 ),
173 tool_path(
174 name = "ld",
175 path = "/usr/bin/ld",
176 ),
177 tool_path(
178 name = "ar",
179 path = "/usr/bin/ar",
180 ),
181 tool_path(
182 name = "cpp",
183 path = "/bin/false",
184 ),
185 tool_path(
186 name = "gcov",
187 path = "/bin/false",
188 ),
189 tool_path(
190 name = "nm",
191 path = "/bin/false",
192 ),
193 tool_path(
194 name = "objdump",
195 path = "/bin/false",
196 ),
197 tool_path(
198 name = "strip",
199 path = "/bin/false",
200 ),
Leopoldbc93c2b2023-01-14 13:12:23 +0100201 tool_path(
202 name = "objcopy",
203 path = "/usr/bin/objcopy",
204 ),
Serge Bazanski385c12f2020-06-17 12:12:42 +0200205 ]
Lorenz Brun735119f2021-03-11 00:30:01 +0100206
Serge Bazanski385c12f2020-06-17 12:12:42 +0200207 return cc_common.create_cc_toolchain_config_info(
208 ctx = ctx,
Serge Bazanski70e5a972023-06-20 13:19:20 +0200209 features = [
210 default_link_flags_feature,
211 link_full_libc_feature,
212 cpp_feature,
213 pie_feature,
214 ],
Serge Bazanski9e861a82020-09-16 13:46:41 +0200215 cxx_builtin_include_directories = ctx.attr.host_includes,
Serge Bazanski385c12f2020-06-17 12:12:42 +0200216 toolchain_identifier = "k8-toolchain",
217 host_system_name = "local",
218 target_system_name = "local",
219 target_cpu = "k8",
220 target_libc = "unknown",
221 compiler = "gcc",
222 abi_version = "unknown",
223 abi_libc_version = "unknown",
224 tool_paths = tool_paths,
Serge Bazanski9e861a82020-09-16 13:46:41 +0200225 builtin_sysroot = ctx.attr.sysroot,
Serge Bazanski385c12f2020-06-17 12:12:42 +0200226 )
227
228host_cc_toolchain_config = rule(
229 implementation = _host_cc_toolchain_impl,
Serge Bazanski9e861a82020-09-16 13:46:41 +0200230 attrs = {
231 "gcc": attr.string(
232 default = "/usr/bin/gcc",
233 ),
Lorenz Brun735119f2021-03-11 00:30:01 +0100234 "has_cpp": attr.bool(default = True),
235 "is_glibc": attr.bool(default = True),
Serge Bazanski9e861a82020-09-16 13:46:41 +0200236 "host_includes": attr.string_list(
237 default = [
Tim Windelschmidtf69d84b2024-07-03 20:32:19 +0200238 "/usr/lib/gcc/x86_64-redhat-linux/14/include/",
Serge Bazanski9e861a82020-09-16 13:46:41 +0200239 "/usr/include",
240 ],
241 ),
242 "sysroot": attr.string(
243 default = "",
244 ),
245 },
Serge Bazanski385c12f2020-06-17 12:12:42 +0200246 provides = [CcToolchainConfigInfo],
247)