xref: /aosp_15_r20/external/pigweed/pw_toolchain/cc/BUILD.bazel (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2024 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", "string_flag")
16load(
17    "@pw_toolchain//cc_toolchain:defs.bzl",
18    "pw_cc_feature",
19    "pw_cc_flag_set",
20)
21
22package(default_visibility = ["//visibility:public"])
23
24# The common set of warnings for clang and arm_gcc.
25pw_cc_flag_set(
26    name = "common_warnings",
27    actions = [
28        "@pw_toolchain//actions:all_c_compiler_actions",
29        "@pw_toolchain//actions:all_cpp_compiler_actions",
30    ],
31    flags = [
32        "-Wall",
33        "-Wextra",
34        "-Wimplicit-fallthrough",
35        "-Wcast-qual",
36        "-Wpointer-arith",
37        "-Wshadow",
38        "-Wredundant-decls",
39        # TODO: b/259746255 - Enable implicit conversion warnings once fixed.
40        # "-Wconversion",
41        # Make all warnings errors, except for the exemptions below.
42        "-Werror",
43        "-Wno-error=cpp",  # preprocessor #warning statement
44        "-Wno-error=deprecated-declarations",  # [[deprecated]] attribute
45    ],
46)
47
48# Enables colors in compiler diagnostics. This uses the GCC spelling of the
49# flag, which Clang supports as an undocumented extension.
50pw_cc_flag_set(
51    name = "color_diagnostics",
52    actions = [
53        "@pw_toolchain//actions:all_asm_actions",
54        "@pw_toolchain//actions:all_c_compiler_actions",
55        "@pw_toolchain//actions:all_cpp_compiler_actions",
56        "@pw_toolchain//actions:all_link_actions",
57    ],
58    flags = ["-fdiagnostics-color"],
59)
60
61# This config contains warnings that are enabled for upstream Pigweed.
62# This config MUST NOT be used downstream to allow for warnings to be
63# added in the future without breaking downstream.
64pw_cc_flag_set(
65    name = "internal_strict_warnings_flags",
66    actions = [
67        "@pw_toolchain//actions:all_c_compiler_actions",
68        "@pw_toolchain//actions:all_cpp_compiler_actions",
69    ],
70    flags = [
71        "-Wswitch-enum",
72        "-Wextra-semi",
73        # TODO: b/361229275 - Add -Wshadow-all, but for Clang only, since GCC
74        # does not support it.
75        # "-Wshadow-all",
76    ] + select({
77        "@platforms//os:windows": [],
78        # TODO: b/243069432 - Enable pedantic warnings on Windows once passing.
79        "//conditions:default": [":pedantic_warnings"],
80    }),
81    visibility = ["//:__subpackages__"],
82)
83
84# Add `--features=internal_strict_warnings` to your Bazel invocation to enable.
85pw_cc_feature(
86    name = "internal_strict_warnings",
87    enabled = False,
88    feature_name = "internal_strict_warnings",
89    flag_sets = [":internal_strict_warnings_flags"],
90    visibility = ["//:__subpackages__"],
91)
92
93string_flag(
94    name = "cxx_standard",
95    build_setting_default = "20",
96    values = [
97        "17",
98        "20",
99    ],
100)
101
102config_setting(
103    name = "c++17_enabled",
104    flag_values = {
105        ":cxx_standard": "17",
106    },
107)
108
109pw_cc_feature(
110    name = "c++17_feature",
111    enabled = select({
112        ":c++17_enabled": True,
113        "//conditions:default": False,
114    }),
115    feature_name = "c++17",
116    flag_sets = ["@pw_toolchain//flag_sets:c++17"],
117)
118
119config_setting(
120    name = "c++20_enabled",
121    flag_values = {
122        ":cxx_standard": "20",
123    },
124)
125
126pw_cc_feature(
127    name = "c++20_feature",
128    enabled = select({
129        ":c++20_enabled": True,
130        "//conditions:default": False,
131    }),
132    feature_name = "c++20",
133    flag_sets = ["@pw_toolchain//flag_sets:c++20"],
134)
135