mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 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() { |
mtklein | 349cece | 2016-08-26 08:13:04 -0700 | [diff] [blame] | 7 | ar = "ar" |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 8 | cc = "cc" |
| 9 | cxx = "c++" |
mtklein | 8079ba6 | 2016-08-16 09:31:16 -0700 | [diff] [blame] | 10 | |
mtklein | 7d6fb2c | 2016-08-25 14:50:44 -0700 | [diff] [blame] | 11 | if (is_android) { |
mtklein | 349cece | 2016-08-26 08:13:04 -0700 | [diff] [blame] | 12 | ar = "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin/ar" |
mtklein | 7d6fb2c | 2016-08-25 14:50:44 -0700 | [diff] [blame] | 13 | cc = "$ndk/toolchains/llvm/prebuilt/$ndk_host/bin/clang" |
| 14 | cxx = "$ndk/toolchains/llvm/prebuilt/$ndk_host/bin/clang++" |
| 15 | } |
| 16 | |
herb | b6318bf | 2016-09-16 13:29:57 -0700 | [diff] [blame^] | 17 | windk = "" |
| 18 | |
mtklein | 8079ba6 | 2016-08-16 09:31:16 -0700 | [diff] [blame] | 19 | extra_cflags = "" |
| 20 | extra_cflags_c = "" |
| 21 | extra_cflags_cc = "" |
mtklein | bb35a6a | 2016-09-01 09:15:44 -0700 | [diff] [blame] | 22 | extra_ldflags = "" |
mtklein | cab0bb7 | 2016-08-26 13:43:19 -0700 | [diff] [blame] | 23 | |
| 24 | compiler_prefix = "" |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 25 | } |
| 26 | |
herb | b6318bf | 2016-09-16 13:29:57 -0700 | [diff] [blame^] | 27 | if (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 | ] |
mtklein | 2b3c2a3 | 2016-09-08 08:39:34 -0700 | [diff] [blame] | 40 | } |
herb | b6318bf | 2016-09-16 13:29:57 -0700 | [diff] [blame^] | 41 | config("no_rtti") { } |
halcanary | 19a9720 | 2016-08-03 15:08:04 -0700 | [diff] [blame] | 42 | |
herb | b6318bf | 2016-09-16 13:29:57 -0700 | [diff] [blame^] | 43 | config("debug_symbols") { } |
mtklein | 88a7ac0 | 2016-09-14 11:16:49 -0700 | [diff] [blame] | 44 | |
herb | b6318bf | 2016-09-16 13:29:57 -0700 | [diff] [blame^] | 45 | } 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" ] |
mtklein | 349cece | 2016-08-26 08:13:04 -0700 | [diff] [blame] | 53 | } |
mtklein | 7d6fb2c | 2016-08-25 14:50:44 -0700 | [diff] [blame] | 54 | } |
| 55 | |
herb | b6318bf | 2016-09-16 13:29:57 -0700 | [diff] [blame^] | 56 | config("no_rtti") { |
| 57 | if (sanitize != "ASAN") { # -fsanitize=vptr requires RTTI |
| 58 | cflags_cc = [ "-fno-rtti" ] |
| 59 | } |
mtklein | 7d6fb2c | 2016-08-25 14:50:44 -0700 | [diff] [blame] | 60 | } |
mtklein | 2b3c2a3 | 2016-09-08 08:39:34 -0700 | [diff] [blame] | 61 | |
herb | b6318bf | 2016-09-16 13:29:57 -0700 | [diff] [blame^] | 62 | config("default") { |
| 63 | asmflags = [] |
| 64 | cflags = [ |
| 65 | "-O1", |
| 66 | "-fstrict-aliasing", |
| 67 | "-fPIC", |
| 68 | "-fvisibility=hidden", |
mtklein | 2b3c2a3 | 2016-09-08 08:39:34 -0700 | [diff] [blame] | 69 | |
herb | b6318bf | 2016-09-16 13:29:57 -0700 | [diff] [blame^] | 70 | "-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", |
mtklein | 2b3c2a3 | 2016-09-08 08:39:34 -0700 | [diff] [blame] | 80 | ] |
herb | b6318bf | 2016-09-16 13:29:57 -0700 | [diff] [blame^] | 81 | 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 | } |
mtklein | 2b3c2a3 | 2016-09-08 08:39:34 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 177 | |
herb | b6318bf | 2016-09-16 13:29:57 -0700 | [diff] [blame^] | 178 | config("release") { |
| 179 | cflags = [ "-O3" ] |
| 180 | defines = [ "NDEBUG" ] |
| 181 | } |
| 182 | |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | config("executable") { |
| 186 | if (is_mac) { |
| 187 | ldflags = [ "-Wl,-rpath,@loader_path/." ] |
| 188 | } else if (is_linux) { |
mtklein | a846c72 | 2016-09-15 10:44:15 -0700 | [diff] [blame] | 189 | ldflags = [ |
| 190 | "-rdynamic", |
| 191 | "-Wl,-rpath,\$ORIGIN", |
| 192 | ] |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
herb | b6318bf | 2016-09-16 13:29:57 -0700 | [diff] [blame^] | 196 | toolchain("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 | |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 290 | toolchain("gcc_like") { |
| 291 | lib_switch = "-l" |
| 292 | lib_dir_switch = "-L" |
| 293 | |
| 294 | tool("cc") { |
| 295 | depfile = "{{output}}.d" |
mtklein | cab0bb7 | 2016-08-26 13:43:19 -0700 | [diff] [blame] | 296 | command = "$compiler_prefix $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} $extra_cflags $extra_cflags_c -c {{source}} -o {{output}}" |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 297 | depsformat = "gcc" |
| 298 | outputs = [ |
| 299 | "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o", |
| 300 | ] |
mtklein | cab0bb7 | 2016-08-26 13:43:19 -0700 | [diff] [blame] | 301 | description = |
| 302 | "$compiler_prefix $cc ... $extra_cflags $extra_cflags_c -o {{output}}" |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | tool("cxx") { |
| 306 | depfile = "{{output}}.d" |
mtklein | cab0bb7 | 2016-08-26 13:43:19 -0700 | [diff] [blame] | 307 | command = "$compiler_prefix $cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} $extra_cflags $extra_cflags_cc -c {{source}} -o {{output}}" |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 308 | depsformat = "gcc" |
| 309 | outputs = [ |
| 310 | "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o", |
| 311 | ] |
mtklein | cab0bb7 | 2016-08-26 13:43:19 -0700 | [diff] [blame] | 312 | description = |
| 313 | "$compiler_prefix $cxx ... $extra_cflags $extra_cflags_cc -o {{output}}" |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | tool("asm") { |
| 317 | depfile = "{{output}}.d" |
mtklein | cab0bb7 | 2016-08-26 13:43:19 -0700 | [diff] [blame] | 318 | command = "$compiler_prefix $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}" |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 319 | depsformat = "gcc" |
| 320 | outputs = [ |
| 321 | "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o", |
| 322 | ] |
mtklein | cab0bb7 | 2016-08-26 13:43:19 -0700 | [diff] [blame] | 323 | description = "$compiler_prefix $cc ... -o {{output}}" |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | tool("alink") { |
mtklein | 349cece | 2016-08-26 08:13:04 -0700 | [diff] [blame] | 327 | command = "rm -f {{output}} && $ar rcs {{output}} {{inputs}}" |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 328 | outputs = [ |
mtklein | 5db44aa | 2016-07-29 09:10:31 -0700 | [diff] [blame] | 329 | "{{root_out_dir}}/{{target_output_name}}{{output_extension}}", |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 330 | ] |
| 331 | default_output_extension = ".a" |
| 332 | output_prefix = "lib" |
mtklein | 349cece | 2016-08-26 08:13:04 -0700 | [diff] [blame] | 333 | description = "$ar {{output}} ..." |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 334 | } |
| 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 | |
mtklein | bb35a6a | 2016-09-01 09:15:44 -0700 | [diff] [blame] | 344 | command = "$compiler_prefix $cxx -shared {{ldflags}} {{inputs}} {{solibs}} {{libs}} $rpath $extra_ldflags -o {{output}}" |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 345 | outputs = [ |
| 346 | "{{root_out_dir}}/$soname", |
| 347 | ] |
| 348 | output_prefix = "lib" |
| 349 | default_output_extension = ".so" |
mtklein | 233eb0a | 2016-09-02 11:24:50 -0700 | [diff] [blame] | 350 | description = |
| 351 | "$compiler_prefix $cxx -shared ... $extra_ldflags -o {{output}}" |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | tool("link") { |
mtklein | bb35a6a | 2016-09-01 09:15:44 -0700 | [diff] [blame] | 355 | command = "$compiler_prefix $cxx {{ldflags}} {{inputs}} {{solibs}} {{libs}} $extra_ldflags -o {{output}}" |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 356 | outputs = [ |
| 357 | "{{root_out_dir}}/{{target_output_name}}{{output_extension}}", |
| 358 | ] |
mtklein | 233eb0a | 2016-09-02 11:24:50 -0700 | [diff] [blame] | 359 | description = "$compiler_prefix $cxx ... $extra_ldflags -o {{output}}" |
mtklein | 7fbfbbe | 2016-07-21 12:25:45 -0700 | [diff] [blame] | 360 | } |
| 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 | } |