blob: 3d7a1e349c221040769e90588656db6b14f23874 [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 Osmana0982652018-12-07 15:30:43 -050036 bin = "$win_vc\\bin"
Kaloyan Donevf7466bd2018-03-16 14:45:35 +020037 } else {
Brian Osmana0982652018-12-07 15:30:43 -050038 bin = "$win_vc\\bin\\amd64"
Kaloyan Donevf7466bd2018-03-16 14:45:35 +020039 }
Ethan Nicholas762466e2017-06-29 10:03:38 -040040 } else {
Brian Osmana0982652018-12-07 15:30:43 -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.
51 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 = ""
Mike Kleinc722f792017-07-31 11:57:21 -040055 if (clang_win != "") {
Ben Wagnerdf574042018-03-13 15:11:14 -040056 if (target_cpu == "x86") {
57 # cl.exe knows implicitly by the choice of executable that it's targeting
58 # x86, but clang-cl.exe needs to be told when targeting non-host
59 # platforms. (All our builders are x86-64, so x86 is always non-host.)
60 cl_m32_flag = "-m32"
61 }
Mike Kleinc722f792017-07-31 11:57:21 -040062 cl = "$clang_win/bin/clang-cl.exe"
63 } else {
64 cl = "$bin/cl.exe"
65 }
66
Ethan Nicholas762466e2017-06-29 10:03:38 -040067 tool("asm") {
68 _ml = "ml"
69 if (target_cpu == "x64") {
70 _ml += "64"
71 }
Brian Osmana0982652018-12-07 15:30:43 -050072 command = "$env_setup \"$bin/$_ml.exe\" {{asmflags}} /nologo /c /Fo {{output}} {{source}}"
Ethan Nicholas762466e2017-06-29 10:03:38 -040073 outputs = [
74 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj",
75 ]
76 description = "assemble {{source}}"
77 }
78
79 tool("cc") {
Ethan Nicholas762466e2017-06-29 10:03:38 -040080 precompiled_header_type = "msvc"
81 pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb"
82
83 # Label names may have spaces so pdbname must be quoted.
Brian Osmane530e512018-07-19 15:48:21 -040084 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 -040085 depsformat = "msvc"
86 outputs = [
87 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj",
88 ]
Ethan Nicholas762466e2017-06-29 10:03:38 -040089 description = "compile {{source}}"
90 }
91
92 tool("cxx") {
Ethan Nicholas762466e2017-06-29 10:03:38 -040093 precompiled_header_type = "msvc"
94 pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb"
95
96 # Label names may have spaces so pdbname must be quoted.
Brian Osmane530e512018-07-19 15:48:21 -040097 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 -040098 depsformat = "msvc"
99 outputs = [
100 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj",
101 ]
Ethan Nicholas762466e2017-06-29 10:03:38 -0400102 description = "compile {{source}}"
103 }
104
105 tool("alink") {
106 rspfile = "{{output}}.rsp"
107
Brian Osmana0982652018-12-07 15:30:43 -0500108 command = "$env_setup \"$bin/lib.exe\" /nologo /ignore:4221 {{arflags}} /OUT:{{output}} @$rspfile"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400109 outputs = [
110 # Ignore {{output_extension}} and always use .lib, there's no reason to
111 # allow targets to override this extension on Windows.
112 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
113 ]
114 default_output_extension = ".lib"
115 default_output_dir = "{{target_out_dir}}"
116
117 # inputs_newline works around a fixed per-line buffer size in the linker.
118 rspfile_content = "{{inputs_newline}}"
119 description = "link {{output}}"
120 }
121
122 tool("solink") {
123 dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
124 libname = "${dllname}.lib"
125 pdbname = "${dllname}.pdb"
126 rspfile = "${dllname}.rsp"
127
Brian Osmana0982652018-12-07 15:30:43 -0500128 command = "$env_setup \"$bin/link.exe\" /nologo /IMPLIB:$libname /DLL /OUT:$dllname /PDB:$pdbname @$rspfile"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400129 outputs = [
130 dllname,
131 libname,
132 pdbname,
133 ]
134 default_output_extension = ".dll"
135 default_output_dir = "{{root_out_dir}}"
136
137 link_output = libname
138 depend_output = libname
139 runtime_outputs = [
140 dllname,
141 pdbname,
142 ]
143
144 # I don't quite understand this. Aping Chrome's toolchain/win/BUILD.gn.
145 restat = true
146
147 # inputs_newline works around a fixed per-line buffer size in the linker.
148 rspfile_content = "{{inputs_newline}} {{libs}} {{solibs}} {{ldflags}}"
149 description = "link {{output}}"
150 }
151
152 tool("link") {
153 exename = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
154 pdbname = "$exename.pdb"
155 rspfile = "$exename.rsp"
156
Brian Osmana0982652018-12-07 15:30:43 -0500157 command = "$env_setup \"$bin/link.exe\" /nologo /OUT:$exename /PDB:$pdbname @$rspfile"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400158
159 default_output_extension = ".exe"
160 default_output_dir = "{{root_out_dir}}"
161 outputs = [
162 exename,
163 ]
164
165 # inputs_newline works around a fixed per-line buffer size in the linker.
166 rspfile_content = "{{inputs_newline}} {{libs}} {{solibs}} {{ldflags}}"
167 description = "link {{output}}"
168 }
169
170 tool("stamp") {
171 command = "$stamp {{output}}"
172 description = "stamp {{output}}"
173 }
174
175 tool("copy") {
176 cp_py = rebase_path("../cp.py")
Ben Wagner96aa5352018-06-19 10:34:32 -0400177 command = "python $cp_py {{source}} {{output}}"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400178 description = "copy {{source}} {{output}}"
179 }
180}
181
182template("gcc_like_toolchain") {
183 toolchain(target_name) {
184 ar = invoker.ar
185 cc = invoker.cc
186 cxx = invoker.cxx
187 lib_switch = "-l"
188 lib_dir_switch = "-L"
189
190 tool("cc") {
191 depfile = "{{output}}.d"
192 command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
193 depsformat = "gcc"
194 outputs = [
195 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
196 ]
197 description = "compile {{source}}"
198 }
199
200 tool("cxx") {
201 depfile = "{{output}}.d"
202 command = "$cc_wrapper $cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -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("objc") {
211 depfile = "{{output}}.d"
212 command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_objc}} -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("objcxx") {
221 depfile = "{{output}}.d"
Greg Daniel6b7e0e22017-07-12 16:21:09 -0400222 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 -0400223 depsformat = "gcc"
224 outputs = [
225 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
226 ]
227 description = "compile {{source}}"
228 }
229
230 tool("asm") {
231 depfile = "{{output}}.d"
232 command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
233 depsformat = "gcc"
234 outputs = [
235 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
236 ]
237 description = "assemble {{source}}"
238 }
239
240 tool("alink") {
241 rspfile = "{{output}}.rsp"
242 rspfile_content = "{{inputs}}"
243 ar_py = rebase_path("../ar.py")
Ben Wagner96aa5352018-06-19 10:34:32 -0400244 command = "python $ar_py $ar {{output}} $rspfile"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400245 outputs = [
246 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
247 ]
248 default_output_extension = ".a"
249 output_prefix = "lib"
250 description = "link {{output}}"
251 }
252
253 tool("solink") {
254 soname = "{{target_output_name}}{{output_extension}}"
255
256 rpath = "-Wl,-soname,$soname"
257 if (is_mac) {
258 rpath = "-Wl,-install_name,@rpath/$soname"
259 }
260
Hal Canarye1053ea2018-02-09 14:45:09 -0500261 rspfile = "{{output}}.rsp"
262 rspfile_content = "{{inputs}}"
263 command = "$cc_wrapper $cxx -shared {{ldflags}} @$rspfile {{solibs}} {{libs}} $rpath -o {{output}}"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400264 outputs = [
265 "{{root_out_dir}}/$soname",
266 ]
267 output_prefix = "lib"
268 default_output_extension = ".so"
269 description = "link {{output}}"
270 }
271
272 tool("link") {
Hal Canarye1053ea2018-02-09 14:45:09 -0500273 rspfile = "{{output}}.rsp"
274 rspfile_content = "{{inputs}}"
Mike Klein26ad6da2018-08-06 15:19:19 -0400275
276 # --start-group/--end-group let us link multiple .a {{inputs}}
277 # without worrying about their relative order on the link line.
278 #
279 # This is mostly important for traditional linkers like GNU ld and Gold.
280 # The Mac/iOS linker neither needs nor accepts these flags.
281 # LLD doesn't need these flags, but accepts and ignores them.
282 _start_group = "-Wl,--start-group"
283 _end_group = "-Wl,--end-group"
284 if (is_mac || is_ios) {
285 _start_group = ""
286 _end_group = ""
287 }
288 command = "$cc_wrapper $cxx {{ldflags}} $_start_group @$rspfile {{solibs}} $_end_group {{libs}} -o {{output}}"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400289 outputs = [
290 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
291 ]
292 description = "link {{output}}"
293 }
294
295 tool("stamp") {
296 command = "$stamp {{output}}"
297 description = "stamp {{output}}"
298 }
299
300 tool("copy") {
301 cp_py = rebase_path("../cp.py")
Ben Wagner96aa5352018-06-19 10:34:32 -0400302 command = "python $cp_py {{source}} {{output}}"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400303 description = "copy {{source}} {{output}}"
304 }
305
Jim Van Verth443a9132017-11-28 09:45:26 -0500306 tool("copy_bundle_data") {
307 cp_py = rebase_path("../cp.py")
Ben Wagner96aa5352018-06-19 10:34:32 -0400308 command = "python $cp_py {{source}} {{output}}"
Jim Van Verth443a9132017-11-28 09:45:26 -0500309 description = "copy_bundle_data {{source}} {{output}}"
310 }
311
312 # We don't currently have any xcasset files so make this a NOP
313 tool("compile_xcassets") {
314 command = "true"
315 description = "compile_xcassets {{output}}"
316 }
317
Ethan Nicholas762466e2017-06-29 10:03:38 -0400318 toolchain_args = {
319 current_cpu = invoker.cpu
320 current_os = invoker.os
321 }
322 }
323}
324
325gcc_like_toolchain("gcc_like") {
326 cpu = current_cpu
327 os = current_os
328 ar = target_ar
329 cc = target_cc
330 cxx = target_cxx
331}
332
333gcc_like_toolchain("gcc_like_host") {
334 cpu = host_cpu
335 os = host_os
336 ar = host_ar
337 cc = host_cc
338 cxx = host_cxx
339}