blob: cf6437b99487b0302c1ed2cc3524b7edd1e5524d [file] [log] [blame]
Ethan Nicholas762466e2017-06-29 10:03:38 -04001declare_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") {
Ethan Nicholas762466e2017-06-29 10:03:38 -040026 stamp = "cmd.exe /c echo >"
27} else {
Ethan Nicholas762466e2017-06-29 10:03:38 -040028 stamp = "touch"
29}
30
31toolchain("msvc") {
32 lib_dir_switch = "/LIBPATH:"
33
34 if (msvc == 2015) {
Kaloyan Donevf7466bd2018-03-16 14:45:35 +020035 if (target_cpu == "x86") {
Brian Osmanb2a00252018-12-10 10:10:55 -050036 bin = "$win_vc/bin"
Kaloyan Donevf7466bd2018-03-16 14:45:35 +020037 } else {
Brian Osmanb2a00252018-12-10 10:10:55 -050038 bin = "$win_vc/bin/amd64"
Kaloyan Donevf7466bd2018-03-16 14:45:35 +020039 }
Ethan Nicholas762466e2017-06-29 10:03:38 -040040 } else {
Brian Osmanb2a00252018-12-10 10:10:55 -050041 bin = "$win_vc/Tools/MSVC/$win_toolchain_version/bin/HostX64/$target_cpu"
Brian Osman852ca312017-12-07 16:16:21 -050042 }
43
44 env_setup = ""
45 if (target_cpu == "x86") {
46 # Toolchain asset includes a script that configures for x86 building.
47 # We don't support x86 builds with local MSVC installations.
48 env_setup = "cmd /c $win_sdk/bin/SetEnv.cmd /x86 && "
Brian Osmana0982652018-12-07 15:30:43 -050049 } else if (target_cpu == "arm64") {
50 # ARM64 compiler is incomplete - it relies on DLLs located in the host toolchain directory.
Brian Osmanb2a00252018-12-10 10:10:55 -050051 env_setup = "cmd /C set \"PATH=%PATH%;$win_vc\\Tools\\MSVC\\$win_toolchain_version\\bin\\HostX64\\x64\" && "
Ethan Nicholas762466e2017-06-29 10:03:38 -040052 }
53
Ben Wagnerdf574042018-03-13 15:11:14 -040054 cl_m32_flag = ""
Brian Salomon42ec7d72018-12-10 07:51:54 -050055
Mike Kleinc722f792017-07-31 11:57:21 -040056 if (clang_win != "") {
Ben Wagnerdf574042018-03-13 15:11:14 -040057 if (target_cpu == "x86") {
58 # cl.exe knows implicitly by the choice of executable that it's targeting
59 # x86, but clang-cl.exe needs to be told when targeting non-host
60 # platforms. (All our builders are x86-64, so x86 is always non-host.)
61 cl_m32_flag = "-m32"
62 }
Brian Salomon42ec7d72018-12-10 07:51:54 -050063 if (host_os == "win") {
Mike Kleina6e0c2b2018-12-10 08:48:03 -050064 cl = "\"$clang_win/bin/clang-cl.exe\""
65 lib = "\"$clang_win/bin/lld-link.exe\" /lib"
66 link = "\"$clang_win/bin/lld-link.exe\""
Brian Salomon42ec7d72018-12-10 07:51:54 -050067 } else {
Brian Osmanb2a00252018-12-10 10:10:55 -050068 cl = "\"$clang_win/bin/clang-cl\""
69 lib = "\"$clang_win/bin/lld-link\" /lib"
70 link = "\"$clang_win/bin/lld-link\""
Brian Salomon42ec7d72018-12-10 07:51:54 -050071 }
Mike Kleinc722f792017-07-31 11:57:21 -040072 } else {
Mike Kleina6e0c2b2018-12-10 08:48:03 -050073 cl = "\"$bin/cl.exe\""
74 lib = "\"$bin/lib.exe\""
75 link = "\"$bin/link.exe\""
Mike Kleinc722f792017-07-31 11:57:21 -040076 }
77
Ethan Nicholas762466e2017-06-29 10:03:38 -040078 tool("asm") {
79 _ml = "ml"
80 if (target_cpu == "x64") {
81 _ml += "64"
82 }
Brian Osmana0982652018-12-07 15:30:43 -050083 command = "$env_setup \"$bin/$_ml.exe\" {{asmflags}} /nologo /c /Fo {{output}} {{source}}"
Ethan Nicholas762466e2017-06-29 10:03:38 -040084 outputs = [
85 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj",
86 ]
87 description = "assemble {{source}}"
88 }
89
90 tool("cc") {
Ethan Nicholas762466e2017-06-29 10:03:38 -040091 precompiled_header_type = "msvc"
92 pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb"
93
94 # Label names may have spaces so pdbname must be quoted.
Mike Kleina6e0c2b2018-12-10 08:48:03 -050095 command = "$env_setup $cc_wrapper $cl /nologo /showIncludes /FC {{defines}} {{include_dirs}} {{cflags}} $cl_m32_flag {{cflags_c}} /c {{source}} /Fo{{output}} /Fd\"$pdbname\""
Ethan Nicholas762466e2017-06-29 10:03:38 -040096 depsformat = "msvc"
97 outputs = [
98 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj",
99 ]
Ethan Nicholas762466e2017-06-29 10:03:38 -0400100 description = "compile {{source}}"
101 }
102
103 tool("cxx") {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400104 precompiled_header_type = "msvc"
105 pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb"
106
107 # Label names may have spaces so pdbname must be quoted.
Mike Kleina6e0c2b2018-12-10 08:48:03 -0500108 command = "$env_setup $cc_wrapper $cl /nologo /showIncludes /FC {{defines}} {{include_dirs}} {{cflags}} $cl_m32_flag {{cflags_cc}} /c {{source}} /Fo{{output}} /Fd\"$pdbname\""
Ethan Nicholas762466e2017-06-29 10:03:38 -0400109 depsformat = "msvc"
110 outputs = [
111 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj",
112 ]
Ethan Nicholas762466e2017-06-29 10:03:38 -0400113 description = "compile {{source}}"
114 }
115
116 tool("alink") {
117 rspfile = "{{output}}.rsp"
118
Brian Salomon42ec7d72018-12-10 07:51:54 -0500119 command = "$env_setup $lib /nologo /ignore:4221 {{arflags}} /OUT:{{output}} @$rspfile"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400120 outputs = [
121 # Ignore {{output_extension}} and always use .lib, there's no reason to
122 # allow targets to override this extension on Windows.
123 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
124 ]
125 default_output_extension = ".lib"
126 default_output_dir = "{{target_out_dir}}"
127
128 # inputs_newline works around a fixed per-line buffer size in the linker.
129 rspfile_content = "{{inputs_newline}}"
130 description = "link {{output}}"
131 }
132
133 tool("solink") {
134 dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
135 libname = "${dllname}.lib"
136 pdbname = "${dllname}.pdb"
137 rspfile = "${dllname}.rsp"
138
Brian Salomon42ec7d72018-12-10 07:51:54 -0500139 command = "$env_setup $link /nologo /IMPLIB:$libname /DLL /OUT:$dllname /PDB:$pdbname @$rspfile"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400140 outputs = [
141 dllname,
142 libname,
143 pdbname,
144 ]
145 default_output_extension = ".dll"
146 default_output_dir = "{{root_out_dir}}"
147
148 link_output = libname
149 depend_output = libname
150 runtime_outputs = [
151 dllname,
152 pdbname,
153 ]
154
155 # I don't quite understand this. Aping Chrome's toolchain/win/BUILD.gn.
156 restat = true
157
158 # inputs_newline works around a fixed per-line buffer size in the linker.
159 rspfile_content = "{{inputs_newline}} {{libs}} {{solibs}} {{ldflags}}"
160 description = "link {{output}}"
161 }
162
163 tool("link") {
164 exename = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
165 pdbname = "$exename.pdb"
166 rspfile = "$exename.rsp"
167
Brian Salomon42ec7d72018-12-10 07:51:54 -0500168 command = "$env_setup $link /nologo /OUT:$exename /PDB:$pdbname @$rspfile"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400169 default_output_extension = ".exe"
170 default_output_dir = "{{root_out_dir}}"
171 outputs = [
172 exename,
173 ]
174
175 # inputs_newline works around a fixed per-line buffer size in the linker.
176 rspfile_content = "{{inputs_newline}} {{libs}} {{solibs}} {{ldflags}}"
177 description = "link {{output}}"
178 }
179
180 tool("stamp") {
181 command = "$stamp {{output}}"
182 description = "stamp {{output}}"
183 }
184
185 tool("copy") {
186 cp_py = rebase_path("../cp.py")
Ben Wagner96aa5352018-06-19 10:34:32 -0400187 command = "python $cp_py {{source}} {{output}}"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400188 description = "copy {{source}} {{output}}"
189 }
190}
191
192template("gcc_like_toolchain") {
193 toolchain(target_name) {
194 ar = invoker.ar
195 cc = invoker.cc
196 cxx = invoker.cxx
197 lib_switch = "-l"
198 lib_dir_switch = "-L"
199
200 tool("cc") {
201 depfile = "{{output}}.d"
202 command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
203 depsformat = "gcc"
204 outputs = [
205 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
206 ]
207 description = "compile {{source}}"
208 }
209
210 tool("cxx") {
211 depfile = "{{output}}.d"
212 command = "$cc_wrapper $cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
213 depsformat = "gcc"
214 outputs = [
215 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
216 ]
217 description = "compile {{source}}"
218 }
219
220 tool("objc") {
221 depfile = "{{output}}.d"
222 command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_objc}} -c {{source}} -o {{output}}"
223 depsformat = "gcc"
224 outputs = [
225 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
226 ]
227 description = "compile {{source}}"
228 }
229
230 tool("objcxx") {
231 depfile = "{{output}}.d"
Greg Daniel6b7e0e22017-07-12 16:21:09 -0400232 command = "$cc_wrapper $cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} {{cflags_objcc}} -c {{source}} -o {{output}}"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400233 depsformat = "gcc"
234 outputs = [
235 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
236 ]
237 description = "compile {{source}}"
238 }
239
240 tool("asm") {
241 depfile = "{{output}}.d"
242 command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
243 depsformat = "gcc"
244 outputs = [
245 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
246 ]
247 description = "assemble {{source}}"
248 }
249
250 tool("alink") {
251 rspfile = "{{output}}.rsp"
252 rspfile_content = "{{inputs}}"
253 ar_py = rebase_path("../ar.py")
Ben Wagner96aa5352018-06-19 10:34:32 -0400254 command = "python $ar_py $ar {{output}} $rspfile"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400255 outputs = [
256 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
257 ]
258 default_output_extension = ".a"
259 output_prefix = "lib"
260 description = "link {{output}}"
261 }
262
263 tool("solink") {
264 soname = "{{target_output_name}}{{output_extension}}"
265
266 rpath = "-Wl,-soname,$soname"
267 if (is_mac) {
268 rpath = "-Wl,-install_name,@rpath/$soname"
269 }
270
Hal Canarye1053ea2018-02-09 14:45:09 -0500271 rspfile = "{{output}}.rsp"
272 rspfile_content = "{{inputs}}"
273 command = "$cc_wrapper $cxx -shared {{ldflags}} @$rspfile {{solibs}} {{libs}} $rpath -o {{output}}"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400274 outputs = [
275 "{{root_out_dir}}/$soname",
276 ]
277 output_prefix = "lib"
278 default_output_extension = ".so"
279 description = "link {{output}}"
280 }
281
282 tool("link") {
Hal Canarye1053ea2018-02-09 14:45:09 -0500283 rspfile = "{{output}}.rsp"
284 rspfile_content = "{{inputs}}"
Mike Klein26ad6da2018-08-06 15:19:19 -0400285
286 # --start-group/--end-group let us link multiple .a {{inputs}}
287 # without worrying about their relative order on the link line.
288 #
289 # This is mostly important for traditional linkers like GNU ld and Gold.
290 # The Mac/iOS linker neither needs nor accepts these flags.
291 # LLD doesn't need these flags, but accepts and ignores them.
292 _start_group = "-Wl,--start-group"
293 _end_group = "-Wl,--end-group"
294 if (is_mac || is_ios) {
295 _start_group = ""
296 _end_group = ""
297 }
298 command = "$cc_wrapper $cxx {{ldflags}} $_start_group @$rspfile {{solibs}} $_end_group {{libs}} -o {{output}}"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400299 outputs = [
300 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
301 ]
302 description = "link {{output}}"
303 }
304
305 tool("stamp") {
306 command = "$stamp {{output}}"
307 description = "stamp {{output}}"
308 }
309
310 tool("copy") {
311 cp_py = rebase_path("../cp.py")
Ben Wagner96aa5352018-06-19 10:34:32 -0400312 command = "python $cp_py {{source}} {{output}}"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400313 description = "copy {{source}} {{output}}"
314 }
315
Jim Van Verth443a9132017-11-28 09:45:26 -0500316 tool("copy_bundle_data") {
317 cp_py = rebase_path("../cp.py")
Ben Wagner96aa5352018-06-19 10:34:32 -0400318 command = "python $cp_py {{source}} {{output}}"
Jim Van Verth443a9132017-11-28 09:45:26 -0500319 description = "copy_bundle_data {{source}} {{output}}"
320 }
321
322 # We don't currently have any xcasset files so make this a NOP
323 tool("compile_xcassets") {
324 command = "true"
325 description = "compile_xcassets {{output}}"
326 }
327
Ethan Nicholas762466e2017-06-29 10:03:38 -0400328 toolchain_args = {
329 current_cpu = invoker.cpu
330 current_os = invoker.os
331 }
332 }
333}
334
335gcc_like_toolchain("gcc_like") {
336 cpu = current_cpu
337 os = current_os
338 ar = target_ar
339 cc = target_cc
340 cxx = target_cxx
341}
342
343gcc_like_toolchain("gcc_like_host") {
344 cpu = host_cpu
345 os = host_os
346 ar = host_ar
347 cc = host_cc
348 cxx = host_cxx
349}