• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(
17    "@pw_toolchain//cc_toolchain:defs.bzl",
18    "pw_cc_action_files",
19    "pw_cc_feature",
20    "pw_cc_feature_set",
21    "pw_cc_flag_set",
22    "pw_cc_toolchain",
23)
24load("@pw_xcode_command_line_tools//:defs.bzl", "XCODE_SDK_PATH")
25
26package(default_visibility = ["//visibility:public"])
27
28licenses(["notice"])
29
30filegroup(name = "empty")
31
32pw_cc_flag_set(
33    name = "macos_link_libs",
34    actions = ["@pw_toolchain//actions:all_link_actions"],
35    flags = [
36        # Force dropping the system libc++.
37        "-nostdlib++",
38        # Use libc++ provided by the toolchain.
39        "external/llvm_toolchain/lib/libc++.a",
40    ],
41    target_compatible_with = ["@platforms//os:macos"],
42)
43
44pw_cc_flag_set(
45    name = "linux_link_libs",
46    actions = ["@pw_toolchain//actions:all_link_actions"],
47    flags = [
48        "-pthread",
49        "-stdlib=libc++",
50        "--rtlib=compiler-rt",
51        "--unwindlib=libunwind",
52    ],
53    target_compatible_with = ["@platforms//os:linux"],
54)
55
56pw_cc_flag_set(
57    name = "libtool_darwin_flags",
58    actions = ["@pw_toolchain//actions:cpp_link_static_library"],
59    flags = ["-no_warning_for_no_symbols"],
60)
61
62# Although we use similar warnings for clang and arm_gcc, we don't have one
63# centralized list, since we might want to use different warnings based on the
64# compiler in the future.
65pw_cc_flag_set(
66    name = "warnings",
67    actions = [
68        "@pw_toolchain//actions:all_c_compiler_actions",
69        "@pw_toolchain//actions:all_cpp_compiler_actions",
70    ],
71    flags = [
72        "-Wall",
73        "-Wextra",
74        # Make all warnings errors, except for the exemptions below.
75        "-Werror",
76        "-Wno-error=cpp",  # preprocessor #warning statement
77        "-Wno-error=deprecated-declarations",  # [[deprecated]] attribute
78    ],
79)
80
81# Thread safety warnings are only supported by Clang.
82pw_cc_flag_set(
83    name = "thread_safety_warnings",
84    actions = [
85        "@pw_toolchain//actions:all_c_compiler_actions",
86        "@pw_toolchain//actions:all_cpp_compiler_actions",
87    ],
88    flags = [
89        "-Wthread-safety",
90        "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS=1",
91    ],
92)
93
94pw_cc_flag_set(
95    name = "verbose_compiler_flags",
96    actions = [
97        "@pw_toolchain//actions:all_asm_actions",
98        "@pw_toolchain//actions:all_c_compiler_actions",
99        "@pw_toolchain//actions:all_cpp_compiler_actions",
100        "@pw_toolchain//actions:all_link_actions",
101    ],
102    flags = ["-v"],
103)
104
105# A feature that can be easily toggled to include extra compiler output to help
106# debug things like include search path ordering and showing all the flags
107# passed to the compiler.
108#
109# Add `--features=verbose_compiler_output` to your Bazel invocation to enable.
110pw_cc_feature(
111    name = "verbose_compiler_output",
112    enabled = False,
113    feature_name = "verbose_compiler_output",
114    flag_sets = [":verbose_compiler_flags"],
115)
116
117pw_cc_flag_set(
118    name = "no_unknown_warning_option",
119    actions = [
120        "@pw_toolchain//actions:all_c_compiler_actions",
121        "@pw_toolchain//actions:all_cpp_compiler_actions",
122    ],
123    flags = [
124        "-Wno-unknown-warning-option",
125    ],
126)
127
128pw_cc_action_files(
129    name = "sysroot_files",
130    srcs = select({
131        "@platforms//os:linux": [
132            "@linux_sysroot//:all",
133        ],
134        "//conditions:default": [],
135    }),
136    actions = [
137        "@pw_toolchain//actions:all_compiler_actions",
138        "@pw_toolchain//actions:all_link_actions",
139    ],
140)
141
142bool_flag(
143    name = "asan",
144    build_setting_default = False,
145)
146
147config_setting(
148    name = "asan_enabled",
149    flag_values = {
150        ":asan": "true",
151    },
152)
153
154pw_cc_feature(
155    name = "asan_feature",
156    enabled = select({
157        ":asan_enabled": True,
158        "//conditions:default": False,
159    }),
160    feature_name = "asan",
161    flag_sets = ["@pw_toolchain//flag_sets:asan"],
162    requires_any_of = [":asan_constraint"],
163)
164
165pw_cc_feature_set(
166    name = "asan_constraint",
167    # Rust uses the C++ linker, but not the C++ compiler, so we need to ensure
168    # -fsanitize=address is not be specified during Rust linking.
169    all_of = [":rules_rust_unsupported_feature"],
170)
171
172# This is a sentinel feature defined by rules_rust. It is by definition
173# unsupported: rules_rust will disable this feature when linking Rust code.
174pw_cc_feature(
175    name = "rules_rust_unsupported_feature",
176    enabled = True,
177    feature_name = "rules_rust_unsupported_feature",
178)
179
180pw_cc_toolchain(
181    name = "host_toolchain",
182    action_configs = [
183        "@llvm_toolchain//:ar",
184        "@llvm_toolchain//:clang",
185        "@llvm_toolchain//:clang++",
186        "@llvm_toolchain//:lld",
187        "@llvm_toolchain//:llvm-cov",
188        "@llvm_toolchain//:llvm-objcopy",
189        "@llvm_toolchain//:llvm-objdump",
190        "@llvm_toolchain//:llvm-strip",
191    ],
192    builtin_sysroot = select({
193        "@platforms//os:linux": "external/linux_sysroot",
194        "@platforms//os:macos": XCODE_SDK_PATH,
195        "//conditions:default": "",
196    }),
197    compiler = "clang",
198    cxx_builtin_include_directories = select({
199        "@platforms//os:linux": [
200            "%package(@llvm_toolchain//)%/include/x86_64-unknown-linux-gnu/c++/v1",
201            "%package(@llvm_toolchain//)%/include/c++/v1",
202            "%package(@llvm_toolchain//)%/lib/clang/18/include",
203            "%sysroot%/usr/local/include",
204            "%sysroot%/usr/include/x86_64-linux-gnu",
205            "%sysroot%/usr/include",
206        ],
207        "@platforms//os:macos": [
208            "%sysroot%/usr/include",
209            "%sysroot%/System/Library/Frameworks/CoreFoundation.framework/Headers",
210            "%sysroot%/System/Library/Frameworks/IOKit.framework/Headers",
211            "%sysroot%/System/Library/Frameworks/Security.framework/Headers",
212            "%package(@llvm_toolchain//)%/include/c++/v1",
213            "%package(@llvm_toolchain//)%/lib/clang/18/include",
214        ],
215        "//conditions:default": [],
216    }),
217    extra_action_files = [":sysroot_files"],
218    flag_sets = select({
219        "@platforms//os:linux": [
220            ":linux_link_libs",
221        ],
222        "@platforms//os:macos": [
223            ":macos_link_libs",
224            ":libtool_darwin_flags",
225        ],
226        "//conditions:default": [],
227    }) + [
228        ":warnings",
229        ":thread_safety_warnings",
230        "@pw_toolchain//flag_sets:c++17",
231        "@pw_toolchain//flag_sets:debugging",
232        "@pw_toolchain//flag_sets:reduced_size",
233        "@pw_toolchain//flag_sets:no_canonical_prefixes",
234        "@pw_toolchain//flag_sets:no_rtti",
235        "@pw_toolchain//flag_sets:wno_register",
236        "@pw_toolchain//flag_sets:wnon_virtual_dtor",
237    ] + select({
238        "//pw_build:kythe": [":no_unknown_warning_option"],
239        "//conditions:default": [],
240    }),
241    # The implementations of some "legacy features" built into Bazel use
242    # `target_libc` to determine if a toolchain targets MacOS,
243    # https://github.com/bazelbuild/bazel/blob/release-7.0.0-pre.20230816.3rc1/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java#L1301-L1304
244    #
245    # `target_cpu` appears to only potentially do magic things for the Android
246    # NDK, so it's omitted here.
247    target_libc = select({
248        "@platforms//os:macos": "macosx",
249        "//conditions:default": None,
250    }),
251    toolchain_features = [
252        ":asan_feature",
253        ":verbose_compiler_output",
254        ":rules_rust_unsupported_feature",
255    ],
256    toolchain_identifier = "host-toolchain",
257)
258
259toolchain(
260    name = "host_cc_toolchain_linux",
261    exec_compatible_with = [
262        "@platforms//os:linux",
263    ],
264    target_compatible_with = [
265        "@platforms//os:linux",
266    ],
267    toolchain = ":host_toolchain",
268    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
269)
270
271toolchain(
272    name = "host_cc_toolchain_macos",
273    exec_compatible_with = [
274        "@platforms//os:macos",
275    ],
276    target_compatible_with = [
277        "@platforms//os:macos",
278    ],
279    toolchain = ":host_toolchain",
280    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
281)
282