• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 cannot define them alongside the defines in //bazel/BUILD.bazel.
8
9We cannot (easily) define them in the C++ toolchain configuration
10(e.g. //toolchain/linux_amd64_toolchain_config.bzl), because that does not support listening
11to arbitrary Bazel flags (e.g. those defined in //bazel/common_config_settings). If we wanted
12to implement these flags in the toolchain, we would need to group them into features [1],
13but we don't control the features implemented by the G3 toolchain. Because we want to
14automatically roll into G3 with minimal changes, the copts cannot go in the toolchain,
15
16Thus, they go here, so we can use select statements to conditionally control them and
17override what they do (if necessary) in G3.
18
19They are divided into several lists/selects and were initially created to be identical to the
20 GN ones [2][3].
21
22The flags here are *not* used when compiling our third_party libraries (although the flags will
23impact the included public headers of those third_party libraries). If we need a flag to impact
24both Skia and a third party dep, it should probably go in the toolchain_config. If that is not
25possible (e.g. the setting depends on a custom flag), we can define a subworkspace and have both
26Skia and the third party dep depend on that.
27
28[1] https://bazel.build/docs/cc-toolchain-config-reference#features
29[2] https://github.com/google/skia/blob/2b07cdb07e88f2870260eabac708f31bc7977d08/gn/BUILDCONFIG.gn#L177-L181
30[3] https://github.com/google/skia/blob/2b07cdb07e88f2870260eabac708f31bc7977d08/gn/skia/BUILD.gn#L593-L630
31"""
32
33CORE_COPTS = [
34    "-fstrict-aliasing",
35    "-fPIC",
36] + select({
37    # SkRawCodec catches any exceptions thrown by dng_sdk, insulating the rest of Skia.
38    "//src/codec:raw_decode_codec": [],
39    "//conditions:default": ["-fno-exceptions"],
40}) + select({
41    "@platforms//os:android": [],
42    "//conditions:default": [
43        # On Android, this option causes the linker to fail
44        # (e.g. "undefined reference to `SkString::data()'").
45        "-fvisibility=hidden",
46    ],
47}) + select({
48    "@platforms//os:windows": [],
49    "//conditions:default": [
50        # In Clang 14, this default was changed. We turn this off to (hopefully) make our
51        # GMs more consistent and avoid some floating-point related test failures on M1 macs.
52        "-ffp-contract=off",
53    ],
54}) + select({
55    # Turning off RTTI reduces code size, but is necessary for connecting C++
56    # and Objective-C code.
57    "@platforms//os:macos": [],
58    "@platforms//os:ios": [],
59    "//conditions:default": [
60        "-fno-rtti",
61    ],
62})
63
64OPT_LEVEL = select({
65    "//bazel/common_config_settings:debug_build": [
66        "--optimize=0",
67        "--debug",
68    ],
69    "//bazel/common_config_settings:release_build": [
70        "--optimize=3",
71        # Strip dead code (in conjunction with linkopts)
72        "-fdata-sections",
73        "-ffunction-sections",
74    ],
75    "//bazel/common_config_settings:fast_build": [
76        "--optimize=0",
77        "-gline-tables-only",
78    ],
79})
80
81WARNINGS = [
82    "-fcolor-diagnostics",
83    "-Wall",
84    "-Werror",
85    "-Weverything",
86    "-Wextra",
87    "-Wpointer-arith",
88    "-Wsign-compare",
89    "-Wvla",
90    #### Warnings we are unlikely to fix ####
91    "-Wno-c++98-compat",
92    "-Wno-c++98-compat-pedantic",
93    "-Wno-covered-switch-default",
94    "-Wno-declaration-after-statement",
95    "-Wno-deprecated",
96    "-Wno-missing-noreturn",
97    "-Wno-newline-eof",
98    "-Wno-old-style-cast",
99    "-Wno-padded",
100    "-Wno-psabi",  # noisy
101    "-Wno-return-std-move-in-c++11",
102    "-Wno-shadow-field-in-constructor",
103    "-Wno-shadow-uncaptured-local",
104    "-Wno-undefined-func-template",
105    "-Wno-unused-parameter",  # It is common to have unused parameters in src/
106    "-Wno-zero-as-null-pointer-constant",  # VK_NULL_HANDLE is defined as 0
107    "-Wno-unsafe-buffer-usage",
108    #### Warnings we would like to fix ####
109    "-Wno-abstract-vbase-init",
110    "-Wno-cast-align",
111    "-Wno-cast-function-type-strict",
112    "-Wno-cast-qual",
113    "-Wno-class-varargs",
114    "-Wno-conversion",  # -Wsign-conversion re-enabled for header sources
115    "-Wno-disabled-macro-expansion",
116    "-Wno-documentation",
117    "-Wno-documentation-unknown-command",
118    "-Wno-double-promotion",
119    "-Wno-exit-time-destructors",  # TODO: OK outside libskia
120    "-Wno-float-equal",
121    "-Wno-global-constructors",  # TODO: OK outside libskia
122    "-Wno-missing-prototypes",
123    "-Wno-missing-variable-declarations",
124    "-Wno-pedantic",
125    "-Wno-reserved-id-macro",
126    "-Wno-reserved-identifier",
127    "-Wno-shift-sign-overflow",
128    "-Wno-signed-enum-bitfield",
129    "-Wno-switch-enum",
130    "-Wno-thread-safety-negative",
131    "-Wno-undef",
132    "-Wno-unreachable-code-break",
133    "-Wno-unreachable-code-return",
134    "-Wno-unused-macros",
135    "-Wno-unused-member-function",
136    "-Wno-weak-template-vtables",  # This was deprecated in Clang 14 and removed in Clang 15.
137    "-Wno-weak-vtables",
138    # https://quuxplusone.github.io/blog/2020/08/26/wrange-loop-analysis/
139    # https://bugzilla.mozilla.org/show_bug.cgi?id=1683213
140    # https://reviews.llvm.org/D73007
141    # May be re-enabled once clang > 12 or XCode > 12 are required.
142    # When this line is removed the -Wrange-loop-construct line below can also be removed.
143    "-Wno-range-loop-analysis",
144    # Wno-range-loop-analysis turns off the whole group, but this warning was later split into
145    # range-loop-construct and range-loop-bind-reference. We want the former but not the latter.
146    # Created from
147    # https://github.com/llvm/llvm-project/blob/bd08f413c089da5a56438cc8902f60df91a08a66/clang/include/clang/Basic/DiagnosticGroups.td
148    "-Wrange-loop-construct",
149    # Wno-deprecated turns off the whole group, but also has its own warnings like
150    # out-of-line definition of constexpr static data member is redundant in C++17 and is deprecated [-Werror,-Wdeprecated]
151    # but we would like others. Created from
152    # https://github.com/llvm/llvm-project/blob/bd08f413c089da5a56438cc8902f60df91a08a66/clang/include/clang/Basic/DiagnosticGroups.td
153    "-Wdeprecated-anon-enum-enum-conversion",
154    "-Wdeprecated-array-compare",
155    "-Wdeprecated-attributes",
156    "-Wdeprecated-comma-subscript",
157    "-Wdeprecated-copy",
158    "-Wdeprecated-copy-dtor",
159    "-Wdeprecated-dynamic-exception-spec",
160    "-Wdeprecated-enum-compare",
161    "-Wdeprecated-enum-compare-conditional",
162    "-Wdeprecated-enum-enum-conversion",
163    "-Wdeprecated-enum-float-conversion",
164    "-Wdeprecated-increment-bool",
165    "-Wdeprecated-register",
166    "-Wdeprecated-this-capture",
167    "-Wdeprecated-volatile",
168    "-Wdeprecated-writable-strings",
169    # A catch-all for when the version of clang we are using does not have the prior options
170    "-Wno-unknown-warning-option",
171] + select({
172    "@platforms//os:windows": [
173        # skbug.com/14203
174        "-Wno-nonportable-system-include-path",
175        "-Wno-unknown-argument",
176    ],
177    "//conditions:default": [],
178})
179
180DEFAULT_COPTS = CORE_COPTS + OPT_LEVEL + WARNINGS
181
182OBJC_COPTS = [
183    "-Wno-direct-ivar-access",
184    "-Wno-objc-interface-ivars",
185]
186
187DEFAULT_OBJC_COPTS = DEFAULT_COPTS + OBJC_COPTS
188