blob: 7beefa13396173c52d6883ffab93c8f940301339 [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
24 compiler_prefix = ""
mtklein7fbfbbe2016-07-21 12:25:45 -070025}
26
herbb6318bf2016-09-16 13:29:57 -070027if (is_win) {
28 config("default") {
29 cflags = [
30 "/FS", # Preserve previous PDB behavior.
31 "/bigobj", # Some of our files are bigger than the regular limits.
32 ]
33 cflags_c = ["/TC"]
34 cflags_cc = ["/TP"]
35 defines = [
36 "_HAS_EXCEPTIONS=0",
37 "WIN32_LEAN_AND_MEAN",
38 "NOMINMAX",
39 ]
mtklein2b3c2a32016-09-08 08:39:34 -070040 }
herbb6318bf2016-09-16 13:29:57 -070041 config("no_rtti") { }
halcanary19a97202016-08-03 15:08:04 -070042
herbb6318bf2016-09-16 13:29:57 -070043 config("debug_symbols") { }
mtklein88a7ac02016-09-14 11:16:49 -070044
herbb6318bf2016-09-16 13:29:57 -070045} else {
46 config("debug_symbols") {
47 # It's annoying to wait for full debug symbols to push over
48 # to Android devices. -gline-tables-only is a lot slimmer.
49 if (is_android) {
50 cflags = [ "-gline-tables-only" ]
51 } else {
52 cflags = [ "-g" ]
mtklein349cece2016-08-26 08:13:04 -070053 }
mtklein7d6fb2c2016-08-25 14:50:44 -070054 }
55
herbb6318bf2016-09-16 13:29:57 -070056 config("no_rtti") {
57 if (sanitize != "ASAN") { # -fsanitize=vptr requires RTTI
58 cflags_cc = [ "-fno-rtti" ]
59 }
mtklein7d6fb2c2016-08-25 14:50:44 -070060 }
mtklein2b3c2a32016-09-08 08:39:34 -070061
herbb6318bf2016-09-16 13:29:57 -070062 config("default") {
63 asmflags = []
64 cflags = [
65 "-O1",
66 "-fstrict-aliasing",
67 "-fPIC",
68 "-fvisibility=hidden",
mtklein2b3c2a32016-09-08 08:39:34 -070069
herbb6318bf2016-09-16 13:29:57 -070070 "-Werror",
71 "-Wall",
72 "-Wextra",
73 "-Winit-self",
74 "-Wpointer-arith",
75 "-Wsign-compare",
76 "-Wvla",
77
78 "-Wno-deprecated-declarations",
79 "-Wno-unused-parameter",
mtklein2b3c2a32016-09-08 08:39:34 -070080 ]
herbb6318bf2016-09-16 13:29:57 -070081 cflags_cc = [
82 "-std=c++11",
83 "-fno-exceptions",
84 "-fno-threadsafe-statics",
85 "-fvisibility-inlines-hidden",
86
87 "-Wnon-virtual-dtor",
88 ]
89 ldflags = []
90
91 if (current_cpu == "arm") {
92 cflags += [
93 "-march=armv7-a",
94 "-mfpu=neon",
95 "-mthumb",
96 ]
97 } else if (current_cpu == "mipsel") {
98 cflags += [
99 "-march=mips32r2",
100 "-mdspr2",
101 ]
102 } else if (current_cpu == "x86") {
103 asmflags += [ "-m32" ]
104 cflags += [
105 "-m32",
106 "-msse2",
107 "-mfpmath=sse",
108 ]
109 ldflags += [ "-m32" ]
110 }
111
112 if (is_android) {
113 asmflags += [
114 "--target=$ndk_target",
115 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
116 ]
117 cflags += [
118 "--sysroot=$ndk/platforms/$ndk_platform",
119 "--target=$ndk_target",
120 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
121 ]
122 cflags_cc += [
123 "-isystem$ndk/sources/android/support/include",
124 "-isystem$ndk/sources/cxx-stl/llvm-libc++/libcxx/include",
125 ]
126 ldflags += [
127 "--sysroot=$ndk/platforms/$ndk_platform",
128 "--target=$ndk_target",
129 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
130 "-pie",
131 ]
132 lib_dirs = [
133 "$ndk/sources/cxx-stl/llvm-libc++/libs/$ndk_stdlib",
134 "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/lib/gcc/$ndk_target/4.9.x",
135 ]
136
137 libs = [
138 # Order matters here! Keep these three in exactly this order.
139 "c++_static",
140 "c++abi",
141 "android_support",
142 ]
143 if (target_cpu == "arm") {
144 libs += [ "unwind" ]
145 }
146 }
147
148 if (is_linux) {
149 libs = [ "pthread" ]
150 }
151
152 if (sanitize != "") {
153 # You can either pass the sanitizers directly, e.g. "address,undefined",
154 # or pass one of the couple common aliases used by the bots.
155 sanitizers = sanitize
156 if (sanitize == "ASAN") {
157 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"
158 } else if (sanitize == "TSAN") {
159 sanitizers = "thread"
160 } else if (sanitize == "MSAN") {
161 sanitizers = "memory"
162 }
163
164 cflags += [
165 "-fsanitize=$sanitizers",
166 "-fno-sanitize-recover=$sanitizers",
167 "-fsanitize-blacklist=" + rebase_path("../tools/xsan.blacklist"),
168 ]
169 ldflags += [ "-fsanitize=$sanitizers" ]
170 if (sanitizers == "memory") {
171 cflags += [ "-fsanitize-memory-track-origins" ]
172 cflags_cc += [ "-stdlib=libc++" ]
173 ldflags += [ "-stdlib=libc++" ]
174 }
mtklein2b3c2a32016-09-08 08:39:34 -0700175 }
176 }
mtklein7fbfbbe2016-07-21 12:25:45 -0700177
herbb6318bf2016-09-16 13:29:57 -0700178 config("release") {
179 cflags = [ "-O3" ]
180 defines = [ "NDEBUG" ]
181 }
182
mtklein7fbfbbe2016-07-21 12:25:45 -0700183}
184
185config("executable") {
186 if (is_mac) {
187 ldflags = [ "-Wl,-rpath,@loader_path/." ]
188 } else if (is_linux) {
mtkleina846c722016-09-15 10:44:15 -0700189 ldflags = [
190 "-rdynamic",
191 "-Wl,-rpath,\$ORIGIN",
192 ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700193 }
194}
195
herbb6318bf2016-09-16 13:29:57 -0700196toolchain("msvc") {
197 vc = "$windk\VC\bin\amd64\cl.exe"
198 vlink = "$windk\VC\bin\amd64\link.exe"
199 vlib = "$windk\VC\bin\amd64\lib.exe"
200 # TODO: add a python function that generates the includes using <VSPATH>/win_sdk/bin/SetEnv.<cpu>.json
201 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 "
202
203 tool("cc") {
204 rspfile = "{{output}}.rsp"
205 precompiled_header_type = "msvc"
206 pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb"
207
208 # Label names may have spaces in them so the pdbname must be quoted. The
209 # source and output don't need to be quoted because GN knows they're a
210 # full file name and will quote automatically when necessary.
211
212 command = "$vc /nologo /showIncludes /FC @$rspfile /c {{source}} /Fo{{output}} /Fd\"$pdbname\""
213 depsformat = "msvc"
214 description = "CC {{output}}"
215 outputs = [
216 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj",
217 # "$object_subdir/{{source_name_part}}.obj",
218 ]
219 rspfile_content = "$windk_include_dirs {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}"
220 }
221
222 tool("cxx") {
223 rspfile = "{{output}}.rsp"
224 precompiled_header_type = "msvc"
225 pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb"
226
227 # Label names may have spaces in them so the pdbname must be quoted. The
228 # source and output don't need to be quoted because GN knows they're a
229 # full file name and will quote automatically when necessary.
230 command = "$vc /nologo /showIncludes /FC @$rspfile /c {{source}} /Fo{{output}} /Fd\"$pdbname\""
231 depsformat = "msvc"
232 description = "C++ {{output}}"
233 outputs = [
234 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj",
235 ]
236 rspfile_content = "$windk_include_dirs {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}"
237 }
238
239 tool("alink") {
240 rspfile = "{{output}}.rsp"
241 # gyp_win_tool_path = rebase_path("../third_party/externals/gyp/pylib/gyp/win_tool.py")
242 command = "$vlib /nologo {{arflags}} /OUT:{{output}} @$rspfile"
243 description = "LIB {{output}}"
244 outputs = [
245 # Ignore {{output_extension}} and always use .lib, there's no reason to
246 # allow targets to override this extension on Windows.
247 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
248 ]
249 default_output_extension = ".lib"
250 default_output_dir = "{{target_out_dir}}"
251
252 # The use of inputs_newline is to work around a fixed per-line buffer
253 # size in the linker.
254 rspfile_content = "{{inputs_newline}}"
255 }
256
257 tool("link") {
258 exename = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
259 pdbname = "$exename.pdb"
260 rspfile = "$exename.rsp"
261
262 # gyp_win_tool_path = rebase_path("../third_party/externals/gyp/pylib/gyp/win_tool.py")
263 command = "$vlink /nologo /OUT:$exename /PDB:$pdbname @$rspfile"
264
265 default_output_extension = ".exe"
266 default_output_dir = "{{root_out_dir}}"
267 description = "LINK {{output}}"
268 outputs = [
269 #"{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
270 exename,
271 ]
272 #if (symbol_level != 0) {
273 # outputs += [ pdbname ]
274 #}
275 #runtime_outputs = outputs
276
277 # The use of inputs_newline is to work around a fixed per-line buffer
278 # size in the linker.
279 rspfile_content = "{{inputs_newline}} {{libs}} {{solibs}} {{ldflags}}"
280 }
281
282
283 tool("stamp") {
284 win_stamp_path = rebase_path("win_stamp.py")
285 command = "python $win_stamp_path {{output}}"
286 }
287}
288
289
mtklein7fbfbbe2016-07-21 12:25:45 -0700290toolchain("gcc_like") {
291 lib_switch = "-l"
292 lib_dir_switch = "-L"
293
294 tool("cc") {
295 depfile = "{{output}}.d"
mtkleincab0bb72016-08-26 13:43:19 -0700296 command = "$compiler_prefix $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 -0700297 depsformat = "gcc"
298 outputs = [
299 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
300 ]
mtkleincab0bb72016-08-26 13:43:19 -0700301 description =
302 "$compiler_prefix $cc ... $extra_cflags $extra_cflags_c -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700303 }
304
305 tool("cxx") {
306 depfile = "{{output}}.d"
mtkleincab0bb72016-08-26 13:43:19 -0700307 command = "$compiler_prefix $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 -0700308 depsformat = "gcc"
309 outputs = [
310 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
311 ]
mtkleincab0bb72016-08-26 13:43:19 -0700312 description =
313 "$compiler_prefix $cxx ... $extra_cflags $extra_cflags_cc -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700314 }
315
316 tool("asm") {
317 depfile = "{{output}}.d"
mtkleincab0bb72016-08-26 13:43:19 -0700318 command = "$compiler_prefix $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700319 depsformat = "gcc"
320 outputs = [
321 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
322 ]
mtkleincab0bb72016-08-26 13:43:19 -0700323 description = "$compiler_prefix $cc ... -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700324 }
325
326 tool("alink") {
mtklein349cece2016-08-26 08:13:04 -0700327 command = "rm -f {{output}} && $ar rcs {{output}} {{inputs}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700328 outputs = [
mtklein5db44aa2016-07-29 09:10:31 -0700329 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
mtklein7fbfbbe2016-07-21 12:25:45 -0700330 ]
331 default_output_extension = ".a"
332 output_prefix = "lib"
mtklein349cece2016-08-26 08:13:04 -0700333 description = "$ar {{output}} ..."
mtklein7fbfbbe2016-07-21 12:25:45 -0700334 }
335
336 tool("solink") {
337 soname = "{{target_output_name}}{{output_extension}}"
338
339 rpath = "-Wl,-soname,$soname"
340 if (is_mac) {
341 rpath = "-Wl,-install_name,@rpath/$soname"
342 }
343
mtkleinbb35a6a2016-09-01 09:15:44 -0700344 command = "$compiler_prefix $cxx -shared {{ldflags}} {{inputs}} {{solibs}} {{libs}} $rpath $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700345 outputs = [
346 "{{root_out_dir}}/$soname",
347 ]
348 output_prefix = "lib"
349 default_output_extension = ".so"
mtklein233eb0a2016-09-02 11:24:50 -0700350 description =
351 "$compiler_prefix $cxx -shared ... $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700352 }
353
354 tool("link") {
mtkleinbb35a6a2016-09-01 09:15:44 -0700355 command = "$compiler_prefix $cxx {{ldflags}} {{inputs}} {{solibs}} {{libs}} $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700356 outputs = [
357 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
358 ]
mtklein233eb0a2016-09-02 11:24:50 -0700359 description = "$compiler_prefix $cxx ... $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700360 }
361
362 tool("stamp") {
363 command = "touch {{output}}"
364 }
365
366 tool("copy") {
367 command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
368 }
369}