blob: 427ab2eec68885e186344df0931abf144c0dd13e [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) {
mtklein349cece2016-08-26 08:13:04 -070012 ar = "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin/ar"
mtklein7d6fb2c2016-08-25 14:50:44 -070013 cc = "$ndk/toolchains/llvm/prebuilt/$ndk_host/bin/clang"
14 cxx = "$ndk/toolchains/llvm/prebuilt/$ndk_host/bin/clang++"
15 }
16
herbb6318bf2016-09-16 13:29:57 -070017 windk = ""
18
mtklein8079ba62016-08-16 09:31:16 -070019 extra_cflags = ""
20 extra_cflags_c = ""
21 extra_cflags_cc = ""
mtkleinbb35a6a2016-09-01 09:15:44 -070022 extra_ldflags = ""
mtkleincab0bb72016-08-26 13:43:19 -070023
mtklein60b7ab72016-09-20 12:09:12 -070024 cc_wrapper = ""
mtklein7fbfbbe2016-07-21 12:25:45 -070025}
26
mtkleinb9be9792016-09-16 14:44:18 -070027config("default") {
28 asmflags = []
29 cflags = []
30 cflags_c = []
31 cflags_cc = []
32 defines = []
33 ldflags = []
34
35 if (is_win) {
36 cflags += [
37 "/FS", # Preserve previous PDB behavior.
herbb6318bf2016-09-16 13:29:57 -070038 "/bigobj", # Some of our files are bigger than the regular limits.
39 ]
mtkleinb9be9792016-09-16 14:44:18 -070040 cflags_c += [ "/TC" ]
41 cflags_cc += [ "/TP" ]
42 defines += [
43 "_HAS_EXCEPTIONS=0",
44 "WIN32_LEAN_AND_MEAN",
45 "NOMINMAX",
herbb6318bf2016-09-16 13:29:57 -070046 ]
mtkleinb9be9792016-09-16 14:44:18 -070047 } else {
48 cflags += [
herbb6318bf2016-09-16 13:29:57 -070049 "-O1",
50 "-fstrict-aliasing",
51 "-fPIC",
52 "-fvisibility=hidden",
mtklein2b3c2a32016-09-08 08:39:34 -070053
herbb6318bf2016-09-16 13:29:57 -070054 "-Werror",
55 "-Wall",
56 "-Wextra",
57 "-Winit-self",
58 "-Wpointer-arith",
59 "-Wsign-compare",
60 "-Wvla",
61
62 "-Wno-deprecated-declarations",
63 "-Wno-unused-parameter",
mtklein2b3c2a32016-09-08 08:39:34 -070064 ]
mtkleinb9be9792016-09-16 14:44:18 -070065 cflags_cc += [
herbb6318bf2016-09-16 13:29:57 -070066 "-std=c++11",
67 "-fno-exceptions",
68 "-fno-threadsafe-statics",
69 "-fvisibility-inlines-hidden",
70
71 "-Wnon-virtual-dtor",
72 ]
mtkleinb9be9792016-09-16 14:44:18 -070073 }
herbb6318bf2016-09-16 13:29:57 -070074
mtkleinb9be9792016-09-16 14:44:18 -070075 if (current_cpu == "arm") {
76 cflags += [
77 "-march=armv7-a",
78 "-mfpu=neon",
79 "-mthumb",
80 ]
81 } else if (current_cpu == "mipsel") {
82 cflags += [
83 "-march=mips32r2",
84 "-mdspr2",
85 ]
86 } else if (current_cpu == "x86") {
87 asmflags += [ "-m32" ]
88 cflags += [
89 "-m32",
90 "-msse2",
91 "-mfpmath=sse",
92 ]
93 ldflags += [ "-m32" ]
94 }
herbb6318bf2016-09-16 13:29:57 -070095
mtkleinb9be9792016-09-16 14:44:18 -070096 if (is_android) {
97 asmflags += [
98 "--target=$ndk_target",
99 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
100 ]
101 cflags += [
102 "--sysroot=$ndk/platforms/$ndk_platform",
103 "--target=$ndk_target",
104 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
105 ]
106 cflags_cc += [
107 "-isystem$ndk/sources/android/support/include",
108 "-isystem$ndk/sources/cxx-stl/llvm-libc++/libcxx/include",
109 ]
110 ldflags += [
111 "--sysroot=$ndk/platforms/$ndk_platform",
112 "--target=$ndk_target",
113 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
114 "-pie",
115 ]
116 lib_dirs = [
117 "$ndk/sources/cxx-stl/llvm-libc++/libs/$ndk_stdlib",
118 "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/lib/gcc/$ndk_target/4.9.x",
119 ]
herbb6318bf2016-09-16 13:29:57 -0700120
mtkleinb9be9792016-09-16 14:44:18 -0700121 libs = [
122 # Order matters here! Keep these three in exactly this order.
123 "c++_static",
124 "c++abi",
125 "android_support",
126 ]
127 if (target_cpu == "arm") {
128 libs += [ "unwind" ]
mtklein2b3c2a32016-09-08 08:39:34 -0700129 }
130 }
mtklein7fbfbbe2016-07-21 12:25:45 -0700131
mtkleinb9be9792016-09-16 14:44:18 -0700132 if (is_linux) {
133 libs = [ "pthread" ]
134 }
135
136 if (sanitize != "") {
137 # You can either pass the sanitizers directly, e.g. "address,undefined",
138 # or pass one of the couple common aliases used by the bots.
139 sanitizers = sanitize
140 if (sanitize == "ASAN") {
141 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"
142 } else if (sanitize == "TSAN") {
143 sanitizers = "thread"
144 } else if (sanitize == "MSAN") {
145 sanitizers = "memory"
146 }
147
148 cflags += [
149 "-fsanitize=$sanitizers",
150 "-fno-sanitize-recover=$sanitizers",
151 "-fsanitize-blacklist=" + rebase_path("../tools/xsan.blacklist"),
152 ]
153 ldflags += [ "-fsanitize=$sanitizers" ]
154 if (sanitizers == "memory") {
155 cflags += [ "-fsanitize-memory-track-origins" ]
156 cflags_cc += [ "-stdlib=libc++" ]
157 ldflags += [ "-stdlib=libc++" ]
158 }
159 }
160}
161
162config("debug_symbols") {
163 # It's annoying to wait for full debug symbols to push over
164 # to Android devices. -gline-tables-only is a lot slimmer.
165 if (is_android) {
166 cflags = [ "-gline-tables-only" ]
167 } else if (!is_win) {
168 cflags = [ "-g" ]
169 }
170}
171
172config("no_rtti") {
173 if (sanitize != "ASAN") { # -fsanitize=vptr requires RTTI
174 if (!is_win) {
175 cflags_cc = [ "-fno-rtti" ]
176 }
177 }
178}
179
180config("release") {
181 if (!is_win) {
herbb6318bf2016-09-16 13:29:57 -0700182 cflags = [ "-O3" ]
herbb6318bf2016-09-16 13:29:57 -0700183 }
mtkleinb9be9792016-09-16 14:44:18 -0700184 defines = [ "NDEBUG" ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700185}
186
187config("executable") {
188 if (is_mac) {
189 ldflags = [ "-Wl,-rpath,@loader_path/." ]
190 } else if (is_linux) {
mtkleina846c722016-09-15 10:44:15 -0700191 ldflags = [
192 "-rdynamic",
193 "-Wl,-rpath,\$ORIGIN",
194 ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700195 }
196}
197
herbb6318bf2016-09-16 13:29:57 -0700198toolchain("msvc") {
mtkleinb9be9792016-09-16 14:44:18 -0700199 vc = "$windk\VC\bin\amd64\cl.exe"
herbb6318bf2016-09-16 13:29:57 -0700200 vlink = "$windk\VC\bin\amd64\link.exe"
mtkleinb9be9792016-09-16 14:44:18 -0700201 vlib = "$windk\VC\bin\amd64\lib.exe"
202
herbb6318bf2016-09-16 13:29:57 -0700203 # TODO: add a python function that generates the includes using <VSPATH>/win_sdk/bin/SetEnv.<cpu>.json
204 windk_include_dirs = "/I$windk\win_sdk\bin\..\..\win_sdk\Include\10.0.10586.0\um /I$windk\win_sdk\bin\..\..\win_sdk\Include\10.0.10586.0\shared /I$windk\win_sdk\bin\..\..\win_sdk\Include\10.0.10586.0\winrt /I$windk\win_sdk\bin\..\..\win_sdk\Include\10.0.10586.0\ucrt /I$windk\win_sdk\bin\..\..\VC\include /I$windk\win_sdk\bin\..\..\VC\atlmfc\include "
205
206 tool("cc") {
207 rspfile = "{{output}}.rsp"
208 precompiled_header_type = "msvc"
209 pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb"
210
211 # Label names may have spaces in them so the pdbname must be quoted. The
212 # source and output don't need to be quoted because GN knows they're a
213 # full file name and will quote automatically when necessary.
mtkleinb9be9792016-09-16 14:44:18 -0700214
herbb6318bf2016-09-16 13:29:57 -0700215 command = "$vc /nologo /showIncludes /FC @$rspfile /c {{source}} /Fo{{output}} /Fd\"$pdbname\""
216 depsformat = "msvc"
217 description = "CC {{output}}"
218 outputs = [
219 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj",
220 # "$object_subdir/{{source_name_part}}.obj",
221 ]
222 rspfile_content = "$windk_include_dirs {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}"
223 }
224
225 tool("cxx") {
226 rspfile = "{{output}}.rsp"
227 precompiled_header_type = "msvc"
228 pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb"
229
230 # Label names may have spaces in them so the pdbname must be quoted. The
231 # source and output don't need to be quoted because GN knows they're a
232 # full file name and will quote automatically when necessary.
233 command = "$vc /nologo /showIncludes /FC @$rspfile /c {{source}} /Fo{{output}} /Fd\"$pdbname\""
234 depsformat = "msvc"
235 description = "C++ {{output}}"
236 outputs = [
237 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj",
238 ]
239 rspfile_content = "$windk_include_dirs {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}"
mtkleinb9be9792016-09-16 14:44:18 -0700240 }
herbb6318bf2016-09-16 13:29:57 -0700241
242 tool("alink") {
243 rspfile = "{{output}}.rsp"
mtkleinb9be9792016-09-16 14:44:18 -0700244
herbb6318bf2016-09-16 13:29:57 -0700245 # gyp_win_tool_path = rebase_path("../third_party/externals/gyp/pylib/gyp/win_tool.py")
246 command = "$vlib /nologo {{arflags}} /OUT:{{output}} @$rspfile"
247 description = "LIB {{output}}"
248 outputs = [
249 # Ignore {{output_extension}} and always use .lib, there's no reason to
250 # allow targets to override this extension on Windows.
251 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
252 ]
253 default_output_extension = ".lib"
254 default_output_dir = "{{target_out_dir}}"
255
256 # The use of inputs_newline is to work around a fixed per-line buffer
257 # size in the linker.
258 rspfile_content = "{{inputs_newline}}"
259 }
260
261 tool("link") {
262 exename = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
263 pdbname = "$exename.pdb"
264 rspfile = "$exename.rsp"
265
266 # gyp_win_tool_path = rebase_path("../third_party/externals/gyp/pylib/gyp/win_tool.py")
267 command = "$vlink /nologo /OUT:$exename /PDB:$pdbname @$rspfile"
268
269 default_output_extension = ".exe"
270 default_output_dir = "{{root_out_dir}}"
271 description = "LINK {{output}}"
272 outputs = [
273 #"{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
274 exename,
275 ]
mtkleinb9be9792016-09-16 14:44:18 -0700276
herbb6318bf2016-09-16 13:29:57 -0700277 #if (symbol_level != 0) {
278 # outputs += [ pdbname ]
279 #}
280 #runtime_outputs = outputs
281
282 # The use of inputs_newline is to work around a fixed per-line buffer
283 # size in the linker.
284 rspfile_content = "{{inputs_newline}} {{libs}} {{solibs}} {{ldflags}}"
285 }
286
herbb6318bf2016-09-16 13:29:57 -0700287 tool("stamp") {
288 win_stamp_path = rebase_path("win_stamp.py")
289 command = "python $win_stamp_path {{output}}"
290 }
291}
292
mtklein7fbfbbe2016-07-21 12:25:45 -0700293toolchain("gcc_like") {
294 lib_switch = "-l"
295 lib_dir_switch = "-L"
296
297 tool("cc") {
298 depfile = "{{output}}.d"
mtklein60b7ab72016-09-20 12:09:12 -0700299 command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} $extra_cflags $extra_cflags_c -c {{source}} -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700300 depsformat = "gcc"
301 outputs = [
302 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
303 ]
mtkleincab0bb72016-08-26 13:43:19 -0700304 description =
mtklein60b7ab72016-09-20 12:09:12 -0700305 "$cc_wrapper $cc ... $extra_cflags $extra_cflags_c -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700306 }
307
308 tool("cxx") {
309 depfile = "{{output}}.d"
mtklein60b7ab72016-09-20 12:09:12 -0700310 command = "$cc_wrapper $cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} $extra_cflags $extra_cflags_cc -c {{source}} -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700311 depsformat = "gcc"
312 outputs = [
313 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
314 ]
mtkleincab0bb72016-08-26 13:43:19 -0700315 description =
mtklein60b7ab72016-09-20 12:09:12 -0700316 "$cc_wrapper $cxx ... $extra_cflags $extra_cflags_cc -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700317 }
318
319 tool("asm") {
320 depfile = "{{output}}.d"
mtklein60b7ab72016-09-20 12:09:12 -0700321 command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700322 depsformat = "gcc"
323 outputs = [
324 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
325 ]
mtklein60b7ab72016-09-20 12:09:12 -0700326 description = "$cc_wrapper $cc ... -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700327 }
328
329 tool("alink") {
mtklein349cece2016-08-26 08:13:04 -0700330 command = "rm -f {{output}} && $ar rcs {{output}} {{inputs}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700331 outputs = [
mtklein5db44aa2016-07-29 09:10:31 -0700332 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
mtklein7fbfbbe2016-07-21 12:25:45 -0700333 ]
334 default_output_extension = ".a"
335 output_prefix = "lib"
mtklein349cece2016-08-26 08:13:04 -0700336 description = "$ar {{output}} ..."
mtklein7fbfbbe2016-07-21 12:25:45 -0700337 }
338
339 tool("solink") {
340 soname = "{{target_output_name}}{{output_extension}}"
341
342 rpath = "-Wl,-soname,$soname"
343 if (is_mac) {
344 rpath = "-Wl,-install_name,@rpath/$soname"
345 }
346
mtklein60b7ab72016-09-20 12:09:12 -0700347 command = "$cc_wrapper $cxx -shared {{ldflags}} {{inputs}} {{solibs}} {{libs}} $rpath $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700348 outputs = [
349 "{{root_out_dir}}/$soname",
350 ]
351 output_prefix = "lib"
352 default_output_extension = ".so"
mtklein60b7ab72016-09-20 12:09:12 -0700353 description = "$cc_wrapper $cxx -shared ... $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700354 }
355
356 tool("link") {
mtklein60b7ab72016-09-20 12:09:12 -0700357 command = "$cc_wrapper $cxx {{ldflags}} {{inputs}} {{solibs}} {{libs}} $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700358 outputs = [
359 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
360 ]
mtklein60b7ab72016-09-20 12:09:12 -0700361 description = "$cc_wrapper $cxx ... $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700362 }
363
364 tool("stamp") {
365 command = "touch {{output}}"
366 }
367
368 tool("copy") {
369 command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
370 }
371}