• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2013 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import("//build/config/clang/clang.gni")
8import("//build/config/compiler/compiler.gni")
9import("//build/config/sanitizers/sanitizers.gni")
10import("//build/config/v8_target_cpu.gni")
11import("//build/toolchain/cc_wrapper.gni")
12import("//build/toolchain/clang_static_analyzer.gni")
13import("//build/toolchain/toolchain.gni")
14
15if (defined(remote_execution) && remote_execution) {
16  import("${rbe_path}/rbe.gni")
17}
18
19if (!is_arkui_x) {
20  import("//build/rust/rustc_toolchain.gni")
21}
22
23if (is_nacl) {
24  # To keep NaCl variables out of builds that don't include NaCl, all
25  # variables defined in nacl/config.gni referenced here should be protected by
26  # is_nacl conditions.
27  import("//build/config/nacl/config.gni")
28}
29
30declare_args() {
31  # Enables allowlist generation for IDR_ grit defines seen by the compiler.
32  # Currently works only on ohos and enabled by default for release builds.
33  # Requires debug info, so disabled for symbol_level=0 & strip_debug_info=true.
34  enable_resource_allowlist_generation =
35      is_ohos && !is_debug &&
36      # Always enable for official builds, but enable for release builds by
37      # default only when other args allow.
38      (is_official_build ||
39       (!strip_debug_info && symbol_level > 0 && !is_component_build))
40}
41
42declare_args() {
43  share_ccache = ""
44  toolchain_uses_remoteexec = false
45}
46
47# When the arg is set via args.gn, it applies to all toolchains. In order to not
48# hit the assert in grit_rule.gni, explicitly disable for host toolchains.
49if (is_linux && target_os == "ohos") {
50  enable_resource_allowlist_generation = false
51}
52
53# Path to the Clang static analysis wrapper script.
54# REVIEWERS: can you suggest a better location for this?
55# GN is really picky about dead stores of variables except at the global scope.
56analyzer_wrapper =
57    rebase_path("//build/toolchain/clang_static_analyzer_wrapper.py",
58                root_build_dir) + " --mode=clang"
59
60# This template defines a toolchain for something that works like gcc
61# (including clang).
62#
63# It requires the following variables specifying the executables to run:
64#  - ar
65#  - cc
66#  - cxx
67#  - ld
68#
69# Optional parameters that control the tools:
70#
71#  - extra_cflags
72#      Extra flags to be appended when compiling C files (but not C++ files).
73#  - extra_cppflags
74#      Extra flags to be appended when compiling both C and C++ files. "CPP"
75#      stands for "C PreProcessor" in this context, although it can be
76#      used for non-preprocessor flags as well. Not to be confused with
77#      "CXX" (which follows).
78#  - extra_cxxflags
79#      Extra flags to be appended when compiling C++ files (but not C files).
80#  - extra_asmflags
81#      Extra flags to be appended when compiling assembly.
82#  - extra_ldflags
83#      Extra flags to be appended when linking
84#
85#  - libs_section_prefix
86#  - libs_section_postfix
87#      The contents of these strings, if specified, will be placed around
88#      the libs section of the linker line. It allows one to inject libraries
89#      at the beginning and end for all targets in a toolchain.
90#  - solink_libs_section_prefix
91#  - solink_libs_section_postfix
92#      Same as libs_section_{pre,post}fix except used for solink instead of link.
93#  - link_outputs
94#      The content of this array, if specified, will be added to the list of
95#      outputs from the link command. This can be useful in conjunction with
96#      the post_link parameter.
97#  - use_unstripped_as_runtime_outputs
98#      When |strip| is set, mark unstripped executables as runtime deps rather
99#      than stripped ones.
100#  - post_link
101#      The content of this string, if specified, will be run as a separate
102#      command following the the link command.
103#  - deps
104#      Just forwarded to the toolchain definition.
105#  - executable_extension
106#      If this string is specified it will be used for the file extension
107#      for an executable, rather than using no extension; targets will
108#      still be able to override the extension using the output_extension
109#      variable.
110#  - rebuild_define
111#      The contents of this string, if specified, will be passed as a #define
112#      to the toolchain. It can be used to force recompiles whenever a
113#      toolchain is updated.
114#  - shlib_extension
115#      If this string is specified it will be used for the file extension
116#      for a shared library, rather than default value specified in
117#      toolchain.gni
118#  - strip
119#      Location of the strip executable. When specified, strip will be run on
120#      all shared libraries and executables as they are built. The pre-stripped
121#      artifacts will be put in lib.unstripped/ and exe.unstripped/.
122template("gcc_toolchain") {
123  toolchain(target_name) {
124    assert(defined(invoker.ar), "gcc_toolchain() must specify a \"ar\" value")
125    assert(defined(invoker.cc), "gcc_toolchain() must specify a \"cc\" value")
126    assert(defined(invoker.cxx), "gcc_toolchain() must specify a \"cxx\" value")
127    assert(defined(invoker.ld), "gcc_toolchain() must specify a \"ld\" value")
128
129    # This define changes when the toolchain changes, forcing a rebuild.
130    # Nothing should ever use this define.
131    if (defined(invoker.rebuild_define)) {
132      rebuild_string = "-D" + invoker.rebuild_define + " "
133    } else {
134      rebuild_string = ""
135    }
136
137    # GN's syntax can't handle more than one scope dereference at once, like
138    # "invoker.toolchain_args.foo", so make a temporary to hold the toolchain
139    # args so we can do "invoker_toolchain_args.foo".
140    assert(defined(invoker.toolchain_args),
141           "Toolchains must specify toolchain_args")
142    invoker_toolchain_args = invoker.toolchain_args
143    assert(defined(invoker_toolchain_args.current_cpu),
144           "toolchain_args must specify a current_cpu")
145    assert(defined(invoker_toolchain_args.current_os),
146           "toolchain_args must specify a current_os")
147
148    # When invoking this toolchain not as the default one, these args will be
149    # passed to the build. They are ignored when this is the default toolchain.
150    toolchain_args = {
151      # Populate toolchain args from the invoker.
152      forward_variables_from(invoker_toolchain_args, "*")
153
154      # The host toolchain value computed by the default toolchain's setup
155      # needs to be passed through unchanged to all secondary toolchains to
156      # ensure that it's always the same, regardless of the values that may be
157      # set on those toolchains.
158      host_toolchain = host_toolchain
159
160      if (!defined(invoker_toolchain_args.v8_current_cpu)) {
161        v8_current_cpu = invoker_toolchain_args.current_cpu
162      }
163    }
164
165    # When the invoker has explicitly overridden remote_execution, otherwise default
166    # to the global one. This works because the only reasonable override
167    # that toolchains might supply for these values are to force-disable them.
168    if (defined(toolchain_args.remote_execution)) {
169      toolchain_uses_remoteexec = toolchain_args.remote_execution
170    } else if (defined(remote_execution)) {
171      toolchain_uses_remoteexec = remote_execution
172    }
173
174    if (is_clang && use_clang_static_analyzer &&
175        (!defined(invoker.is_clang_analysis_supported) ||
176         invoker.is_clang_analysis_supported)) {
177      compiler_prefix = "${analyzer_wrapper} "
178      asm = invoker.cc
179    } else {
180      if (defined(toolchain_args.cc_wrapper)) {
181        toolchain_cc_wrapper = toolchain_args.cc_wrapper
182      } else {
183        toolchain_cc_wrapper = cc_wrapper
184      }
185      if (share_ccache != "") {
186        compiler_prefix = "CCACHE_DIR=" + share_ccache +
187                          " CCACHE_NOHASHDIR=1 ${toolchain_cc_wrapper} "
188      } else {
189        compiler_prefix = "${toolchain_cc_wrapper} "
190      }
191    }
192
193    # For remote execution
194    link_prefix = ""
195    ar_prefix = ""
196    if (toolchain_uses_remoteexec) {
197      compiler_prefix = "${rbe_bin_dir}/rewrapper -cfg=${rbe_cfg_dir}/rewrapper_compile.cfg ${rbe_compile_missing_inputs} -exec_root=${exec_root} "
198    }
199
200    # For hitest
201    if (defined(use_hitest) && use_hitest) {
202      compiler_prefix = "${hitest_wrapper} "
203      link_prefix = "${hitest_wrapper} "
204    }
205    cc = compiler_prefix + invoker.cc
206    cxx = compiler_prefix + invoker.cxx
207    ar = ar_prefix + invoker.ar
208    ld = link_prefix + invoker.ld
209    if (!defined(asm)) {
210      if (!toolchain_uses_remoteexec) {
211        asm = cc
212      } else {
213        asm = invoker.cc
214      }
215    }
216    if (defined(invoker.readelf)) {
217      readelf = invoker.readelf
218    } else {
219      readelf = "readelf"
220    }
221    if (defined(invoker.nm)) {
222      nm = invoker.nm
223    } else {
224      nm = "nm"
225    }
226
227    if (defined(invoker.shlib_extension)) {
228      default_shlib_extension = invoker.shlib_extension
229    } else {
230      default_shlib_extension = shlib_extension
231    }
232
233    if (defined(invoker.executable_extension)) {
234      default_executable_extension = invoker.executable_extension
235    } else {
236      default_executable_extension = ""
237    }
238
239    if (!is_arkui_x) {
240      if (defined(invoker.dylib_extension)) {
241        default_dylib_extension = invoker.dylib_extension
242      } else {
243        default_dylib_extension = dylib_extension
244      }
245
246      if (defined(invoker.rlib_extension)) {
247        default_rlib_extension = invoker.rlib_extension
248      } else {
249        default_rlib_extension = rlib_extension
250      }
251    }
252
253    # Bring these into our scope for string interpolation with default values.
254    if (defined(invoker.libs_section_prefix)) {
255      libs_section_prefix = invoker.libs_section_prefix
256    } else {
257      libs_section_prefix = ""
258    }
259
260    if (defined(invoker.libs_section_postfix)) {
261      libs_section_postfix = invoker.libs_section_postfix
262    } else {
263      libs_section_postfix = ""
264    }
265
266    if (defined(invoker.solink_libs_section_prefix)) {
267      solink_libs_section_prefix = invoker.solink_libs_section_prefix
268    } else {
269      solink_libs_section_prefix = ""
270    }
271
272    if (defined(invoker.solink_libs_section_postfix)) {
273      solink_libs_section_postfix = invoker.solink_libs_section_postfix
274    } else {
275      solink_libs_section_postfix = ""
276    }
277
278    if (defined(invoker.extra_cflags) && invoker.extra_cflags != "") {
279      extra_cflags = " " + invoker.extra_cflags
280    } else {
281      extra_cflags = ""
282    }
283
284    if (defined(invoker.extra_cppflags) && invoker.extra_cppflags != "") {
285      extra_cppflags = " " + invoker.extra_cppflags
286    } else {
287      extra_cppflags = ""
288    }
289
290    if (defined(invoker.extra_cxxflags) && invoker.extra_cxxflags != "") {
291      extra_cxxflags = " " + invoker.extra_cxxflags
292    } else {
293      extra_cxxflags = ""
294    }
295
296    if (defined(invoker.extra_asmflags) && invoker.extra_asmflags != "") {
297      extra_asmflags = " " + invoker.extra_asmflags
298    } else {
299      extra_asmflags = ""
300    }
301
302    if (defined(invoker.extra_ldflags) && invoker.extra_ldflags != "") {
303      extra_ldflags = " " + invoker.extra_ldflags
304    } else {
305      extra_ldflags = ""
306    }
307
308    enable_linker_map = defined(invoker.enable_linker_map) &&
309                        invoker.enable_linker_map && generate_linker_map
310
311    # These library switches can apply to all tools below.
312    lib_switch = "-l"
313    lib_dir_switch = "-L"
314
315    # Object files go in this directory.
316    object_subdir = "{{source_out_dir}}/{{label_name}}"
317
318    tool("cc") {
319      depfile = "{{output}}.d"
320      command = "$cc -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}${extra_cppflags}${extra_cflags} -c {{source}} -o {{output}}"
321      depsformat = "gcc"
322      description = "CC {{output}}"
323      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
324    }
325
326    tool("cxx") {
327      depfile = "{{output}}.d"
328      command = "$cxx -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}${extra_cppflags}${extra_cxxflags} -c {{source}} -o {{output}}"
329      depsformat = "gcc"
330      description = "CXX {{output}}"
331      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
332    }
333
334    tool("asm") {
335      # For GCC we can just use the C compiler to compile assembly.
336      depfile = "{{output}}.d"
337      command = "$asm -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{asmflags}}${extra_asmflags} -c {{source}} -o {{output}}"
338      depsformat = "gcc"
339      description = "ASM {{output}}"
340      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
341    }
342
343    tool("alink") {
344      if (current_os == "aix") {
345        # AIX does not support either -D (deterministic output) or response
346        # files.
347        command = "$ar -X64 {{arflags}} -r -c -s {{output}} {{inputs}}"
348      } else {
349        rspfile = "{{output}}.rsp"
350        rspfile_content = "{{inputs}}"
351        command = "\"$ar\" {{arflags}} -r -c -s -D {{output}} @\"$rspfile\""
352      }
353
354      # Remove the output file first so that ar doesn't try to modify the
355      # existing file.
356      if (host_os == "win") {
357        tool_wrapper_path =
358            rebase_path("//build/toolchain/win/tool_wrapper.py", root_build_dir)
359        command = "cmd /c $python_path $tool_wrapper_path delete-file {{output}} && $command"
360      } else {
361        command = "rm -f {{output}} && $command"
362      }
363
364      # Almost all targets build with //build/config/compiler:thin_archive which
365      # adds -T to arflags.
366      description = "AR {{output}}"
367      outputs = [ "{{output_dir}}/{{target_output_name}}{{output_extension}}" ]
368
369      # Shared libraries go in the target out directory by default so we can
370      # generate different targets with the same name and not have them collide.
371      default_output_dir = "{{target_out_dir}}"
372      default_output_extension = ".a"
373      output_prefix = "lib"
374    }
375
376    tool("solink") {
377      soname = "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.so".
378      sofile = "{{output_dir}}/$soname"  # Possibly including toolchain dir.
379      rspfile = sofile + ".rsp"
380      pool = "//build/toolchain:link_pool($default_toolchain)"
381
382      is_mingw_link = false
383      if (invoker_toolchain_args.current_os == "mingw") {
384        is_mingw_link = true
385        libname = "{{target_output_name}}.lib"
386        libfile = "{{output_dir}}/$libname"
387      }
388
389      if (defined(invoker.strip)) {
390        unstripped_sofile = "{{root_out_dir}}/lib.unstripped/$sofile"
391      } else {
392        unstripped_sofile = sofile
393      }
394
395      link_command = "$ld -shared {{ldflags}}${extra_ldflags} -o \"$unstripped_sofile\" @\"$rspfile\""
396
397      if (!is_mingw_link) {
398        link_command = "$link_command -Wl,-soname=\"$soname\""
399      } else {
400        link_command = "$link_command -Wl,--out-implib,{{root_out_dir}}/lib.unstripped/$libfile"
401      }
402
403      # Generate a map file to be used for binary size analysis.
404      # Map file adds ~10% to the link time on a z620.
405      map_switch = ""
406      if (enable_linker_map && is_official_build) {
407        map_file = "$unstripped_sofile.map.gz"
408        map_switch = " --map-file \"$map_file\""
409      }
410
411      assert(defined(readelf), "to solink you must have a readelf")
412      assert(defined(nm), "to solink you must have an nm")
413      strip_switch = ""
414      if (defined(invoker.strip)) {
415        strip_switch = "--strip=${invoker.strip} "
416        if (current_cpu == "arm" && is_standard_system) {
417          strip_debug_whitelist =
418              rebase_path("//build/toolchain/strip_debug_whitelist.txt",
419                          root_build_dir)
420          strip_switch += "--strip-debug-whitelist=${strip_debug_whitelist} "
421        }
422      }
423      adlt_switch = ""
424      if (enable_adlt && allowed_lib_list != "") {
425        adlt_switch = "--target-name={{label_name}}" +
426                      " --target-out-dir={{target_out_dir}}" +
427                      " --allowed-lib-list=" + rebase_path(allowed_lib_list)
428      }
429
430      # This needs a Python script to avoid using a complex shell command
431      # requiring sh control structures, pipelines, and POSIX utilities.
432      # The host might not have a POSIX shell and utilities (e.g. Windows).
433      solink_wrapper =
434          rebase_path("//build/toolchain/gcc_solink_wrapper.py", root_build_dir)
435      _clang_base_dir = rebase_path(toolchains_dir)
436      command = "$python_path \"$solink_wrapper\" --readelf=\"$readelf\" --nm=\"$nm\" $strip_switch $adlt_switch --sofile=\"$unstripped_sofile\" $map_switch --output=\"$sofile\" --clang-base-dir=\"$_clang_base_dir\""
437      if (is_mingw_link) {
438        command = "$command --libfile=\"$libfile\""
439      }
440      if (full_mini_debug && !is_debug) {
441        command = "$command --mini-debug"
442      }
443      command = "$command -- $link_command"
444
445      rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"
446
447      description = "SOLINK $sofile"
448
449      # Declares toolchain solink to be compiled as whole-archive by default.
450      toolchain_whole_archive = true
451
452      # Use this for {{output_extension}} expansions unless a target manually
453      # overrides it (in which case {{output_extension}} will be what the target
454      # specifies).
455      default_output_extension = default_shlib_extension
456
457      default_output_dir = "{{root_out_dir}}"
458
459      output_prefix = "lib"
460
461      # Since the above commands only updates the .TOC file when it changes, ask
462      # Ninja to check if the timestamp actually changed to know if downstream
463      # dependencies should be recompiled.
464      restat = true
465
466      # Tell GN about the output files. It will link to the sofile
467      outputs = [ sofile ]
468      if (sofile != unstripped_sofile) {
469        outputs += [ unstripped_sofile ]
470        if (defined(invoker.use_unstripped_as_runtime_outputs) &&
471            invoker.use_unstripped_as_runtime_outputs) {
472          runtime_outputs = [ unstripped_sofile ]
473        }
474      }
475      if (defined(map_file)) {
476        outputs += [ map_file ]
477      }
478
479      if (is_mingw_link) {
480        outputs += [ libfile ]
481        link_output = libfile
482        depend_output = libfile
483      } else {
484        link_output = sofile
485        depend_output = sofile
486      }
487    }
488
489    tool("solink_module") {
490      soname = "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.so".
491      sofile = "{{output_dir}}/$soname"
492      rspfile = sofile + ".rsp"
493      pool = "//build/toolchain:link_pool($default_toolchain)"
494
495      if (defined(invoker.strip)) {
496        unstripped_sofile = "{{root_out_dir}}/lib.unstripped/$sofile"
497      } else {
498        unstripped_sofile = sofile
499      }
500
501      command = "$ld -shared {{ldflags}}${extra_ldflags} -o \"$unstripped_sofile\" -Wl,-soname=\"$soname\" @\"$rspfile\""
502
503      if (defined(invoker.strip)) {
504        strip_command = "${invoker.strip} -o \"$sofile\" \"$unstripped_sofile\""
505        command += " && " + strip_command
506      }
507      rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"
508
509      description = "SOLINK_MODULE $sofile"
510
511      # Use this for {{output_extension}} expansions unless a target manually
512      # overrides it (in which case {{output_extension}} will be what the target
513      # specifies).
514      if (defined(invoker.loadable_module_extension)) {
515        default_output_extension = invoker.loadable_module_extension
516      } else {
517        default_output_extension = default_shlib_extension
518      }
519
520      default_output_dir = "{{root_out_dir}}"
521
522      output_prefix = "lib"
523
524      outputs = [ sofile ]
525      if (sofile != unstripped_sofile) {
526        outputs += [ unstripped_sofile ]
527        if (defined(invoker.use_unstripped_as_runtime_outputs) &&
528            invoker.use_unstripped_as_runtime_outputs) {
529          runtime_outputs = [ unstripped_sofile ]
530        }
531      }
532    }
533
534    tool("link") {
535      exename = "{{target_output_name}}{{output_extension}}"
536      outfile = "{{output_dir}}/$exename"
537      rspfile = "$outfile.rsp"
538      unstripped_outfile = outfile
539      pool = "//build/toolchain:link_pool($default_toolchain)"
540
541      # Use this for {{output_extension}} expansions unless a target manually
542      # overrides it (in which case {{output_extension}} will be what the target
543      # specifies).
544      default_output_extension = default_executable_extension
545
546      default_output_dir = "{{root_out_dir}}"
547
548      if (defined(invoker.strip)) {
549        unstripped_outfile = "{{root_out_dir}}/exe.unstripped/$outfile"
550      }
551
552      # Generate a map file to be used for binary size analysis.
553      # Map file adds ~10% to the link time on a z620.
554      map_switch = ""
555      if (enable_linker_map && is_official_build) {
556        map_file = "$unstripped_outfile.map.gz"
557        map_switch = " --map-file \"$map_file\""
558      }
559
560      start_group_flag = ""
561      end_group_flag = ""
562      if (current_os != "aix") {
563        # the "--start-group .. --end-group" feature isn't available on the aix ld.
564        start_group_flag = "-Wl,--start-group"
565        end_group_flag = "-Wl,--end-group "
566      }
567      _clang_rt_dso_full_path = ""
568      if (is_asan && invoker_toolchain_args.current_os == "ohos") {
569        if (use_hwasan) {
570          _clang_rt_dso_full_path = rebase_path(
571                  "$clang_base_path/lib/clang/$clang_version/lib/aarch64-linux-ohos/libclang_rt.hwasan.so",
572                  root_build_dir)
573        } else if (invoker_toolchain_args.current_cpu == "arm64") {
574          _clang_rt_dso_full_path = rebase_path(
575                  "$clang_base_path/lib/clang/$clang_version/lib/aarch64-linux-ohos/libclang_rt.asan.so",
576                  root_build_dir)
577        } else if (invoker_toolchain_args.current_cpu == "riscv64") {
578          _clang_rt_dso_full_path = rebase_path(
579                  "$clang_base_path/lib/clang/$clang_version/lib/riscv64-linux-ohos/libclang_rt.asan.so",
580                  root_build_dir)
581        } else if (invoker_toolchain_args.current_cpu == "loongarch64") {
582          _clang_rt_dso_full_path = rebase_path(
583                  "$clang_base_path/lib/clang/$clang_version/lib/loongarch64-linux-ohos/libclang_rt.asan.so",
584                  root_build_dir)
585        } else {
586          _clang_rt_dso_full_path = rebase_path(
587                  "$clang_base_path/lib/clang/$clang_version/lib/arm-linux-ohos/libclang_rt.asan.so",
588                  root_build_dir)
589        }
590      } else if (is_tsan && target_cpu == "arm64" &&
591                 invoker_toolchain_args.current_os == "ohos" && !is_win &&
592                 !is_mingw) {
593        _clang_rt_dso_full_path = rebase_path(
594                "$clang_base_path/lib/clang/$clang_version/lib/aarch64-linux-ohos/libclang_rt.tsan.so",
595                root_build_dir)
596      }
597      link_command = "$ld {{ldflags}}${extra_ldflags} -o \"$unstripped_outfile\" $libs_section_prefix $start_group_flag $_clang_rt_dso_full_path @\"$rspfile\" {{solibs}} {{libs}} $end_group_flag $libs_section_postfix"
598
599      strip_switch = ""
600
601      if (defined(invoker.strip)) {
602        strip_switch = " --strip=\"${invoker.strip}\" --unstripped-file=\"$unstripped_outfile\""
603      }
604      if ((is_asan || is_tsan) && invoker_toolchain_args.current_os == "ohos") {
605        strip_switch =
606            "$strip_switch --clang_rt_dso_path=\"$_clang_rt_dso_full_path\""
607      }
608
609      link_wrapper =
610          rebase_path("//build/toolchain/gcc_link_wrapper.py", root_build_dir)
611      _clang_base_dir = rebase_path(toolchains_dir)
612      command = "$python_path \"$link_wrapper\" --output=\"$outfile\"$strip_switch$map_switch --clang-base-dir=\"$_clang_base_dir\" "
613      if (full_mini_debug && !is_debug) {
614        command = "$command --mini-debug"
615      }
616      command = "$command -- $link_command"
617      description = "LINK $outfile"
618      rspfile_content = "{{inputs}}"
619      outputs = [ outfile ]
620      if (outfile != unstripped_outfile) {
621        outputs += [ unstripped_outfile ]
622        if (defined(invoker.use_unstripped_as_runtime_outputs) &&
623            invoker.use_unstripped_as_runtime_outputs) {
624          runtime_outputs = [ unstripped_outfile ]
625        }
626      }
627      if (defined(invoker.link_outputs)) {
628        outputs += invoker.link_outputs
629      }
630      if (defined(map_file)) {
631        outputs += [ map_file ]
632      }
633    }
634
635    # These two are really entirely generic, but have to be repeated in
636    # each toolchain because GN doesn't allow a template to be used here.
637    # See //build/toolchain/toolchain.gni for details.
638    tool("stamp") {
639      command = stamp_command
640      description = stamp_description
641    }
642    tool("copy") {
643      command = copy_command
644      description = copy_description
645    }
646
647    if (!is_arkui_x) {
648      cc_command_args = ""
649      if (defined(invoker.cc_command_args)) {
650        cc_command_args = invoker.cc_command_args
651      }
652      if (invoker_toolchain_args.current_os == "mingw") {
653        rust_dynamic = ""
654      } else {
655        rust_dynamic = "-C prefer-dynamic"
656      }
657      minidebug_switch = ""
658      if (full_mini_debug && !is_debug) {
659        minidebug_switch = " --mini-debug"
660      }
661      rust_sysroot_relative_to_out = rebase_path(rust_sysroot, root_out_dir)
662      rustc_wrapper =
663          rebase_path("//build/toolchain/rustc_wrapper.py", root_build_dir)
664      _clang_base_dir = rebase_path(toolchains_dir)
665      tool("rust_staticlib") {
666        staticlibname =
667            "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.a".
668        outfile = "{{target_out_dir}}/$staticlibname"
669        depfile = "$outfile.d"
670        rspfile = "$outfile.rsp"
671        rspfile_content = "{{rustdeps}} {{externs}}"
672
673        pool = "//build/toolchain:link_pool($default_toolchain)"
674
675        command = "$python_path \"$rustc_wrapper\" --clang-base-dir=$_clang_base_dir --clippy-driver=$clippy_driver --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- --crate-name {{crate_name}} $rust_dynamic {{source}} --crate-type {{crate_type}} $cc_command_args --emit=dep-info=$depfile,link -Z dep-info-omit-d-target -Z unstable-options {{rustflags}} -o $outfile LDFLAGS RUSTENV {{rustenv}}"
676
677        description = "RUST staticlib $outfile"
678        rust_sysroot = rust_sysroot_relative_to_out
679        outputs = [ outfile ]
680        default_output_extension = ".a"
681        output_prefix = "lib"
682      }
683
684      tool("rust_rlib") {
685        rlibname =
686            "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.rlib".
687        outfile = "{{output_dir}}/$rlibname"
688        depfile = "$outfile.d"
689        rspfile = "$outfile.rsp"
690        rspfile_content = "{{rustdeps}} {{externs}}"
691
692        # Don't add rspfile in rust_rlib tool.
693        pool = "//build/toolchain:link_pool($default_toolchain)"
694
695        command = "$python_path \"$rustc_wrapper\" --clang-base-dir=$_clang_base_dir --clippy-driver=$clippy_driver --rustc=$rustc --depfile=$depfile -- --crate-name {{crate_name}} $rust_dynamic {{source}} --crate-type {{crate_type}} $cc_command_args {{rustdeps}} {{externs}} --emit=dep-info=$depfile,link -Z dep-info-omit-d-target -Z unstable-options {{rustflags}} -o $outfile LDFLAGS RUSTENV {{rustenv}}"
696        description = "RUST rlib $outfile"
697        rust_sysroot = rust_sysroot_relative_to_out
698        outputs = [ outfile ]
699        default_output_extension = default_rlib_extension
700        output_prefix = "lib"
701      }
702      if (invoker_toolchain_args.current_os == "mingw") {
703        cdylib_link_option = "--out-implib"
704      } else {
705        cdylib_link_option = "-soname"
706      }
707      tool("rust_cdylib") {
708        cdylibname =
709            "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.z.so".
710        outfile = "{{output_dir}}/$cdylibname"
711        depfile = "$outfile.d"
712        rspfile = "$outfile.rsp"
713        rspfile_content = "{{rustdeps}} {{externs}}"
714
715        unstripped_outfile = "{{root_out_dir}}/lib.unstripped/$outfile"
716
717        pool = "//build/toolchain:link_pool($default_toolchain)"
718
719        strip_level = "none"  # rustc supports none, debuginfo and symbols
720                              # three strip degree.
721
722        strip_switch = " -C strip=$strip_level"
723
724        minidebug_switch = ""
725        if (full_mini_debug && !is_debug) {
726          minidebug_switch = " --mini-debug"
727        }
728
729        command = "$python_path \"$rustc_wrapper\" --clang-base-dir=$_clang_base_dir --clippy-driver=$clippy_driver --rustc=$rustc --depfile=$depfile --rsp=$rspfile --output=$outfile --unstripped-file=$unstripped_outfile --strip=$llvm_strip $minidebug_switch -- --crate-name {{crate_name}} $rust_dynamic $strip_switch {{source}} --crate-type {{crate_type}} $cc_command_args --emit=dep-info=$depfile,link -Z dep-info-omit-d-target -Z unstable-options {{rustflags}} -Clink-arg=-Wl,$cdylib_link_option=\"$cdylibname\" -o $unstripped_outfile LDFLAGS {{ldflags}} RUSTENV {{rustenv}}"
730
731        description = "RUST cdylib $outfile"
732        rust_sysroot = rust_sysroot_relative_to_out
733        outputs = [ unstripped_outfile ]
734        outputs += [ outfile ]
735        default_output_extension = default_shlib_extension
736        output_prefix = "lib"
737      }
738
739      tool("rust_bin") {
740        exename = "{{target_output_name}}{{output_extension}}"
741        outfile = "{{output_dir}}/$exename"
742        depfile = "$outfile.d"
743        rspfile = "$outfile.rsp"
744        rspfile_content = "{{rustdeps}} {{externs}}"
745
746        unstripped_outfile = "{{root_out_dir}}/exe.unstripped/$outfile"
747
748        pool = "//build/toolchain:link_pool($default_toolchain)"
749
750        strip_level = "none"  # rustc supports none, debuginfo and symbols
751                              # three strip degree.
752
753        strip_switch = " -C strip=$strip_level"
754        minidebug_switch = ""
755        if (full_mini_debug && !is_debug) {
756          minidebug_switch = " --mini-debug"
757        }
758        command = "$python_path \"$rustc_wrapper\" --clang-base-dir=$_clang_base_dir --clippy-driver=$clippy_driver --rustc=$rustc --depfile=$depfile --rsp=$rspfile --output=$outfile --unstripped-file=$unstripped_outfile --strip=$llvm_strip $minidebug_switch -- --crate-name {{crate_name}} $strip_switch {{source}} --crate-type {{crate_type}} $cc_command_args --emit=dep-info=$depfile,link -Z dep-info-omit-d-target -Z unstable-options {{rustflags}} -o $unstripped_outfile LDFLAGS RUSTENV {{rustenv}}"
759
760        description = "RUST bin $outfile"
761        rust_sysroot = rust_sysroot_relative_to_out
762        outputs = [ unstripped_outfile ]
763        outputs += [ outfile ]
764        default_output_extension = default_executable_extension
765      }
766
767      tool("rust_dylib") {
768        dylibname =
769            "{{target_output_name}}{{output_extension}}"  # e.g.
770                                                          # "libfoo.dylib.so".
771
772        outfile = "{{output_dir}}/$dylibname"
773        depfile = "$outfile.d"
774        rspfile = "$outfile.rsp"
775        rspfile_content = "{{rustdeps}} {{externs}}"
776
777        unstripped_outfile = "{{root_out_dir}}/lib.unstripped/$outfile"
778
779        pool = "//build/toolchain:link_pool($default_toolchain)"
780
781        strip_level = "none"  # rustc supports none, debuginfo and symbols
782                              # three strip degree.
783
784        strip_switch = " -C strip=$strip_level"
785        minidebug_switch = ""
786        if (full_mini_debug && !is_debug) {
787          minidebug_switch = " --mini-debug"
788        }
789        command = "$python_path \"$rustc_wrapper\" --clang-base-dir=$_clang_base_dir --clippy-driver=$clippy_driver --rustc=$rustc --depfile=$depfile --rsp=$rspfile --output=$outfile --unstripped-file=$unstripped_outfile --strip=$llvm_strip $minidebug_switch -- --crate-name {{crate_name}} $rust_dynamic $strip_switch {{source}} --crate-type {{crate_type}} $cc_command_args --emit=dep-info=$depfile,link -Z dep-info-omit-d-target -Z unstable-options {{rustflags}} -o $unstripped_outfile LDFLAGS {{ldflags}} RUSTENV {{rustenv}}"
790
791        description = "RUST dylib $outfile"
792        rust_sysroot = rust_sysroot_relative_to_out
793        outputs = [ unstripped_outfile ]
794        outputs += [ outfile ]
795        default_output_extension = default_dylib_extension
796        output_prefix = "lib"
797      }
798
799      tool("rust_macro") {
800        dylibname =
801            "{{target_output_name}}{{output_extension}}"  # e.g.
802                                                          # "libfoo.dylib.so".
803
804        outfile = "{{output_dir}}/$dylibname"
805        depfile = "$outfile.d"
806        rspfile = "$outfile.rsp"
807        rspfile_content = "{{rustdeps}} {{externs}}"
808
809        unstripped_outfile = "{{root_out_dir}}/lib.unstripped/$outfile"
810
811        pool = "//build/toolchain:link_pool($default_toolchain)"
812
813        strip_level = "none"  # rustc supports none, debuginfo and symbols
814                              # three strip degree.
815
816        strip_switch = " -C strip=$strip_level"
817        minidebug_switch = ""
818        if (full_mini_debug && !is_debug) {
819          minidebug_switch = " --mini-debug"
820        }
821        command = "$python_path \"$rustc_wrapper\" --clang-base-dir=$_clang_base_dir --clippy-driver=$clippy_driver --rustc=$rustc --depfile=$depfile --rsp=$rspfile --output=$outfile --unstripped-file=$unstripped_outfile --strip=$llvm_strip $minidebug_switch -- --crate-name {{crate_name}} $rust_dynamic $strip_switch {{source}} --crate-type {{crate_type}} $cc_command_args --emit=dep-info=$depfile,link -Z dep-info-omit-d-target -Z unstable-options {{rustflags}} -o $unstripped_outfile LDFLAGS {{ldflags}} RUSTENV {{rustenv}}"
822
823        description = "RUST proc-macro $outfile"
824        rust_sysroot = rust_sysroot_relative_to_out
825        outputs = [ unstripped_outfile ]
826        outputs += [ outfile ]
827        default_output_extension = default_dylib_extension
828        output_prefix = "lib"
829      }
830    }
831
832    tool("action") {
833      pool = "//build/toolchain:action_pool($default_toolchain)"
834    }
835
836    forward_variables_from(invoker, [ "deps" ])
837  }
838}
839
840# This is a shorthand for gcc_toolchain instances based on the Chromium-built
841# version of Clang. Only the toolchain_cpu and toolchain_os variables need to
842# be specified by the invoker, and optionally toolprefix if it's a
843# cross-compile case. Note that for a cross-compile case this toolchain
844# requires a config to pass the appropriate -target option, or else it will
845# actually just be doing a native compile. The invoker can optionally override
846# use_gold too.
847template("clang_toolchain") {
848  if (defined(invoker.toolprefix)) {
849    toolprefix = invoker.toolprefix
850  } else {
851    toolprefix = ""
852  }
853
854  gcc_toolchain(target_name) {
855    prefix = rebase_path("$clang_base_path/bin", root_build_dir)
856    cc = "$prefix/clang"
857    cxx = "$prefix/clang++"
858    ld = cxx
859    readelf = "${toolprefix}readelf"
860    ar = "${prefix}/llvm-ar"
861    nm = "${toolprefix}nm"
862
863    forward_variables_from(invoker,
864                           [
865                             "strip",
866                             "is_clang_analysis_supported",
867                             "enable_linker_map",
868                             "use_unstripped_as_runtime_outputs",
869                             "rust_abi_target",
870                           ])
871
872    toolchain_args = {
873      if (defined(invoker.toolchain_args)) {
874        forward_variables_from(invoker.toolchain_args, "*")
875      }
876      is_clang = true
877    }
878
879    if (defined(invoker.shlib_extension) && invoker.shlib_extension != "") {
880      shlib_extension = invoker.shlib_extension
881    }
882    if (defined(rust_abi_target)) {
883      if (rust_abi_target == "x86_64-unknown-linux-gnu") {
884        clang_lib_path = rebase_path("$clang_base_path/lib", root_build_dir)
885        cc_command_args = "--target=${rust_abi_target} -Clinker=$clang -lstdc++ -lclang -L${clang_lib_path} -Clink-arg=-fuse-ld=lld -Clink-arg=-v"
886      }
887    }
888  }
889}
890