blob: 52c08387376286bea8a3e560510c1d5aaa967650 [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
209 if (sanitize == "ASAN") {
Ben Wagnerbee6cac2017-12-04 11:15:58 -0500210 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 -0400211 if (!is_mac) {
Mike Klein46d6c682017-10-05 11:22:09 -0400212 sanitizers += ",function" # Not supported on Mac.
213 }
214 if (!is_debug) {
215 sanitizers += ",object-size" # No-op with somewhat annoying warning at -O0.
Mike Kleine4781e92017-09-28 11:42:28 -0400216 }
mtkleinb9be9792016-09-16 14:44:18 -0700217 } else if (sanitize == "TSAN") {
218 sanitizers = "thread"
219 } else if (sanitize == "MSAN") {
220 sanitizers = "memory"
221 }
222
223 cflags += [
224 "-fsanitize=$sanitizers",
225 "-fno-sanitize-recover=$sanitizers",
226 "-fsanitize-blacklist=" + rebase_path("../tools/xsan.blacklist"),
227 ]
228 ldflags += [ "-fsanitize=$sanitizers" ]
229 if (sanitizers == "memory") {
230 cflags += [ "-fsanitize-memory-track-origins" ]
231 cflags_cc += [ "-stdlib=libc++" ]
232 ldflags += [ "-stdlib=libc++" ]
233 }
234 }
235}
236
Mike Klein6e55fef2016-10-26 11:41:47 -0400237config("no_exceptions") {
Mike Klein228276d2017-08-16 13:20:20 +0000238 # Exceptions are disabled by default on Windows. (Use /EHsc to enable them.)
Mike Klein6e55fef2016-10-26 11:41:47 -0400239 if (!is_win) {
240 cflags_cc = [ "-fno-exceptions" ]
241 }
242}
243
Mike Kleinc7165c22016-10-12 23:58:06 -0400244config("warnings") {
245 cflags = []
246 cflags_cc = []
Mike Klein43c25262016-10-20 10:17:47 -0400247 cflags_objc = []
Greg Daniel6b7e0e22017-07-12 16:21:09 -0400248 cflags_objcc = []
Mike Kleinc7165c22016-10-12 23:58:06 -0400249 if (is_win) {
250 cflags += [
251 "/W3", # Turn on lots of warnings.
252
253 # Disable a bunch of warnings:
254 "/wd4244", # conversion from 'float' to 'int', possible loss of data
255 "/wd4267", # conversion from 'size_t' to 'int', possible loss of data
256 "/wd4800", # forcing value to bool 'true' or 'false' (performance warning)
257
258 # Probably only triggers when /EHsc is enabled.
259 "/wd4291", # no matching operator delete found;
260 # memory will not be freed if initialization throws an exception
261 ]
262 } else {
263 cflags += [
264 "-Wall",
265 "-Wextra",
266 "-Winit-self",
267 "-Wpointer-arith",
268 "-Wsign-compare",
269 "-Wvla",
270
271 "-Wno-deprecated-declarations",
Mike Kleindb402ca2016-12-13 12:46:05 -0500272 "-Wno-maybe-uninitialized",
Mike Kleinc7165c22016-10-12 23:58:06 -0400273 ]
274 cflags_cc += [ "-Wnon-virtual-dtor" ]
Chris Dalton1ef80942017-12-04 12:01:30 -0700275 }
Mike Kleinc7165c22016-10-12 23:58:06 -0400276
Chris Dalton1ef80942017-12-04 12:01:30 -0700277 if (is_clang) {
278 cflags += [
279 "-Weverything",
280 "-Wno-unknown-warning-option", # Let older Clangs ignore newer Clangs' warnings.
281 ]
Mike Kleinc7165c22016-10-12 23:58:06 -0400282
Chris Dalton1ef80942017-12-04 12:01:30 -0700283 if (target_cpu == "arm" && is_ios) {
284 # Clang seems to think new/malloc will only be 4-byte aligned on 32-bit iOS.
285 # We're pretty sure it's actually 8-byte alignment.
286 cflags += [ "-Wno-over-aligned" ]
Mike Kleinc7165c22016-10-12 23:58:06 -0400287 }
Chris Dalton1ef80942017-12-04 12:01:30 -0700288
289 # Shouldn't be necessary for local builds. With distributed Windows builds, files may lose
290 # their case during copy, causing case-sensitivity mismatch on remote machines.
291 cflags += [
292 "-Wno-nonportable-include-path",
293 "-Wno-nonportable-system-include-path",
294 ]
295
296 # TODO: These would all be really great warnings to turn on.
297 cflags += [
298 "-Wno-cast-align",
299 "-Wno-cast-qual",
300 "-Wno-conditional-uninitialized",
301 "-Wno-conversion",
302 "-Wno-disabled-macro-expansion",
303 "-Wno-documentation",
304 "-Wno-documentation-unknown-command",
305 "-Wno-double-promotion",
306 "-Wno-exit-time-destructors", # TODO: OK outside libskia
307 "-Wno-float-conversion",
308 "-Wno-float-equal",
309 "-Wno-format-nonliteral",
310 "-Wno-global-constructors", # TODO: OK outside libskia
311 "-Wno-gnu-zero-variadic-macro-arguments",
312 "-Wno-missing-prototypes",
313 "-Wno-missing-variable-declarations",
314 "-Wno-pedantic",
315 "-Wno-reserved-id-macro",
316 "-Wno-shadow",
317 "-Wno-shift-sign-overflow",
318 "-Wno-sign-conversion",
319 "-Wno-signed-enum-bitfield",
320 "-Wno-switch-enum",
321 "-Wno-undef",
322 "-Wno-unreachable-code",
323 "-Wno-unreachable-code-break",
324 "-Wno-unreachable-code-return",
325 "-Wno-unused-macros",
326 "-Wno-unused-member-function",
327 "-Wno-unused-template",
328 "-Wno-zero-as-null-pointer-constant",
329 ]
330 cflags_cc += [
331 "-Wno-abstract-vbase-init",
332 "-Wno-weak-vtables",
333 ]
334
335 # We are unlikely to want to fix these.
336 cflags += [
337 "-Wno-covered-switch-default",
338 "-Wno-deprecated",
339 "-Wno-implicit-fallthrough",
340 "-Wno-missing-noreturn",
341 "-Wno-old-style-cast",
342 "-Wno-padded",
Chris Daltond1ec4102017-12-04 13:31:21 -0700343 "-Wno-newline-eof",
Chris Dalton1ef80942017-12-04 12:01:30 -0700344 ]
345 cflags_cc += [
346 "-Wno-c++98-compat",
347 "-Wno-c++98-compat-pedantic",
348 "-Wno-undefined-func-template",
349 ]
350 cflags_objc += [
351 "-Wno-direct-ivar-access",
352 "-Wno-objc-interface-ivars",
353 ]
354 cflags_objcc += [
355 "-Wno-direct-ivar-access",
356 "-Wno-objcc-interface-ivars",
357 ]
Mike Kleinc7165c22016-10-12 23:58:06 -0400358 }
359}
Mike Klein50500ad2016-11-16 12:13:44 -0500360config("warnings_except_public_headers") {
Chris Dalton1ef80942017-12-04 12:01:30 -0700361 if (!is_win || is_clang) {
Mike Klein50500ad2016-11-16 12:13:44 -0500362 cflags = [ "-Wno-unused-parameter" ]
363 }
364}
Mike Kleinc7165c22016-10-12 23:58:06 -0400365
Mike Klein121563e2016-10-04 17:09:13 -0400366config("extra_flags") {
Kevin Lubick2677a9a2017-03-28 15:45:41 -0400367 asmflags = extra_asmflags
Mike Klein121563e2016-10-04 17:09:13 -0400368 cflags = extra_cflags
369 cflags_c = extra_cflags_c
370 cflags_cc = extra_cflags_cc
371 ldflags = extra_ldflags
372}
373
mtkleinb9be9792016-09-16 14:44:18 -0700374config("debug_symbols") {
375 # It's annoying to wait for full debug symbols to push over
376 # to Android devices. -gline-tables-only is a lot slimmer.
377 if (is_android) {
Mike Klein1b4602b2017-08-30 10:23:01 -0400378 cflags = [
379 "-gline-tables-only",
380 "-funwind-tables", # Helps make in-process backtraces fuller.
381 ]
Mike Kleincc300a12016-10-12 16:25:27 -0400382 } else if (is_win) {
Mike Klein67a86d52017-12-15 07:34:27 -0500383 cflags = [ "/Z7" ]
Brian Osmana99d9822017-12-28 14:53:10 -0500384 ldflags = [ "/DEBUG:FASTLINK" ]
Mike Kleincc300a12016-10-12 16:25:27 -0400385 } else {
mtkleinb9be9792016-09-16 14:44:18 -0700386 cflags = [ "-g" ]
387 }
388}
389
390config("no_rtti") {
391 if (sanitize != "ASAN") { # -fsanitize=vptr requires RTTI
Mike Kleincc300a12016-10-12 16:25:27 -0400392 if (is_win) {
393 cflags_cc = [ "/GR-" ]
394 } else {
mtkleinb9be9792016-09-16 14:44:18 -0700395 cflags_cc = [ "-fno-rtti" ]
396 }
397 }
398}
399
400config("release") {
Mike Kleincc300a12016-10-12 16:25:27 -0400401 if (is_win) {
Mike Klein916ca1d2016-10-20 13:34:18 -0400402 cflags = [
403 "/O2",
404 "/Zc:inline",
405 ]
Mike Klein90a381f2016-10-20 13:52:38 -0400406 ldflags = [
407 "/OPT:ICF",
408 "/OPT:REF",
409 ]
Mike Kleincc300a12016-10-12 16:25:27 -0400410 } else {
Mike Klein0bcfeac2016-10-19 22:24:10 -0400411 cflags = [
412 "-O3",
Mike Kleinbd9be4d2017-02-10 07:59:39 -0500413 "-fdata-sections",
414 "-ffunction-sections",
Mike Klein0bcfeac2016-10-19 22:24:10 -0400415 ]
Mike Kleinbd9be4d2017-02-10 07:59:39 -0500416 if (is_mac || is_ios) {
417 ldflags = [ "-dead_strip" ]
418 } else {
419 ldflags = [ "-Wl,--gc-sections" ]
420 }
Kevin Lubickebf648e2017-09-21 13:45:16 -0400421 if (target_cpu == "wasm") {
422 # The compiler asks us to add an optimization flag to both cflags
423 # and ldflags to cut down on the local variables,
424 # for performance reasons.
425 # The "linking" step is the conversion to javascript.
426 ldflags += [ "-O3" ]
427 }
herbb6318bf2016-09-16 13:29:57 -0700428 }
mtkleinb9be9792016-09-16 14:44:18 -0700429 defines = [ "NDEBUG" ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700430}
431
432config("executable") {
Mike Kleinc5875fb2016-12-06 10:46:02 -0500433 if (is_android) {
Mike Klein1b4602b2017-08-30 10:23:01 -0400434 ldflags = [
435 "-pie",
436 "-rdynamic",
437 ]
Mike Kleinc5875fb2016-12-06 10:46:02 -0500438 } else if (is_mac) {
mtklein7fbfbbe2016-07-21 12:25:45 -0700439 ldflags = [ "-Wl,-rpath,@loader_path/." ]
440 } else if (is_linux) {
mtkleina846c722016-09-15 10:44:15 -0700441 ldflags = [
442 "-rdynamic",
443 "-Wl,-rpath,\$ORIGIN",
444 ]
Mike Klein4b6b5032016-11-06 11:54:19 -0500445 } else if (is_win) {
446 ldflags = [
447 "/SUBSYSTEM:CONSOLE", # Quiet "no subsystem specified; CONSOLE assumed".
448 "/INCREMENTAL:NO", # Quiet warnings about failing to incrementally link by never trying to.
449 ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700450 }
451}