blob: 96da6775ec442ebebd1ac38447027893df562cd6 [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() {
mtklein349cece2016-08-26 08:13:04 -07007 ar = "ar"
mtklein7fbfbbe2016-07-21 12:25:45 -07008 cc = "cc"
9 cxx = "c++"
mtklein8079ba62016-08-16 09:31:16 -070010
mtklein7d6fb2c2016-08-25 14:50:44 -070011 if (is_android) {
Mike Klein82364ba2016-10-24 16:49:15 -040012 if (host_os == "win") {
Mike Klein67702f62016-12-09 10:32:28 -050013 ar = "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin/ar.exe"
14 cc = "$ndk/toolchains/llvm/prebuilt/$ndk_host/bin/clang.exe"
15 cxx = "$ndk/toolchains/llvm/prebuilt/$ndk_host/bin/clang++.exe"
16 } else {
17 ar = "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin/ar"
18 cc = "$ndk/toolchains/llvm/prebuilt/$ndk_host/bin/clang"
19 cxx = "$ndk/toolchains/llvm/prebuilt/$ndk_host/bin/clang++"
Mike Klein82364ba2016-10-24 16:49:15 -040020 }
mtklein7d6fb2c2016-08-25 14:50:44 -070021 }
22
Ben Wagner4e8472c2017-03-20 16:53:05 -040023 msvc = 2015
herbb6318bf2016-09-16 13:29:57 -070024
Kevin Lubick2677a9a2017-03-28 15:45:41 -040025 extra_asmflags = []
Mike Klein121563e2016-10-04 17:09:13 -040026 extra_cflags = []
27 extra_cflags_c = []
28 extra_cflags_cc = []
29 extra_ldflags = []
mtkleincab0bb72016-08-26 13:43:19 -070030
mtklein60b7ab72016-09-20 12:09:12 -070031 cc_wrapper = ""
Herb Derby76073c12016-12-08 19:00:40 -050032 malloc = ""
mtklein7fbfbbe2016-07-21 12:25:45 -070033}
Ben Wagner4e8472c2017-03-20 16:53:05 -040034declare_args() {
35 if (msvc == 2015) {
36 windk = "C:/Program Files (x86)/Microsoft Visual Studio 14.0"
37 } else {
38 windk = "C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional"
39 }
40}
mtklein7fbfbbe2016-07-21 12:25:45 -070041
Mike Klein82364ba2016-10-24 16:49:15 -040042if (host_os == "win") {
43 python = "python.bat"
44 stamp = "cmd.exe /c echo >"
45} else {
46 python = "python"
47 stamp = "touch"
48}
49
Mike Klein87c36f22016-11-13 10:29:25 -050050is_clang = is_android || is_ios || is_mac || (cc == "clang" && cxx == "clang++")
51if (!is_clang && !is_win) {
Mike Klein461d3282016-10-11 13:51:55 -040052 is_clang = exec_script("is_clang.py",
53 [
54 cc,
55 cxx,
56 ],
57 "value")
58}
mtkleinf347c512016-09-26 08:40:12 -070059
Mike Klein7d302882016-11-03 14:06:31 -040060if (is_ios) {
Matthew Leibowitz3150ec62017-03-14 16:22:32 -040061 if (is_tvos) {
62 sdk = "appletvos"
63 if (target_cpu == "x86" || target_cpu == "x64") {
64 sdk = "appletvsimulator"
65 }
66 } else {
67 sdk = "iphoneos"
68 if (target_cpu == "x86" || target_cpu == "x64") {
69 sdk = "iphonesimulator"
70 }
Mike Klein7a5e0f32017-03-13 09:25:33 -070071 }
72 ios_sysroot = exec_script("find_ios_sysroot.py", [ sdk ], "trim string")
Mike Klein7d302882016-11-03 14:06:31 -040073}
74
mtkleinb9be9792016-09-16 14:44:18 -070075config("default") {
76 asmflags = []
77 cflags = []
78 cflags_c = []
79 cflags_cc = []
80 defines = []
81 ldflags = []
Herb Derby76073c12016-12-08 19:00:40 -050082 libs = []
mtkleinb9be9792016-09-16 14:44:18 -070083
84 if (is_win) {
85 cflags += [
86 "/FS", # Preserve previous PDB behavior.
herbb6318bf2016-09-16 13:29:57 -070087 "/bigobj", # Some of our files are bigger than the regular limits.
Mike Kleinc7165c22016-10-12 23:58:06 -040088 "/WX", # Treat warnings as errors.
Ben Wagnere6b274e2017-01-03 17:09:59 -050089 "/utf-8", # Set Source and Executable character sets to UTF-8.
herbb6318bf2016-09-16 13:29:57 -070090 ]
mtkleinb9be9792016-09-16 14:44:18 -070091 defines += [
Mike Kleinc7165c22016-10-12 23:58:06 -040092 "_CRT_SECURE_NO_WARNINGS", # Disables warnings about sscanf().
93 "_HAS_EXCEPTIONS=0", # Disables exceptions in MSVC STL.
mtkleinb9be9792016-09-16 14:44:18 -070094 "WIN32_LEAN_AND_MEAN",
95 "NOMINMAX",
herbb6318bf2016-09-16 13:29:57 -070096 ]
Mike Klein0f61faa2016-10-11 16:26:57 -040097 include_dirs = [
Ben Wagner4e8472c2017-03-20 16:53:05 -040098 #2017
99 "$windk/VC/Tools/MSVC/14.10.25017/include",
100
101 #2015
Mike Kleind3016832016-10-12 15:52:44 -0400102 "$windk/VC/include",
Mike Kleincc300a12016-10-12 16:25:27 -0400103
Mike Kleind3016832016-10-12 15:52:44 -0400104 # For local builds.
Ben Wagner4e8472c2017-03-20 16:53:05 -0400105 # 2017
106 "$windk/../../../Windows Kits/10/Include/10.0.14393.0/shared",
107 "$windk/../../../Windows Kits/10/Include/10.0.14393.0/ucrt",
108 "$windk/../../../Windows Kits/10/Include/10.0.14393.0/um",
109 "$windk/../../../Windows Kits/10/Include/10.0.14393.0/winrt",
110
111 # 2015
Mike Klein0f61faa2016-10-11 16:26:57 -0400112 "$windk/../Windows Kits/8.1/Include/shared",
Ben Wagner4e8472c2017-03-20 16:53:05 -0400113 "$windk/../Windows Kits/10/Include/10.0.10150.0/ucrt",
Mike Klein3eb71212016-10-11 17:08:53 -0400114 "$windk/../Windows Kits/8.1/Include/um",
Brian Osman21d742d2017-01-10 13:31:33 -0500115 "$windk/../Windows Kits/8.1/Include/winrt",
Mike Kleincc300a12016-10-12 16:25:27 -0400116
Mike Kleind3016832016-10-12 15:52:44 -0400117 # For builds using win_toolchain asset.
Brian Osman3f289332017-01-10 10:38:23 -0500118 "$windk/win_sdk/Include/10.0.14393.0/shared",
119 "$windk/win_sdk/Include/10.0.14393.0/ucrt",
120 "$windk/win_sdk/Include/10.0.14393.0/um",
Brian Osman21d742d2017-01-10 13:31:33 -0500121 "$windk/win_sdk/Include/10.0.14393.0/winrt",
Mike Klein0f61faa2016-10-11 16:26:57 -0400122 ]
Mike Klein3eb71212016-10-11 17:08:53 -0400123 lib_dirs = [
Mike Kleind3016832016-10-12 15:52:44 -0400124 # For local builds.
Ben Wagner4e8472c2017-03-20 16:53:05 -0400125 # 2017
126 "$windk/../../../Windows Kits/10/Lib/10.0.14393.0/ucrt/$target_cpu",
127 "$windk/../../../Windows Kits/10/Lib/10.0.14393.0/um/$target_cpu",
128
129 #2015
Mike Klein0bc5a762016-10-12 22:42:55 -0400130 "$windk/../Windows Kits/10/Lib/10.0.10150.0/ucrt/$target_cpu",
131 "$windk/../Windows Kits/8.1/Lib/winv6.3/um/$target_cpu",
Mike Kleincc300a12016-10-12 16:25:27 -0400132
Mike Kleind3016832016-10-12 15:52:44 -0400133 # For builds using win_toolchain asset.
Brian Osman3f289332017-01-10 10:38:23 -0500134 "$windk/win_sdk/Lib/10.0.14393.0/ucrt/$target_cpu",
135 "$windk/win_sdk/Lib/10.0.14393.0/um/$target_cpu",
Mike Klein3eb71212016-10-11 17:08:53 -0400136 ]
Ben Wagner4e8472c2017-03-20 16:53:05 -0400137
138 #2017
139 lib_dirs += [ "$windk/VC/Tools/MSVC/14.10.25017/lib/$target_cpu" ]
140
141 #2015
Mike Klein0bc5a762016-10-12 22:42:55 -0400142 if (target_cpu == "x86") {
143 lib_dirs += [ "$windk/VC/lib" ]
144 } else {
145 lib_dirs += [ "$windk/VC/lib/amd64" ]
146 }
mtkleinb9be9792016-09-16 14:44:18 -0700147 } else {
148 cflags += [
herbb6318bf2016-09-16 13:29:57 -0700149 "-fstrict-aliasing",
150 "-fPIC",
herbb6318bf2016-09-16 13:29:57 -0700151 "-Werror",
mtklein2b3c2a32016-09-08 08:39:34 -0700152 ]
Mike Klein28f5b772017-04-06 23:04:42 -0400153 cflags_cc += [ "-std=c++11" ]
Mike Kleineec23d12017-03-26 20:11:48 -0400154
155 # The main idea is to slim the exported API, but these flags also improve link time on Mac.
156 # These would make stack traces worse on Linux, so we don't just set them willy-nilly.
Mike Klein8cb66482017-03-28 14:16:22 -0400157 if (is_component_build || is_ios || is_mac) {
Mike Kleineec23d12017-03-26 20:11:48 -0400158 cflags += [ "-fvisibility=hidden" ]
159 cflags_cc += [ "-fvisibility-inlines-hidden" ]
160 }
mtkleinb9be9792016-09-16 14:44:18 -0700161 }
herbb6318bf2016-09-16 13:29:57 -0700162
mtkleinb9be9792016-09-16 14:44:18 -0700163 if (current_cpu == "arm") {
164 cflags += [
165 "-march=armv7-a",
166 "-mfpu=neon",
167 "-mthumb",
168 ]
169 } else if (current_cpu == "mipsel") {
170 cflags += [
Mike Kleine6320032017-03-04 05:39:54 -0500171 "-no-integrated-as", # Clang <4.0 doesn't understand 'usw' mnemonic.
mtkleinb9be9792016-09-16 14:44:18 -0700172 "-march=mips32r2",
mtkleinb9be9792016-09-16 14:44:18 -0700173 ]
Mike Klein5d8cf292016-10-12 19:36:09 -0400174 } else if (current_cpu == "x86" && !is_win) {
mtkleinb9be9792016-09-16 14:44:18 -0700175 asmflags += [ "-m32" ]
176 cflags += [
177 "-m32",
178 "-msse2",
179 "-mfpmath=sse",
180 ]
181 ldflags += [ "-m32" ]
182 }
herbb6318bf2016-09-16 13:29:57 -0700183
Herb Derby76073c12016-12-08 19:00:40 -0500184 if (malloc != "" && !is_win) {
185 cflags += [
186 "-fno-builtin-malloc",
187 "-fno-builtin-calloc",
188 "-fno-builtin-realloc",
189 "-fno-builtin-free",
190 ]
191 libs += [ malloc ]
192 }
193
mtkleinb9be9792016-09-16 14:44:18 -0700194 if (is_android) {
195 asmflags += [
196 "--target=$ndk_target",
197 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
198 ]
199 cflags += [
200 "--sysroot=$ndk/platforms/$ndk_platform",
201 "--target=$ndk_target",
202 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
203 ]
204 cflags_cc += [
205 "-isystem$ndk/sources/android/support/include",
Mike Kleinb7ce80b2016-12-14 13:17:53 -0500206 "-isystem$ndk/sources/cxx-stl/gnu-libstdc++/4.9/include",
207 "-isystem$ndk/sources/cxx-stl/gnu-libstdc++/4.9/libs/$ndk_stdlib/include",
mtkleinb9be9792016-09-16 14:44:18 -0700208 ]
209 ldflags += [
210 "--sysroot=$ndk/platforms/$ndk_platform",
211 "--target=$ndk_target",
212 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
mtkleinb9be9792016-09-16 14:44:18 -0700213 ]
214 lib_dirs = [
Mike Kleinb7ce80b2016-12-14 13:17:53 -0500215 "$ndk/sources/cxx-stl/gnu-libstdc++/4.9/libs/$ndk_stdlib",
mtkleinb9be9792016-09-16 14:44:18 -0700216 "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/lib/gcc/$ndk_target/4.9.x",
217 ]
herbb6318bf2016-09-16 13:29:57 -0700218
Mike Klein4fdc5432016-10-11 11:21:36 -0400219 if (current_cpu == "mips64el") {
220 # The r13 NDK omits /usr/lib from the MIPS64 sysroots, but Clang searches
221 # for /usr/lib64 as $PATH_TO_USR_LIB/../lib64. If there's no /usr/lib,
222 # it can't find /usr/lib64. We must point Clang at /usr/lib64 manually.
223 lib_dirs += [ "$ndk/platforms/$ndk_platform/usr/lib64" ]
Mike Klein978cceb2016-12-08 10:29:27 -0500224 ldflags += [ "-B$ndk/platforms/$ndk_platform/usr/lib64" ]
Mike Klein4fdc5432016-10-11 11:21:36 -0400225 }
226
Mike Kleinb7ce80b2016-12-14 13:17:53 -0500227 libs += [ "gnustl_static" ]
mtklein2b3c2a32016-09-08 08:39:34 -0700228 }
mtklein7fbfbbe2016-07-21 12:25:45 -0700229
Mike Klein7d302882016-11-03 14:06:31 -0400230 if (is_ios) {
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500231 _target = target_cpu
232 if (target_cpu == "arm") {
233 _target = "armv7"
Mike Klein7a5e0f32017-03-13 09:25:33 -0700234 } else if (target_cpu == "x86") {
235 _target = "i386"
236 } else if (target_cpu == "x64") {
237 _target = "x86_64"
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500238 }
Mike Klein4fe2b152017-02-17 15:36:01 -0500239 asmflags += [
240 "-isysroot",
241 ios_sysroot,
242 "-arch",
243 _target,
244 ]
Mike Klein7d302882016-11-03 14:06:31 -0400245 cflags += [
Mike Klein6749af42016-11-07 15:38:48 -0500246 "-isysroot",
247 ios_sysroot,
248 "-arch",
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500249 _target,
Mike Klein7d302882016-11-03 14:06:31 -0400250 ]
251 cflags_cc += [ "-stdlib=libc++" ]
252 ldflags += [
Mike Klein6749af42016-11-07 15:38:48 -0500253 "-isysroot",
254 ios_sysroot,
255 "-arch",
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500256 _target,
Mike Klein7d302882016-11-03 14:06:31 -0400257 "-stdlib=libc++",
258 ]
Herb Derby76073c12016-12-08 19:00:40 -0500259 libs += [ "objc" ]
Mike Klein7d302882016-11-03 14:06:31 -0400260 }
261
mtkleinb9be9792016-09-16 14:44:18 -0700262 if (is_linux) {
Herb Derby76073c12016-12-08 19:00:40 -0500263 libs += [ "pthread" ]
mtkleinb9be9792016-09-16 14:44:18 -0700264 }
265
266 if (sanitize != "") {
267 # You can either pass the sanitizers directly, e.g. "address,undefined",
268 # or pass one of the couple common aliases used by the bots.
269 sanitizers = sanitize
270 if (sanitize == "ASAN") {
271 sanitizers = "address,bool,function,integer-divide-by-zero,nonnull-attribute,null,object-size,return,returns-nonnull-attribute,shift,signed-integer-overflow,unreachable,vla-bound,vptr"
272 } else if (sanitize == "TSAN") {
273 sanitizers = "thread"
274 } else if (sanitize == "MSAN") {
275 sanitizers = "memory"
276 }
277
278 cflags += [
279 "-fsanitize=$sanitizers",
280 "-fno-sanitize-recover=$sanitizers",
281 "-fsanitize-blacklist=" + rebase_path("../tools/xsan.blacklist"),
282 ]
283 ldflags += [ "-fsanitize=$sanitizers" ]
284 if (sanitizers == "memory") {
285 cflags += [ "-fsanitize-memory-track-origins" ]
286 cflags_cc += [ "-stdlib=libc++" ]
287 ldflags += [ "-stdlib=libc++" ]
288 }
289 }
290}
291
Mike Klein6e55fef2016-10-26 11:41:47 -0400292config("no_exceptions") {
293 # Exceptions are disabled by default on Windows. (Use /EHsc to enable them.)
294 if (!is_win) {
295 cflags_cc = [ "-fno-exceptions" ]
296 }
297}
298
Mike Kleinc7165c22016-10-12 23:58:06 -0400299config("warnings") {
300 cflags = []
301 cflags_cc = []
Mike Klein43c25262016-10-20 10:17:47 -0400302 cflags_objc = []
Mike Kleinc7165c22016-10-12 23:58:06 -0400303 if (is_win) {
304 cflags += [
305 "/W3", # Turn on lots of warnings.
306
307 # Disable a bunch of warnings:
308 "/wd4244", # conversion from 'float' to 'int', possible loss of data
309 "/wd4267", # conversion from 'size_t' to 'int', possible loss of data
310 "/wd4800", # forcing value to bool 'true' or 'false' (performance warning)
311
312 # Probably only triggers when /EHsc is enabled.
313 "/wd4291", # no matching operator delete found;
314 # memory will not be freed if initialization throws an exception
315 ]
316 } else {
317 cflags += [
318 "-Wall",
319 "-Wextra",
320 "-Winit-self",
321 "-Wpointer-arith",
322 "-Wsign-compare",
323 "-Wvla",
324
325 "-Wno-deprecated-declarations",
Mike Kleindb402ca2016-12-13 12:46:05 -0500326 "-Wno-maybe-uninitialized",
Mike Kleinc7165c22016-10-12 23:58:06 -0400327 ]
328 cflags_cc += [ "-Wnon-virtual-dtor" ]
329
330 if (is_clang) {
331 cflags += [
332 "-Weverything",
333 "-Wno-unknown-warning-option", # Let older Clangs ignore newer Clangs' warnings.
334 ]
335
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500336 if ((target_cpu == "x86" && is_android) ||
337 (target_cpu == "arm" && is_ios)) {
338 # Clang seems to think new/malloc will only be 4-byte aligned on x86 Android and 32-bit iOS.
Mike Kleinc7165c22016-10-12 23:58:06 -0400339 # We're pretty sure it's actually 8-byte alignment.
340 cflags += [ "-Wno-over-aligned" ]
341 }
342
343 cflags += [
344 "-Wno-cast-align",
345 "-Wno-conditional-uninitialized",
346 "-Wno-conversion",
347 "-Wno-disabled-macro-expansion",
348 "-Wno-documentation",
349 "-Wno-documentation-unknown-command",
350 "-Wno-double-promotion",
351 "-Wno-exit-time-destructors", # TODO: OK outside libskia
352 "-Wno-float-conversion",
353 "-Wno-float-equal",
354 "-Wno-format-nonliteral",
355 "-Wno-global-constructors", # TODO: OK outside libskia
356 "-Wno-gnu-zero-variadic-macro-arguments",
357 "-Wno-missing-prototypes",
358 "-Wno-missing-variable-declarations",
359 "-Wno-pedantic",
360 "-Wno-reserved-id-macro",
361 "-Wno-shadow",
362 "-Wno-shift-sign-overflow",
363 "-Wno-sign-conversion",
Matt Sarettd3ed4022017-01-04 17:13:44 -0500364 "-Wno-signed-enum-bitfield",
Mike Kleinc7165c22016-10-12 23:58:06 -0400365 "-Wno-switch-enum",
366 "-Wno-undef",
367 "-Wno-unreachable-code",
368 "-Wno-unreachable-code-break",
369 "-Wno-unreachable-code-return",
370 "-Wno-unused-macros",
371 "-Wno-unused-member-function",
372 ]
373 cflags_cc += [
374 "-Wno-abstract-vbase-init",
Mike Kleinc7165c22016-10-12 23:58:06 -0400375 "-Wno-weak-vtables",
376 ]
377
378 # We are unlikely to want to fix these.
379 cflags += [
380 "-Wno-covered-switch-default",
381 "-Wno-deprecated",
382 "-Wno-implicit-fallthrough",
383 "-Wno-missing-noreturn",
384 "-Wno-old-style-cast",
385 "-Wno-padded",
386 ]
387 cflags_cc += [
388 "-Wno-c++98-compat",
389 "-Wno-c++98-compat-pedantic",
390 "-Wno-undefined-func-template",
391 ]
Mike Klein43c25262016-10-20 10:17:47 -0400392 cflags_objc += [
393 "-Wno-direct-ivar-access",
394 "-Wno-objc-interface-ivars",
395 ]
Mike Kleinc7165c22016-10-12 23:58:06 -0400396 }
397 }
398}
Mike Klein50500ad2016-11-16 12:13:44 -0500399config("warnings_except_public_headers") {
400 if (!is_win) {
401 cflags = [ "-Wno-unused-parameter" ]
402 }
403}
Mike Kleinc7165c22016-10-12 23:58:06 -0400404
Mike Klein121563e2016-10-04 17:09:13 -0400405config("extra_flags") {
Kevin Lubick2677a9a2017-03-28 15:45:41 -0400406 asmflags = extra_asmflags
Mike Klein121563e2016-10-04 17:09:13 -0400407 cflags = extra_cflags
408 cflags_c = extra_cflags_c
409 cflags_cc = extra_cflags_cc
410 ldflags = extra_ldflags
411}
412
mtkleinb9be9792016-09-16 14:44:18 -0700413config("debug_symbols") {
414 # It's annoying to wait for full debug symbols to push over
415 # to Android devices. -gline-tables-only is a lot slimmer.
416 if (is_android) {
417 cflags = [ "-gline-tables-only" ]
Mike Kleincc300a12016-10-12 16:25:27 -0400418 } else if (is_win) {
419 cflags = [ "/Zi" ]
Mike Klein5286d6c2016-10-13 13:19:25 -0400420 ldflags = [ "/DEBUG" ]
Mike Kleincc300a12016-10-12 16:25:27 -0400421 } else {
mtkleinb9be9792016-09-16 14:44:18 -0700422 cflags = [ "-g" ]
423 }
424}
425
426config("no_rtti") {
427 if (sanitize != "ASAN") { # -fsanitize=vptr requires RTTI
Mike Kleincc300a12016-10-12 16:25:27 -0400428 if (is_win) {
429 cflags_cc = [ "/GR-" ]
430 } else {
mtkleinb9be9792016-09-16 14:44:18 -0700431 cflags_cc = [ "-fno-rtti" ]
432 }
433 }
434}
435
436config("release") {
Mike Kleincc300a12016-10-12 16:25:27 -0400437 if (is_win) {
Mike Klein916ca1d2016-10-20 13:34:18 -0400438 cflags = [
439 "/O2",
440 "/Zc:inline",
Mike Klein8ffb2602016-10-20 15:45:02 -0400441 "/GS-",
Mike Klein916ca1d2016-10-20 13:34:18 -0400442 ]
Mike Klein90a381f2016-10-20 13:52:38 -0400443 ldflags = [
444 "/OPT:ICF",
445 "/OPT:REF",
446 ]
Mike Kleincc300a12016-10-12 16:25:27 -0400447 } else {
Mike Klein0bcfeac2016-10-19 22:24:10 -0400448 cflags = [
449 "-O3",
Mike Kleinbd9be4d2017-02-10 07:59:39 -0500450 "-fdata-sections",
451 "-ffunction-sections",
Mike Klein0bcfeac2016-10-19 22:24:10 -0400452 ]
Mike Kleinbd9be4d2017-02-10 07:59:39 -0500453 if (is_mac || is_ios) {
454 ldflags = [ "-dead_strip" ]
455 } else {
456 ldflags = [ "-Wl,--gc-sections" ]
457 }
herbb6318bf2016-09-16 13:29:57 -0700458 }
mtkleinb9be9792016-09-16 14:44:18 -0700459 defines = [ "NDEBUG" ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700460}
461
462config("executable") {
Mike Kleinc5875fb2016-12-06 10:46:02 -0500463 if (is_android) {
464 ldflags = [ "-pie" ]
465 } else if (is_mac) {
mtklein7fbfbbe2016-07-21 12:25:45 -0700466 ldflags = [ "-Wl,-rpath,@loader_path/." ]
467 } else if (is_linux) {
mtkleina846c722016-09-15 10:44:15 -0700468 ldflags = [
469 "-rdynamic",
470 "-Wl,-rpath,\$ORIGIN",
471 ]
Mike Klein4b6b5032016-11-06 11:54:19 -0500472 } else if (is_win) {
473 ldflags = [
474 "/SUBSYSTEM:CONSOLE", # Quiet "no subsystem specified; CONSOLE assumed".
475 "/INCREMENTAL:NO", # Quiet warnings about failing to incrementally link by never trying to.
476 ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700477 }
478}
479
herbb6318bf2016-09-16 13:29:57 -0700480toolchain("msvc") {
Mike Klein3eb71212016-10-11 17:08:53 -0400481 lib_dir_switch = "/LIBPATH:"
482
Ben Wagner4e8472c2017-03-20 16:53:05 -0400483 if (msvc == 2015) {
484 bin = "$windk/VC/bin/amd64"
485 env_setup = ""
486 if (target_cpu == "x86") {
487 bin += "_x86"
488 env_setup = "cmd /c $windk/win_sdk/bin/SetEnv.cmd /x86 && "
489 }
490 } else {
491 bin = "$windk/VC/Tools/MSVC/14.10.25017/bin/HostX64/$target_cpu"
492 env_setup = ""
493 if (target_cpu == "x86") {
494 print("Be sure to run")
495 print("$windk/VC/Auxiliary/Build/vcvarsall.bat amd64_x86")
496 print("to set up your environment before running ninja.")
497 }
Mike Klein0bc5a762016-10-12 22:42:55 -0400498 }
herbb6318bf2016-09-16 13:29:57 -0700499
Mike Kleined1b9022017-02-21 12:13:44 -0500500 tool("asm") {
Mike Klein5f804852017-03-29 19:29:13 -0400501 _ml = "ml"
502 if (target_cpu == "x64") {
503 _ml += "64"
504 }
505 command = "$env_setup$bin/$_ml.exe /nologo /c /Fo {{output}} {{source}}"
Mike Kleined1b9022017-02-21 12:13:44 -0500506 outputs = [
507 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj",
508 ]
509 description = "assemble {{source}}"
510 }
511
herbb6318bf2016-09-16 13:29:57 -0700512 tool("cc") {
513 rspfile = "{{output}}.rsp"
514 precompiled_header_type = "msvc"
515 pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb"
516
Mike Klein0f61faa2016-10-11 16:26:57 -0400517 # Label names may have spaces so pdbname must be quoted.
Mike Klein0bc5a762016-10-12 22:42:55 -0400518 command = "$env_setup$bin/cl.exe /nologo /showIncludes /FC @$rspfile /c {{source}} /Fo{{output}} /Fd\"$pdbname\""
herbb6318bf2016-09-16 13:29:57 -0700519 depsformat = "msvc"
herbb6318bf2016-09-16 13:29:57 -0700520 outputs = [
521 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj",
herbb6318bf2016-09-16 13:29:57 -0700522 ]
Mike Klein0f61faa2016-10-11 16:26:57 -0400523 rspfile_content = "{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}"
Mike Kleinc756e862016-10-13 14:31:01 -0400524 description = "compile {{source}}"
herbb6318bf2016-09-16 13:29:57 -0700525 }
526
527 tool("cxx") {
528 rspfile = "{{output}}.rsp"
529 precompiled_header_type = "msvc"
530 pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb"
531
Mike Klein0f61faa2016-10-11 16:26:57 -0400532 # Label names may have spaces so pdbname must be quoted.
Mike Klein0bc5a762016-10-12 22:42:55 -0400533 command = "$env_setup$bin/cl.exe /nologo /showIncludes /FC @$rspfile /c {{source}} /Fo{{output}} /Fd\"$pdbname\""
herbb6318bf2016-09-16 13:29:57 -0700534 depsformat = "msvc"
herbb6318bf2016-09-16 13:29:57 -0700535 outputs = [
536 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj",
537 ]
Mike Klein0f61faa2016-10-11 16:26:57 -0400538 rspfile_content = "{{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}"
Mike Kleinc756e862016-10-13 14:31:01 -0400539 description = "compile {{source}}"
mtkleinb9be9792016-09-16 14:44:18 -0700540 }
herbb6318bf2016-09-16 13:29:57 -0700541
542 tool("alink") {
543 rspfile = "{{output}}.rsp"
mtkleinb9be9792016-09-16 14:44:18 -0700544
Mike Kleinc756e862016-10-13 14:31:01 -0400545 command = "$env_setup$bin/lib.exe /nologo /ignore:4221 {{arflags}} /OUT:{{output}} @$rspfile"
herbb6318bf2016-09-16 13:29:57 -0700546 outputs = [
547 # Ignore {{output_extension}} and always use .lib, there's no reason to
548 # allow targets to override this extension on Windows.
549 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
550 ]
551 default_output_extension = ".lib"
552 default_output_dir = "{{target_out_dir}}"
553
Mike Klein0f61faa2016-10-11 16:26:57 -0400554 # inputs_newline works around a fixed per-line buffer size in the linker.
herbb6318bf2016-09-16 13:29:57 -0700555 rspfile_content = "{{inputs_newline}}"
Mike Kleinc756e862016-10-13 14:31:01 -0400556 description = "link {{output}}"
herbb6318bf2016-09-16 13:29:57 -0700557 }
558
Mike Klein1a8d6752016-10-17 11:51:11 -0400559 tool("solink") {
560 dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
561 libname = "${dllname}.lib"
562 pdbname = "${dllname}.pdb"
563 rspfile = "${dllname}.rsp"
564
565 command = "$env_setup$bin/link.exe /nologo /IMPLIB:$libname /DLL /OUT:$dllname /PDB:$pdbname @$rspfile"
566 outputs = [
567 dllname,
568 libname,
569 pdbname,
570 ]
571 default_output_extension = ".dll"
572 default_output_dir = "{{root_out_dir}}"
573
574 link_output = libname
575 depend_output = libname
576 runtime_outputs = [
577 dllname,
578 pdbname,
579 ]
580
581 # I don't quite understand this. Aping Chrome's toolchain/win/BUILD.gn.
582 restat = true
583
584 # inputs_newline works around a fixed per-line buffer size in the linker.
585 rspfile_content = "{{inputs_newline}} {{libs}} {{solibs}} {{ldflags}}"
586 description = "link {{output}}"
587 }
588
herbb6318bf2016-09-16 13:29:57 -0700589 tool("link") {
590 exename = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
591 pdbname = "$exename.pdb"
592 rspfile = "$exename.rsp"
593
Mike Klein0bc5a762016-10-12 22:42:55 -0400594 command =
595 "$env_setup$bin/link.exe /nologo /OUT:$exename /PDB:$pdbname @$rspfile"
herbb6318bf2016-09-16 13:29:57 -0700596
597 default_output_extension = ".exe"
598 default_output_dir = "{{root_out_dir}}"
herbb6318bf2016-09-16 13:29:57 -0700599 outputs = [
herbb6318bf2016-09-16 13:29:57 -0700600 exename,
601 ]
mtkleinb9be9792016-09-16 14:44:18 -0700602
Mike Klein0f61faa2016-10-11 16:26:57 -0400603 # inputs_newline works around a fixed per-line buffer size in the linker.
herbb6318bf2016-09-16 13:29:57 -0700604 rspfile_content = "{{inputs_newline}} {{libs}} {{solibs}} {{ldflags}}"
Mike Kleinc756e862016-10-13 14:31:01 -0400605 description = "link {{output}}"
herbb6318bf2016-09-16 13:29:57 -0700606 }
607
herbb6318bf2016-09-16 13:29:57 -0700608 tool("stamp") {
Mike Klein82364ba2016-10-24 16:49:15 -0400609 command = "$stamp {{output}}"
Mike Kleinc756e862016-10-13 14:31:01 -0400610 description = "stamp {{output}}"
herbb6318bf2016-09-16 13:29:57 -0700611 }
Mike Klein1a8d6752016-10-17 11:51:11 -0400612
613 tool("copy") {
614 cp_py = rebase_path("cp.py")
Mike Klein82364ba2016-10-24 16:49:15 -0400615 command = "$python $cp_py {{source}} {{output}}"
Mike Klein1a8d6752016-10-17 11:51:11 -0400616 description = "copy {{source}} {{output}}"
617 }
herbb6318bf2016-09-16 13:29:57 -0700618}
619
mtklein7fbfbbe2016-07-21 12:25:45 -0700620toolchain("gcc_like") {
621 lib_switch = "-l"
622 lib_dir_switch = "-L"
623
624 tool("cc") {
625 depfile = "{{output}}.d"
Mike Klein121563e2016-10-04 17:09:13 -0400626 command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700627 depsformat = "gcc"
628 outputs = [
629 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
630 ]
Mike Klein24267ff2016-10-17 10:41:41 -0400631 description = "compile {{source}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700632 }
633
634 tool("cxx") {
635 depfile = "{{output}}.d"
Mike Klein121563e2016-10-04 17:09:13 -0400636 command = "$cc_wrapper $cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700637 depsformat = "gcc"
638 outputs = [
639 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
640 ]
Mike Klein24267ff2016-10-17 10:41:41 -0400641 description = "compile {{source}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700642 }
643
Mike Klein43c25262016-10-20 10:17:47 -0400644 tool("objc") {
645 depfile = "{{output}}.d"
646 command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_objc}} -c {{source}} -o {{output}}"
647 depsformat = "gcc"
648 outputs = [
649 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
650 ]
651 description = "compile {{source}}"
652 }
653
654 tool("objcxx") {
655 depfile = "{{output}}.d"
656 command = "$cc_wrapper $cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} {{cflags_objc}} -c {{source}} -o {{output}}"
657 depsformat = "gcc"
658 outputs = [
659 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
660 ]
661 description = "compile {{source}}"
662 }
663
mtklein7fbfbbe2016-07-21 12:25:45 -0700664 tool("asm") {
665 depfile = "{{output}}.d"
mtklein60b7ab72016-09-20 12:09:12 -0700666 command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700667 depsformat = "gcc"
668 outputs = [
669 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
670 ]
Mike Klein39312c82017-04-26 10:33:39 -0400671 description = "assemble {{source}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700672 }
673
674 tool("alink") {
Mike Klein44b36a22016-11-06 11:20:09 -0500675 rspfile = "{{output}}.rsp"
676 rspfile_content = "{{inputs}}"
677 ar_py = rebase_path("ar.py")
678 command = "$python $ar_py $ar {{output}} $rspfile"
mtklein7fbfbbe2016-07-21 12:25:45 -0700679 outputs = [
mtklein5db44aa2016-07-29 09:10:31 -0700680 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
mtklein7fbfbbe2016-07-21 12:25:45 -0700681 ]
682 default_output_extension = ".a"
683 output_prefix = "lib"
Mike Klein24267ff2016-10-17 10:41:41 -0400684 description = "link {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700685 }
686
687 tool("solink") {
688 soname = "{{target_output_name}}{{output_extension}}"
689
690 rpath = "-Wl,-soname,$soname"
691 if (is_mac) {
692 rpath = "-Wl,-install_name,@rpath/$soname"
693 }
694
Mike Klein121563e2016-10-04 17:09:13 -0400695 command = "$cc_wrapper $cxx -shared {{ldflags}} {{inputs}} {{solibs}} {{libs}} $rpath -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700696 outputs = [
697 "{{root_out_dir}}/$soname",
698 ]
699 output_prefix = "lib"
700 default_output_extension = ".so"
Mike Klein24267ff2016-10-17 10:41:41 -0400701 description = "link {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700702 }
703
704 tool("link") {
Mike Klein121563e2016-10-04 17:09:13 -0400705 command = "$cc_wrapper $cxx {{ldflags}} {{inputs}} {{solibs}} {{libs}} -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700706 outputs = [
707 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
708 ]
Mike Klein24267ff2016-10-17 10:41:41 -0400709 description = "link {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700710 }
711
712 tool("stamp") {
Mike Klein82364ba2016-10-24 16:49:15 -0400713 command = "$stamp {{output}}"
Mike Klein24267ff2016-10-17 10:41:41 -0400714 description = "stamp {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700715 }
716
717 tool("copy") {
Mike Klein1a8d6752016-10-17 11:51:11 -0400718 cp_py = rebase_path("cp.py")
Mike Klein82364ba2016-10-24 16:49:15 -0400719 command = "$python $cp_py {{source}} {{output}}"
Mike Klein24267ff2016-10-17 10:41:41 -0400720 description = "copy {{source}} {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700721 }
722}