1declare_args() { 2 host_ar = ar 3 host_cc = cc 4 host_cxx = cxx 5 6 if (is_android) { 7 if (host_os == "win") { 8 target_ar = "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin/ar.exe" 9 target_cc = "$ndk/toolchains/llvm/prebuilt/$ndk_host/bin/clang.exe" 10 target_cxx = "$ndk/toolchains/llvm/prebuilt/$ndk_host/bin/clang++.exe" 11 } else { 12 target_ar = "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin/ar" 13 target_cc = "$ndk/toolchains/llvm/prebuilt/$ndk_host/bin/clang" 14 target_cxx = "$ndk/toolchains/llvm/prebuilt/$ndk_host/bin/clang++" 15 } 16 } else { 17 target_ar = ar 18 target_cc = cc 19 target_cxx = cxx 20 } 21 22 cc_wrapper = "" 23} 24 25if (host_os == "win") { 26 python = "python.bat" 27 stamp = "cmd.exe /c echo >" 28} else { 29 python = "python" 30 stamp = "touch" 31} 32 33toolchain("msvc") { 34 lib_dir_switch = "/LIBPATH:" 35 36 if (msvc == 2015) { 37 bin = "$windk/VC/bin/amd64" 38 env_setup = "" 39 if (target_cpu == "x86") { 40 bin += "_x86" 41 env_setup = "cmd /c $windk/win_sdk/bin/SetEnv.cmd /x86 && " 42 } 43 } else { 44 bin = "$windk/VC/Tools/MSVC/14.10.25017/bin/HostX64/$target_cpu" 45 env_setup = "" 46 if (target_cpu == "x86") { 47 print("Be sure to run") 48 print("\"$windk/VC/Auxiliary/Build/vcvarsall.bat\" amd64_x86") 49 print("to set up your environment before running ninja.") 50 } 51 } 52 53 tool("asm") { 54 _ml = "ml" 55 if (target_cpu == "x64") { 56 _ml += "64" 57 } 58 command = "$env_setup$bin/$_ml.exe /nologo /c /Fo {{output}} {{source}}" 59 outputs = [ 60 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj", 61 ] 62 description = "assemble {{source}}" 63 } 64 65 tool("cc") { 66 rspfile = "{{output}}.rsp" 67 precompiled_header_type = "msvc" 68 pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb" 69 70 # Label names may have spaces so pdbname must be quoted. 71 command = "$env_setup$bin/cl.exe /nologo /showIncludes /FC @$rspfile /c {{source}} /Fo{{output}} /Fd\"$pdbname\"" 72 depsformat = "msvc" 73 outputs = [ 74 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj", 75 ] 76 rspfile_content = "{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}" 77 description = "compile {{source}}" 78 } 79 80 tool("cxx") { 81 rspfile = "{{output}}.rsp" 82 precompiled_header_type = "msvc" 83 pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb" 84 85 # Label names may have spaces so pdbname must be quoted. 86 command = "$env_setup$bin/cl.exe /nologo /showIncludes /FC @$rspfile /c {{source}} /Fo{{output}} /Fd\"$pdbname\"" 87 depsformat = "msvc" 88 outputs = [ 89 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj", 90 ] 91 rspfile_content = "{{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}" 92 description = "compile {{source}}" 93 } 94 95 tool("alink") { 96 rspfile = "{{output}}.rsp" 97 98 command = "$env_setup$bin/lib.exe /nologo /ignore:4221 {{arflags}} /OUT:{{output}} @$rspfile" 99 outputs = [ 100 # Ignore {{output_extension}} and always use .lib, there's no reason to 101 # allow targets to override this extension on Windows. 102 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}", 103 ] 104 default_output_extension = ".lib" 105 default_output_dir = "{{target_out_dir}}" 106 107 # inputs_newline works around a fixed per-line buffer size in the linker. 108 rspfile_content = "{{inputs_newline}}" 109 description = "link {{output}}" 110 } 111 112 tool("solink") { 113 dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}" 114 libname = "${dllname}.lib" 115 pdbname = "${dllname}.pdb" 116 rspfile = "${dllname}.rsp" 117 118 command = "$env_setup$bin/link.exe /nologo /IMPLIB:$libname /DLL /OUT:$dllname /PDB:$pdbname @$rspfile" 119 outputs = [ 120 dllname, 121 libname, 122 pdbname, 123 ] 124 default_output_extension = ".dll" 125 default_output_dir = "{{root_out_dir}}" 126 127 link_output = libname 128 depend_output = libname 129 runtime_outputs = [ 130 dllname, 131 pdbname, 132 ] 133 134 # I don't quite understand this. Aping Chrome's toolchain/win/BUILD.gn. 135 restat = true 136 137 # inputs_newline works around a fixed per-line buffer size in the linker. 138 rspfile_content = "{{inputs_newline}} {{libs}} {{solibs}} {{ldflags}}" 139 description = "link {{output}}" 140 } 141 142 tool("link") { 143 exename = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}" 144 pdbname = "$exename.pdb" 145 rspfile = "$exename.rsp" 146 147 command = 148 "$env_setup$bin/link.exe /nologo /OUT:$exename /PDB:$pdbname @$rspfile" 149 150 default_output_extension = ".exe" 151 default_output_dir = "{{root_out_dir}}" 152 outputs = [ 153 exename, 154 ] 155 156 # inputs_newline works around a fixed per-line buffer size in the linker. 157 rspfile_content = "{{inputs_newline}} {{libs}} {{solibs}} {{ldflags}}" 158 description = "link {{output}}" 159 } 160 161 tool("stamp") { 162 command = "$stamp {{output}}" 163 description = "stamp {{output}}" 164 } 165 166 tool("copy") { 167 cp_py = rebase_path("../cp.py") 168 command = "$python $cp_py {{source}} {{output}}" 169 description = "copy {{source}} {{output}}" 170 } 171} 172 173template("gcc_like_toolchain") { 174 toolchain(target_name) { 175 ar = invoker.ar 176 cc = invoker.cc 177 cxx = invoker.cxx 178 lib_switch = "-l" 179 lib_dir_switch = "-L" 180 181 tool("cc") { 182 depfile = "{{output}}.d" 183 command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}" 184 depsformat = "gcc" 185 outputs = [ 186 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o", 187 ] 188 description = "compile {{source}}" 189 } 190 191 tool("cxx") { 192 depfile = "{{output}}.d" 193 command = "$cc_wrapper $cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}" 194 depsformat = "gcc" 195 outputs = [ 196 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o", 197 ] 198 description = "compile {{source}}" 199 } 200 201 tool("objc") { 202 depfile = "{{output}}.d" 203 command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_objc}} -c {{source}} -o {{output}}" 204 depsformat = "gcc" 205 outputs = [ 206 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o", 207 ] 208 description = "compile {{source}}" 209 } 210 211 tool("objcxx") { 212 depfile = "{{output}}.d" 213 command = "$cc_wrapper $cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} {{cflags_objcc}} -c {{source}} -o {{output}}" 214 depsformat = "gcc" 215 outputs = [ 216 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o", 217 ] 218 description = "compile {{source}}" 219 } 220 221 tool("asm") { 222 depfile = "{{output}}.d" 223 command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}" 224 depsformat = "gcc" 225 outputs = [ 226 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o", 227 ] 228 description = "assemble {{source}}" 229 } 230 231 tool("alink") { 232 rspfile = "{{output}}.rsp" 233 rspfile_content = "{{inputs}}" 234 ar_py = rebase_path("../ar.py") 235 command = "$python $ar_py $ar {{output}} $rspfile" 236 outputs = [ 237 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}", 238 ] 239 default_output_extension = ".a" 240 output_prefix = "lib" 241 description = "link {{output}}" 242 } 243 244 tool("solink") { 245 soname = "{{target_output_name}}{{output_extension}}" 246 247 rpath = "-Wl,-soname,$soname" 248 if (is_mac) { 249 rpath = "-Wl,-install_name,@rpath/$soname" 250 } 251 252 command = "$cc_wrapper $cxx -shared {{ldflags}} {{inputs}} {{solibs}} {{libs}} $rpath -o {{output}}" 253 outputs = [ 254 "{{root_out_dir}}/$soname", 255 ] 256 output_prefix = "lib" 257 default_output_extension = ".so" 258 description = "link {{output}}" 259 } 260 261 tool("link") { 262 command = "$cc_wrapper $cxx {{ldflags}} {{inputs}} {{solibs}} {{libs}} -o {{output}}" 263 outputs = [ 264 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}", 265 ] 266 description = "link {{output}}" 267 } 268 269 tool("stamp") { 270 command = "$stamp {{output}}" 271 description = "stamp {{output}}" 272 } 273 274 tool("copy") { 275 cp_py = rebase_path("../cp.py") 276 command = "$python $cp_py {{source}} {{output}}" 277 description = "copy {{source}} {{output}}" 278 } 279 280 toolchain_args = { 281 current_cpu = invoker.cpu 282 current_os = invoker.os 283 } 284 } 285} 286 287gcc_like_toolchain("gcc_like") { 288 cpu = current_cpu 289 os = current_os 290 ar = target_ar 291 cc = target_cc 292 cxx = target_cxx 293} 294 295gcc_like_toolchain("gcc_like_host") { 296 cpu = host_cpu 297 os = host_os 298 ar = host_ar 299 cc = host_cc 300 cxx = host_cxx 301} 302