blob: c5db0a96608b0760588e559d24a6a44ac4182f09 [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
mtklein8079ba62016-08-16 09:31:16 -070017 extra_cflags = ""
18 extra_cflags_c = ""
19 extra_cflags_cc = ""
mtkleinbb35a6a2016-09-01 09:15:44 -070020 extra_ldflags = ""
mtkleincab0bb72016-08-26 13:43:19 -070021
22 compiler_prefix = ""
mtklein7fbfbbe2016-07-21 12:25:45 -070023}
24
halcanary19a97202016-08-03 15:08:04 -070025config("no_rtti") {
mtklein2b3c2a32016-09-08 08:39:34 -070026 if (sanitize != "ASAN") { # -fsanitize=vptr requires RTTI
27 cflags_cc = [ "-fno-rtti" ]
28 }
halcanary19a97202016-08-03 15:08:04 -070029}
30
mtklein7fbfbbe2016-07-21 12:25:45 -070031config("default") {
32 cflags = [
mtklein5e3a6302016-09-07 19:04:36 -070033 "-O1",
mtklein7fbfbbe2016-07-21 12:25:45 -070034 "-g",
35 "-fstrict-aliasing",
36 "-fPIC",
mtklein8796ff12016-07-27 14:59:08 -070037 "-fvisibility=hidden",
mtklein7fbfbbe2016-07-21 12:25:45 -070038
39 "-Werror",
40 "-Wall",
41 "-Wextra",
42 "-Winit-self",
43 "-Wpointer-arith",
44 "-Wsign-compare",
45 "-Wvla",
46
47 "-Wno-deprecated-declarations",
48 "-Wno-unused-parameter",
49 ]
50 cflags_cc = [
51 "-std=c++11",
52 "-fno-exceptions",
mtklein7fbfbbe2016-07-21 12:25:45 -070053 "-fno-threadsafe-statics",
mtklein8796ff12016-07-27 14:59:08 -070054 "-fvisibility-inlines-hidden",
mtklein7fbfbbe2016-07-21 12:25:45 -070055
56 "-Wnon-virtual-dtor",
57 ]
mtklein2b3c2a32016-09-08 08:39:34 -070058 ldflags = []
59
mtklein7d6fb2c2016-08-25 14:50:44 -070060 if (current_cpu == "arm") {
mtklein349cece2016-08-26 08:13:04 -070061 cflags += [
62 "-march=armv7-a",
63 "-mfpu=neon",
64 "-mthumb",
65 ]
66 } else if (current_cpu == "mipsel") {
67 cflags += [
68 "-march=mips32r2",
69 "-mdspr2",
70 ]
mtklein7d6fb2c2016-08-25 14:50:44 -070071 }
72
73 if (is_android) {
mtklein349cece2016-08-26 08:13:04 -070074 asmflags = [
75 "--target=$ndk_target",
76 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
77 ]
mtklein7d6fb2c2016-08-25 14:50:44 -070078 cflags += [
79 "--sysroot=$ndk/platforms/$ndk_platform",
80 "--target=$ndk_target",
mtklein349cece2016-08-26 08:13:04 -070081 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
mtklein7d6fb2c2016-08-25 14:50:44 -070082 ]
83 cflags_cc += [
84 "-isystem$ndk/sources/android/support/include",
85 "-isystem$ndk/sources/cxx-stl/llvm-libc++/libcxx/include",
86 ]
mtklein2b3c2a32016-09-08 08:39:34 -070087 ldflags += [
mtklein7d6fb2c2016-08-25 14:50:44 -070088 "--sysroot=$ndk/platforms/$ndk_platform",
89 "--target=$ndk_target",
mtklein349cece2016-08-26 08:13:04 -070090 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
mtklein7d6fb2c2016-08-25 14:50:44 -070091 "-pie",
92 ]
93 lib_dirs = [
94 "$ndk/sources/cxx-stl/llvm-libc++/libs/$ndk_stdlib",
mtklein349cece2016-08-26 08:13:04 -070095 "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/lib/gcc/$ndk_target/4.9.x",
mtklein7d6fb2c2016-08-25 14:50:44 -070096 ]
97 libs = [
98 # Order matters here! Keep these three in exactly this order.
99 "c++_static",
100 "c++abi",
101 "android_support",
102 ]
mtklein349cece2016-08-26 08:13:04 -0700103 if (target_cpu == "arm") {
104 libs += [ "unwind" ]
105 }
mtklein7d6fb2c2016-08-25 14:50:44 -0700106 }
107
108 if (is_linux) {
109 libs = [ "pthread" ]
110 }
mtklein2b3c2a32016-09-08 08:39:34 -0700111
112 if (sanitize != "") {
113 # You can either pass the sanitizers directly, e.g. "address,undefined",
114 # or pass one of the couple common aliases used by the bots.
115 sanitizers = sanitize
116 if (sanitize == "ASAN") {
117 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"
118 } else if (sanitize == "TSAN") {
119 sanitizers = "thread"
120 } else if (sanitize == "MSAN") {
121 sanitizers = "memory"
122 }
123
124 cflags += [
125 "-fsanitize=$sanitizers",
126 "-fno-sanitize-recover=$sanitizers",
127 "-fsanitize-blacklist=" + rebase_path("../tools/xsan.blacklist"),
128 ]
129 ldflags += [ "-fsanitize=$sanitizers" ]
130 if (sanitizers == "memory") {
131 cflags += [ "-fsanitize-memory-track-origins" ]
132 cflags_cc += [ "-stdlib=libc++" ]
133 ldflags += [ "-stdlib=libc++" ]
134 }
135 }
mtklein7fbfbbe2016-07-21 12:25:45 -0700136}
137
138config("release") {
mtklein79578722016-08-29 10:23:15 -0700139 cflags = [ "-O3" ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700140 defines = [ "NDEBUG" ]
141}
142
143config("executable") {
144 if (is_mac) {
145 ldflags = [ "-Wl,-rpath,@loader_path/." ]
146 } else if (is_linux) {
147 ldflags = [ "-Wl,-rpath,\$ORIGIN" ]
148 }
149}
150
151toolchain("gcc_like") {
152 lib_switch = "-l"
153 lib_dir_switch = "-L"
154
155 tool("cc") {
156 depfile = "{{output}}.d"
mtkleincab0bb72016-08-26 13:43:19 -0700157 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 -0700158 depsformat = "gcc"
159 outputs = [
160 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
161 ]
mtkleincab0bb72016-08-26 13:43:19 -0700162 description =
163 "$compiler_prefix $cc ... $extra_cflags $extra_cflags_c -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700164 }
165
166 tool("cxx") {
167 depfile = "{{output}}.d"
mtkleincab0bb72016-08-26 13:43:19 -0700168 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 -0700169 depsformat = "gcc"
170 outputs = [
171 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
172 ]
mtkleincab0bb72016-08-26 13:43:19 -0700173 description =
174 "$compiler_prefix $cxx ... $extra_cflags $extra_cflags_cc -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700175 }
176
177 tool("asm") {
178 depfile = "{{output}}.d"
mtkleincab0bb72016-08-26 13:43:19 -0700179 command = "$compiler_prefix $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700180 depsformat = "gcc"
181 outputs = [
182 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
183 ]
mtkleincab0bb72016-08-26 13:43:19 -0700184 description = "$compiler_prefix $cc ... -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700185 }
186
187 tool("alink") {
mtklein349cece2016-08-26 08:13:04 -0700188 command = "rm -f {{output}} && $ar rcs {{output}} {{inputs}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700189 outputs = [
mtklein5db44aa2016-07-29 09:10:31 -0700190 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
mtklein7fbfbbe2016-07-21 12:25:45 -0700191 ]
192 default_output_extension = ".a"
193 output_prefix = "lib"
mtklein349cece2016-08-26 08:13:04 -0700194 description = "$ar {{output}} ..."
mtklein7fbfbbe2016-07-21 12:25:45 -0700195 }
196
197 tool("solink") {
198 soname = "{{target_output_name}}{{output_extension}}"
199
200 rpath = "-Wl,-soname,$soname"
201 if (is_mac) {
202 rpath = "-Wl,-install_name,@rpath/$soname"
203 }
204
mtkleinbb35a6a2016-09-01 09:15:44 -0700205 command = "$compiler_prefix $cxx -shared {{ldflags}} {{inputs}} {{solibs}} {{libs}} $rpath $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700206 outputs = [
207 "{{root_out_dir}}/$soname",
208 ]
209 output_prefix = "lib"
210 default_output_extension = ".so"
mtklein233eb0a2016-09-02 11:24:50 -0700211 description =
212 "$compiler_prefix $cxx -shared ... $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700213 }
214
215 tool("link") {
mtkleinbb35a6a2016-09-01 09:15:44 -0700216 command = "$compiler_prefix $cxx {{ldflags}} {{inputs}} {{solibs}} {{libs}} $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700217 outputs = [
218 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
219 ]
mtklein233eb0a2016-09-02 11:24:50 -0700220 description = "$compiler_prefix $cxx ... $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700221 }
222
223 tool("stamp") {
224 command = "touch {{output}}"
225 }
226
227 tool("copy") {
228 command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
229 }
230}