df45ada0f5f6dd517d9d27a651a7d7ee3562a825
[platform/upstream/libSkiaSharp.git] / gn / BUILD.gn
1 # 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
6 declare_args() {
7   ar = "ar"
8   cc = "cc"
9   cxx = "c++"
10
11   if (is_android) {
12     ar = "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin/ar"
13     cc = "$ndk/toolchains/llvm/prebuilt/$ndk_host/bin/clang"
14     cxx = "$ndk/toolchains/llvm/prebuilt/$ndk_host/bin/clang++"
15   }
16
17   windk = "C:/Program Files (x86)/Microsoft Visual Studio 14.0"
18
19   extra_cflags = []
20   extra_cflags_c = []
21   extra_cflags_cc = []
22   extra_ldflags = []
23
24   cc_wrapper = ""
25 }
26
27 if (!is_win) {
28   is_clang = exec_script("is_clang.py",
29                          [
30                            cc,
31                            cxx,
32                          ],
33                          "value")
34 }
35
36 config("default") {
37   asmflags = []
38   cflags = []
39   cflags_c = []
40   cflags_cc = []
41   defines = []
42   ldflags = []
43
44   if (is_win) {
45     cflags += [
46       "/FS",  # Preserve previous PDB behavior.
47       "/bigobj",  # Some of our files are bigger than the regular limits.
48       "/WX",  # Treat warnings as errors.
49     ]
50     defines += [
51       "_CRT_SECURE_NO_WARNINGS",  # Disables warnings about sscanf().
52       "_HAS_EXCEPTIONS=0",  # Disables exceptions in MSVC STL.
53       "WIN32_LEAN_AND_MEAN",
54       "NOMINMAX",
55     ]
56     include_dirs = [
57       "$windk/VC/include",
58
59       # For local builds.
60       "$windk/../Windows Kits/10/Include/10.0.10150.0/ucrt",
61       "$windk/../Windows Kits/8.1/Include/shared",
62       "$windk/../Windows Kits/8.1/Include/um",
63
64       # For builds using win_toolchain asset.
65       "$windk/win_sdk/Include/10.0.10586.0/shared",
66       "$windk/win_sdk/Include/10.0.10586.0/ucrt",
67       "$windk/win_sdk/Include/10.0.10586.0/um",
68     ]
69     lib_dirs = [
70       # For local builds.
71       "$windk/../Windows Kits/10/Lib/10.0.10150.0/ucrt/$target_cpu",
72       "$windk/../Windows Kits/8.1/Lib/winv6.3/um/$target_cpu",
73
74       # For builds using win_toolchain asset.
75       "$windk/win_sdk/Lib/10.0.10586.0/ucrt/$target_cpu",
76       "$windk/win_sdk/Lib/10.0.10586.0/um/$target_cpu",
77     ]
78     if (target_cpu == "x86") {
79       lib_dirs += [ "$windk/VC/lib" ]
80     } else {
81       lib_dirs += [ "$windk/VC/lib/amd64" ]
82     }
83   } else {
84     cflags += [
85       "-O1",
86       "-fstrict-aliasing",
87       "-fPIC",
88       "-fvisibility=hidden",
89       "-Werror",
90     ]
91     cflags_cc += [
92       "-std=c++11",
93       "-fno-exceptions",
94       "-fno-threadsafe-statics",
95       "-fvisibility-inlines-hidden",
96     ]
97   }
98
99   if (current_cpu == "arm") {
100     cflags += [
101       "-march=armv7-a",
102       "-mfpu=neon",
103       "-mthumb",
104     ]
105   } else if (current_cpu == "mipsel") {
106     cflags += [
107       "-march=mips32r2",
108       "-mdspr2",
109     ]
110   } else if (current_cpu == "x86" && !is_win) {
111     asmflags += [ "-m32" ]
112     cflags += [
113       "-m32",
114       "-msse2",
115       "-mfpmath=sse",
116     ]
117     ldflags += [ "-m32" ]
118   }
119
120   if (is_android) {
121     asmflags += [
122       "--target=$ndk_target",
123       "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
124     ]
125     cflags += [
126       "--sysroot=$ndk/platforms/$ndk_platform",
127       "--target=$ndk_target",
128       "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
129     ]
130     cflags_cc += [
131       "-isystem$ndk/sources/android/support/include",
132       "-isystem$ndk/sources/cxx-stl/llvm-libc++/libcxx/include",  # Through r12b.
133       "-isystem$ndk/sources/cxx-stl/llvm-libc++/include",  # Since r13.
134     ]
135     ldflags += [
136       "--sysroot=$ndk/platforms/$ndk_platform",
137       "--target=$ndk_target",
138       "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
139       "-pie",
140     ]
141     lib_dirs = [
142       "$ndk/sources/cxx-stl/llvm-libc++/libs/$ndk_stdlib",
143       "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/lib/gcc/$ndk_target/4.9.x",
144     ]
145
146     if (current_cpu == "mips64el") {
147       # The r13 NDK omits /usr/lib from the MIPS64 sysroots, but Clang searches
148       # for /usr/lib64 as $PATH_TO_USR_LIB/../lib64.  If there's no /usr/lib,
149       # it can't find /usr/lib64.  We must point Clang at /usr/lib64 manually.
150       lib_dirs += [ "$ndk/platforms/$ndk_platform/usr/lib64" ]
151       ldflags += [
152         # Clang will try to link these two files, but not find them.  Again, do it manually.
153         "-nostartfiles",
154         "$ndk/platforms/$ndk_platform/usr/lib64/crtbegin_dynamic.o",
155         "$ndk/platforms/$ndk_platform/usr/lib64/crtend_android.o",
156       ]
157     }
158
159     libs = [
160       # Order matters here!  Keep these three in exactly this order.
161       "c++_static",
162       "c++abi",
163       "android_support",
164     ]
165     if (target_cpu == "arm") {
166       libs += [ "unwind" ]
167     }
168   }
169
170   if (is_linux) {
171     libs = [ "pthread" ]
172   }
173
174   if (sanitize != "") {
175     # You can either pass the sanitizers directly, e.g. "address,undefined",
176     # or pass one of the couple common aliases used by the bots.
177     sanitizers = sanitize
178     if (sanitize == "ASAN") {
179       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"
180     } else if (sanitize == "TSAN") {
181       sanitizers = "thread"
182     } else if (sanitize == "MSAN") {
183       sanitizers = "memory"
184     }
185
186     cflags += [
187       "-fsanitize=$sanitizers",
188       "-fno-sanitize-recover=$sanitizers",
189       "-fsanitize-blacklist=" + rebase_path("../tools/xsan.blacklist"),
190     ]
191     ldflags += [ "-fsanitize=$sanitizers" ]
192     if (sanitizers == "memory") {
193       cflags += [ "-fsanitize-memory-track-origins" ]
194       cflags_cc += [ "-stdlib=libc++" ]
195       ldflags += [ "-stdlib=libc++" ]
196     }
197   }
198 }
199
200 config("warnings") {
201   cflags = []
202   cflags_cc = []
203   if (is_win) {
204     cflags += [
205       "/W3",  # Turn on lots of warnings.
206
207       # Disable a bunch of warnings:
208       "/wd4244",  # conversion from 'float' to 'int', possible loss of data
209       "/wd4267",  # conversion from 'size_t' to 'int', possible loss of data
210       "/wd4800",  # forcing value to bool 'true' or 'false' (performance warning)
211
212       # Probably only triggers when /EHsc is enabled.
213       "/wd4291",  # no matching operator delete found;
214                   # memory will not be freed if initialization throws an exception
215     ]
216   } else {
217     cflags += [
218       "-Wall",
219       "-Wextra",
220       "-Winit-self",
221       "-Wpointer-arith",
222       "-Wsign-compare",
223       "-Wvla",
224
225       "-Wno-deprecated-declarations",
226       "-Wno-unused-parameter",
227     ]
228     cflags_cc += [ "-Wnon-virtual-dtor" ]
229
230     if (is_clang) {
231       cflags += [
232         "-Weverything",
233         "-Wno-unknown-warning-option",  # Let older Clangs ignore newer Clangs' warnings.
234       ]
235
236       if (is_android && target_cpu == "x86") {
237         # Clang seems to think new/malloc will only be 4-byte aligned on x86 Android.
238         # We're pretty sure it's actually 8-byte alignment.
239         cflags += [ "-Wno-over-aligned" ]
240       }
241
242       cflags += [
243         "-Wno-cast-align",
244         "-Wno-conditional-uninitialized",
245         "-Wno-conversion",
246         "-Wno-disabled-macro-expansion",
247         "-Wno-documentation",
248         "-Wno-documentation-unknown-command",
249         "-Wno-double-promotion",
250         "-Wno-exit-time-destructors",  # TODO: OK outside libskia
251         "-Wno-float-conversion",
252         "-Wno-float-equal",
253         "-Wno-format-nonliteral",
254         "-Wno-global-constructors",  # TODO: OK outside libskia
255         "-Wno-gnu-zero-variadic-macro-arguments",
256         "-Wno-missing-prototypes",
257         "-Wno-missing-variable-declarations",
258         "-Wno-pedantic",
259         "-Wno-reserved-id-macro",
260         "-Wno-shadow",
261         "-Wno-shift-sign-overflow",
262         "-Wno-sign-conversion",
263         "-Wno-switch-enum",
264         "-Wno-undef",
265         "-Wno-unreachable-code",
266         "-Wno-unreachable-code-break",
267         "-Wno-unreachable-code-return",
268         "-Wno-unused-macros",
269         "-Wno-unused-member-function",
270       ]
271       cflags_cc += [
272         "-Wno-abstract-vbase-init",
273         "-Wno-range-loop-analysis",
274         "-Wno-weak-vtables",
275       ]
276
277       # We are unlikely to want to fix these.
278       cflags += [
279         "-Wno-covered-switch-default",
280         "-Wno-deprecated",
281         "-Wno-implicit-fallthrough",
282         "-Wno-missing-noreturn",
283         "-Wno-old-style-cast",
284         "-Wno-padded",
285       ]
286       cflags_cc += [
287         "-Wno-c++98-compat",
288         "-Wno-c++98-compat-pedantic",
289         "-Wno-undefined-func-template",
290       ]
291     }
292   }
293 }
294
295 config("extra_flags") {
296   cflags = extra_cflags
297   cflags_c = extra_cflags_c
298   cflags_cc = extra_cflags_cc
299   ldflags = extra_ldflags
300 }
301
302 config("debug_symbols") {
303   # It's annoying to wait for full debug symbols to push over
304   # to Android devices.  -gline-tables-only is a lot slimmer.
305   if (is_android) {
306     cflags = [ "-gline-tables-only" ]
307   } else if (is_win) {
308     cflags = [ "/Zi" ]
309     ldflags = [ "/DEBUG" ]
310   } else {
311     cflags = [ "-g" ]
312   }
313 }
314
315 config("no_rtti") {
316   if (sanitize != "ASAN") {  # -fsanitize=vptr requires RTTI
317     if (is_win) {
318       cflags_cc = [ "/GR-" ]
319     } else {
320       cflags_cc = [ "-fno-rtti" ]
321     }
322   }
323 }
324
325 config("release") {
326   if (is_win) {
327     cflags = [ "/O2" ]
328   } else {
329     cflags = [ "-O3" ]
330   }
331   defines = [ "NDEBUG" ]
332 }
333
334 config("executable") {
335   if (is_mac) {
336     ldflags = [ "-Wl,-rpath,@loader_path/." ]
337   } else if (is_linux) {
338     ldflags = [
339       "-rdynamic",
340       "-Wl,-rpath,\$ORIGIN",
341     ]
342   }
343 }
344
345 toolchain("msvc") {
346   lib_dir_switch = "/LIBPATH:"
347
348   bin = "$windk/VC/bin/amd64"
349   env_setup = ""
350   if (target_cpu == "x86") {
351     bin += "_x86"
352     env_setup = "cmd /c $windk/win_sdk/bin/SetEnv.cmd /x86 && "
353   }
354
355   tool("cc") {
356     rspfile = "{{output}}.rsp"
357     precompiled_header_type = "msvc"
358     pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb"
359
360     # Label names may have spaces so pdbname must be quoted.
361     command = "$env_setup$bin/cl.exe /nologo /showIncludes /FC @$rspfile /c {{source}} /Fo{{output}} /Fd\"$pdbname\""
362     depsformat = "msvc"
363     outputs = [
364       "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj",
365     ]
366     rspfile_content = "{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}"
367     description = "compile {{source}}"
368   }
369
370   tool("cxx") {
371     rspfile = "{{output}}.rsp"
372     precompiled_header_type = "msvc"
373     pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb"
374
375     # Label names may have spaces so pdbname must be quoted.
376     command = "$env_setup$bin/cl.exe /nologo /showIncludes /FC @$rspfile /c {{source}} /Fo{{output}} /Fd\"$pdbname\""
377     depsformat = "msvc"
378     outputs = [
379       "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj",
380     ]
381     rspfile_content = "{{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}"
382     description = "compile {{source}}"
383   }
384
385   tool("alink") {
386     rspfile = "{{output}}.rsp"
387
388     command = "$env_setup$bin/lib.exe /nologo /ignore:4221 {{arflags}} /OUT:{{output}} @$rspfile"
389     outputs = [
390       # Ignore {{output_extension}} and always use .lib, there's no reason to
391       # allow targets to override this extension on Windows.
392       "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
393     ]
394     default_output_extension = ".lib"
395     default_output_dir = "{{target_out_dir}}"
396
397     # inputs_newline works around a fixed per-line buffer size in the linker.
398     rspfile_content = "{{inputs_newline}}"
399     description = "link {{output}}"
400   }
401
402   tool("link") {
403     exename = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
404     pdbname = "$exename.pdb"
405     rspfile = "$exename.rsp"
406
407     command =
408         "$env_setup$bin/link.exe /nologo /OUT:$exename /PDB:$pdbname @$rspfile"
409
410     default_output_extension = ".exe"
411     default_output_dir = "{{root_out_dir}}"
412     outputs = [
413       exename,
414     ]
415
416     # inputs_newline works around a fixed per-line buffer size in the linker.
417     rspfile_content = "{{inputs_newline}} {{libs}} {{solibs}} {{ldflags}}"
418     description = "link {{output}}"
419   }
420
421   tool("stamp") {
422     command = "cmd.exe /c echo > {{output}}"
423     description = "stamp {{output}}"
424   }
425 }
426
427 toolchain("gcc_like") {
428   lib_switch = "-l"
429   lib_dir_switch = "-L"
430
431   tool("cc") {
432     depfile = "{{output}}.d"
433     command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
434     depsformat = "gcc"
435     outputs = [
436       "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
437     ]
438     description = "compile {{source}}"
439   }
440
441   tool("cxx") {
442     depfile = "{{output}}.d"
443     command = "$cc_wrapper $cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
444     depsformat = "gcc"
445     outputs = [
446       "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
447     ]
448     description = "compile {{source}}"
449   }
450
451   tool("asm") {
452     depfile = "{{output}}.d"
453     command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
454     depsformat = "gcc"
455     outputs = [
456       "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
457     ]
458     description = "compile {{source}}"
459   }
460
461   tool("alink") {
462     command = "rm -f {{output}} && $ar rcs {{output}} {{inputs}}"
463     outputs = [
464       "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
465     ]
466     default_output_extension = ".a"
467     output_prefix = "lib"
468     description = "link {{output}}"
469   }
470
471   tool("solink") {
472     soname = "{{target_output_name}}{{output_extension}}"
473
474     rpath = "-Wl,-soname,$soname"
475     if (is_mac) {
476       rpath = "-Wl,-install_name,@rpath/$soname"
477     }
478
479     command = "$cc_wrapper $cxx -shared {{ldflags}} {{inputs}} {{solibs}} {{libs}} $rpath -o {{output}}"
480     outputs = [
481       "{{root_out_dir}}/$soname",
482     ]
483     output_prefix = "lib"
484     default_output_extension = ".so"
485     description = "link {{output}}"
486   }
487
488   tool("link") {
489     command = "$cc_wrapper $cxx {{ldflags}} {{inputs}} {{solibs}} {{libs}} -o {{output}}"
490     outputs = [
491       "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
492     ]
493     description = "link {{output}}"
494   }
495
496   tool("stamp") {
497     command = "touch {{output}}"
498     description = "stamp {{output}}"
499   }
500
501   tool("copy") {
502     command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
503     description = "copy {{source}} {{output}}"
504   }
505 }