blob: 76470212da16efc3b4ff6591534f58b462575b6b [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
62all_link_actions = [
63 ACTION_NAMES.cpp_link_executable,
64 ACTION_NAMES.cpp_link_dynamic_library,
65 ACTION_NAMES.cpp_link_nodeps_dynamic_library,
66]
67
68lto_index_actions = [
69 ACTION_NAMES.lto_index_for_executable,
70 ACTION_NAMES.lto_index_for_dynamic_library,
71 ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
72]
Serge Bazanski385c12f2020-06-17 12:12:42 +020073
Serge Bazanski9e861a82020-09-16 13:46:41 +020074# This defines a minimal, barely parametrized toolchain configuration rule that
75# uses the host GCC with some possible overrides.
Serge Bazanski385c12f2020-06-17 12:12:42 +020076
77def _host_cc_toolchain_impl(ctx):
Lorenz Brun735119f2021-03-11 00:30:01 +010078 cpp_feature = feature(
79 name = "cpp",
80 enabled = ctx.attr.has_cpp,
81 flag_sets = [
82 flag_set(
83 actions = all_cpp_compile_actions + [ACTION_NAMES.lto_backend],
84 flag_groups = ([
85 flag_group(
86 flags = ["-std=c++17"],
87 ),
88 ]),
89 ),
90 flag_set(
91 actions = all_link_actions + lto_index_actions,
92 flag_groups = ([
93 flag_group(
94 flags = [
95 "-lstdc++",
96 ],
97 ),
98 ]),
99 ),
100 ],
101 )
102 link_full_libc_feature = feature(
103 name = "link_full_libc",
104 enabled = True,
105 flag_sets = [
106 flag_set(
107 actions = all_link_actions + lto_index_actions,
108 flag_groups = ([
109 flag_group(
110 flags = [
111 "-lm",
112 "-lutil",
113 "-lpthread",
114 ],
115 ),
116 ] if ctx.attr.is_glibc else []), # musl just works
117 ),
118 ],
119 )
120 default_link_flags_feature = feature(
121 name = "default_link_flags",
122 enabled = True,
123 flag_sets = [
124 flag_set(
125 actions = all_link_actions + lto_index_actions,
126 flag_groups = ([
127 flag_group(
128 flags = [
129 "-Wl,-z,relro,-z,now",
130 "-pass-exit-codes",
131 ],
132 ),
133 ]),
134 ),
135 ],
136 )
Serge Bazanski385c12f2020-06-17 12:12:42 +0200137 tool_paths = [
138 tool_path(
139 name = "gcc",
Serge Bazanski9e861a82020-09-16 13:46:41 +0200140 path = ctx.attr.gcc,
Serge Bazanski385c12f2020-06-17 12:12:42 +0200141 ),
142 tool_path(
143 name = "ld",
144 path = "/usr/bin/ld",
145 ),
146 tool_path(
147 name = "ar",
148 path = "/usr/bin/ar",
149 ),
150 tool_path(
151 name = "cpp",
152 path = "/bin/false",
153 ),
154 tool_path(
155 name = "gcov",
156 path = "/bin/false",
157 ),
158 tool_path(
159 name = "nm",
160 path = "/bin/false",
161 ),
162 tool_path(
163 name = "objdump",
164 path = "/bin/false",
165 ),
166 tool_path(
167 name = "strip",
168 path = "/bin/false",
169 ),
170 ]
Lorenz Brun735119f2021-03-11 00:30:01 +0100171
Serge Bazanski385c12f2020-06-17 12:12:42 +0200172 return cc_common.create_cc_toolchain_config_info(
173 ctx = ctx,
Lorenz Brun735119f2021-03-11 00:30:01 +0100174 features = [default_link_flags_feature, link_full_libc_feature, cpp_feature],
Serge Bazanski9e861a82020-09-16 13:46:41 +0200175 cxx_builtin_include_directories = ctx.attr.host_includes,
Serge Bazanski385c12f2020-06-17 12:12:42 +0200176 toolchain_identifier = "k8-toolchain",
177 host_system_name = "local",
178 target_system_name = "local",
179 target_cpu = "k8",
180 target_libc = "unknown",
181 compiler = "gcc",
182 abi_version = "unknown",
183 abi_libc_version = "unknown",
184 tool_paths = tool_paths,
Serge Bazanski9e861a82020-09-16 13:46:41 +0200185 builtin_sysroot = ctx.attr.sysroot,
Serge Bazanski385c12f2020-06-17 12:12:42 +0200186 )
187
188host_cc_toolchain_config = rule(
189 implementation = _host_cc_toolchain_impl,
Serge Bazanski9e861a82020-09-16 13:46:41 +0200190 attrs = {
191 "gcc": attr.string(
192 default = "/usr/bin/gcc",
193 ),
Lorenz Brun735119f2021-03-11 00:30:01 +0100194 "has_cpp": attr.bool(default = True),
195 "is_glibc": attr.bool(default = True),
Serge Bazanski9e861a82020-09-16 13:46:41 +0200196 "host_includes": attr.string_list(
197 default = [
Lorenz Brun942f5e22022-01-27 15:03:10 +0100198 "/usr/lib/gcc/x86_64-redhat-linux/11/include/",
Serge Bazanski9e861a82020-09-16 13:46:41 +0200199 "/usr/include",
200 ],
201 ),
202 "sysroot": attr.string(
203 default = "",
204 ),
205 },
Serge Bazanski385c12f2020-06-17 12:12:42 +0200206 provides = [CcToolchainConfigInfo],
207)