xref: /aosp_15_r20/external/skia/include/config/copts.bzl (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1"""
2THIS IS THE EXTERNAL-ONLY VERSION OF THIS FILE. G3 HAS ITS OWN.
3
4This file contains flags for the C++ compiler, referred to by Bazel as copts.
5
6The copts in a cc_library to not flow to the children (dependencies) nor the parents
7(dependents), so we use skia_cc_library to inject them in every library along the chain.
8
9Now that we have a modular build, this file could maybe go away and folded into our toolchains.
10
11They are divided into several lists/selects and were initially created to be identical to the
12 GN ones [2][3].
13
14[2] https://github.com/google/skia/blob/2b07cdb07e88f2870260eabac708f31bc7977d08/gn/BUILDCONFIG.gn#L177-L181
15[3] https://github.com/google/skia/blob/2b07cdb07e88f2870260eabac708f31bc7977d08/gn/skia/BUILD.gn#L593-L630
16"""
17
18CORE_COPTS = [
19    "-fstrict-aliasing",
20] + select({
21    "@platforms//os:android": [],
22    "//conditions:default": [
23        # On Android, this option causes the linker to fail
24        # (e.g. "undefined reference to `SkString::data()'").
25        "-fvisibility=hidden",
26    ],
27}) + select({
28    "@platforms//os:windows": [],
29    "//conditions:default": [
30        # In Clang 14, this default was changed. We turn this off to (hopefully) make our
31        # GMs more consistent and avoid some floating-point related test failures on M1 macs.
32        "-ffp-contract=off",
33        # Windows doesn't support position-independent code.
34        "-fPIC",
35    ],
36}) + select({
37    # Turning off RTTI reduces code size, but is necessary for connecting C++
38    # and Objective-C code.
39    "@platforms//os:macos": [],
40    "@platforms//os:ios": [],
41    "//conditions:default": [
42        "-fno-rtti",
43    ],
44})
45
46OPT_LEVEL = select({
47    "//bazel/common_config_settings:debug_build": [
48        "--optimize=0",
49        "--debug",
50    ],
51    "//bazel/common_config_settings:release_build": [
52        "--optimize=3",
53        # Strip dead code (in conjunction with linkopts)
54        "-fdata-sections",
55        "-ffunction-sections",
56    ],
57    "//bazel/common_config_settings:fast_build": [
58        "--optimize=0",
59        "-gline-tables-only",
60    ],
61})
62
63WARNINGS = [
64    "-fcolor-diagnostics",
65    "-Wall",
66    "-Werror",
67    "-Weverything",
68    "-Wextra",
69    "-Wpointer-arith",
70    "-Wsign-compare",
71    "-Wvla",
72    #### Warnings we are unlikely to fix ####
73    "-Wno-c++98-compat",
74    "-Wno-c++98-compat-pedantic",
75    "-Wno-covered-switch-default",
76    "-Wno-declaration-after-statement",
77    "-Wno-deprecated",
78    "-Wno-missing-noreturn",
79    "-Wno-newline-eof",
80    "-Wno-old-style-cast",
81    "-Wno-padded",
82    "-Wno-psabi",  # noisy
83    "-Wno-return-std-move-in-c++11",
84    "-Wno-shadow-field-in-constructor",
85    "-Wno-shadow-uncaptured-local",
86    "-Wno-undefined-func-template",
87    "-Wno-unused-parameter",  # It is common to have unused parameters in src/
88    "-Wno-zero-as-null-pointer-constant",  # VK_NULL_HANDLE is defined as 0
89    "-Wno-unsafe-buffer-usage",
90    #### Warnings we would like to fix ####
91    "-Wno-abstract-vbase-init",
92    "-Wno-cast-align",
93    "-Wno-cast-function-type-strict",
94    "-Wno-cast-qual",
95    "-Wno-class-varargs",
96    "-Wno-conversion",  # -Wsign-conversion re-enabled for header sources
97    "-Wno-disabled-macro-expansion",
98    "-Wno-documentation",
99    "-Wno-documentation-unknown-command",
100    "-Wno-double-promotion",
101    "-Wno-exit-time-destructors",  # TODO: OK outside libskia
102    "-Wno-float-equal",
103    "-Wno-global-constructors",  # TODO: OK outside libskia
104    "-Wno-missing-prototypes",
105    "-Wno-missing-variable-declarations",
106    "-Wno-pedantic",
107    "-Wno-reserved-id-macro",
108    "-Wno-reserved-identifier",
109    "-Wno-shift-sign-overflow",
110    "-Wno-signed-enum-bitfield",
111    "-Wno-switch-enum",
112    "-Wno-thread-safety-negative",
113    "-Wno-undef",
114    "-Wno-unreachable-code-break",
115    "-Wno-unreachable-code-return",
116    "-Wno-unused-macros",
117    "-Wno-unused-member-function",
118    "-Wno-weak-template-vtables",  # This was deprecated in Clang 14 and removed in Clang 15.
119    "-Wno-weak-vtables",
120    # https://quuxplusone.github.io/blog/2020/08/26/wrange-loop-analysis/
121    # https://bugzilla.mozilla.org/show_bug.cgi?id=1683213
122    # https://reviews.llvm.org/D73007
123    # May be re-enabled once clang > 12 or XCode > 12 are required.
124    # When this line is removed the -Wrange-loop-construct line below can also be removed.
125    "-Wno-range-loop-analysis",
126    # Wno-range-loop-analysis turns off the whole group, but this warning was later split into
127    # range-loop-construct and range-loop-bind-reference. We want the former but not the latter.
128    # Created from
129    # https://github.com/llvm/llvm-project/blob/bd08f413c089da5a56438cc8902f60df91a08a66/clang/include/clang/Basic/DiagnosticGroups.td
130    "-Wrange-loop-construct",
131    # Wno-deprecated turns off the whole group, but also has its own warnings like
132    # out-of-line definition of constexpr static data member is redundant in C++17 and is deprecated [-Werror,-Wdeprecated]
133    # but we would like others. Created from
134    # https://github.com/llvm/llvm-project/blob/bd08f413c089da5a56438cc8902f60df91a08a66/clang/include/clang/Basic/DiagnosticGroups.td
135    "-Wdeprecated-anon-enum-enum-conversion",
136    "-Wdeprecated-array-compare",
137    "-Wdeprecated-attributes",
138    "-Wdeprecated-comma-subscript",
139    "-Wdeprecated-copy",
140    "-Wdeprecated-copy-dtor",
141    "-Wdeprecated-dynamic-exception-spec",
142    "-Wdeprecated-enum-compare",
143    "-Wdeprecated-enum-compare-conditional",
144    "-Wdeprecated-enum-enum-conversion",
145    "-Wdeprecated-enum-float-conversion",
146    "-Wdeprecated-increment-bool",
147    "-Wdeprecated-register",
148    "-Wdeprecated-this-capture",
149    "-Wdeprecated-volatile",
150    "-Wdeprecated-writable-strings",
151    # A catch-all for when the version of clang we are using does not have the prior options
152    "-Wno-unknown-warning-option",
153] + select({
154    "@platforms//os:windows": [
155        # skbug.com/14203
156        "-Wno-nonportable-system-include-path",
157        "-Wno-unknown-argument",
158        # Clang warns even when all enum values are covered.
159        "-Wno-switch-default",
160        # The Windows build fails without these options:
161        "-Wno-extra-semi-stmt",
162        "-Wno-invalid-constexpr",
163        "-D_CRT_USE_BUILTIN_OFFSETOF",
164    ],
165    "//conditions:default": [],
166})
167
168DEFAULT_COPTS = CORE_COPTS + OPT_LEVEL + WARNINGS
169
170OBJC_COPTS = [
171    "-Wno-direct-ivar-access",
172    "-Wno-objc-interface-ivars",
173]
174
175DEFAULT_OBJC_COPTS = DEFAULT_COPTS + OBJC_COPTS
176