blob: c94d2372d85afe04edac6222bfc2e497844bb4e0 [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 += [
herbb6318bf2016-09-16 13:29:57 -070042 "/bigobj", # Some of our files are bigger than the regular limits.
Mike Kleinc7165c22016-10-12 23:58:06 -040043 "/WX", # Treat warnings as errors.
Ben Wagnere6b274e2017-01-03 17:09:59 -050044 "/utf-8", # Set Source and Executable character sets to UTF-8.
herbb6318bf2016-09-16 13:29:57 -070045 ]
Mike Kleina91ec582017-07-31 16:35:17 -040046 if (is_clang) {
47 cflags += [ "-fms-compatibility-version=19" ] # 2015
48 }
mtkleinb9be9792016-09-16 14:44:18 -070049 defines += [
Mike Kleinc7165c22016-10-12 23:58:06 -040050 "_CRT_SECURE_NO_WARNINGS", # Disables warnings about sscanf().
51 "_HAS_EXCEPTIONS=0", # Disables exceptions in MSVC STL.
mtkleinb9be9792016-09-16 14:44:18 -070052 "WIN32_LEAN_AND_MEAN",
53 "NOMINMAX",
herbb6318bf2016-09-16 13:29:57 -070054 ]
Mike Klein98adfa82017-07-31 15:46:47 -040055
Brian Osmand34c4a32017-12-06 13:26:38 -050056 if (msvc == 2015) {
Brian Osman852ca312017-12-07 16:16:21 -050057 _include_dirs = [ "$win_vc/include" ]
Brian Osman5f872622017-12-06 15:21:44 -050058 } else { # 2017
Brian Osman852ca312017-12-07 16:16:21 -050059 _include_dirs = [ "$win_vc/Tools/MSVC/$win_toolchain_version/include" ]
Ravi Mistryf5ab2692017-12-06 21:51:23 +000060 }
Brian Osman5f872622017-12-06 15:21:44 -050061 _include_dirs += [
62 "$win_sdk/Include/$win_sdk_version/shared",
63 "$win_sdk/Include/$win_sdk_version/ucrt",
64 "$win_sdk/Include/$win_sdk_version/um",
65 "$win_sdk/Include/$win_sdk_version/winrt",
66 ]
67
Mike Klein98adfa82017-07-31 15:46:47 -040068 if (is_clang) {
69 foreach(dir, _include_dirs) {
70 cflags += [
71 "-imsvc",
72 dir,
73 ]
74 }
75 } else {
76 include_dirs = _include_dirs
77 }
78
Brian Osman5f872622017-12-06 15:21:44 -050079 lib_dirs = [
80 "$win_sdk/Lib/$win_sdk_version/ucrt/$target_cpu",
81 "$win_sdk/Lib/$win_sdk_version/um/$target_cpu",
82 ]
Brian Osmand34c4a32017-12-06 13:26:38 -050083 if (msvc == 2015) {
Brian Osmand34c4a32017-12-06 13:26:38 -050084 if (target_cpu == "x86") {
Brian Osman852ca312017-12-07 16:16:21 -050085 lib_dirs += [ "$win_vc/lib" ]
Brian Osmand34c4a32017-12-06 13:26:38 -050086 } else {
Brian Osman852ca312017-12-07 16:16:21 -050087 lib_dirs += [ "$win_vc/lib/amd64" ]
Brian Osmand34c4a32017-12-06 13:26:38 -050088 }
Brian Osman5f872622017-12-06 15:21:44 -050089 } else { # 2017
90 lib_dirs +=
Brian Osman852ca312017-12-07 16:16:21 -050091 [ "$win_vc/Tools/MSVC/$win_toolchain_version/lib/$target_cpu" ]
Mike Klein0bc5a762016-10-12 22:42:55 -040092 }
mtkleinb9be9792016-09-16 14:44:18 -070093 } else {
94 cflags += [
herbb6318bf2016-09-16 13:29:57 -070095 "-fstrict-aliasing",
96 "-fPIC",
herbb6318bf2016-09-16 13:29:57 -070097 "-Werror",
mtklein2b3c2a32016-09-08 08:39:34 -070098 ]
Mike Klein28f5b772017-04-06 23:04:42 -040099 cflags_cc += [ "-std=c++11" ]
Mike Kleineec23d12017-03-26 20:11:48 -0400100
101 # The main idea is to slim the exported API, but these flags also improve link time on Mac.
102 # These would make stack traces worse on Linux, so we don't just set them willy-nilly.
Mike Klein8cb66482017-03-28 14:16:22 -0400103 if (is_component_build || is_ios || is_mac) {
Mike Kleineec23d12017-03-26 20:11:48 -0400104 cflags += [ "-fvisibility=hidden" ]
105 cflags_cc += [ "-fvisibility-inlines-hidden" ]
106 }
mtkleinb9be9792016-09-16 14:44:18 -0700107 }
herbb6318bf2016-09-16 13:29:57 -0700108
mtkleinb9be9792016-09-16 14:44:18 -0700109 if (current_cpu == "arm") {
110 cflags += [
111 "-march=armv7-a",
112 "-mfpu=neon",
113 "-mthumb",
114 ]
Mike Klein5d8cf292016-10-12 19:36:09 -0400115 } else if (current_cpu == "x86" && !is_win) {
mtkleinb9be9792016-09-16 14:44:18 -0700116 asmflags += [ "-m32" ]
117 cflags += [
118 "-m32",
119 "-msse2",
120 "-mfpmath=sse",
121 ]
122 ldflags += [ "-m32" ]
123 }
herbb6318bf2016-09-16 13:29:57 -0700124
Herb Derby76073c12016-12-08 19:00:40 -0500125 if (malloc != "" && !is_win) {
126 cflags += [
127 "-fno-builtin-malloc",
128 "-fno-builtin-calloc",
129 "-fno-builtin-realloc",
130 "-fno-builtin-free",
131 ]
132 libs += [ malloc ]
133 }
134
mtkleinb9be9792016-09-16 14:44:18 -0700135 if (is_android) {
Derek Sollenberger19b2a562017-06-27 11:55:05 -0400136 asmflags += [ "--target=$ndk_target" ]
mtkleinb9be9792016-09-16 14:44:18 -0700137 cflags += [
Derek Sollenberger19b2a562017-06-27 11:55:05 -0400138 "--sysroot=$ndk/sysroot",
139 "-isystem$ndk/sysroot/usr/include/$ndk_target",
140 "-D__ANDROID_API__=$ndk_api",
mtkleinb9be9792016-09-16 14:44:18 -0700141 "--target=$ndk_target",
mtkleinb9be9792016-09-16 14:44:18 -0700142 ]
143 cflags_cc += [
144 "-isystem$ndk/sources/android/support/include",
Mike Kleinb7ce80b2016-12-14 13:17:53 -0500145 "-isystem$ndk/sources/cxx-stl/gnu-libstdc++/4.9/include",
146 "-isystem$ndk/sources/cxx-stl/gnu-libstdc++/4.9/libs/$ndk_stdlib/include",
mtkleinb9be9792016-09-16 14:44:18 -0700147 ]
148 ldflags += [
149 "--sysroot=$ndk/platforms/$ndk_platform",
150 "--target=$ndk_target",
151 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
mtkleinb9be9792016-09-16 14:44:18 -0700152 ]
153 lib_dirs = [
Mike Kleinb7ce80b2016-12-14 13:17:53 -0500154 "$ndk/sources/cxx-stl/gnu-libstdc++/4.9/libs/$ndk_stdlib",
mtkleinb9be9792016-09-16 14:44:18 -0700155 "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/lib/gcc/$ndk_target/4.9.x",
156 ]
herbb6318bf2016-09-16 13:29:57 -0700157
Mike Kleinb7ce80b2016-12-14 13:17:53 -0500158 libs += [ "gnustl_static" ]
mtklein2b3c2a32016-09-08 08:39:34 -0700159 }
mtklein7fbfbbe2016-07-21 12:25:45 -0700160
Mike Klein7d302882016-11-03 14:06:31 -0400161 if (is_ios) {
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500162 _target = target_cpu
163 if (target_cpu == "arm") {
164 _target = "armv7"
Mike Klein7a5e0f32017-03-13 09:25:33 -0700165 } else if (target_cpu == "x86") {
166 _target = "i386"
167 } else if (target_cpu == "x64") {
168 _target = "x86_64"
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500169 }
Mike Klein4fe2b152017-02-17 15:36:01 -0500170 asmflags += [
171 "-isysroot",
172 ios_sysroot,
173 "-arch",
174 _target,
175 ]
Mike Klein7d302882016-11-03 14:06:31 -0400176 cflags += [
Mike Klein6749af42016-11-07 15:38:48 -0500177 "-isysroot",
178 ios_sysroot,
179 "-arch",
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500180 _target,
Mike Klein7d302882016-11-03 14:06:31 -0400181 ]
182 cflags_cc += [ "-stdlib=libc++" ]
183 ldflags += [
Mike Klein6749af42016-11-07 15:38:48 -0500184 "-isysroot",
185 ios_sysroot,
186 "-arch",
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500187 _target,
Mike Klein7d302882016-11-03 14:06:31 -0400188 "-stdlib=libc++",
189 ]
Herb Derby76073c12016-12-08 19:00:40 -0500190 libs += [ "objc" ]
Mike Klein7d302882016-11-03 14:06:31 -0400191 }
192
mtkleinb9be9792016-09-16 14:44:18 -0700193 if (is_linux) {
Herb Derby76073c12016-12-08 19:00:40 -0500194 libs += [ "pthread" ]
mtkleinb9be9792016-09-16 14:44:18 -0700195 }
Mike Klein15f46c32017-12-07 12:48:57 -0500196 if (is_mac) {
197 # Disable linker warnings. They're usually just annoyances like,
198 # ld: warning: text-based stub file
199 # /System/Library/Frameworks/foo.framework/foo.tbd and library file
200 # /System/Library/Frameworks/foo.framework/foo are out of sync.
201 # Falling back to library file for linking.
202 ldflags += [ "-Wl,-w" ]
203 }
mtkleinb9be9792016-09-16 14:44:18 -0700204
205 if (sanitize != "") {
206 # You can either pass the sanitizers directly, e.g. "address,undefined",
207 # or pass one of the couple common aliases used by the bots.
208 sanitizers = sanitize
Ben Wagner314d7c22018-01-03 11:04:30 -0500209 if (sanitize == "ASAN" || sanitize == "UBSAN") {
210 # ASAN implicitly runs all UBSAN checks also.
211 sanitizers = "bool,float-cast-overflow,integer-divide-by-zero,nonnull-attribute,null,return,returns-nonnull-attribute,shift,signed-integer-overflow,unreachable,vla-bound"
212 if (!is_mac && !is_win) {
213 sanitizers += ",function" # Not supported on Mac or Win.
Mike Klein46d6c682017-10-05 11:22:09 -0400214 }
Ben Wagner314d7c22018-01-03 11:04:30 -0500215 if (!is_win) {
216 sanitizers += ",vptr" # Not supported on Win.
217 }
218 if (!is_debug && !is_win) {
219 # No-op with somewhat annoying warning at -O0.
220 # Seems to be broken on Win.
221 sanitizers += ",object-size"
222 }
223 if (sanitize == "ASAN") {
224 sanitizers += ",address"
Mike Kleine4781e92017-09-28 11:42:28 -0400225 }
mtkleinb9be9792016-09-16 14:44:18 -0700226 } else if (sanitize == "TSAN") {
227 sanitizers = "thread"
228 } else if (sanitize == "MSAN") {
229 sanitizers = "memory"
230 }
231
232 cflags += [
233 "-fsanitize=$sanitizers",
234 "-fno-sanitize-recover=$sanitizers",
235 "-fsanitize-blacklist=" + rebase_path("../tools/xsan.blacklist"),
236 ]
237 ldflags += [ "-fsanitize=$sanitizers" ]
238 if (sanitizers == "memory") {
239 cflags += [ "-fsanitize-memory-track-origins" ]
240 cflags_cc += [ "-stdlib=libc++" ]
241 ldflags += [ "-stdlib=libc++" ]
242 }
243 }
244}
245
Mike Klein6e55fef2016-10-26 11:41:47 -0400246config("no_exceptions") {
Mike Klein228276d2017-08-16 13:20:20 +0000247 # Exceptions are disabled by default on Windows. (Use /EHsc to enable them.)
Mike Klein6e55fef2016-10-26 11:41:47 -0400248 if (!is_win) {
249 cflags_cc = [ "-fno-exceptions" ]
250 }
251}
252
Mike Kleinc7165c22016-10-12 23:58:06 -0400253config("warnings") {
254 cflags = []
255 cflags_cc = []
Mike Klein43c25262016-10-20 10:17:47 -0400256 cflags_objc = []
Greg Daniel6b7e0e22017-07-12 16:21:09 -0400257 cflags_objcc = []
Mike Kleinc7165c22016-10-12 23:58:06 -0400258 if (is_win) {
259 cflags += [
260 "/W3", # Turn on lots of warnings.
261
262 # Disable a bunch of warnings:
263 "/wd4244", # conversion from 'float' to 'int', possible loss of data
264 "/wd4267", # conversion from 'size_t' to 'int', possible loss of data
265 "/wd4800", # forcing value to bool 'true' or 'false' (performance warning)
266
267 # Probably only triggers when /EHsc is enabled.
268 "/wd4291", # no matching operator delete found;
269 # memory will not be freed if initialization throws an exception
270 ]
271 } else {
272 cflags += [
273 "-Wall",
274 "-Wextra",
275 "-Winit-self",
276 "-Wpointer-arith",
277 "-Wsign-compare",
278 "-Wvla",
279
280 "-Wno-deprecated-declarations",
Mike Kleindb402ca2016-12-13 12:46:05 -0500281 "-Wno-maybe-uninitialized",
Mike Kleinc7165c22016-10-12 23:58:06 -0400282 ]
283 cflags_cc += [ "-Wnon-virtual-dtor" ]
Chris Dalton1ef80942017-12-04 12:01:30 -0700284 }
Mike Kleinc7165c22016-10-12 23:58:06 -0400285
Chris Dalton1ef80942017-12-04 12:01:30 -0700286 if (is_clang) {
287 cflags += [
288 "-Weverything",
289 "-Wno-unknown-warning-option", # Let older Clangs ignore newer Clangs' warnings.
290 ]
Mike Kleinc7165c22016-10-12 23:58:06 -0400291
Chris Dalton1ef80942017-12-04 12:01:30 -0700292 if (target_cpu == "arm" && is_ios) {
293 # Clang seems to think new/malloc will only be 4-byte aligned on 32-bit iOS.
294 # We're pretty sure it's actually 8-byte alignment.
295 cflags += [ "-Wno-over-aligned" ]
Mike Kleinc7165c22016-10-12 23:58:06 -0400296 }
Chris Dalton1ef80942017-12-04 12:01:30 -0700297
298 # Shouldn't be necessary for local builds. With distributed Windows builds, files may lose
299 # their case during copy, causing case-sensitivity mismatch on remote machines.
300 cflags += [
301 "-Wno-nonportable-include-path",
302 "-Wno-nonportable-system-include-path",
303 ]
304
305 # TODO: These would all be really great warnings to turn on.
306 cflags += [
307 "-Wno-cast-align",
308 "-Wno-cast-qual",
309 "-Wno-conditional-uninitialized",
310 "-Wno-conversion",
311 "-Wno-disabled-macro-expansion",
312 "-Wno-documentation",
313 "-Wno-documentation-unknown-command",
314 "-Wno-double-promotion",
315 "-Wno-exit-time-destructors", # TODO: OK outside libskia
316 "-Wno-float-conversion",
317 "-Wno-float-equal",
318 "-Wno-format-nonliteral",
319 "-Wno-global-constructors", # TODO: OK outside libskia
320 "-Wno-gnu-zero-variadic-macro-arguments",
321 "-Wno-missing-prototypes",
322 "-Wno-missing-variable-declarations",
323 "-Wno-pedantic",
324 "-Wno-reserved-id-macro",
325 "-Wno-shadow",
326 "-Wno-shift-sign-overflow",
327 "-Wno-sign-conversion",
328 "-Wno-signed-enum-bitfield",
329 "-Wno-switch-enum",
330 "-Wno-undef",
331 "-Wno-unreachable-code",
332 "-Wno-unreachable-code-break",
333 "-Wno-unreachable-code-return",
334 "-Wno-unused-macros",
335 "-Wno-unused-member-function",
336 "-Wno-unused-template",
337 "-Wno-zero-as-null-pointer-constant",
338 ]
339 cflags_cc += [
340 "-Wno-abstract-vbase-init",
341 "-Wno-weak-vtables",
342 ]
343
344 # We are unlikely to want to fix these.
345 cflags += [
346 "-Wno-covered-switch-default",
347 "-Wno-deprecated",
348 "-Wno-implicit-fallthrough",
349 "-Wno-missing-noreturn",
350 "-Wno-old-style-cast",
351 "-Wno-padded",
Chris Daltond1ec4102017-12-04 13:31:21 -0700352 "-Wno-newline-eof",
Chris Dalton1ef80942017-12-04 12:01:30 -0700353 ]
354 cflags_cc += [
355 "-Wno-c++98-compat",
356 "-Wno-c++98-compat-pedantic",
357 "-Wno-undefined-func-template",
358 ]
359 cflags_objc += [
360 "-Wno-direct-ivar-access",
361 "-Wno-objc-interface-ivars",
362 ]
363 cflags_objcc += [
364 "-Wno-direct-ivar-access",
365 "-Wno-objcc-interface-ivars",
366 ]
Mike Kleinc7165c22016-10-12 23:58:06 -0400367 }
368}
Mike Klein50500ad2016-11-16 12:13:44 -0500369config("warnings_except_public_headers") {
Chris Dalton1ef80942017-12-04 12:01:30 -0700370 if (!is_win || is_clang) {
Mike Klein50500ad2016-11-16 12:13:44 -0500371 cflags = [ "-Wno-unused-parameter" ]
372 }
373}
Mike Kleinc7165c22016-10-12 23:58:06 -0400374
Mike Klein121563e2016-10-04 17:09:13 -0400375config("extra_flags") {
Kevin Lubick2677a9a2017-03-28 15:45:41 -0400376 asmflags = extra_asmflags
Mike Klein121563e2016-10-04 17:09:13 -0400377 cflags = extra_cflags
378 cflags_c = extra_cflags_c
379 cflags_cc = extra_cflags_cc
380 ldflags = extra_ldflags
381}
382
mtkleinb9be9792016-09-16 14:44:18 -0700383config("debug_symbols") {
384 # It's annoying to wait for full debug symbols to push over
385 # to Android devices. -gline-tables-only is a lot slimmer.
386 if (is_android) {
Mike Klein1b4602b2017-08-30 10:23:01 -0400387 cflags = [
388 "-gline-tables-only",
389 "-funwind-tables", # Helps make in-process backtraces fuller.
390 ]
Mike Kleincc300a12016-10-12 16:25:27 -0400391 } else if (is_win) {
Mike Klein67a86d52017-12-15 07:34:27 -0500392 cflags = [ "/Z7" ]
Brian Osmana99d9822017-12-28 14:53:10 -0500393 ldflags = [ "/DEBUG:FASTLINK" ]
Mike Kleincc300a12016-10-12 16:25:27 -0400394 } else {
mtkleinb9be9792016-09-16 14:44:18 -0700395 cflags = [ "-g" ]
396 }
397}
398
399config("no_rtti") {
400 if (sanitize != "ASAN") { # -fsanitize=vptr requires RTTI
Mike Kleincc300a12016-10-12 16:25:27 -0400401 if (is_win) {
402 cflags_cc = [ "/GR-" ]
403 } else {
mtkleinb9be9792016-09-16 14:44:18 -0700404 cflags_cc = [ "-fno-rtti" ]
405 }
406 }
407}
408
409config("release") {
Mike Kleincc300a12016-10-12 16:25:27 -0400410 if (is_win) {
Mike Klein916ca1d2016-10-20 13:34:18 -0400411 cflags = [
412 "/O2",
413 "/Zc:inline",
414 ]
Mike Klein90a381f2016-10-20 13:52:38 -0400415 ldflags = [
416 "/OPT:ICF",
417 "/OPT:REF",
418 ]
Mike Kleincc300a12016-10-12 16:25:27 -0400419 } else {
Mike Klein0bcfeac2016-10-19 22:24:10 -0400420 cflags = [
421 "-O3",
Mike Kleinbd9be4d2017-02-10 07:59:39 -0500422 "-fdata-sections",
423 "-ffunction-sections",
Mike Klein0bcfeac2016-10-19 22:24:10 -0400424 ]
Mike Kleinbd9be4d2017-02-10 07:59:39 -0500425 if (is_mac || is_ios) {
426 ldflags = [ "-dead_strip" ]
427 } else {
428 ldflags = [ "-Wl,--gc-sections" ]
429 }
Kevin Lubickebf648e2017-09-21 13:45:16 -0400430 if (target_cpu == "wasm") {
431 # The compiler asks us to add an optimization flag to both cflags
432 # and ldflags to cut down on the local variables,
433 # for performance reasons.
434 # The "linking" step is the conversion to javascript.
435 ldflags += [ "-O3" ]
436 }
herbb6318bf2016-09-16 13:29:57 -0700437 }
mtkleinb9be9792016-09-16 14:44:18 -0700438 defines = [ "NDEBUG" ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700439}
440
441config("executable") {
Mike Kleinc5875fb2016-12-06 10:46:02 -0500442 if (is_android) {
Mike Klein1b4602b2017-08-30 10:23:01 -0400443 ldflags = [
444 "-pie",
445 "-rdynamic",
446 ]
Mike Kleinc5875fb2016-12-06 10:46:02 -0500447 } else if (is_mac) {
mtklein7fbfbbe2016-07-21 12:25:45 -0700448 ldflags = [ "-Wl,-rpath,@loader_path/." ]
449 } else if (is_linux) {
mtkleina846c722016-09-15 10:44:15 -0700450 ldflags = [
451 "-rdynamic",
452 "-Wl,-rpath,\$ORIGIN",
453 ]
Mike Klein4b6b5032016-11-06 11:54:19 -0500454 } else if (is_win) {
455 ldflags = [
456 "/SUBSYSTEM:CONSOLE", # Quiet "no subsystem specified; CONSOLE assumed".
457 "/INCREMENTAL:NO", # Quiet warnings about failing to incrementally link by never trying to.
458 ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700459 }
460}