blob: 4819ee151aba519591f07d584c9d7387ca792c4a [file] [log] [blame]
mtklein7fbfbbe2016-07-21 12:25:45 -07001# Copyright 2016 Google Inc.
2#
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6declare_args() {
Kevin Lubick2677a9a2017-03-28 15:45:41 -04007 extra_asmflags = []
Mike Klein121563e2016-10-04 17:09:13 -04008 extra_cflags = []
9 extra_cflags_c = []
10 extra_cflags_cc = []
11 extra_ldflags = []
mtkleincab0bb72016-08-26 13:43:19 -070012
Herb Derby76073c12016-12-08 19:00:40 -050013 malloc = ""
mtklein7fbfbbe2016-07-21 12:25:45 -070014}
Mike Klein82364ba2016-10-24 16:49:15 -040015
Mike Klein7d302882016-11-03 14:06:31 -040016if (is_ios) {
Matthew Leibowitz3150ec62017-03-14 16:22:32 -040017 if (is_tvos) {
18 sdk = "appletvos"
19 if (target_cpu == "x86" || target_cpu == "x64") {
20 sdk = "appletvsimulator"
21 }
22 } else {
23 sdk = "iphoneos"
24 if (target_cpu == "x86" || target_cpu == "x64") {
25 sdk = "iphonesimulator"
26 }
Mike Klein7a5e0f32017-03-13 09:25:33 -070027 }
28 ios_sysroot = exec_script("find_ios_sysroot.py", [ sdk ], "trim string")
Mike Klein7d302882016-11-03 14:06:31 -040029}
30
mtkleinb9be9792016-09-16 14:44:18 -070031config("default") {
32 asmflags = []
33 cflags = []
34 cflags_c = []
35 cflags_cc = []
36 defines = []
37 ldflags = []
Herb Derby76073c12016-12-08 19:00:40 -050038 libs = []
mtkleinb9be9792016-09-16 14:44:18 -070039
40 if (is_win) {
41 cflags += [
42 "/FS", # Preserve previous PDB behavior.
herbb6318bf2016-09-16 13:29:57 -070043 "/bigobj", # Some of our files are bigger than the regular limits.
Mike Kleinc7165c22016-10-12 23:58:06 -040044 "/WX", # Treat warnings as errors.
Ben Wagnere6b274e2017-01-03 17:09:59 -050045 "/utf-8", # Set Source and Executable character sets to UTF-8.
herbb6318bf2016-09-16 13:29:57 -070046 ]
Mike Kleina91ec582017-07-31 16:35:17 -040047 if (is_clang) {
48 cflags += [ "-fms-compatibility-version=19" ] # 2015
49 }
mtkleinb9be9792016-09-16 14:44:18 -070050 defines += [
Mike Kleinc7165c22016-10-12 23:58:06 -040051 "_CRT_SECURE_NO_WARNINGS", # Disables warnings about sscanf().
52 "_HAS_EXCEPTIONS=0", # Disables exceptions in MSVC STL.
mtkleinb9be9792016-09-16 14:44:18 -070053 "WIN32_LEAN_AND_MEAN",
54 "NOMINMAX",
herbb6318bf2016-09-16 13:29:57 -070055 ]
Mike Klein98adfa82017-07-31 15:46:47 -040056
Brian Osmand34c4a32017-12-06 13:26:38 -050057 if (msvc == 2015) {
Brian Osman852ca312017-12-07 16:16:21 -050058 _include_dirs = [ "$win_vc/include" ]
Brian Osman5f872622017-12-06 15:21:44 -050059 } else { # 2017
Brian Osman852ca312017-12-07 16:16:21 -050060 _include_dirs = [ "$win_vc/Tools/MSVC/$win_toolchain_version/include" ]
Ravi Mistryf5ab2692017-12-06 21:51:23 +000061 }
Brian Osman5f872622017-12-06 15:21:44 -050062 _include_dirs += [
63 "$win_sdk/Include/$win_sdk_version/shared",
64 "$win_sdk/Include/$win_sdk_version/ucrt",
65 "$win_sdk/Include/$win_sdk_version/um",
66 "$win_sdk/Include/$win_sdk_version/winrt",
67 ]
68
Mike Klein98adfa82017-07-31 15:46:47 -040069 if (is_clang) {
70 foreach(dir, _include_dirs) {
71 cflags += [
72 "-imsvc",
73 dir,
74 ]
75 }
76 } else {
77 include_dirs = _include_dirs
78 }
79
Brian Osman5f872622017-12-06 15:21:44 -050080 lib_dirs = [
81 "$win_sdk/Lib/$win_sdk_version/ucrt/$target_cpu",
82 "$win_sdk/Lib/$win_sdk_version/um/$target_cpu",
83 ]
Brian Osmand34c4a32017-12-06 13:26:38 -050084 if (msvc == 2015) {
Brian Osmand34c4a32017-12-06 13:26:38 -050085 if (target_cpu == "x86") {
Brian Osman852ca312017-12-07 16:16:21 -050086 lib_dirs += [ "$win_vc/lib" ]
Brian Osmand34c4a32017-12-06 13:26:38 -050087 } else {
Brian Osman852ca312017-12-07 16:16:21 -050088 lib_dirs += [ "$win_vc/lib/amd64" ]
Brian Osmand34c4a32017-12-06 13:26:38 -050089 }
Brian Osman5f872622017-12-06 15:21:44 -050090 } else { # 2017
91 lib_dirs +=
Brian Osman852ca312017-12-07 16:16:21 -050092 [ "$win_vc/Tools/MSVC/$win_toolchain_version/lib/$target_cpu" ]
Mike Klein0bc5a762016-10-12 22:42:55 -040093 }
mtkleinb9be9792016-09-16 14:44:18 -070094 } else {
95 cflags += [
herbb6318bf2016-09-16 13:29:57 -070096 "-fstrict-aliasing",
97 "-fPIC",
herbb6318bf2016-09-16 13:29:57 -070098 "-Werror",
mtklein2b3c2a32016-09-08 08:39:34 -070099 ]
Mike Klein28f5b772017-04-06 23:04:42 -0400100 cflags_cc += [ "-std=c++11" ]
Mike Kleineec23d12017-03-26 20:11:48 -0400101
102 # The main idea is to slim the exported API, but these flags also improve link time on Mac.
103 # These would make stack traces worse on Linux, so we don't just set them willy-nilly.
Mike Klein8cb66482017-03-28 14:16:22 -0400104 if (is_component_build || is_ios || is_mac) {
Mike Kleineec23d12017-03-26 20:11:48 -0400105 cflags += [ "-fvisibility=hidden" ]
106 cflags_cc += [ "-fvisibility-inlines-hidden" ]
107 }
mtkleinb9be9792016-09-16 14:44:18 -0700108 }
herbb6318bf2016-09-16 13:29:57 -0700109
mtkleinb9be9792016-09-16 14:44:18 -0700110 if (current_cpu == "arm") {
111 cflags += [
112 "-march=armv7-a",
113 "-mfpu=neon",
114 "-mthumb",
115 ]
Mike Klein5d8cf292016-10-12 19:36:09 -0400116 } else if (current_cpu == "x86" && !is_win) {
mtkleinb9be9792016-09-16 14:44:18 -0700117 asmflags += [ "-m32" ]
118 cflags += [
119 "-m32",
120 "-msse2",
121 "-mfpmath=sse",
122 ]
123 ldflags += [ "-m32" ]
124 }
herbb6318bf2016-09-16 13:29:57 -0700125
Herb Derby76073c12016-12-08 19:00:40 -0500126 if (malloc != "" && !is_win) {
127 cflags += [
128 "-fno-builtin-malloc",
129 "-fno-builtin-calloc",
130 "-fno-builtin-realloc",
131 "-fno-builtin-free",
132 ]
133 libs += [ malloc ]
134 }
135
mtkleinb9be9792016-09-16 14:44:18 -0700136 if (is_android) {
Derek Sollenberger19b2a562017-06-27 11:55:05 -0400137 asmflags += [ "--target=$ndk_target" ]
mtkleinb9be9792016-09-16 14:44:18 -0700138 cflags += [
Derek Sollenberger19b2a562017-06-27 11:55:05 -0400139 "--sysroot=$ndk/sysroot",
140 "-isystem$ndk/sysroot/usr/include/$ndk_target",
141 "-D__ANDROID_API__=$ndk_api",
mtkleinb9be9792016-09-16 14:44:18 -0700142 "--target=$ndk_target",
mtkleinb9be9792016-09-16 14:44:18 -0700143 ]
144 cflags_cc += [
145 "-isystem$ndk/sources/android/support/include",
Mike Kleinb7ce80b2016-12-14 13:17:53 -0500146 "-isystem$ndk/sources/cxx-stl/gnu-libstdc++/4.9/include",
147 "-isystem$ndk/sources/cxx-stl/gnu-libstdc++/4.9/libs/$ndk_stdlib/include",
mtkleinb9be9792016-09-16 14:44:18 -0700148 ]
149 ldflags += [
150 "--sysroot=$ndk/platforms/$ndk_platform",
151 "--target=$ndk_target",
152 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
mtkleinb9be9792016-09-16 14:44:18 -0700153 ]
154 lib_dirs = [
Mike Kleinb7ce80b2016-12-14 13:17:53 -0500155 "$ndk/sources/cxx-stl/gnu-libstdc++/4.9/libs/$ndk_stdlib",
mtkleinb9be9792016-09-16 14:44:18 -0700156 "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/lib/gcc/$ndk_target/4.9.x",
157 ]
herbb6318bf2016-09-16 13:29:57 -0700158
Mike Kleinb7ce80b2016-12-14 13:17:53 -0500159 libs += [ "gnustl_static" ]
mtklein2b3c2a32016-09-08 08:39:34 -0700160 }
mtklein7fbfbbe2016-07-21 12:25:45 -0700161
Mike Klein7d302882016-11-03 14:06:31 -0400162 if (is_ios) {
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500163 _target = target_cpu
164 if (target_cpu == "arm") {
165 _target = "armv7"
Mike Klein7a5e0f32017-03-13 09:25:33 -0700166 } else if (target_cpu == "x86") {
167 _target = "i386"
168 } else if (target_cpu == "x64") {
169 _target = "x86_64"
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500170 }
Mike Klein4fe2b152017-02-17 15:36:01 -0500171 asmflags += [
172 "-isysroot",
173 ios_sysroot,
174 "-arch",
175 _target,
176 ]
Mike Klein7d302882016-11-03 14:06:31 -0400177 cflags += [
Mike Klein6749af42016-11-07 15:38:48 -0500178 "-isysroot",
179 ios_sysroot,
180 "-arch",
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500181 _target,
Mike Klein7d302882016-11-03 14:06:31 -0400182 ]
183 cflags_cc += [ "-stdlib=libc++" ]
184 ldflags += [
Mike Klein6749af42016-11-07 15:38:48 -0500185 "-isysroot",
186 ios_sysroot,
187 "-arch",
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500188 _target,
Mike Klein7d302882016-11-03 14:06:31 -0400189 "-stdlib=libc++",
190 ]
Herb Derby76073c12016-12-08 19:00:40 -0500191 libs += [ "objc" ]
Mike Klein7d302882016-11-03 14:06:31 -0400192 }
193
mtkleinb9be9792016-09-16 14:44:18 -0700194 if (is_linux) {
Herb Derby76073c12016-12-08 19:00:40 -0500195 libs += [ "pthread" ]
mtkleinb9be9792016-09-16 14:44:18 -0700196 }
Mike Klein15f46c32017-12-07 12:48:57 -0500197 if (is_mac) {
198 # Disable linker warnings. They're usually just annoyances like,
199 # ld: warning: text-based stub file
200 # /System/Library/Frameworks/foo.framework/foo.tbd and library file
201 # /System/Library/Frameworks/foo.framework/foo are out of sync.
202 # Falling back to library file for linking.
203 ldflags += [ "-Wl,-w" ]
204 }
mtkleinb9be9792016-09-16 14:44:18 -0700205
206 if (sanitize != "") {
207 # You can either pass the sanitizers directly, e.g. "address,undefined",
208 # or pass one of the couple common aliases used by the bots.
209 sanitizers = sanitize
210 if (sanitize == "ASAN") {
Ben Wagnerbee6cac2017-12-04 11:15:58 -0500211 sanitizers = "address,bool,float-cast-overflow,integer-divide-by-zero,nonnull-attribute,null,return,returns-nonnull-attribute,shift,signed-integer-overflow,unreachable,vla-bound,vptr"
Mike Kleine4781e92017-09-28 11:42:28 -0400212 if (!is_mac) {
Mike Klein46d6c682017-10-05 11:22:09 -0400213 sanitizers += ",function" # Not supported on Mac.
214 }
215 if (!is_debug) {
216 sanitizers += ",object-size" # No-op with somewhat annoying warning at -O0.
Mike Kleine4781e92017-09-28 11:42:28 -0400217 }
mtkleinb9be9792016-09-16 14:44:18 -0700218 } else if (sanitize == "TSAN") {
219 sanitizers = "thread"
220 } else if (sanitize == "MSAN") {
221 sanitizers = "memory"
222 }
223
224 cflags += [
225 "-fsanitize=$sanitizers",
226 "-fno-sanitize-recover=$sanitizers",
227 "-fsanitize-blacklist=" + rebase_path("../tools/xsan.blacklist"),
228 ]
229 ldflags += [ "-fsanitize=$sanitizers" ]
230 if (sanitizers == "memory") {
231 cflags += [ "-fsanitize-memory-track-origins" ]
232 cflags_cc += [ "-stdlib=libc++" ]
233 ldflags += [ "-stdlib=libc++" ]
234 }
235 }
236}
237
Mike Klein6e55fef2016-10-26 11:41:47 -0400238config("no_exceptions") {
Mike Klein228276d2017-08-16 13:20:20 +0000239 # Exceptions are disabled by default on Windows. (Use /EHsc to enable them.)
Mike Klein6e55fef2016-10-26 11:41:47 -0400240 if (!is_win) {
241 cflags_cc = [ "-fno-exceptions" ]
242 }
243}
244
Mike Kleinc7165c22016-10-12 23:58:06 -0400245config("warnings") {
246 cflags = []
247 cflags_cc = []
Mike Klein43c25262016-10-20 10:17:47 -0400248 cflags_objc = []
Greg Daniel6b7e0e22017-07-12 16:21:09 -0400249 cflags_objcc = []
Mike Kleinc7165c22016-10-12 23:58:06 -0400250 if (is_win) {
251 cflags += [
252 "/W3", # Turn on lots of warnings.
253
254 # Disable a bunch of warnings:
255 "/wd4244", # conversion from 'float' to 'int', possible loss of data
256 "/wd4267", # conversion from 'size_t' to 'int', possible loss of data
257 "/wd4800", # forcing value to bool 'true' or 'false' (performance warning)
258
259 # Probably only triggers when /EHsc is enabled.
260 "/wd4291", # no matching operator delete found;
261 # memory will not be freed if initialization throws an exception
262 ]
263 } else {
264 cflags += [
265 "-Wall",
266 "-Wextra",
267 "-Winit-self",
268 "-Wpointer-arith",
269 "-Wsign-compare",
270 "-Wvla",
271
272 "-Wno-deprecated-declarations",
Mike Kleindb402ca2016-12-13 12:46:05 -0500273 "-Wno-maybe-uninitialized",
Mike Kleinc7165c22016-10-12 23:58:06 -0400274 ]
275 cflags_cc += [ "-Wnon-virtual-dtor" ]
Chris Dalton1ef80942017-12-04 12:01:30 -0700276 }
Mike Kleinc7165c22016-10-12 23:58:06 -0400277
Chris Dalton1ef80942017-12-04 12:01:30 -0700278 if (is_clang) {
279 cflags += [
280 "-Weverything",
281 "-Wno-unknown-warning-option", # Let older Clangs ignore newer Clangs' warnings.
282 ]
Mike Kleinc7165c22016-10-12 23:58:06 -0400283
Chris Dalton1ef80942017-12-04 12:01:30 -0700284 if (target_cpu == "arm" && is_ios) {
285 # Clang seems to think new/malloc will only be 4-byte aligned on 32-bit iOS.
286 # We're pretty sure it's actually 8-byte alignment.
287 cflags += [ "-Wno-over-aligned" ]
Mike Kleinc7165c22016-10-12 23:58:06 -0400288 }
Chris Dalton1ef80942017-12-04 12:01:30 -0700289
290 # Shouldn't be necessary for local builds. With distributed Windows builds, files may lose
291 # their case during copy, causing case-sensitivity mismatch on remote machines.
292 cflags += [
293 "-Wno-nonportable-include-path",
294 "-Wno-nonportable-system-include-path",
295 ]
296
297 # TODO: These would all be really great warnings to turn on.
298 cflags += [
299 "-Wno-cast-align",
300 "-Wno-cast-qual",
301 "-Wno-conditional-uninitialized",
302 "-Wno-conversion",
303 "-Wno-disabled-macro-expansion",
304 "-Wno-documentation",
305 "-Wno-documentation-unknown-command",
306 "-Wno-double-promotion",
307 "-Wno-exit-time-destructors", # TODO: OK outside libskia
308 "-Wno-float-conversion",
309 "-Wno-float-equal",
310 "-Wno-format-nonliteral",
311 "-Wno-global-constructors", # TODO: OK outside libskia
312 "-Wno-gnu-zero-variadic-macro-arguments",
313 "-Wno-missing-prototypes",
314 "-Wno-missing-variable-declarations",
315 "-Wno-pedantic",
316 "-Wno-reserved-id-macro",
317 "-Wno-shadow",
318 "-Wno-shift-sign-overflow",
319 "-Wno-sign-conversion",
320 "-Wno-signed-enum-bitfield",
321 "-Wno-switch-enum",
322 "-Wno-undef",
323 "-Wno-unreachable-code",
324 "-Wno-unreachable-code-break",
325 "-Wno-unreachable-code-return",
326 "-Wno-unused-macros",
327 "-Wno-unused-member-function",
328 "-Wno-unused-template",
329 "-Wno-zero-as-null-pointer-constant",
330 ]
331 cflags_cc += [
332 "-Wno-abstract-vbase-init",
333 "-Wno-weak-vtables",
334 ]
335
336 # We are unlikely to want to fix these.
337 cflags += [
338 "-Wno-covered-switch-default",
339 "-Wno-deprecated",
340 "-Wno-implicit-fallthrough",
341 "-Wno-missing-noreturn",
342 "-Wno-old-style-cast",
343 "-Wno-padded",
Chris Daltond1ec4102017-12-04 13:31:21 -0700344 "-Wno-newline-eof",
Chris Dalton1ef80942017-12-04 12:01:30 -0700345 ]
346 cflags_cc += [
347 "-Wno-c++98-compat",
348 "-Wno-c++98-compat-pedantic",
349 "-Wno-undefined-func-template",
350 ]
351 cflags_objc += [
352 "-Wno-direct-ivar-access",
353 "-Wno-objc-interface-ivars",
354 ]
355 cflags_objcc += [
356 "-Wno-direct-ivar-access",
357 "-Wno-objcc-interface-ivars",
358 ]
Mike Kleinc7165c22016-10-12 23:58:06 -0400359 }
360}
Mike Klein50500ad2016-11-16 12:13:44 -0500361config("warnings_except_public_headers") {
Chris Dalton1ef80942017-12-04 12:01:30 -0700362 if (!is_win || is_clang) {
Mike Klein50500ad2016-11-16 12:13:44 -0500363 cflags = [ "-Wno-unused-parameter" ]
364 }
365}
Mike Kleinc7165c22016-10-12 23:58:06 -0400366
Mike Klein121563e2016-10-04 17:09:13 -0400367config("extra_flags") {
Kevin Lubick2677a9a2017-03-28 15:45:41 -0400368 asmflags = extra_asmflags
Mike Klein121563e2016-10-04 17:09:13 -0400369 cflags = extra_cflags
370 cflags_c = extra_cflags_c
371 cflags_cc = extra_cflags_cc
372 ldflags = extra_ldflags
373}
374
mtkleinb9be9792016-09-16 14:44:18 -0700375config("debug_symbols") {
376 # It's annoying to wait for full debug symbols to push over
377 # to Android devices. -gline-tables-only is a lot slimmer.
378 if (is_android) {
Mike Klein1b4602b2017-08-30 10:23:01 -0400379 cflags = [
380 "-gline-tables-only",
381 "-funwind-tables", # Helps make in-process backtraces fuller.
382 ]
Mike Kleincc300a12016-10-12 16:25:27 -0400383 } else if (is_win) {
384 cflags = [ "/Zi" ]
Mike Klein5286d6c2016-10-13 13:19:25 -0400385 ldflags = [ "/DEBUG" ]
Mike Kleincc300a12016-10-12 16:25:27 -0400386 } else {
mtkleinb9be9792016-09-16 14:44:18 -0700387 cflags = [ "-g" ]
388 }
389}
390
391config("no_rtti") {
392 if (sanitize != "ASAN") { # -fsanitize=vptr requires RTTI
Mike Kleincc300a12016-10-12 16:25:27 -0400393 if (is_win) {
394 cflags_cc = [ "/GR-" ]
395 } else {
mtkleinb9be9792016-09-16 14:44:18 -0700396 cflags_cc = [ "-fno-rtti" ]
397 }
398 }
399}
400
401config("release") {
Mike Kleincc300a12016-10-12 16:25:27 -0400402 if (is_win) {
Mike Klein916ca1d2016-10-20 13:34:18 -0400403 cflags = [
404 "/O2",
405 "/Zc:inline",
Mike Klein8ffb2602016-10-20 15:45:02 -0400406 "/GS-",
Mike Klein916ca1d2016-10-20 13:34:18 -0400407 ]
Mike Klein90a381f2016-10-20 13:52:38 -0400408 ldflags = [
409 "/OPT:ICF",
410 "/OPT:REF",
411 ]
Mike Kleincc300a12016-10-12 16:25:27 -0400412 } else {
Mike Klein0bcfeac2016-10-19 22:24:10 -0400413 cflags = [
414 "-O3",
Mike Kleinbd9be4d2017-02-10 07:59:39 -0500415 "-fdata-sections",
416 "-ffunction-sections",
Mike Klein0bcfeac2016-10-19 22:24:10 -0400417 ]
Mike Kleinbd9be4d2017-02-10 07:59:39 -0500418 if (is_mac || is_ios) {
419 ldflags = [ "-dead_strip" ]
420 } else {
421 ldflags = [ "-Wl,--gc-sections" ]
422 }
Kevin Lubickebf648e2017-09-21 13:45:16 -0400423 if (target_cpu == "wasm") {
424 # The compiler asks us to add an optimization flag to both cflags
425 # and ldflags to cut down on the local variables,
426 # for performance reasons.
427 # The "linking" step is the conversion to javascript.
428 ldflags += [ "-O3" ]
429 }
herbb6318bf2016-09-16 13:29:57 -0700430 }
mtkleinb9be9792016-09-16 14:44:18 -0700431 defines = [ "NDEBUG" ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700432}
433
434config("executable") {
Mike Kleinc5875fb2016-12-06 10:46:02 -0500435 if (is_android) {
Mike Klein1b4602b2017-08-30 10:23:01 -0400436 ldflags = [
437 "-pie",
438 "-rdynamic",
439 ]
Mike Kleinc5875fb2016-12-06 10:46:02 -0500440 } else if (is_mac) {
mtklein7fbfbbe2016-07-21 12:25:45 -0700441 ldflags = [ "-Wl,-rpath,@loader_path/." ]
442 } else if (is_linux) {
mtkleina846c722016-09-15 10:44:15 -0700443 ldflags = [
444 "-rdynamic",
445 "-Wl,-rpath,\$ORIGIN",
446 ]
Mike Klein4b6b5032016-11-06 11:54:19 -0500447 } else if (is_win) {
448 ldflags = [
449 "/SUBSYSTEM:CONSOLE", # Quiet "no subsystem specified; CONSOLE assumed".
450 "/INCREMENTAL:NO", # Quiet warnings about failing to incrementally link by never trying to.
451 ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700452 }
453}