xref: /aosp_15_r20/external/cronet/build/toolchain/gcc_toolchain.gni (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2013 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import("//build/config/clang/clang.gni")
6import("//build/config/compiler/compiler.gni")
7import("//build/config/coverage/coverage.gni")
8import("//build/config/rust.gni")
9import("//build/config/sanitizers/sanitizers.gni")
10import("//build/config/sysroot.gni")
11import("//build/config/v8_target_cpu.gni")
12import("//build/toolchain/cc_wrapper.gni")
13import("//build/toolchain/goma.gni")
14import("//build/toolchain/rbe.gni")
15import("//build/toolchain/toolchain.gni")
16
17if (is_nacl) {
18  # To keep NaCl variables out of builds that don't include NaCl, all
19  # variables defined in nacl/config.gni referenced here should be protected by
20  # is_nacl conditions.
21  import("//build/config/nacl/config.gni")
22}
23
24declare_args() {
25  # Enables allowlist generation for IDR_ grit defines seen by the compiler.
26  # Currently works only on some platforms and enabled by default for official
27  # builds. Requires debug info.
28  enable_resource_allowlist_generation =
29      is_official_build &&
30      # Don't enable for Android-on-Chrome OS.
31      (target_os == "android" || target_os == "win")
32
33  # Use -MD instead of -MMD for compiler commands. This is useful for tracking
34  # the comprehensive set of dependencies.  It's also required when building
35  # without the sysroot so that updates to system header files trigger a
36  # rebuild (when using the sysroot, the CR_SYSROOT_KEY define takes care of
37  # this already).
38  system_headers_in_deps = !use_sysroot
39}
40
41# When the arg is set via args.gn, it applies to all toolchains. In order to not
42# hit the assert in grit_rule.gni, explicitly disable for host toolchains.
43if ((is_linux || is_chromeos) && target_os == "android") {
44  enable_resource_allowlist_generation = false
45}
46
47# Ensure enable_resource_allowlist_generation is enabled only when it will work.
48if (enable_resource_allowlist_generation) {
49  assert(
50      !strip_debug_info,
51      "enable_resource_allowlist_generation=true requires strip_debug_info=false")
52  assert(
53      !is_component_build,
54      "enable_resource_allowlist_generation=true requires is_component_build=false")
55  assert(
56      target_os == "android" || target_os == "win",
57      "enable_resource_allowlist_generation=true does not work for target_os=$target_os")
58}
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#  - link_outputs
86#      The content of this array, if specified, will be added to the list of
87#      outputs from the link command. This can be useful in conjunction with
88#      the post_link parameter.
89#  - use_unstripped_as_runtime_outputs
90#      When |strip| is set, mark unstripped executables as runtime deps rather
91#      than stripped ones.
92#  - post_link
93#      The content of this string, if specified, will be run as a separate
94#      command following the the link command.
95#  - deps
96#      Just forwarded to the toolchain definition.
97#  - executable_extension
98#      If this string is specified it will be used for the file extension
99#      for an executable, rather than using no extension; targets will
100#      still be able to override the extension using the output_extension
101#      variable.
102#  - rebuild_define
103#      The contents of this string, if specified, will be passed as a #define
104#      to the toolchain. It can be used to force recompiles whenever a
105#      toolchain is updated.
106#  - shlib_extension
107#      If this string is specified it will be used for the file extension
108#      for a shared library, rather than default value specified in
109#      toolchain.gni
110#  - strip
111#      Location of the strip executable. When specified, strip will be run on
112#      all shared libraries and executables as they are built. The pre-stripped
113#      artifacts will be put in lib.unstripped/ and exe.unstripped/.
114#
115# Callers will normally want to invoke "gcc_toolchain" instead, which makes an
116# additional toolchain for Rust targets that are build-time artificts such as
117# proc macros.
118template("single_gcc_toolchain") {
119  toolchain(target_name) {
120    assert(defined(invoker.ar), "gcc_toolchain() must specify a \"ar\" value")
121    assert(defined(invoker.cc), "gcc_toolchain() must specify a \"cc\" value")
122    assert(defined(invoker.cxx), "gcc_toolchain() must specify a \"cxx\" value")
123    assert(defined(invoker.ld), "gcc_toolchain() must specify a \"ld\" value")
124
125    # This define changes when the toolchain changes, forcing a rebuild.
126    # Nothing should ever use this define.
127    if (defined(invoker.rebuild_define)) {
128      rebuild_string = "-D" + invoker.rebuild_define + " "
129    } else {
130      rebuild_string = ""
131    }
132
133    # GN's syntax can't handle more than one scope dereference at once, like
134    # "invoker.toolchain_args.foo", so make a temporary to hold the toolchain
135    # args so we can do "invoker_toolchain_args.foo".
136    assert(defined(invoker.toolchain_args),
137           "Toolchains must specify toolchain_args")
138    invoker_toolchain_args = invoker.toolchain_args
139    assert(defined(invoker_toolchain_args.current_cpu),
140           "toolchain_args must specify a current_cpu")
141    assert(defined(invoker_toolchain_args.current_os),
142           "toolchain_args must specify a current_os")
143
144    # When invoking this toolchain not as the default one, these args will be
145    # passed to the build. They are ignored when this is the default toolchain.
146    toolchain_args = {
147      # Populate toolchain args from the invoker.
148      forward_variables_from(invoker_toolchain_args, "*")
149
150      # The host toolchain value computed by the default toolchain's setup
151      # needs to be passed through unchanged to all secondary toolchains to
152      # ensure that it's always the same, regardless of the values that may be
153      # set on those toolchains.
154      host_toolchain = host_toolchain
155
156      if (!defined(invoker_toolchain_args.v8_current_cpu)) {
157        v8_current_cpu = invoker_toolchain_args.current_cpu
158      }
159    }
160
161    # When the invoker has explicitly overridden use_remoteexec, use_goma or
162    # cc_wrapper in the toolchain args, use those values, otherwise default
163    # to the global one.  This works because the only reasonable override
164    # that toolchains might supply for these values are to force-disable them.
165    if (defined(toolchain_args.use_remoteexec)) {
166      toolchain_uses_remoteexec = toolchain_args.use_remoteexec
167    } else {
168      toolchain_uses_remoteexec = use_remoteexec
169    }
170    if (defined(toolchain_args.use_remoteexec_links)) {
171      toolchain_uses_remoteexec_links = toolchain_args.use_remoteexec_links
172    } else {
173      toolchain_uses_remoteexec_links = use_remoteexec_links
174    }
175    if (defined(toolchain_args.use_goma)) {
176      toolchain_uses_goma = toolchain_args.use_goma
177    } else {
178      toolchain_uses_goma = use_goma
179    }
180
181    # x86_64-nacl-* is ELF-32 and Goma/RBE won't support ELF-32.
182    if (toolchain_uses_goma &&
183        get_path_info(invoker.cc, "name") == "x86_64-nacl-gcc") {
184      # it will also disable x86_64-nacl-g++ since these are in
185      # the same toolchain.
186      toolchain_uses_goma = false
187    }
188    if (defined(toolchain_args.cc_wrapper)) {
189      toolchain_cc_wrapper = toolchain_args.cc_wrapper
190    } else {
191      toolchain_cc_wrapper = cc_wrapper
192    }
193    assert(!(toolchain_uses_remoteexec && toolchain_uses_goma),
194           "Goma and re-client can't be used together.")
195    assert(!(toolchain_cc_wrapper != "" && toolchain_uses_remoteexec),
196           "re-client and cc_wrapper can't be used together.")
197    assert(!(toolchain_cc_wrapper != "" && toolchain_uses_goma),
198           "Goma and cc_wrapper can't be used together.")
199
200    # When the invoker has explicitly overridden use_goma or cc_wrapper in the
201    # toolchain args, use those values, otherwise default to the global one.
202    # This works because the only reasonable override that toolchains might
203    # supply for these values are to force-disable them.
204    # But if needs_gomacc_path_arg is set in a Chrome OS build, the toolchain
205    # wrapper will have picked up gomacc via cmd-line arg. So need to prepend
206    # gomacc in that case.
207    goma_path = "$goma_dir/gomacc"
208    if (toolchain_uses_remoteexec && (!defined(invoker.needs_gomacc_path_arg) ||
209                                      !invoker.needs_gomacc_path_arg)) {
210      if (defined(toolchain_args.rbe_cc_cfg_file)) {
211        toolchain_rbe_cc_cfg_file = toolchain_args.rbe_cc_cfg_file
212      } else {
213        toolchain_rbe_cc_cfg_file = rbe_cc_cfg_file
214      }
215
216      # C/C++ (clang) rewrapper prefix to use when use_remoteexec is true.
217      compiler_prefix = "${rbe_bin_dir}/rewrapper -cfg=${toolchain_rbe_cc_cfg_file}${rbe_bug_326584510_missing_inputs} -exec_root=${rbe_exec_root} "
218    } else if (toolchain_uses_goma &&
219               (!defined(invoker.needs_gomacc_path_arg) ||
220                !invoker.needs_gomacc_path_arg)) {
221      compiler_prefix = "${goma_path} "
222    } else {
223      compiler_prefix = "${toolchain_cc_wrapper} "
224
225      # Prevent warning about unused variable since it is not read in the code
226      # paths when goma is not needed.
227      not_needed(invoker, [ "needs_gomacc_path_arg" ])
228    }
229
230    if (toolchain_uses_remoteexec_links) {
231      if (defined(toolchain_args.rbe_link_cfg_file)) {
232        toolchain_rbe_link_cfg_file = toolchain_args.rbe_link_cfg_file
233      } else {
234        toolchain_rbe_link_cfg_file = rbe_link_cfg_file
235      }
236      link_prefix = "${rbe_bin_dir}/rewrapper -cfg=${toolchain_rbe_link_cfg_file} -exec_root=${rbe_exec_root} "
237      not_needed([ "goma_path" ])
238    } else {
239      link_prefix = ""
240      not_needed([ "goma_path" ])
241    }
242
243    # A specific toolchain may wish to avoid coverage instrumentation, so we
244    # allow the global "use_clang_coverage" arg to be overridden.
245    if (defined(toolchain_args.use_clang_coverage)) {
246      toolchain_use_clang_coverage = toolchain_args.use_clang_coverage
247    } else {
248      toolchain_use_clang_coverage = use_clang_coverage
249    }
250
251    # For a coverage build, we use the wrapper script globally so that it can
252    # remove coverage cflags from files that should not have them.
253    if (toolchain_use_clang_coverage) {
254      # "coverage_instrumentation_input_file" is set in args.gn, but it can be
255      # overridden by a toolchain config.
256      if (defined(toolchain_args.coverage_instrumentation_input_file)) {
257        toolchain_coverage_instrumentation_input_file =
258            toolchain_args.coverage_instrumentation_input_file
259      } else {
260        toolchain_coverage_instrumentation_input_file =
261            coverage_instrumentation_input_file
262      }
263
264      _coverage_wrapper =
265          rebase_path("//build/toolchain/clang_code_coverage_wrapper.py",
266                      root_build_dir)
267
268      # The wrapper needs to know what OS we target because it uses that to
269      # select a list of files that should not be instrumented.
270      _coverage_wrapper = _coverage_wrapper + " --target-os=" +
271                          invoker_toolchain_args.current_os
272
273      # We want to instrument everything if there is no input file set.
274      # If there is a file we need to give it to the wrapper script so it can
275      # instrument only those files.
276      if (toolchain_coverage_instrumentation_input_file != "") {
277        _coverage_wrapper =
278            _coverage_wrapper + " --files-to-instrument=" +
279            rebase_path(toolchain_coverage_instrumentation_input_file,
280                        root_build_dir)
281      }
282      compiler_prefix =
283          "\"$python_path\" ${_coverage_wrapper} " + compiler_prefix
284    }
285
286    cc = compiler_prefix + invoker.cc
287    cxx = compiler_prefix + invoker.cxx
288
289    # "asm" doesn't support any of toolchain_cc_wrapper, toolchain_uses_goma and
290    # toolchain_uses_remoteexec. The coverage flags are also nonsensical on
291    # assembler runs.
292    asm = invoker.cc
293    ar = invoker.ar
294    ld = link_prefix + invoker.ld
295    if (defined(invoker.readelf)) {
296      readelf = invoker.readelf
297    } else {
298      readelf = "readelf"
299    }
300    if (defined(invoker.nm)) {
301      nm = invoker.nm
302    } else {
303      nm = "nm"
304    }
305    if (defined(invoker.dwp)) {
306      dwp_switch = " --dwp=\"${invoker.dwp}\""
307    } else {
308      dwp_switch = ""
309    }
310
311    if (defined(invoker.shlib_extension)) {
312      default_shlib_extension = invoker.shlib_extension
313    } else {
314      default_shlib_extension = shlib_extension
315    }
316
317    if (defined(invoker.default_shlib_subdir)) {
318      default_shlib_subdir = invoker.default_shlib_subdir
319    } else {
320      default_shlib_subdir = ""
321    }
322
323    if (defined(invoker.executable_extension)) {
324      default_executable_extension = invoker.executable_extension
325    } else {
326      default_executable_extension = ""
327    }
328
329    # Bring these into our scope for string interpolation with default values.
330    if (defined(invoker.extra_cflags) && invoker.extra_cflags != "") {
331      extra_cflags = " " + invoker.extra_cflags
332    } else {
333      extra_cflags = ""
334    }
335
336    if (defined(invoker.extra_cppflags) && invoker.extra_cppflags != "") {
337      extra_cppflags = " " + invoker.extra_cppflags
338    } else {
339      extra_cppflags = ""
340    }
341
342    if (defined(invoker.extra_cxxflags) && invoker.extra_cxxflags != "") {
343      extra_cxxflags = " " + invoker.extra_cxxflags
344    } else {
345      extra_cxxflags = ""
346    }
347
348    if (defined(invoker.extra_asmflags) && invoker.extra_asmflags != "") {
349      extra_asmflags = " " + invoker.extra_asmflags
350    } else {
351      extra_asmflags = ""
352    }
353
354    if (defined(invoker.extra_ldflags) && invoker.extra_ldflags != "") {
355      extra_ldflags = " " + invoker.extra_ldflags
356    } else {
357      extra_ldflags = ""
358    }
359
360    if (system_headers_in_deps) {
361      md = "-MD"
362    } else {
363      md = "-MMD"
364    }
365
366    enable_linker_map = defined(invoker.enable_linker_map) &&
367                        invoker.enable_linker_map && generate_linker_map
368
369    # These library switches can apply to all tools below.
370    lib_switch = "-l"
371    lib_dir_switch = "-L"
372
373    # Object files go in this directory.
374    object_subdir = "{{target_out_dir}}/{{label_name}}"
375
376    tool("cc") {
377      depfile = "{{output}}.d"
378      precompiled_header_type = "gcc"
379      command = "$cc $md -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}${extra_cppflags}${extra_cflags} -c {{source}} -o {{output}}"
380      depsformat = "gcc"
381      description = "CC {{output}}"
382      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
383    }
384
385    tool("cxx") {
386      depfile = "{{output}}.d"
387      precompiled_header_type = "gcc"
388      command = "$cxx $md -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}${extra_cppflags}${extra_cxxflags} -c {{source}} -o {{output}}"
389      depsformat = "gcc"
390      description = "CXX {{output}}"
391      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
392    }
393
394    tool("asm") {
395      # For GCC we can just use the C compiler to compile assembly.
396      depfile = "{{output}}.d"
397      command = "$asm $md -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{asmflags}}${extra_asmflags} -c {{source}} -o {{output}}"
398      depsformat = "gcc"
399      description = "ASM {{output}}"
400      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
401    }
402
403    tool("alink") {
404      if (current_os == "aix") {
405        # AIX does not support either -D (deterministic output) or response
406        # files.
407        command = "$ar -X64 {{arflags}} -r -c -s {{output}} {{inputs}}"
408      } else {
409        rspfile = "{{output}}.rsp"
410        rspfile_content = "{{inputs}}"
411        command = "\"$ar\" {{arflags}} -r -c -s -D {{output}} @\"$rspfile\""
412      }
413
414      # Remove the output file first so that ar doesn't try to modify the
415      # existing file.
416      if (host_os == "win") {
417        tool_wrapper_path =
418            rebase_path("//build/toolchain/win/tool_wrapper.py", root_build_dir)
419        command = "cmd /s /c \"\"$python_path\" $tool_wrapper_path delete-file {{output}} && $command\""
420      } else {
421        command = "rm -f {{output}} && $command"
422      }
423
424      # Almost all targets build with //build/config/compiler:thin_archive which
425      # adds -T to arflags.
426      description = "AR {{output}}"
427      outputs = [ "{{output_dir}}/{{target_output_name}}{{output_extension}}" ]
428
429      # Static libraries go in the target out directory by default so we can
430      # generate different targets with the same name and not have them collide.
431      default_output_dir = "{{target_out_dir}}"
432      default_output_extension = ".a"
433      output_prefix = "lib"
434    }
435
436    tool("solink") {
437      soname = "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.so".
438      sofile = "{{output_dir}}/$soname"  # Possibly including toolchain dir.
439      rspfile = sofile + ".rsp"
440
441      pool = "//build/toolchain:link_pool($default_toolchain)"
442
443      if (defined(invoker.strip)) {
444        unstripped_sofile = "{{root_out_dir}}/lib.unstripped/$soname"
445      } else {
446        unstripped_sofile = sofile
447      }
448
449      # These variables are not built into GN but are helpers that
450      # implement (1) linking to produce a .so, (2) extracting the symbols
451      # from that file (3) if the extracted list differs from the existing
452      # .TOC file, overwrite it, otherwise, don't change it.
453      tocfile = sofile + ".TOC"
454
455      soname_flag = ""
456      if (current_os != "aix") {
457        # -soname flag is not available on aix ld
458        soname_flag = "-Wl,-soname=\"$soname\""
459      }
460      link_command = "$ld -shared $soname_flag {{ldflags}}${extra_ldflags} -o \"$unstripped_sofile\" @\"$rspfile\" {{rlibs}}"
461
462      # Generate a map file to be used for binary size analysis.
463      # Map file adds ~10% to the link time on a z620.
464      # With target_os="android", libchrome.so.map.gz is ~20MB.
465      map_switch = ""
466      if (enable_linker_map) {
467        map_file = "$unstripped_sofile.map.gz"
468        map_switch = " --map-file \"$map_file\""
469      }
470
471      assert(defined(readelf), "to solink you must have a readelf")
472      assert(defined(nm), "to solink you must have an nm")
473      strip_switch = ""
474      if (defined(invoker.strip)) {
475        strip_switch = "--strip=${invoker.strip} "
476      }
477
478      # This needs a Python script to avoid using a complex shell command
479      # requiring sh control structures, pipelines, and POSIX utilities.
480      # The host might not have a POSIX shell and utilities (e.g. Windows).
481      solink_wrapper =
482          rebase_path("//build/toolchain/gcc_solink_wrapper.py", root_build_dir)
483      solink_extra_flags = ""
484      if (current_os == "aix") {
485        # to be intercepted by solink_wrapper, so that we exit immediately
486        # after linking the shared object, without generating the TOC file
487        # (skipped on Aix)
488        solink_extra_flags = "--partitioned-library"
489      }
490      command = "\"$python_path\" \"$solink_wrapper\" --readelf=\"$readelf\" --nm=\"$nm\" $strip_switch$dwp_switch --sofile=\"$unstripped_sofile\" --tocfile=\"$tocfile\"$map_switch --output=\"$sofile\" -- $link_command $solink_extra_flags"
491
492      if (target_cpu == "mipsel" && is_component_build && is_android) {
493        rspfile_content = "-Wl,--start-group -Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive {{libs}} -Wl,--end-group"
494      } else if (current_os == "aix") {
495        # --whole-archive, --no-whole-archive flags are not available on the aix
496        # ld.
497        rspfile_content = "{{inputs}} {{solibs}} {{libs}}"
498      } else {
499        rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive {{libs}}"
500      }
501
502      description = "SOLINK $sofile"
503
504      # Use this for {{output_extension}} expansions unless a target manually
505      # overrides it (in which case {{output_extension}} will be what the target
506      # specifies).
507      default_output_extension = default_shlib_extension
508
509      default_output_dir = "{{root_out_dir}}${default_shlib_subdir}"
510
511      output_prefix = "lib"
512
513      # Since the above commands only updates the .TOC file when it changes, ask
514      # Ninja to check if the timestamp actually changed to know if downstream
515      # dependencies should be recompiled.
516      restat = true
517
518      # Tell GN about the output files. It will link to the sofile but use the
519      # tocfile for dependency management.
520      outputs = [
521        sofile,
522        tocfile,
523      ]
524      if (sofile != unstripped_sofile) {
525        outputs += [ unstripped_sofile ]
526        if (defined(invoker.use_unstripped_as_runtime_outputs) &&
527            invoker.use_unstripped_as_runtime_outputs) {
528          runtime_outputs = [ unstripped_sofile ]
529        }
530      }
531
532      # Clank build will generate DWP files when Fission is used.
533      # Other builds generate DWP files outside of the gn link targets, if at
534      # all.
535      if (defined(invoker.dwp)) {
536        outputs += [ unstripped_sofile + ".dwp" ]
537        if (defined(invoker.use_unstripped_as_runtime_outputs) &&
538            invoker.use_unstripped_as_runtime_outputs) {
539          runtime_outputs += [ unstripped_sofile + ".dwp" ]
540        }
541      }
542      if (defined(map_file)) {
543        outputs += [ map_file ]
544      }
545      link_output = sofile
546      depend_output = tocfile
547    }
548
549    tool("solink_module") {
550      soname = "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.so".
551      sofile = "{{output_dir}}/$soname"
552      rspfile = sofile + ".rsp"
553
554      pool = "//build/toolchain:link_pool($default_toolchain)"
555
556      if (defined(invoker.strip)) {
557        unstripped_sofile = "{{root_out_dir}}/lib.unstripped/$soname"
558      } else {
559        unstripped_sofile = sofile
560      }
561
562      soname_flag = ""
563      whole_archive_flag = ""
564      no_whole_archive_flag = ""
565      if (current_os != "aix") {
566        # -soname, --whole-archive, --no-whole-archive flags are not available
567        # on aix ld
568        soname_flag = "-Wl,-soname=\"$soname\""
569        whole_archive_flag = "-Wl,--whole-archive"
570        no_whole_archive_flag = "-Wl,--no-whole-archive"
571      }
572      command = "$ld -shared {{ldflags}}${extra_ldflags} -o \"$unstripped_sofile\" $soname_flag @\"$rspfile\""
573
574      if (defined(invoker.strip)) {
575        strip_command = "${invoker.strip} -o \"$sofile\" \"$unstripped_sofile\""
576        command += " && " + strip_command
577      }
578      rspfile_content = "$whole_archive_flag {{inputs}} {{solibs}} $no_whole_archive_flag {{libs}} {{rlibs}}"
579
580      description = "SOLINK_MODULE $sofile"
581
582      # Use this for {{output_extension}} expansions unless a target manually
583      # overrides it (in which case {{output_extension}} will be what the target
584      # specifies).
585      if (defined(invoker.loadable_module_extension)) {
586        default_output_extension = invoker.loadable_module_extension
587      } else {
588        default_output_extension = default_shlib_extension
589      }
590
591      default_output_dir = "{{root_out_dir}}${default_shlib_subdir}"
592
593      output_prefix = "lib"
594
595      outputs = [ sofile ]
596      if (sofile != unstripped_sofile) {
597        outputs += [ unstripped_sofile ]
598        if (defined(invoker.use_unstripped_as_runtime_outputs) &&
599            invoker.use_unstripped_as_runtime_outputs) {
600          runtime_outputs = [ unstripped_sofile ]
601        }
602      }
603    }
604
605    tool("link") {
606      exename = "{{target_output_name}}{{output_extension}}"
607      outfile = "{{output_dir}}/$exename"
608      rspfile = "$outfile.rsp"
609      unstripped_outfile = outfile
610
611      pool = "//build/toolchain:link_pool($default_toolchain)"
612
613      # Use this for {{output_extension}} expansions unless a target manually
614      # overrides it (in which case {{output_extension}} will be what the target
615      # specifies).
616      default_output_extension = default_executable_extension
617
618      default_output_dir = "{{root_out_dir}}"
619
620      if (defined(invoker.strip)) {
621        unstripped_outfile = "{{root_out_dir}}/exe.unstripped/$exename"
622      }
623
624      start_group_flag = ""
625      end_group_flag = ""
626      if (current_os != "aix") {
627        # the "--start-group .. --end-group" feature isn't available on the aix
628        # ld.
629        start_group_flag = "-Wl,--start-group"
630        end_group_flag = "-Wl,--end-group "
631      }
632      link_command = "$ld {{ldflags}}${extra_ldflags} -o \"$unstripped_outfile\" $start_group_flag @\"$rspfile\" {{solibs}} $end_group_flag {{libs}} {{rlibs}}"
633
634      # Generate a map file to be used for binary size analysis.
635      # Map file adds ~10% to the link time on a z620.
636      # With target_os="android", libchrome.so.map.gz is ~20MB.
637      map_switch = ""
638      if (enable_linker_map) {
639        map_file = "$unstripped_outfile.map.gz"
640        map_switch = " --map-file \"$map_file\""
641      }
642
643      strip_switch = ""
644      if (defined(invoker.strip)) {
645        strip_switch = " --strip=\"${invoker.strip}\" --unstripped-file=\"$unstripped_outfile\""
646      }
647
648      link_wrapper =
649          rebase_path("//build/toolchain/gcc_link_wrapper.py", root_build_dir)
650      command = "\"$python_path\" \"$link_wrapper\" --output=\"$outfile\"$strip_switch$map_switch$dwp_switch -- $link_command"
651
652      description = "LINK $outfile"
653
654      rspfile_content = "{{inputs}}"
655      outputs = [ outfile ]
656      if (outfile != unstripped_outfile) {
657        outputs += [ unstripped_outfile ]
658        if (defined(invoker.use_unstripped_as_runtime_outputs) &&
659            invoker.use_unstripped_as_runtime_outputs) {
660          runtime_outputs = [ unstripped_outfile ]
661        }
662      }
663
664      # Clank build will generate DWP files when Fission is used.
665      # Other builds generate DWP files outside of the gn link targets, if at
666      # all.
667      if (defined(invoker.dwp)) {
668        outputs += [ unstripped_outfile + ".dwp" ]
669        if (defined(invoker.use_unstripped_as_runtime_outputs) &&
670            invoker.use_unstripped_as_runtime_outputs) {
671          runtime_outputs += [ unstripped_outfile + ".dwp" ]
672        }
673      }
674      if (defined(invoker.link_outputs)) {
675        outputs += invoker.link_outputs
676      }
677      if (defined(map_file)) {
678        outputs += [ map_file ]
679      }
680    }
681
682    # These two are really entirely generic, but have to be repeated in
683    # each toolchain because GN doesn't allow a template to be used here.
684    # See //build/toolchain/toolchain.gni for details.
685    tool("stamp") {
686      command = stamp_command
687      description = stamp_description
688    }
689    tool("copy") {
690      command = copy_command
691      description = copy_description
692    }
693
694    tool("action") {
695      pool = "//build/toolchain:action_pool($default_toolchain)"
696    }
697
698    if (toolchain_has_rust) {
699      if (!defined(rust_compiler_prefix)) {
700        rust_compiler_prefix = ""
701      }
702      rust_sysroot_relative = rebase_path(rust_sysroot, root_build_dir)
703      rustc_bin = "$rust_sysroot_relative/bin/rustc"
704      rustc = "$rust_compiler_prefix${rustc_bin}"
705      rustc_wrapper =
706          rebase_path("//build/rust/rustc_wrapper.py", root_build_dir)
707
708      # RSP manipulation due to https://bugs.chromium.org/p/gn/issues/detail?id=249
709      tool("rust_staticlib") {
710        libname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
711        rspfile = "$libname.rsp"
712        depfile = "$libname.d"
713
714        default_output_extension = ".a"
715        output_prefix = "lib"
716
717        # Static libraries go in the target out directory by default so we can
718        # generate different targets with the same name and not have them
719        # collide.
720        default_output_dir = "{{target_out_dir}}"
721        description = "RUST(STATICLIB) {{output}}"
722        outputs = [ libname ]
723
724        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
725        command = "\"$python_path\" \"$rustc_wrapper\" --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- -Clinker=\"${invoker.cxx}\" $rustc_common_args --emit=dep-info=$depfile,link -o $libname LDFLAGS RUSTENV {{rustenv}}"
726        rust_sysroot = rust_sysroot_relative
727      }
728
729      tool("rust_rlib") {
730        # We must always prefix with `lib` even if the library already starts
731        # with that prefix or else our stdlib is unable to find libc.rlib (or
732        # actually liblibc.rlib).
733        rlibname =
734            "{{output_dir}}/lib{{target_output_name}}{{output_extension}}"
735        rspfile = "$rlibname.rsp"
736        depfile = "$rlibname.d"
737
738        default_output_extension = ".rlib"
739
740        # This is prefixed unconditionally in `rlibname`.
741        # output_prefix = "lib"
742        # Static libraries go in the target out directory by default so we can
743        # generate different targets with the same name and not have them
744        # collide.
745        default_output_dir = "{{target_out_dir}}"
746        description = "RUST {{output}}"
747        outputs = [ rlibname ]
748
749        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
750        command = "\"$python_path\" \"$rustc_wrapper\" --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- -Clinker=\"${invoker.cxx}\" $rustc_common_args --emit=dep-info=$depfile,link -o $rlibname LDFLAGS RUSTENV {{rustenv}}"
751        rust_sysroot = rust_sysroot_relative
752      }
753
754      tool("rust_bin") {
755        exename = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
756        depfile = "$exename.d"
757        rspfile = "$exename.rsp"
758        pool = "//build/toolchain:link_pool($default_toolchain)"
759
760        default_output_extension = default_executable_extension
761        default_output_dir = "{{root_out_dir}}"
762        description = "RUST(BIN) {{output}}"
763        outputs = [ exename ]
764
765        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
766        command = "\"$python_path\" \"$rustc_wrapper\" --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- -Clinker=\"${invoker.cxx}\" $rustc_common_args --emit=dep-info=$depfile,link -o $exename LDFLAGS {{ldflags}} ${extra_ldflags} RUSTENV {{rustenv}}"
767        rust_sysroot = rust_sysroot_relative
768      }
769
770      tool("rust_cdylib") {
771        dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
772        depfile = "$dllname.d"
773        rspfile = "$dllname.rsp"
774        pool = "//build/toolchain:link_pool($default_toolchain)"
775
776        default_output_extension = default_shlib_extension
777        output_prefix = "lib"
778        default_output_dir = "{{root_out_dir}}${default_shlib_subdir}"
779        description = "RUST(CDYLIB) {{output}}"
780        outputs = [ dllname ]
781
782        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
783        command = "\"$python_path\" \"$rustc_wrapper\" --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- -Clinker=\"${invoker.cxx}\" $rustc_common_args --emit=dep-info=$depfile,link -o $dllname LDFLAGS {{ldflags}} ${extra_ldflags} RUSTENV {{rustenv}}"
784        rust_sysroot = rust_sysroot_relative
785      }
786
787      tool("rust_macro") {
788        dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
789        depfile = "$dllname.d"
790        rspfile = "$dllname.rsp"
791        pool = "//build/toolchain:link_pool($default_toolchain)"
792
793        default_output_extension = default_shlib_extension
794        output_prefix = "lib"
795        default_output_dir = "{{root_out_dir}}${default_shlib_subdir}"
796        description = "RUST(MACRO) {{output}}"
797        outputs = [ dllname ]
798
799        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
800        command = "\"$python_path\" \"$rustc_wrapper\" --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- -Clinker=\"${invoker.cxx}\" $rustc_common_args --emit=dep-info=$depfile,link -o $dllname LDFLAGS {{ldflags}} ${extra_ldflags} RUSTENV {{rustenv}}"
801        rust_sysroot = rust_sysroot_relative
802      }
803    }
804
805    forward_variables_from(invoker,
806                           [
807                             "deps",
808                             "propagates_configs",
809                           ])
810  }
811}
812
813# Makes a GCC toolchain for the target, and an equivalent toolchain with the
814# prebuilt Rust stdlib for building proc macros (and other for-build-use
815# artifacts).
816template("gcc_toolchain") {
817  single_gcc_toolchain(target_name) {
818    assert(defined(invoker.toolchain_args),
819           "Toolchains must declare toolchain_args")
820    forward_variables_from(invoker,
821                           "*",
822                           [
823                             "visibility",
824                             "test_only",
825                           ])
826
827    # No need to forward visibility and test_only as they apply to targets not
828    # toolchains, but presubmit checks require that we explicitly exclude them
829  }
830
831  if (enable_rust && current_toolchain == default_toolchain) {
832    # Make an additional toolchain which is used for making tools that are run
833    # on the host machine as part of the build process (such as proc macros
834    # and Cargo build scripts). This toolchain uses the prebuilt stdlib that
835    # comes with the compiler, so it doesn't have to wait for the stdlib to be
836    # built before building other stuff. And this ensures its proc macro
837    # outputs have the right ABI to be loaded by the compiler, and it can be
838    # used to compile build scripts that are part of the stdlib that is built
839    # for the default toolchain.
840    single_gcc_toolchain("${target_name}_for_rust_host_build_tools") {
841      assert(defined(invoker.toolchain_args),
842             "Toolchains must declare toolchain_args")
843      forward_variables_from(invoker,
844                             "*",
845                             [
846                               "toolchain_args",
847                               "visibility",
848                               "test_only",
849                             ])
850      toolchain_args = {
851        # Populate toolchain args from the invoker.
852        forward_variables_from(invoker.toolchain_args, "*")
853        toolchain_for_rust_host_build_tools = true
854
855        # The host build tools are static release builds to make the Chromium
856        # build faster.
857        is_debug = false
858        is_component_build = false
859        is_official_build = false
860        use_clang_coverage = false
861        use_sanitizer_coverage = false
862        generate_linker_map = false
863        use_thin_lto = false
864      }
865
866      # When cross-compiling we don't want to use the target platform's file
867      # extensions.
868      shlib_extension = host_shlib_extension
869    }
870  }
871}
872
873# This is a shorthand for gcc_toolchain instances based on the Chromium-built
874# version of Clang. Only the toolchain_cpu and toolchain_os variables need to
875# be specified by the invoker, and optionally toolprefix if it's a
876# cross-compile case. Note that for a cross-compile case this toolchain
877# requires a config to pass the appropriate -target option, or else it will
878# actually just be doing a native compile. The invoker can optionally override
879# use_gold too.
880template("clang_toolchain") {
881  gcc_toolchain(target_name) {
882    _path = "$clang_base_path/bin"
883    _is_path_absolute = get_path_info(_path, "abspath") == _path
884
885    # Preserve absolute paths for tools like distcc.
886    if (_is_path_absolute && filter_include([ _path ], [ "//*" ]) == []) {
887      prefix = _path
888    } else {
889      prefix = rebase_path(_path, root_build_dir)
890    }
891
892    cc = "${prefix}/clang"
893    cxx = "${prefix}/clang++"
894    ld = cxx
895    readelf = "${prefix}/llvm-readelf"
896    ar = "${prefix}/llvm-ar"
897    nm = "${prefix}/llvm-nm"
898
899    forward_variables_from(invoker, "*", [ "toolchain_args" ])
900
901    toolchain_args = {
902      if (defined(invoker.toolchain_args)) {
903        forward_variables_from(invoker.toolchain_args, "*")
904      }
905      is_clang = true
906    }
907  }
908}
909