blob: 13fdb907faee4d8d5a761843e57db9391941bb25 [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
mtklein88a7ac02016-09-14 11:16:49 -070031config("debug_symbols") {
32 # It's annoying to wait for full debug symbols to push over
33 # to Android devices. -gline-tables-only is a lot slimmer.
34 if (is_android) {
35 cflags = [ "-gline-tables-only" ]
36 } else {
37 cflags = [ "-g" ]
38 }
39}
40
mtklein7fbfbbe2016-07-21 12:25:45 -070041config("default") {
42 cflags = [
mtklein5e3a6302016-09-07 19:04:36 -070043 "-O1",
mtklein7fbfbbe2016-07-21 12:25:45 -070044 "-fstrict-aliasing",
45 "-fPIC",
mtklein8796ff12016-07-27 14:59:08 -070046 "-fvisibility=hidden",
mtklein7fbfbbe2016-07-21 12:25:45 -070047
48 "-Werror",
49 "-Wall",
50 "-Wextra",
51 "-Winit-self",
52 "-Wpointer-arith",
53 "-Wsign-compare",
54 "-Wvla",
55
56 "-Wno-deprecated-declarations",
57 "-Wno-unused-parameter",
58 ]
59 cflags_cc = [
60 "-std=c++11",
61 "-fno-exceptions",
mtklein7fbfbbe2016-07-21 12:25:45 -070062 "-fno-threadsafe-statics",
mtklein8796ff12016-07-27 14:59:08 -070063 "-fvisibility-inlines-hidden",
mtklein7fbfbbe2016-07-21 12:25:45 -070064
65 "-Wnon-virtual-dtor",
66 ]
mtklein2b3c2a32016-09-08 08:39:34 -070067 ldflags = []
68
mtklein7d6fb2c2016-08-25 14:50:44 -070069 if (current_cpu == "arm") {
mtklein349cece2016-08-26 08:13:04 -070070 cflags += [
71 "-march=armv7-a",
72 "-mfpu=neon",
73 "-mthumb",
74 ]
75 } else if (current_cpu == "mipsel") {
76 cflags += [
77 "-march=mips32r2",
78 "-mdspr2",
79 ]
mtklein7d6fb2c2016-08-25 14:50:44 -070080 }
81
82 if (is_android) {
mtklein349cece2016-08-26 08:13:04 -070083 asmflags = [
84 "--target=$ndk_target",
85 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
86 ]
mtklein7d6fb2c2016-08-25 14:50:44 -070087 cflags += [
88 "--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 ]
92 cflags_cc += [
93 "-isystem$ndk/sources/android/support/include",
94 "-isystem$ndk/sources/cxx-stl/llvm-libc++/libcxx/include",
95 ]
mtklein2b3c2a32016-09-08 08:39:34 -070096 ldflags += [
mtklein7d6fb2c2016-08-25 14:50:44 -070097 "--sysroot=$ndk/platforms/$ndk_platform",
98 "--target=$ndk_target",
mtklein349cece2016-08-26 08:13:04 -070099 "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
mtklein7d6fb2c2016-08-25 14:50:44 -0700100 "-pie",
101 ]
102 lib_dirs = [
103 "$ndk/sources/cxx-stl/llvm-libc++/libs/$ndk_stdlib",
mtklein349cece2016-08-26 08:13:04 -0700104 "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/lib/gcc/$ndk_target/4.9.x",
mtklein7d6fb2c2016-08-25 14:50:44 -0700105 ]
106 libs = [
107 # Order matters here! Keep these three in exactly this order.
108 "c++_static",
109 "c++abi",
110 "android_support",
111 ]
mtklein349cece2016-08-26 08:13:04 -0700112 if (target_cpu == "arm") {
113 libs += [ "unwind" ]
114 }
mtklein7d6fb2c2016-08-25 14:50:44 -0700115 }
116
117 if (is_linux) {
118 libs = [ "pthread" ]
119 }
mtklein2b3c2a32016-09-08 08:39:34 -0700120
121 if (sanitize != "") {
122 # You can either pass the sanitizers directly, e.g. "address,undefined",
123 # or pass one of the couple common aliases used by the bots.
124 sanitizers = sanitize
125 if (sanitize == "ASAN") {
126 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"
127 } else if (sanitize == "TSAN") {
128 sanitizers = "thread"
129 } else if (sanitize == "MSAN") {
130 sanitizers = "memory"
131 }
132
133 cflags += [
134 "-fsanitize=$sanitizers",
135 "-fno-sanitize-recover=$sanitizers",
136 "-fsanitize-blacklist=" + rebase_path("../tools/xsan.blacklist"),
137 ]
138 ldflags += [ "-fsanitize=$sanitizers" ]
139 if (sanitizers == "memory") {
140 cflags += [ "-fsanitize-memory-track-origins" ]
141 cflags_cc += [ "-stdlib=libc++" ]
142 ldflags += [ "-stdlib=libc++" ]
143 }
144 }
mtklein7fbfbbe2016-07-21 12:25:45 -0700145}
146
147config("release") {
mtklein79578722016-08-29 10:23:15 -0700148 cflags = [ "-O3" ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700149 defines = [ "NDEBUG" ]
150}
151
152config("executable") {
153 if (is_mac) {
154 ldflags = [ "-Wl,-rpath,@loader_path/." ]
155 } else if (is_linux) {
mtkleina846c722016-09-15 10:44:15 -0700156 ldflags = [
157 "-rdynamic",
158 "-Wl,-rpath,\$ORIGIN",
159 ]
mtklein7fbfbbe2016-07-21 12:25:45 -0700160 }
161}
162
163toolchain("gcc_like") {
164 lib_switch = "-l"
165 lib_dir_switch = "-L"
166
167 tool("cc") {
168 depfile = "{{output}}.d"
mtkleincab0bb72016-08-26 13:43:19 -0700169 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 -0700170 depsformat = "gcc"
171 outputs = [
172 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
173 ]
mtkleincab0bb72016-08-26 13:43:19 -0700174 description =
175 "$compiler_prefix $cc ... $extra_cflags $extra_cflags_c -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700176 }
177
178 tool("cxx") {
179 depfile = "{{output}}.d"
mtkleincab0bb72016-08-26 13:43:19 -0700180 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 -0700181 depsformat = "gcc"
182 outputs = [
183 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
184 ]
mtkleincab0bb72016-08-26 13:43:19 -0700185 description =
186 "$compiler_prefix $cxx ... $extra_cflags $extra_cflags_cc -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700187 }
188
189 tool("asm") {
190 depfile = "{{output}}.d"
mtkleincab0bb72016-08-26 13:43:19 -0700191 command = "$compiler_prefix $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700192 depsformat = "gcc"
193 outputs = [
194 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
195 ]
mtkleincab0bb72016-08-26 13:43:19 -0700196 description = "$compiler_prefix $cc ... -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700197 }
198
199 tool("alink") {
mtklein349cece2016-08-26 08:13:04 -0700200 command = "rm -f {{output}} && $ar rcs {{output}} {{inputs}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700201 outputs = [
mtklein5db44aa2016-07-29 09:10:31 -0700202 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
mtklein7fbfbbe2016-07-21 12:25:45 -0700203 ]
204 default_output_extension = ".a"
205 output_prefix = "lib"
mtklein349cece2016-08-26 08:13:04 -0700206 description = "$ar {{output}} ..."
mtklein7fbfbbe2016-07-21 12:25:45 -0700207 }
208
209 tool("solink") {
210 soname = "{{target_output_name}}{{output_extension}}"
211
212 rpath = "-Wl,-soname,$soname"
213 if (is_mac) {
214 rpath = "-Wl,-install_name,@rpath/$soname"
215 }
216
mtkleinbb35a6a2016-09-01 09:15:44 -0700217 command = "$compiler_prefix $cxx -shared {{ldflags}} {{inputs}} {{solibs}} {{libs}} $rpath $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700218 outputs = [
219 "{{root_out_dir}}/$soname",
220 ]
221 output_prefix = "lib"
222 default_output_extension = ".so"
mtklein233eb0a2016-09-02 11:24:50 -0700223 description =
224 "$compiler_prefix $cxx -shared ... $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700225 }
226
227 tool("link") {
mtkleinbb35a6a2016-09-01 09:15:44 -0700228 command = "$compiler_prefix $cxx {{ldflags}} {{inputs}} {{solibs}} {{libs}} $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700229 outputs = [
230 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
231 ]
mtklein233eb0a2016-09-02 11:24:50 -0700232 description = "$compiler_prefix $cxx ... $extra_ldflags -o {{output}}"
mtklein7fbfbbe2016-07-21 12:25:45 -0700233 }
234
235 tool("stamp") {
236 command = "touch {{output}}"
237 }
238
239 tool("copy") {
240 command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
241 }
242}