1 import("//llvm/utils/gn/build/toolchain/compiler.gni")
4 # If is_goma is true, the location of the goma client install.
5 # Set this to the output of `goma_ctl goma_dir`.
9 assert(!use_goma || goma_dir != "",
10 "set `goma_dir` to the output of `goma_ctl goma_dir` in your args.gn")
12 unix_copy_command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
14 template("unix_toolchain") {
15 toolchain(target_name) {
16 # https://groups.google.com/a/chromium.org/g/gn-dev/c/F_lv5T-tNDM
17 forward_variables_from(invoker.toolchain_args, "*")
20 forward_variables_from(invoker, "*")
25 if (clang_base_path != "") {
26 cc = rebase_path(clang_base_path, root_build_dir) + "/bin/clang"
27 cxx = rebase_path(clang_base_path, root_build_dir) + "/bin/clang++"
30 ld = cxx # Don't use goma wrapper for linking.
32 cc = "$goma_dir/gomacc $cc"
33 cxx = "$goma_dir/gomacc $cxx"
37 depfile = "{{output}}.d"
38 command = "$cc -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}"
40 description = "CC {{output}}"
41 outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.o" ]
45 depfile = "{{output}}.d"
46 command = "$cxx -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}"
48 description = "CXX {{output}}"
49 outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.o" ]
53 depfile = "{{output}}.d"
54 command = "$cxx -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_objcc}}"
56 description = "OBJCXX {{output}}"
57 outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.o" ]
61 depfile = "{{output}}.d"
62 command = "$cc -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{asmflags}}"
64 description = "ASM {{output}}"
65 outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.o" ]
69 if (current_os == "ios" || current_os == "mac") {
70 command = "libtool -D -static -no_warning_for_no_symbols {{arflags}} -o {{output}} {{inputs}}"
72 # Remove the output file first so that ar doesn't try to modify the
75 "rm -f {{output}} && $ar rcsD {{arflags}} {{output}} {{inputs}}"
77 description = "AR {{output}}"
78 outputs = [ "{{output_dir}}/{{target_output_name}}.a" ]
80 default_output_dir = "{{root_out_dir}}/lib"
83 if (current_os == "ios" || current_os == "mac") {
84 # gn < 1693 (e214b5d35898) doesn't support |frameworks|, requiring
85 # frameworks to be listed in |libs|, but gn >= 1808 (3028c6a426a4) forbids
86 # frameworks from appearing in |libs|. This assertion provides a helpful
87 # cue to upgrade, and is much more user-friendly than the failure that
88 # occurs when an older gn encounters |frameworks|.
90 # gn_version doesn’t actually exist in gn < 1709 (52cb644a3fb4), and
91 # defined(gn_version) doesn't actually work as expected
92 # (https://crbug.com/gn/183), so 1709 is the true minimum enforced by
93 # this construct, and if gn_version is not available, this line will still
94 # be blamed, making the resolution somewhat discoverable.
95 assert(gn_version >= 1693,
96 "Your GN is too old! " +
97 "Update it, perhaps by running llvm/utils/gn/get.py")
100 # Make these apply to all tools below.
102 lib_dir_switch = "-L"
105 outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
106 if (current_os == "ios" || current_os == "mac") {
107 command = "$ld -shared {{ldflags}} -o $outfile {{inputs}} {{libs}} {{frameworks}}"
108 default_output_extension = ".dylib"
110 command = "$ld -shared {{ldflags}} -Wl,-soname,{{target_output_name}}{{output_extension}} -o $outfile {{inputs}} {{libs}}"
111 default_output_extension = ".so"
113 description = "SOLINK $outfile"
114 outputs = [ outfile ]
115 output_prefix = "lib"
116 default_output_dir = "{{root_out_dir}}/lib"
119 tool("solink_module") {
120 outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
121 if (current_os == "ios" || current_os == "mac") {
122 command = "$ld -shared {{ldflags}} -Wl,-flat_namespace -Wl,-undefined,suppress -o $outfile {{inputs}} {{libs}} {{frameworks}}"
123 default_output_extension = ".dylib"
125 command = "$ld -shared {{ldflags}} -Wl,-soname,{{target_output_name}}{{output_extension}} -o $outfile {{inputs}} {{libs}}"
126 default_output_extension = ".so"
128 description = "SOLINK $outfile"
129 outputs = [ outfile ]
130 default_output_dir = "{{root_out_dir}}/lib"
134 outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
135 if (current_os == "ios" || current_os == "mac") {
137 "$ld {{ldflags}} -o $outfile {{inputs}} {{libs}} {{frameworks}}"
139 command = "$ld {{ldflags}} -o $outfile -Wl,--start-group {{inputs}} -Wl,--end-group {{libs}}"
141 description = "LINK $outfile"
142 outputs = [ outfile ]
144 # Setting this allows targets to override the default executable output by
145 # setting output_dir.
146 default_output_dir = "{{root_out_dir}}/bin"
150 command = unix_copy_command
151 description = "COPY {{source}} {{output}}"
154 if (current_os == "ios" || current_os == "mac") {
155 tool("copy_bundle_data") {
156 # https://github.com/nico/hack/blob/master/notes/copydir.md
157 _copydir = "cd {{source}} && " +
158 "find . | cpio -pdl \"\$OLDPWD\"/{{output}} 2>/dev/null"
159 command = "rm -rf {{output}} && if [[ -d {{source}} ]]; then " +
160 _copydir + "; else " + unix_copy_command + "; fi"
161 description = "COPY_BUNDLE_DATA {{source}} {{output}}"
163 tool("compile_xcassets") {
165 description = "The LLVM build doesn't use any xcasset files"
170 command = "touch {{output}}"
171 description = "STAMP {{output}}"
176 unix_toolchain("unix") {
177 if (current_os != "ios" && current_os != "mac") {
178 if (clang_base_path != "") {
179 ar = rebase_path(clang_base_path, root_build_dir) + "/bin/llvm-ar"
187 current_cpu = host_cpu
191 # This template defines a toolchain that uses just-built clang and lld
192 # as compiler and linker.
193 template("stage2_unix_toolchain") {
194 unix_toolchain(target_name) {
196 forward_variables_from(invoker.toolchain_args, "*")
198 clang_base_path = root_build_dir
203 "//:clang($host_toolchain)",
204 "//:lld($host_toolchain)",
206 if (toolchain_args.current_os != "ios" &&
207 toolchain_args.current_os != "mac") {
209 deps += [ "//:llvm-ar($host_toolchain)" ]
214 stage2_unix_toolchain("stage2_unix") {
217 current_cpu = host_cpu
221 if (android_ndk_path != "") {
222 # Android compiler-rt libraries don't really work with per-target runtime
223 # directories yet so force it off.
224 # https://discourse.llvm.org/t/handling-version-numbers-in-per-target-runtime-directories/62717.
225 stage2_unix_toolchain("stage2_android_aarch64") {
227 current_os = "android"
228 current_cpu = "arm64"
229 clang_enable_per_target_runtime_dir = false
233 stage2_unix_toolchain("stage2_android_arm") {
235 current_os = "android"
237 clang_enable_per_target_runtime_dir = false
241 stage2_unix_toolchain("stage2_android_x64") {
243 current_os = "android"
245 clang_enable_per_target_runtime_dir = false
249 stage2_unix_toolchain("stage2_android_x86") {
251 current_os = "android"
253 clang_enable_per_target_runtime_dir = false
258 if (host_os == "mac") {
259 stage2_unix_toolchain("stage2_ios_aarch64") {
262 current_cpu = "arm64"
266 stage2_unix_toolchain("stage2_iossim_x64") {
274 stage2_unix_toolchain("stage2_baremetal_aarch64") {
276 current_os = "baremetal"
277 current_cpu = "arm64"
279 # FIXME: These should be set in all toolchains building sanitizers,
280 # see discussion at https://reviews.llvm.org/D127906#3587329
287 template("win_toolchain") {
288 toolchain(target_name) {
289 # https://groups.google.com/a/chromium.org/d/msg/gn-dev/F_lv5T-tNDM
290 forward_variables_from(invoker.toolchain_args, "*")
293 forward_variables_from(invoker, "*")
298 if (clang_base_path != "") {
299 cl = rebase_path(clang_base_path, root_build_dir) + "/bin/clang-cl"
301 link = rebase_path(clang_base_path, root_build_dir) + "/bin/lld-link"
306 cl = "$goma_dir/gomacc $cl"
310 command = "$cl /nologo /showIncludes /Fo{{output}} /c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}"
312 description = "CC {{output}}"
313 outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.obj" ]
317 command = "$cl /nologo /showIncludes /Fo{{output}} /c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}"
319 description = "CXX {{output}}"
320 outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.obj" ]
324 command = "$link /lib /nologo {{arflags}} /out:{{output}} {{inputs}}"
325 description = "LIB {{output}}"
326 outputs = [ "{{output_dir}}/{{target_output_name}}.lib" ]
327 default_output_dir = "{{root_out_dir}}/lib"
330 # Make these apply to all tools below.
332 lib_dir_switch = "/LIBPATH:"
335 outprefix = "{{output_dir}}/{{target_output_name}}"
336 dllfile = "$outprefix{{output_extension}}"
337 libfile = "$outprefix.lib"
338 pdbfile = "$outprefix.pdb"
339 command = "$link /nologo /dll {{ldflags}} /out:$dllfile /implib:$libfile /pdb:$pdbfile {{inputs}} {{libs}} "
340 description = "LINK $dllfile"
341 link_output = libfile
342 depend_output = libfile
343 runtime_outputs = [ dllfile ]
348 default_output_extension = ".dll"
351 # Put dlls next to the executables in bin/ on Windows, since Windows
352 # doesn't have a configurable rpath. This matches initialization of
353 # module_dir to bin/ in AddLLVM.cmake's set_output_directory().
354 default_output_dir = "{{root_out_dir}}/bin"
357 # Plugins for opt and clang and so on don't work in LLVM's Windows build
358 # since the code doesn't have export annotations, but there are a few
359 # standalone loadable modules used for unit-testing LLVM's dynamic library
361 tool("solink_module") {
362 outprefix = "{{output_dir}}/{{target_output_name}}"
363 dllfile = "$outprefix{{output_extension}}"
364 pdbfile = "$outprefix.pdb"
365 command = "$link /nologo /dll {{ldflags}} /out:$dllfile /pdb:$pdbfile {{inputs}} {{libs}} "
366 description = "LINK_MODULE $dllfile"
367 outputs = [ dllfile ]
368 runtime_outputs = outputs
369 default_output_extension = ".dll"
371 # No default_output_dir, all clients set output_dir.
375 outprefix = "{{output_dir}}/{{target_output_name}}"
376 outfile = "$outprefix{{output_extension}}"
377 pdbfile = "$outprefix.pdb"
378 command = "$link /nologo {{ldflags}} /out:$outfile /pdb:$pdbfile {{inputs}} {{libs}}"
379 description = "LINK $outfile"
380 outputs = [ outfile ]
381 default_output_extension = ".exe"
383 # Setting this allows targets to override the default executable output by
384 # setting output_dir.
385 default_output_dir = "{{root_out_dir}}/bin"
389 if (host_os == "win") {
390 # GN hands out slash-using paths, but cmd's copy needs backslashes.
391 # Use cmd's %foo:a=b% substitution feature to convert.
392 command = "cmd /c set source=\"{{source}}\" & set output=\"{{output}}\" & call copy /Y %source:/=\% %output:\=/% > nul"
394 command = unix_copy_command
397 description = "COPY {{source}} {{output}}"
401 if (host_os == "win") {
402 command = "cmd /c type nul > {{output}}"
404 command = "touch {{output}}"
406 description = "STAMP {{output}}"
411 win_toolchain("win") {
414 current_cpu = host_cpu
418 win_toolchain("stage2_win_x64") {
423 if (host_os != "win") {
424 sysroot = win_sysroot
426 clang_base_path = root_build_dir
430 "//:clang($host_toolchain)",
431 "//:lld($host_toolchain)",
435 win_toolchain("stage2_win_x86") {
440 if (host_os != "win") {
441 sysroot = win_sysroot
443 clang_base_path = root_build_dir
447 "//:clang($host_toolchain)",
448 "//:lld($host_toolchain)",