blob: 33925c65c449ea55f2f051d87e93fd70f2866036 [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/build_defs/cc:action_names.bzl", "ACTION_NAMES")
Tim Windelschmidt156248b2025-01-10 00:27:45 +010018load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "feature", "flag_group", "flag_set", "tool_path")
Tim Windelschmidtd4817492025-06-16 15:03:12 +020019load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
Lorenz Brun735119f2021-03-11 00:30:01 +010020
21all_compile_actions = [
22 ACTION_NAMES.c_compile,
23 ACTION_NAMES.cpp_compile,
24 ACTION_NAMES.linkstamp_compile,
25 ACTION_NAMES.assemble,
26 ACTION_NAMES.preprocess_assemble,
27 ACTION_NAMES.cpp_header_parsing,
28 ACTION_NAMES.cpp_module_compile,
29 ACTION_NAMES.cpp_module_codegen,
30 ACTION_NAMES.clif_match,
31 ACTION_NAMES.lto_backend,
32]
33
34all_cpp_compile_actions = [
35 ACTION_NAMES.cpp_compile,
36 ACTION_NAMES.linkstamp_compile,
37 ACTION_NAMES.cpp_header_parsing,
38 ACTION_NAMES.cpp_module_compile,
39 ACTION_NAMES.cpp_module_codegen,
40 ACTION_NAMES.clif_match,
41]
42
43preprocessor_compile_actions = [
44 ACTION_NAMES.c_compile,
45 ACTION_NAMES.cpp_compile,
46 ACTION_NAMES.linkstamp_compile,
47 ACTION_NAMES.preprocess_assemble,
48 ACTION_NAMES.cpp_header_parsing,
49 ACTION_NAMES.cpp_module_compile,
50 ACTION_NAMES.clif_match,
51]
52
53codegen_compile_actions = [
54 ACTION_NAMES.c_compile,
55 ACTION_NAMES.cpp_compile,
56 ACTION_NAMES.linkstamp_compile,
57 ACTION_NAMES.assemble,
58 ACTION_NAMES.preprocess_assemble,
59 ACTION_NAMES.cpp_module_codegen,
60 ACTION_NAMES.lto_backend,
61]
62
Serge Bazanski70e5a972023-06-20 13:19:20 +020063executable_link_actions = [
64 ACTION_NAMES.cpp_link_executable,
65 ACTION_NAMES.lto_index_for_executable,
66]
67
Lorenz Brun735119f2021-03-11 00:30:01 +010068all_link_actions = [
69 ACTION_NAMES.cpp_link_executable,
70 ACTION_NAMES.cpp_link_dynamic_library,
71 ACTION_NAMES.cpp_link_nodeps_dynamic_library,
72]
73
74lto_index_actions = [
75 ACTION_NAMES.lto_index_for_executable,
76 ACTION_NAMES.lto_index_for_dynamic_library,
77 ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
78]
Serge Bazanski385c12f2020-06-17 12:12:42 +020079
Serge Bazanski9e861a82020-09-16 13:46:41 +020080# This defines a minimal, barely parametrized toolchain configuration rule that
81# uses the host GCC with some possible overrides.
Serge Bazanski385c12f2020-06-17 12:12:42 +020082
83def _host_cc_toolchain_impl(ctx):
Lorenz Brun735119f2021-03-11 00:30:01 +010084 cpp_feature = feature(
85 name = "cpp",
86 enabled = ctx.attr.has_cpp,
87 flag_sets = [
88 flag_set(
89 actions = all_cpp_compile_actions + [ACTION_NAMES.lto_backend],
90 flag_groups = ([
91 flag_group(
92 flags = ["-std=c++17"],
93 ),
94 ]),
95 ),
96 flag_set(
97 actions = all_link_actions + lto_index_actions,
98 flag_groups = ([
99 flag_group(
100 flags = [
101 "-lstdc++",
102 ],
103 ),
104 ]),
105 ),
106 ],
107 )
108 link_full_libc_feature = feature(
109 name = "link_full_libc",
110 enabled = True,
111 flag_sets = [
112 flag_set(
113 actions = all_link_actions + lto_index_actions,
114 flag_groups = ([
115 flag_group(
116 flags = [
117 "-lm",
118 "-lutil",
119 "-lpthread",
120 ],
121 ),
122 ] if ctx.attr.is_glibc else []), # musl just works
123 ),
124 ],
125 )
Serge Bazanski70e5a972023-06-20 13:19:20 +0200126 pie_feature = feature(
127 name = "pie",
128 enabled = False,
129 flag_sets = [
130 flag_set(
131 actions = executable_link_actions,
132 flag_groups = ([
133 flag_group(
134 flags = [
135 "-static-pie",
136 ],
137 ),
138 ]),
139 ),
140 flag_set(
141 actions = all_compile_actions,
142 flag_groups = ([
143 flag_group(
144 flags = [
145 "-fPIE",
146 ],
147 ),
148 ]),
149 ),
150 ],
151 )
Lorenz Brun735119f2021-03-11 00:30:01 +0100152 default_link_flags_feature = feature(
153 name = "default_link_flags",
154 enabled = True,
155 flag_sets = [
156 flag_set(
157 actions = all_link_actions + lto_index_actions,
158 flag_groups = ([
159 flag_group(
160 flags = [
161 "-Wl,-z,relro,-z,now",
162 "-pass-exit-codes",
163 ],
164 ),
165 ]),
166 ),
167 ],
168 )
Serge Bazanski385c12f2020-06-17 12:12:42 +0200169 tool_paths = [
170 tool_path(
171 name = "gcc",
Serge Bazanski9e861a82020-09-16 13:46:41 +0200172 path = ctx.attr.gcc,
Serge Bazanski385c12f2020-06-17 12:12:42 +0200173 ),
174 tool_path(
175 name = "ld",
176 path = "/usr/bin/ld",
177 ),
178 tool_path(
179 name = "ar",
180 path = "/usr/bin/ar",
181 ),
182 tool_path(
183 name = "cpp",
184 path = "/bin/false",
185 ),
186 tool_path(
187 name = "gcov",
188 path = "/bin/false",
189 ),
190 tool_path(
191 name = "nm",
192 path = "/bin/false",
193 ),
194 tool_path(
195 name = "objdump",
196 path = "/bin/false",
197 ),
198 tool_path(
199 name = "strip",
200 path = "/bin/false",
201 ),
Leopoldbc93c2b2023-01-14 13:12:23 +0100202 tool_path(
203 name = "objcopy",
204 path = "/usr/bin/objcopy",
205 ),
Serge Bazanski385c12f2020-06-17 12:12:42 +0200206 ]
Lorenz Brun735119f2021-03-11 00:30:01 +0100207
Serge Bazanski385c12f2020-06-17 12:12:42 +0200208 return cc_common.create_cc_toolchain_config_info(
209 ctx = ctx,
Serge Bazanski70e5a972023-06-20 13:19:20 +0200210 features = [
211 default_link_flags_feature,
212 link_full_libc_feature,
213 cpp_feature,
214 pie_feature,
215 ],
Serge Bazanski9e861a82020-09-16 13:46:41 +0200216 cxx_builtin_include_directories = ctx.attr.host_includes,
Serge Bazanski385c12f2020-06-17 12:12:42 +0200217 toolchain_identifier = "k8-toolchain",
218 host_system_name = "local",
219 target_system_name = "local",
220 target_cpu = "k8",
221 target_libc = "unknown",
222 compiler = "gcc",
223 abi_version = "unknown",
224 abi_libc_version = "unknown",
225 tool_paths = tool_paths,
Serge Bazanski9e861a82020-09-16 13:46:41 +0200226 builtin_sysroot = ctx.attr.sysroot,
Serge Bazanski385c12f2020-06-17 12:12:42 +0200227 )
228
229host_cc_toolchain_config = rule(
230 implementation = _host_cc_toolchain_impl,
Serge Bazanski9e861a82020-09-16 13:46:41 +0200231 attrs = {
232 "gcc": attr.string(
233 default = "/usr/bin/gcc",
234 ),
Lorenz Brun735119f2021-03-11 00:30:01 +0100235 "has_cpp": attr.bool(default = True),
236 "is_glibc": attr.bool(default = True),
Serge Bazanski9e861a82020-09-16 13:46:41 +0200237 "host_includes": attr.string_list(
238 default = [
Tim Windelschmidtf69d84b2024-07-03 20:32:19 +0200239 "/usr/lib/gcc/x86_64-redhat-linux/14/include/",
Serge Bazanski9e861a82020-09-16 13:46:41 +0200240 "/usr/include",
241 ],
242 ),
243 "sysroot": attr.string(
244 default = "",
245 ),
246 },
Serge Bazanski385c12f2020-06-17 12:12:42 +0200247 provides = [CcToolchainConfigInfo],
248)