blob: 30a8669808492c53728de6a81111d046950e9644 [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 ]
mtkleinb9be9792016-09-16 14:44:18 -0700153 cflags_cc += [
herbb6318bf2016-09-16 13:29:57 -0700154 "-std=c++11",
herbb6318bf2016-09-16 13:29:57 -0700155 "-fno-threadsafe-statics",
herbb6318bf2016-09-16 13:29:57 -0700156 ]
Mike Kleineec23d12017-03-26 20:11:48 -0400157
158 # The main idea is to slim the exported API, but these flags also improve link time on Mac.
159 # These would make stack traces worse on Linux, so we don't just set them willy-nilly.
Mike Klein8cb66482017-03-28 14:16:22 -0400160 if (is_component_build || is_ios || is_mac) {
Mike Kleineec23d12017-03-26 20:11:48 -0400161 cflags += [ "-fvisibility=hidden" ]
162 cflags_cc += [ "-fvisibility-inlines-hidden" ]
163 }
mtkleinb9be9792016-09-16 14:44:18 -0700164 }
herbb6318bf2016-09-16 13:29:57 -0700165
mtkleinb9be9792016-09-16 14:44:18 -0700166 if (current_cpu == "arm") {
167 cflags += [
168 "-march=armv7-a",
169 "-mfpu=neon",
170 "-mthumb",
171 ]
172 } else if (current_cpu == "mipsel") {
173 cflags += [
Mike Kleine6320032017-03-04 05:39:54 -0500174 "-no-integrated-as", # Clang <4.0 doesn't understand 'usw' mnemonic.
mtkleinb9be9792016-09-16 14:44:18 -0700175 "-march=mips32r2",
176 "-mdspr2",
177 ]
Mike Klein5d8cf292016-10-12 19:36:09 -0400178 } else if (current_cpu == "x86" && !is_win) {
mtkleinb9be9792016-09-16 14:44:18 -0700179 asmflags += [ "-m32" ]
180 cflags += [
181 "-m32",
182 "-msse2",
183 "-mfpmath=sse",
184 ]
185 ldflags += [ "-m32" ]
186 }
herbb6318bf2016-09-16 13:29:57 -0700187
Herb Derby76073c12016-12-08 19:00:40 -0500188 if (malloc != "" && !is_win) {
189 cflags += [
190 "-fno-builtin-malloc",
191 "-fno-builtin-calloc",
192 "-fno-builtin-realloc",
193 "-fno-builtin-free",
194 ]
195 libs += [ malloc ]
196 }
197
mtkleinb9be9792016-09-16 14:44:18 -0700198 if (is_android) {
199 asmflags += [
200 "--target=$ndk_target",
201 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
202 ]
203 cflags += [
204 "--sysroot=$ndk/platforms/$ndk_platform",
205 "--target=$ndk_target",
206 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
207 ]
208 cflags_cc += [
209 "-isystem$ndk/sources/android/support/include",
Mike Kleinb7ce80b2016-12-14 13:17:53 -0500210 "-isystem$ndk/sources/cxx-stl/gnu-libstdc++/4.9/include",
211 "-isystem$ndk/sources/cxx-stl/gnu-libstdc++/4.9/libs/$ndk_stdlib/include",
mtkleinb9be9792016-09-16 14:44:18 -0700212 ]
213 ldflags += [
214 "--sysroot=$ndk/platforms/$ndk_platform",
215 "--target=$ndk_target",
216 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
mtkleinb9be9792016-09-16 14:44:18 -0700217 ]
218 lib_dirs = [
Mike Kleinb7ce80b2016-12-14 13:17:53 -0500219 "$ndk/sources/cxx-stl/gnu-libstdc++/4.9/libs/$ndk_stdlib",
mtkleinb9be9792016-09-16 14:44:18 -0700220 "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/lib/gcc/$ndk_target/4.9.x",
221 ]
herbb6318bf2016-09-16 13:29:57 -0700222
Mike Klein4fdc5432016-10-11 11:21:36 -0400223 if (current_cpu == "mips64el") {
224 # The r13 NDK omits /usr/lib from the MIPS64 sysroots, but Clang searches
225 # for /usr/lib64 as $PATH_TO_USR_LIB/../lib64. If there's no /usr/lib,
226 # it can't find /usr/lib64. We must point Clang at /usr/lib64 manually.
227 lib_dirs += [ "$ndk/platforms/$ndk_platform/usr/lib64" ]
Mike Klein978cceb2016-12-08 10:29:27 -0500228 ldflags += [ "-B$ndk/platforms/$ndk_platform/usr/lib64" ]
Mike Klein4fdc5432016-10-11 11:21:36 -0400229 }
230
Mike Kleinb7ce80b2016-12-14 13:17:53 -0500231 libs += [ "gnustl_static" ]
mtklein2b3c2a32016-09-08 08:39:34 -0700232 }
mtklein7fbfbbe2016-07-21 12:25:45 -0700233
Mike Klein7d302882016-11-03 14:06:31 -0400234 if (is_ios) {
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500235 _target = target_cpu
236 if (target_cpu == "arm") {
237 _target = "armv7"
Mike Klein7a5e0f32017-03-13 09:25:33 -0700238 } else if (target_cpu == "x86") {
239 _target = "i386"
240 } else if (target_cpu == "x64") {
241 _target = "x86_64"
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500242 }
Mike Klein4fe2b152017-02-17 15:36:01 -0500243 asmflags += [
244 "-isysroot",
245 ios_sysroot,
246 "-arch",
247 _target,
248 ]
Mike Klein7d302882016-11-03 14:06:31 -0400249 cflags += [
Mike Klein6749af42016-11-07 15:38:48 -0500250 "-isysroot",
251 ios_sysroot,
252 "-arch",
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500253 _target,
Mike Klein7d302882016-11-03 14:06:31 -0400254 ]
255 cflags_cc += [ "-stdlib=libc++" ]
256 ldflags += [
Mike Klein6749af42016-11-07 15:38:48 -0500257 "-isysroot",
258 ios_sysroot,
259 "-arch",
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500260 _target,
Mike Klein7d302882016-11-03 14:06:31 -0400261 "-stdlib=libc++",
262 ]
Herb Derby76073c12016-12-08 19:00:40 -0500263 libs += [ "objc" ]
Mike Klein7d302882016-11-03 14:06:31 -0400264 }
265
mtkleinb9be9792016-09-16 14:44:18 -0700266 if (is_linux) {
Herb Derby76073c12016-12-08 19:00:40 -0500267 libs += [ "pthread" ]
mtkleinb9be9792016-09-16 14:44:18 -0700268 }
269
270 if (sanitize != "") {
271 # You can either pass the sanitizers directly, e.g. "address,undefined",
272 # or pass one of the couple common aliases used by the bots.
273 sanitizers = sanitize
274 if (sanitize == "ASAN") {
275 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"
276 } else if (sanitize == "TSAN") {
277 sanitizers = "thread"
278 } else if (sanitize == "MSAN") {
279 sanitizers = "memory"
280 }
281
282 cflags += [
283 "-fsanitize=$sanitizers",
284 "-fno-sanitize-recover=$sanitizers",
285 "-fsanitize-blacklist=" + rebase_path("../tools/xsan.blacklist"),
286 ]
287 ldflags += [ "-fsanitize=$sanitizers" ]
288 if (sanitizers == "memory") {
289 cflags += [ "-fsanitize-memory-track-origins" ]
290 cflags_cc += [ "-stdlib=libc++" ]
291 ldflags += [ "-stdlib=libc++" ]
292 }
293 }
294}
295
Mike Klein6e55fef2016-10-26 11:41:47 -0400296config("no_exceptions") {
297 # Exceptions are disabled by default on Windows. (Use /EHsc to enable them.)
298 if (!is_win) {
299 cflags_cc = [ "-fno-exceptions" ]
300 }
301}
302
Mike Kleinc7165c22016-10-12 23:58:06 -0400303config("warnings") {
304 cflags = []
305 cflags_cc = []
Mike Klein43c25262016-10-20 10:17:47 -0400306 cflags_objc = []
Mike Kleinc7165c22016-10-12 23:58:06 -0400307 if (is_win) {
308 cflags += [
309 "/W3", # Turn on lots of warnings.
310
311 # Disable a bunch of warnings:
312 "/wd4244", # conversion from 'float' to 'int', possible loss of data
313 "/wd4267", # conversion from 'size_t' to 'int', possible loss of data
314 "/wd4800", # forcing value to bool 'true' or 'false' (performance warning)
315
316 # Probably only triggers when /EHsc is enabled.
317 "/wd4291", # no matching operator delete found;
318 # memory will not be freed if initialization throws an exception
319 ]
320 } else {
321 cflags += [
322 "-Wall",
323 "-Wextra",
324 "-Winit-self",
325 "-Wpointer-arith",
326 "-Wsign-compare",
327 "-Wvla",
328
329 "-Wno-deprecated-declarations",
Mike Kleindb402ca2016-12-13 12:46:05 -0500330 "-Wno-maybe-uninitialized",
Mike Kleinc7165c22016-10-12 23:58:06 -0400331 ]
332 cflags_cc += [ "-Wnon-virtual-dtor" ]
333
334 if (is_clang) {
335 cflags += [
336 "-Weverything",
337 "-Wno-unknown-warning-option", # Let older Clangs ignore newer Clangs' warnings.
338 ]
339
Mike Kleinb48fd3c2017-01-23 11:58:53 -0500340 if ((target_cpu == "x86" && is_android) ||
341 (target_cpu == "arm" && is_ios)) {
342 # 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 -0400343 # We're pretty sure it's actually 8-byte alignment.
344 cflags += [ "-Wno-over-aligned" ]
345 }
346
347 cflags += [
348 "-Wno-cast-align",
349 "-Wno-conditional-uninitialized",
350 "-Wno-conversion",
351 "-Wno-disabled-macro-expansion",
352 "-Wno-documentation",
353 "-Wno-documentation-unknown-command",
354 "-Wno-double-promotion",
355 "-Wno-exit-time-destructors", # TODO: OK outside libskia
356 "-Wno-float-conversion",
357 "-Wno-float-equal",
358 "-Wno-format-nonliteral",
359 "-Wno-global-constructors", # TODO: OK outside libskia
360 "-Wno-gnu-zero-variadic-macro-arguments",
361 "-Wno-missing-prototypes",
362 "-Wno-missing-variable-declarations",
363 "-Wno-pedantic",
364 "-Wno-reserved-id-macro",
365 "-Wno-shadow",
366 "-Wno-shift-sign-overflow",
367 "-Wno-sign-conversion",
Matt Sarettd3ed4022017-01-04 17:13:44 -0500368 "-Wno-signed-enum-bitfield",
Mike Kleinc7165c22016-10-12 23:58:06 -0400369 "-Wno-switch-enum",
370 "-Wno-undef",
371 "-Wno-unreachable-code",
372 "-Wno-unreachable-code-break",
373 "-Wno-unreachable-code-return",
374 "-Wno-unused-macros",
375 "-Wno-unused-member-function",
376 ]
377 cflags_cc += [
378 "-Wno-abstract-vbase-init",
Mike Kleinc7165c22016-10-12 23:58:06 -0400379 "-Wno-weak-vtables",
380 ]
381
382 # We are unlikely to want to fix these.
383 cflags += [
384 "-Wno-covered-switch-default",
385 "-Wno-deprecated",
386 "-Wno-implicit-fallthrough",
387 "-Wno-missing-noreturn",
388 "-Wno-old-style-cast",
389 "-Wno-padded",
390 ]
391 cflags_cc += [
392 "-Wno-c++98-compat",
393 "-Wno-c++98-compat-pedantic",
394 "-Wno-undefined-func-template",
395 ]
Mike Klein43c25262016-10-20 10:17:47 -0400396 cflags_objc += [
397 "-Wno-direct-ivar-access",
398 "-Wno-objc-interface-ivars",
399 ]
Mike Kleinc7165c22016-10-12 23:58:06 -0400400 }
401 }
402}
Mike Klein50500ad2016-11-16 12:13:44 -0500403config("warnings_except_public_headers") {
404 if (!is_win) {
405 cflags = [ "-Wno-unused-parameter" ]
406 }
407}
Mike Kleinc7165c22016-10-12 23:58:06 -0400408
Mike Klein121563e2016-10-04 17:09:13 -0400409config("extra_flags") {
Kevin Lubick2677a9a2017-03-28 15:45:41 -0400410 asmflags = extra_asmflags
Mike Klein121563e2016-10-04 17:09:13 -0400411 cflags = extra_cflags
412 cflags_c = extra_cflags_c
413 cflags_cc = extra_cflags_cc
414 ldflags = extra_ldflags
415}
416
mtkleinb9be9792016-09-16 14:44:18 -0700417config("debug_symbols") {
418 # It's annoying to wait for full debug symbols to push over
419 # to Android devices. -gline-tables-only is a lot slimmer.
420 if (is_android) {
421 cflags = [ "-gline-tables-only" ]
Mike Kleincc300a12016-10-12 16:25:27 -0400422 } else if (is_win) {
423 cflags = [ "/Zi" ]
Mike Klein5286d6c2016-10-13 13:19:25 -0400424 ldflags = [ "/DEBUG" ]
Mike Kleincc300a12016-10-12 16:25:27 -0400425 } else {
mtkleinb9be9792016-09-16 14:44:18 -0700426 cflags = [ "-g" ]
427 }
428}
429
430config("no_rtti") {
431 if (sanitize != "ASAN") { # -fsanitize=vptr requires RTTI
Mike Kleincc300a12016-10-12 16:25:27 -0400432 if (is_win) {
433 cflags_cc = [ "/GR-" ]
434 } else {
mtkleinb9be9792016-09-16 14:44:18 -0700435 cflags_cc = [ "-fno-rtti" ]
436 }
437 }
438}
439
440config("release") {
Mike Kleincc300a12016-10-12 16:25:27 -0400441 if (is_win) {
Mike Klein916ca1d2016-10-20 13:34:18 -0400442 cflags = [
443 "/O2",
444 "/Zc:inline",
Mike Klein8ffb2602016-10-20 15:45:02 -0400445 "/GS-",
Mike Klein916ca1d2016-10-20 13:34:18 -0400446 ]
Mike Klein90a381f2016-10-20 13:52:38 -0400447 ldflags = [
448 "/OPT:ICF",
449 "/OPT:REF",
450 ]
Mike Kleincc300a12016-10-12 16:25:27 -0400451 } else {
Mike Klein0bcfeac2016-10-19 22:24:10 -0400452 cflags = [
453 "-O3",
Mike Kleinbd9be4d2017-02-10 07:59:39 -0500454 "-fdata-sections",
455 "-ffunction-sections",
Mike Klein0bcfeac2016-10-19 22:24:10 -0400456 ]
Mike Kleinbd9be4d2017-02-10 07:59:39 -0500457 if (is_mac || is_ios) {
458 ldflags = [ "-dead_strip" ]
459 } else {
460 ldflags = [ "-Wl,--gc-sections" ]
461 }
herbb6318bf2016-09-16 13:29:57 -0700462 }
mtkleinb9be9792016-09-16 14:44:18 -0700463 defines = [ "NDEBUG" ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700464}
465
466config("executable") {
Mike Kleinc5875fb2016-12-06 10:46:02 -0500467 if (is_android) {
468 ldflags = [ "-pie" ]
469 } else if (is_mac) {
mtklein7fbfbbe2016-07-21 12:25:45 -0700470 ldflags = [ "-Wl,-rpath,@loader_path/." ]
471 } else if (is_linux) {
mtkleina846c722016-09-15 10:44:15 -0700472 ldflags = [
473 "-rdynamic",
474 "-Wl,-rpath,\$ORIGIN",
475 ]
Mike Klein4b6b5032016-11-06 11:54:19 -0500476 } else if (is_win) {
477 ldflags = [
478 "/SUBSYSTEM:CONSOLE", # Quiet "no subsystem specified; CONSOLE assumed".
479 "/INCREMENTAL:NO", # Quiet warnings about failing to incrementally link by never trying to.
480 ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700481 }
482}
483
herbb6318bf2016-09-16 13:29:57 -0700484toolchain("msvc") {
Mike Klein3eb71212016-10-11 17:08:53 -0400485 lib_dir_switch = "/LIBPATH:"
486
Ben Wagner4e8472c2017-03-20 16:53:05 -0400487 if (msvc == 2015) {
488 bin = "$windk/VC/bin/amd64"
489 env_setup = ""
490 if (target_cpu == "x86") {
491 bin += "_x86"
492 env_setup = "cmd /c $windk/win_sdk/bin/SetEnv.cmd /x86 && "
493 }
494 } else {
495 bin = "$windk/VC/Tools/MSVC/14.10.25017/bin/HostX64/$target_cpu"
496 env_setup = ""
497 if (target_cpu == "x86") {
498 print("Be sure to run")
499 print("$windk/VC/Auxiliary/Build/vcvarsall.bat amd64_x86")
500 print("to set up your environment before running ninja.")
501 }
Mike Klein0bc5a762016-10-12 22:42:55 -0400502 }
herbb6318bf2016-09-16 13:29:57 -0700503
Mike Kleined1b9022017-02-21 12:13:44 -0500504 tool("asm") {
505 command = "$env_setup$bin/ml64.exe /nologo /c /Fo {{output}} {{source}}"
506 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 Klein24267ff2016-10-17 10:41:41 -0400671 description = "compile {{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}