xref: /aosp_15_r20/external/pigweed/pw_toolchain/host_clang/BUILD.bazel (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2023 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
16load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
17load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
18load("@rules_cc//cc/toolchains:feature.bzl", "cc_feature")
19load("@rules_cc//cc/toolchains:feature_constraint.bzl", "cc_feature_constraint")
20load("@rules_cc//cc/toolchains:feature_set.bzl", "cc_feature_set")
21load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain")
22load("//pw_toolchain/host_clang:paths.bzl", "LLVM_TOOLCHAIN")
23
24package(default_visibility = ["//visibility:public"])
25
26licenses(["notice"])
27
28filegroup(name = "empty")
29
30cc_args(
31    name = "link_with_lld",
32    actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
33    args = ["-fuse-ld=lld"],
34)
35
36cc_args(
37    name = "macos_link_libs",
38    actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
39    args = [
40        # Force dropping the system libc++.
41        "-nostdlib++",
42        # Use libc++ provided by the toolchain.
43        LLVM_TOOLCHAIN + "/lib/libc++.a",
44    ],
45    target_compatible_with = ["@platforms//os:macos"],
46)
47
48cc_args(
49    name = "linux_link_libs",
50    actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
51    args = [
52        "-pthread",
53        "-stdlib=libc++",
54        "--rtlib=compiler-rt",
55        "--unwindlib=libunwind",
56    ],
57    target_compatible_with = ["@platforms//os:linux"],
58)
59
60cc_args(
61    name = "libtool_darwin_flags",
62    actions = ["@rules_cc//cc/toolchains/actions:cpp_link_static_library"],
63    args = ["-no_warning_for_no_symbols"],
64)
65
66# Thread safety warnings are only supported by Clang.
67cc_args(
68    name = "thread_safety_warnings",
69    actions = [
70        "@rules_cc//cc/toolchains/actions:c_compile_actions",
71        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
72    ],
73    args = [
74        "-Wthread-safety",
75        "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS=1",
76    ],
77)
78
79cc_args(
80    name = "verbose_compiler_flags",
81    actions = [
82        "@rules_cc//cc/toolchains/actions:compile_actions",
83        "@rules_cc//cc/toolchains/actions:link_actions",
84    ],
85    args = ["-v"],
86)
87
88# A feature that can be easily toggled to include extra compiler output to help
89# debug things like include search path ordering and showing all the flags
90# passed to the compiler.
91#
92# Add `--features=verbose_compiler_output` to your Bazel invocation to enable.
93cc_feature(
94    name = "verbose_compiler_output",
95    args = [":verbose_compiler_flags"],
96    feature_name = "verbose_compiler_output",
97)
98
99cc_args(
100    name = "no_unknown_warning_option",
101    actions = [
102        "@rules_cc//cc/toolchains/actions:c_compile_actions",
103        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
104    ],
105    args = [
106        "-Wno-unknown-warning-option",
107    ],
108)
109
110bool_flag(
111    name = "asan",
112    build_setting_default = False,
113)
114
115config_setting(
116    name = "asan_enabled",
117    flag_values = {
118        ":asan": "true",
119    },
120)
121
122cc_feature(
123    name = "asan_feature",
124    args = ["//pw_toolchain/cc/args:asan"],
125    feature_name = "asan",
126    requires_any_of = [":asan_constraint"],
127)
128
129cc_feature_set(
130    name = "asan_constraint",
131    # Rust uses the C++ linker, but not the C++ compiler, so we need to ensure
132    # -fsanitize=address is not be specified during Rust linking.
133    all_of = [":rules_rust_unsupported_feature"],
134)
135
136bool_flag(
137    name = "ubsan",
138    build_setting_default = False,
139)
140
141config_setting(
142    name = "ubsan_enabled",
143    flag_values = {
144        ":ubsan": "true",
145    },
146)
147
148cc_feature(
149    name = "ubsan_feature",
150    args = ["//pw_toolchain/cc/args:ubsan"],
151    feature_name = "ubsan",
152    requires_any_of = [":ubsan_constraint"],
153)
154
155cc_feature_set(
156    name = "ubsan_constraint",
157    # Rust uses the C++ linker, but not the C++ compiler, so we need to ensure
158    # -fsanitize=undefined is not be specified during Rust linking.
159    all_of = [":rules_rust_unsupported_feature"],
160)
161
162bool_flag(
163    name = "tsan",
164    build_setting_default = False,
165)
166
167config_setting(
168    name = "tsan_enabled",
169    flag_values = {
170        ":tsan": "true",
171    },
172)
173
174cc_feature(
175    name = "tsan_feature",
176    args = ["//pw_toolchain/cc/args:tsan"],
177    feature_name = "tsan",
178    requires_any_of = [":tsan_constraint"],
179)
180
181cc_feature_set(
182    name = "tsan_constraint",
183    # Rust uses the C++ linker, but not the C++ compiler, so we need to ensure
184    # -fsanitize=undefined is not be specified during Rust linking.
185    all_of = [":rules_rust_unsupported_feature"],
186)
187
188bool_flag(
189    name = "fuzztest",
190    build_setting_default = False,
191)
192
193config_setting(
194    name = "fuzztest_enabled",
195    flag_values = {
196        ":fuzztest": "true",
197    },
198)
199
200cc_feature(
201    name = "fuzztest_feature",
202    args = ["//pw_toolchain/cc/args:fuzztest"],
203    feature_name = "fuzztest",
204)
205
206# This is a sentinel feature defined by rules_rust. It is by definition
207# unsupported: rules_rust will disable this feature when linking Rust code.
208cc_feature(
209    name = "rules_rust_unsupported_feature",
210    feature_name = "rules_rust_unsupported_feature",
211)
212
213# This is a sentinel feature defined by rules_go. It is by definition
214# unsupported: rules_go will disable this feature when linking Go code.
215cc_feature(
216    name = "rules_go_unsupported_feature",
217    feature_name = "rules_go_unsupported_feature",
218)
219
220cc_feature_constraint(
221    name = "rules_go_constraint",
222
223    # This constraint is saying "not not Golang" (yes Golang / only Golang).
224    none_of = [":rules_go_unsupported_feature"],
225)
226
227# Golang doesn't link with PIE enabled. See pwbug.dev/347708308.
228#
229# We want to disable PIE only when we're *not* compiling Golang code.
230cc_args(
231    name = "no_pie_for_go_flags",
232    actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
233    args = [
234        "-no-pie",
235    ],
236    requires_any_of = [":rules_go_constraint"],
237    target_compatible_with = ["@platforms//os:linux"],
238)
239
240cc_args(
241    name = "silence_cgo_warnings",
242    actions = [
243        "@rules_cc//cc/toolchains/actions:compile_actions",
244        "@rules_cc//cc/toolchains/actions:link_actions",
245    ],
246    args = ["-Wno-unused-parameter"],
247    requires_any_of = [":rules_go_constraint"],
248)
249
250cc_feature(
251    name = "supports_pic",
252    overrides = "@rules_cc//cc/toolchains/features:supports_pic",
253)
254
255# Symlink to clangd, for user convenience.
256copy_file(
257    name = "copy_clangd",
258    src = "@llvm_toolchain//:bin/clangd",
259    out = "clangd",
260    allow_symlink = True,
261)
262
263cc_toolchain(
264    name = "host_toolchain",
265    args = select({
266        "@platforms//os:linux": [
267            ":linux_link_libs",
268            ":no_pie_for_go_flags",
269            "@linux_sysroot//:sysroot",
270        ],
271        "@platforms//os:macos": [
272            ":macos_link_libs",
273            ":libtool_darwin_flags",
274            "@macos_sysroot//:sysroot",
275        ],
276        "//conditions:default": [],
277    }) + [
278        ":thread_safety_warnings",
279        ":link_with_lld",
280        "//pw_toolchain/cc/args:debugging",
281        "//pw_toolchain/cc/args:reduced_size",
282        "//pw_toolchain/cc/args:no_canonical_prefixes",
283        "//pw_toolchain/cc/args:no_rtti",
284        "//pw_toolchain/cc/args:wno_register",
285        "//pw_toolchain/cc/args:wnon_virtual_dtor",
286        "//pw_toolchain/cc/args:common_warnings",
287        "//pw_toolchain/cc/args:color_diagnostics",
288        # Must go after the general warnings that are enabled.
289        ":silence_cgo_warnings",
290    ] + select({
291        "//pw_build:kythe": [":no_unknown_warning_option"],
292        "//conditions:default": [],
293    }),
294    enabled_features = [
295        "@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features",
296        ":supports_pic",
297        ":rules_go_unsupported_feature",
298        ":rules_rust_unsupported_feature",
299        "//pw_toolchain/cc/capability:compiler_is_clang",
300        "//pw_toolchain/cc/capability:linker_is_clang",
301    ] + select({
302        ":asan_enabled": [":asan_feature"],
303        "//conditions:default": [],
304    }) + select({
305        ":ubsan_enabled": [":ubsan_feature"],
306        "//conditions:default": [],
307    }) + select({
308        ":tsan_enabled": [":tsan_feature"],
309        "//conditions:default": [],
310    }) + select({
311        "//pw_toolchain/cc:c++17_enabled": ["//pw_toolchain/cc/args:c++17_feature"],
312        "//conditions:default": [],
313    }) + select({
314        "//pw_toolchain/cc:c++20_enabled": ["//pw_toolchain/cc/args:c++20_feature"],
315        "//conditions:default": [],
316    }) + select({
317        ":fuzztest_enabled": [":fuzztest_feature"],
318        "//conditions:default": [],
319    }),
320    known_features = [
321        "@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features",
322        ":asan_feature",
323        ":ubsan_feature",
324        ":tsan_feature",
325        ":verbose_compiler_output",
326        ":rules_rust_unsupported_feature",
327        ":supports_pic",
328        "//pw_toolchain/cc/args:c++17_feature",
329        "//pw_toolchain/cc/args:c++20_feature",
330        "//pw_toolchain/cc/capability:compiler_is_clang",
331        "//pw_toolchain/cc/capability:linker_is_clang",
332    ],
333    tool_map = "@llvm_toolchain//:all_tools",
334)
335
336toolchain(
337    name = "host_cc_toolchain_linux",
338    exec_compatible_with = [
339        "@platforms//os:linux",
340    ],
341    target_compatible_with = [
342        "@platforms//os:linux",
343    ],
344    toolchain = ":host_toolchain",
345    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
346)
347
348toolchain(
349    name = "host_cc_toolchain_macos",
350    exec_compatible_with = [
351        "@platforms//os:macos",
352    ],
353    target_compatible_with = [
354        "@platforms//os:macos",
355    ],
356    toolchain = ":host_toolchain",
357    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
358)
359