• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5# TODO(brettw) Use "gcc_toolchain.gni" like the Linux toolchains. This requires
6# some enhancements since the commands on Mac are slightly different than on
7# Linux.
8
9import("//build/config/apple/symbols.gni")
10import("//build/config/clang/clang.gni")
11import("//build/config/compiler/compiler.gni")
12import("//build/config/coverage/coverage.gni")
13import("//build/config/rust.gni")
14import("//build/toolchain/cc_wrapper.gni")
15import("//build/toolchain/rbe.gni")
16import("//build/toolchain/toolchain.gni")
17import("//build_overrides/build.gni")
18
19# TODO(crbug.com/40869822): This import is required to detect whether the
20# build is for the catalyst environment in order to disable the hermetic
21# swift compiler (as it does not include support for catalyst). Remove it
22# once the support is available.
23if (is_ios) {
24  import("//build/config/apple/mobile_config.gni")
25  import("//build/config/ios/ios_sdk.gni")
26}
27
28assert((target_os == "ios" && host_os == "mac") || host_os != "win")
29
30declare_args() {
31  # This controls whether whole module optimization is enabled when building
32  # Swift modules. If enabled, the compiler will compile the module as one
33  # unit, generating just one single object file. Otherwise, it will generate
34  # one object file per .swift file. If unspecified, will default to "true"
35  # for official builds, and "false" for all other builds.
36  swift_whole_module_optimization = -1
37
38  # If true, the intermediate build products of swift module compilation will
39  # be kept after the invocation of the swiftc compiler. Otherwise they will
40  # deleted between each invocation.
41  swift_keep_intermediate_files = false
42
43  # If unspecified, will use the toolchain downloaded via deps.
44  swift_toolchain_path = -1
45}
46
47# TODO(crbug.com/40869822): Remove this and replace with `build_with_chromium`
48# once the support for catalyst is available in the hermetic swift compiler.
49_can_use_hermetic_swift =
50    build_with_chromium && is_ios && target_environment != "catalyst"
51
52if (swift_toolchain_path == -1) {
53  # TODO(crbug.com/40915887) The custom swift toolchain not does currently work
54  # with Xcode 15 beta 1.
55  if (_can_use_hermetic_swift && !(is_ios && xcode_version_int >= 1500)) {
56    # Version of the hermetic compiler. Needs to be updated when a new version of
57    # the compiler is rolled to ensure that all outputs are regenerated. It must
58    # be kept in sync with the `version` of `third_party/swift-toolchain` in
59    # //DEPS.
60    swiftc_version = "swift-5.8-release"
61
62    # Use the hermetic swift toolchain.
63    swift_toolchain_path = "//third_party/swift-toolchain/"
64  } else {
65    swift_toolchain_path = ""
66  }
67}
68
69if (swift_whole_module_optimization == -1) {
70  swift_whole_module_optimization = is_official_build
71}
72
73# When implementing tools using Python scripts, a TOOL_VERSION=N env
74# variable is placed in front of the command. The N should be incremented
75# whenever the script is changed, so that the build system rebuilds all
76# edges that utilize the script. Ideally this should be changed to use
77# proper input-dirty checking, but that could be expensive. Instead, use a
78# script to get the tool scripts' modification time to use as the version.
79# This won't cause a re-generation of GN files when the tool script changes
80# but it will cause edges to be marked as dirty if the ninja files are
81# regenerated. See https://crbug.com/619083 for details. A proper fix
82# would be to have inputs to tools (https://crbug.com/621119).
83tool_versions = exec_script(
84        "get_tool_mtime.py",
85        rebase_path(
86            [
87              "//build/toolchain/apple/compile_xcassets.py",
88              "//build/toolchain/apple/filter_libtool.py",
89              "//build/toolchain/apple/linker_driver.py",
90              "//build/toolchain/apple/swift_const_gather_protocols.json",
91              "//build/toolchain/apple/swiftc.py",
92            ],
93            root_build_dir),
94        "trim scope")
95
96# Shared toolchain definition. Invocations should set current_os to set the
97# build args in this definition. This is titled "single_apple_toolchain"
98# because it makes exactly one toolchain. Callers will normally want to
99# invoke instead "apple_toolchain" which makes an additional toolchain for
100# Rust targets that are build-time artificts such as proc macros.
101template("single_apple_toolchain") {
102  toolchain(target_name) {
103    # When invoking this toolchain not as the default one, these args will be
104    # passed to the build. They are ignored when this is the default toolchain.
105    assert(defined(invoker.toolchain_args),
106           "Toolchains must declare toolchain_args")
107    toolchain_args = {
108      # Populate toolchain args from the invoker.
109      forward_variables_from(invoker.toolchain_args, "*")
110
111      # The host toolchain value computed by the default toolchain's setup
112      # needs to be passed through unchanged to all secondary toolchains to
113      # ensure that it's always the same, regardless of the values that may be
114      # set on those toolchains.
115      host_toolchain = host_toolchain
116    }
117
118    # When the invoker has explicitly overridden cc_wrapper in the
119    # toolchain args, use those values, otherwise default to the global one.
120    # This works because the only reasonable override that toolchains might
121    # supply for these values are to force-disable them.
122    if (defined(toolchain_args.use_reclient)) {
123      toolchain_uses_reclient = toolchain_args.use_reclient
124    } else {
125      toolchain_uses_reclient = use_reclient
126    }
127    if (defined(toolchain_args.cc_wrapper)) {
128      toolchain_cc_wrapper = toolchain_args.cc_wrapper
129    } else {
130      toolchain_cc_wrapper = cc_wrapper
131    }
132    assert(!(toolchain_cc_wrapper != "" && toolchain_uses_reclient),
133           "re-client and cc_wrapper can't be used together.")
134
135    if (defined(toolchain_args.use_lld)) {
136      toolchain_uses_lld = toolchain_args.use_lld
137    } else {
138      toolchain_uses_lld = use_lld
139    }
140
141    # The value of all global variables (such as `is_component_build`) is the
142    # one from the default toolchain when evaluating a secondary toolchain
143    # (see https://crbug.com/gn/286). This mean that the value may change when
144    # evaluating target/configs in the new toolchain if the variable default
145    # value depends on variable set in `toolchain_args`.
146    #
147    # For this reason, "ios" needs to override `is_component_build` as its
148    # default value depends on `current_os`. Use the overridden value if it
149    # is set in `toolchain_args`.
150    if (defined(toolchain_args.is_component_build)) {
151      toolchain_is_component_build = toolchain_args.is_component_build
152    } else {
153      toolchain_is_component_build = is_component_build
154    }
155
156    prefix = rebase_path("$clang_base_path/bin/", root_build_dir)
157    _cc = "${prefix}clang"
158    _cxx = "${prefix}clang++"
159
160    swiftmodule_switch = "-Wl,-add_ast_path,"
161
162    # Compute the compiler prefix.
163    if (toolchain_uses_reclient) {
164      if (defined(toolchain_args.reclient_cc_cfg_file)) {
165        toolchain_reclient_cc_cfg_file = toolchain_args.reclient_cc_cfg_file
166      } else {
167        toolchain_reclient_cc_cfg_file = reclient_cc_cfg_file
168      }
169
170      # C/C++ (clang) rewrapper prefix to use when use_reclient is true.
171      compiler_prefix = "${reclient_bin_dir}/rewrapper -cfg=${toolchain_reclient_cc_cfg_file}${rbe_bug_326584510_missing_inputs} -exec_root=${rbe_exec_root} "
172    } else if (toolchain_cc_wrapper != "") {
173      compiler_prefix = toolchain_cc_wrapper + " "
174    } else {
175      compiler_prefix = ""
176    }
177
178    cc = compiler_prefix + _cc
179    cxx = compiler_prefix + _cxx
180    ld = _cxx
181
182    # Set the explicit search path for clang++ so it uses the right linker
183    # binary.
184    if (!toolchain_uses_lld) {
185      ld += " -B " + invoker.bin_path
186    }
187
188    if (defined(toolchain_args.coverage_instrumentation_input_file)) {
189      toolchain_coverage_instrumentation_input_file =
190          toolchain_args.coverage_instrumentation_input_file
191    } else {
192      toolchain_coverage_instrumentation_input_file =
193          coverage_instrumentation_input_file
194    }
195    _use_clang_coverage_wrapper =
196        toolchain_coverage_instrumentation_input_file != ""
197    if (_use_clang_coverage_wrapper) {
198      _coverage_wrapper =
199          rebase_path("//build/toolchain/clang_code_coverage_wrapper.py",
200                      root_build_dir) + " --files-to-instrument=" +
201          rebase_path(toolchain_coverage_instrumentation_input_file,
202                      root_build_dir) + " --target-os=" + target_os
203      cc = "\"$python_path\" $_coverage_wrapper ${cc}"
204      cxx = "\"$python_path\" $_coverage_wrapper ${cxx}"
205    }
206
207    linker_driver_env = "TOOL_VERSION=${tool_versions.linker_driver}"
208    linker_driver =
209        rebase_path("//build/toolchain/apple/linker_driver.py", root_build_dir)
210    linker_driver_args = "-Wcrl,driver,$ld"
211
212    # Specify an explicit path for the strip binary.
213    _strippath = invoker.bin_path + "strip"
214    _installnametoolpath = "${prefix}llvm-install-name-tool"
215    linker_driver_args += " -Wcrl,strippath,${_strippath} -Wcrl,installnametoolpath,${_installnametoolpath}"
216    _enable_dsyms = enable_dsyms
217    _save_unstripped_output = save_unstripped_output
218
219    # Make these apply to all tools below.
220    lib_switch = "-l"
221    lib_dir_switch = "-L"
222
223    # Object files go in this directory. Use label_name instead of
224    # target_output_name since labels will generally have no spaces and will be
225    # unique in the directory.
226    object_subdir = "{{target_out_dir}}/{{label_name}}"
227
228    # If dSYMs are enabled, this flag will be added to the link tools.
229    if (_enable_dsyms) {
230      dsym_switch = " -Wcrl,dsym,{{root_out_dir}} "
231      dsym_switch += "-Wcrl,dsymutilpath," +
232                     rebase_path("//tools/clang/dsymutil/bin/dsymutil",
233                                 root_build_dir) + " "
234
235      dsym_output_dir =
236          "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.dSYM"
237      dsym_output = [
238        "$dsym_output_dir/Contents/Info.plist",
239        "$dsym_output_dir/Contents/Resources/DWARF/" +
240            "{{target_output_name}}{{output_extension}}",
241      ]
242    } else {
243      dsym_switch = ""
244    }
245
246    if (_save_unstripped_output) {
247      _unstripped_output = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.unstripped"
248    }
249
250    if (toolchain_has_rust) {
251      if (!defined(rust_compiler_prefix)) {
252        rust_compiler_prefix = ""
253      }
254      rust_sysroot_relative = rebase_path(rust_sysroot, root_build_dir)
255      rustc_bin = "$rust_sysroot_relative/bin/rustc"
256      rustc = "$rust_compiler_prefix${rustc_bin}"
257      rustc_wrapper =
258          rebase_path("//build/rust/rustc_wrapper.py", root_build_dir)
259
260      tool("rust_staticlib") {
261        libname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
262        rspfile = "$libname.rsp"
263        depfile = "$libname.d"
264
265        default_output_extension = ".a"
266        output_prefix = "lib"
267
268        # Static libraries go in the target out directory by default so we can
269        # generate different targets with the same name and not have them
270        # collide.
271        default_output_dir = "{{target_out_dir}}"
272        description = "RUST(STATICLIB) {{output}}"
273        outputs = [ libname ]
274
275        # TODO(danakj): When `!toolchain_uses_lld` do we need to specify a path
276        # to libtool like the "alink" rule?
277
278        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
279        command = "\"$python_path\" \"$rustc_wrapper\" --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- -Clinker=\"$_cxx\" $rustc_common_args --emit=dep-info=$depfile,link -o $libname LDFLAGS RUSTENV {{rustenv}}"
280        rust_sysroot = rust_sysroot_relative
281      }
282
283      tool("rust_rlib") {
284        # We must always prefix with `lib` even if the library already starts
285        # with that prefix or else our stdlib is unable to find libc.rlib (or
286        # actually liblibc.rlib).
287        rlibname =
288            "{{output_dir}}/lib{{target_output_name}}{{output_extension}}"
289        rspfile = "$rlibname.rsp"
290        depfile = "$rlibname.d"
291
292        default_output_extension = ".rlib"
293
294        # This is prefixed unconditionally in `rlibname`.
295        # output_prefix = "lib"
296
297        # Static libraries go in the target out directory by default so we can
298        # generate different targets with the same name and not have them
299        # collide.
300        default_output_dir = "{{target_out_dir}}"
301        description = "RUST {{output}}"
302        outputs = [ rlibname ]
303
304        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
305        command = "\"$python_path\" \"$rustc_wrapper\" --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- -Clinker=\"$_cxx\" $rustc_common_args {{rustdeps}} {{externs}} --emit=dep-info=$depfile,link -o $rlibname LDFLAGS RUSTENV {{rustenv}}"
306        rust_sysroot = rust_sysroot_relative
307      }
308
309      tool("rust_bin") {
310        exename = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
311        rspfile = "$exename.rsp"
312        depfile = "$exename.d"
313        pool = "//build/toolchain:link_pool($default_toolchain)"
314
315        # TODO(danakj): solink can generate TOC files for re-exporting library
316        # symbols, and we should do the same here.
317
318        default_output_dir = "{{root_out_dir}}"
319        description = "RUST(BIN) {{output}}"
320        outputs = [ exename ]
321
322        # TODO(danakj): Support dsym_switch like C++ targets.
323        # link_command += dsym_switch
324        # if (_enable_dsyms) {
325        #   outputs += dsym_output
326        # }
327        # if (_save_unstripped_output) {
328        #   outputs += [ _unstripped_output ]
329        # }
330
331        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
332        command = "$linker_driver_env \"$python_path\" \"$rustc_wrapper\" --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- -Clinker=\"$linker_driver\" $rustc_common_args --emit=dep-info=$depfile,link -o $exename LDFLAGS $linker_driver_args {{ldflags}} RUSTENV {{rustenv}}"
333        rust_sysroot = rust_sysroot_relative
334      }
335
336      tool("rust_cdylib") {
337        dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
338        rspfile = "$dllname.rsp"
339        depfile = "$dllname.d"
340        pool = "//build/toolchain:link_pool($default_toolchain)"
341
342        # TODO(danakj): solink can generate TOC files for re-exporting library
343        # symbols, and we should do the same here.
344
345        default_output_extension = ".dylib"
346        output_prefix = "lib"
347        default_output_dir = "{{root_out_dir}}"
348        description = "RUST(CDYLIB) {{output}}"
349        outputs = [ dllname ]
350
351        # TODO(danakj): Support dsym_switch like C++ targets.
352        # link_command += dsym_switch
353        # if (_enable_dsyms) {
354        #   outputs += dsym_output
355        # }
356        # if (_save_unstripped_output) {
357        #   outputs += [ _unstripped_output ]
358        # }
359
360        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
361        command = "$linker_driver_env \"$python_path\" \"$rustc_wrapper\" --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- -Clinker=\"$linker_driver\" $rustc_common_args --emit=dep-info=$depfile,link -o $dllname LDFLAGS $linker_driver_args {{ldflags}} RUSTENV {{rustenv}}"
362        rust_sysroot = rust_sysroot_relative
363      }
364
365      tool("rust_macro") {
366        dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
367        rspfile = "$dllname.rsp"
368        depfile = "$dllname.d"
369        pool = "//build/toolchain:link_pool($default_toolchain)"
370
371        # TODO(danakj): solink can generate TOC files for re-exporting library
372        # symbols, and we should do the same here.
373
374        default_output_extension = ".dylib"
375        output_prefix = "lib"
376        default_output_dir = "{{root_out_dir}}"
377        description = "RUST(MACRO) {{output}}"
378        outputs = [ dllname ]
379
380        # TODO(danakj): Support dsym_switch like C++ targets.
381        # link_command += dsym_switch
382        # if (_enable_dsyms) {
383        #   outputs += dsym_output
384        # }
385        # if (_save_unstripped_output) {
386        #   outputs += [ _unstripped_output ]
387        # }
388
389        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
390        command = "\"$python_path\" \"$rustc_wrapper\" --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- -Clinker=\"${_cxx}\" $rustc_common_args --emit=dep-info=$depfile,link -o $dllname LDFLAGS {{ldflags}} RUSTENV {{rustenv}}"
391        rust_sysroot = rust_sysroot_relative
392      }
393    }
394
395    tool("cc") {
396      depfile = "{{output}}.d"
397      precompiled_header_type = "gcc"
398      command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
399      depsformat = "gcc"
400      description = "CC {{output}}"
401      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
402    }
403
404    tool("cxx") {
405      depfile = "{{output}}.d"
406      precompiled_header_type = "gcc"
407      command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} {{module_deps_no_self}} -c {{source}} -o {{output}}"
408      depsformat = "gcc"
409      description = "CXX {{output}}"
410      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
411    }
412
413    tool("cxx_module") {
414      depfile = "{{output}}.d"
415      precompiled_header_type = "gcc"
416      command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} {{module_deps_no_self}} -fmodule-name={{label_name}} -c -x c++ -Xclang -emit-module {{source}} -o {{output}}"
417      depsformat = "gcc"
418      description = "CXX_MODULE {{output}}"
419      outputs = [ "$object_subdir/{{source_name_part}}.pcm" ]
420    }
421
422    tool("asm") {
423      # For GCC we can just use the C compiler to compile assembly.
424      depfile = "{{output}}.d"
425      command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
426      depsformat = "gcc"
427      description = "ASM {{output}}"
428      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
429    }
430
431    tool("objc") {
432      depfile = "{{output}}.d"
433      precompiled_header_type = "gcc"
434      command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{framework_dirs}} {{cflags}} {{cflags_objc}} -c {{source}} -o {{output}}"
435      depsformat = "gcc"
436      description = "OBJC {{output}}"
437      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
438    }
439
440    tool("objcxx") {
441      depfile = "{{output}}.d"
442      precompiled_header_type = "gcc"
443      command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{framework_dirs}} {{cflags}} {{cflags_objcc}} -c {{source}} -o {{output}}"
444      depsformat = "gcc"
445      description = "OBJCXX {{output}}"
446      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
447    }
448
449    tool("alink") {
450      rspfile = "{{output}}.rsp"
451      rspfile_content = "{{inputs}}"
452
453      if (!toolchain_uses_lld) {
454        script = rebase_path("//build/toolchain/apple/filter_libtool.py",
455                             root_build_dir)
456
457        # Specify explicit path for libtool.
458        libtool = invoker.bin_path + "libtool"
459        command = "rm -f {{output}} && TOOL_VERSION=${tool_versions.filter_libtool} $python_path $script $libtool -static -D {{arflags}} -o {{output}} @$rspfile"
460        description = "LIBTOOL-STATIC {{output}}"
461      } else {
462        ar = "${prefix}llvm-ar"
463        command = "\"$ar\" {{arflags}} -r -c -s -D {{output}} @$rspfile"
464
465        # Remove the output file first so that ar doesn't try to modify the
466        # existing file.
467        command = "rm -f {{output}} && $command"
468        description = "AR {{output}}"
469      }
470      outputs = [ "{{output_dir}}/{{target_output_name}}{{output_extension}}" ]
471      default_output_dir = "{{target_out_dir}}"
472      default_output_extension = ".a"
473      output_prefix = "lib"
474    }
475
476    tool("solink") {
477      # E.g. "./libfoo.dylib":
478      dylib = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
479      rspfile = dylib + ".rsp"
480      pool = "//build/toolchain:link_pool($default_toolchain)"
481
482      # These variables are not built into GN but are helpers that implement
483      # (1) linking to produce a .dylib, (2) extracting the symbols from that
484      # file to a temporary file, (3) if the temporary file has differences from
485      # the existing .TOC file, overwrite it, otherwise, don't change it.
486      #
487      # As a special case, if the library reexports symbols from other dynamic
488      # libraries, we always update the .TOC and skip the temporary file and
489      # diffing steps, since that library always needs to be re-linked.
490      tocname = dylib + ".TOC"
491
492      # Use explicit paths to binaries. The binaries present on the default
493      # search path in /usr/bin are thin wrappers around xcrun, which requires a
494      # full CommandLineTools or Xcode install, and still may not choose the
495      # appropriate binary if there are multiple installs.
496      if (host_os == "mac") {
497        nm = invoker.bin_path + "nm"
498        otool = invoker.bin_path + "otool"
499      } else {
500        nm = "${prefix}llvm-nm"
501        otool = "${prefix}llvm-otool"
502      }
503
504      link_command = "$linker_driver_env $linker_driver"
505      link_command += " -Wcrl,otoolpath,$otool -Wcrl,nmpath,$nm"
506      link_command += " -Wcrl,tocname,\"$tocname\""
507      link_command += " $linker_driver_args -shared "
508      if (toolchain_is_component_build) {
509        link_command += " -Wl,-install_name,@rpath/\"{{target_output_name}}{{output_extension}}\" "
510      }
511      link_command += dsym_switch
512      link_command += "{{ldflags}} -o \"$dylib\" \"@$rspfile\" {{rlibs}}"
513
514      command = "$link_command"
515
516      rspfile_content =
517          "{{inputs}} {{frameworks}} {{swiftmodules}} {{solibs}} {{libs}}"
518
519      description = "SOLINK {{output}}"
520
521      # Use this for {{output_extension}} expansions unless a target manually
522      # overrides it (in which case {{output_extension}} will be what the target
523      # specifies).
524      default_output_dir = "{{root_out_dir}}"
525      default_output_extension = ".dylib"
526
527      output_prefix = "lib"
528
529      # Since the above commands only updates the .TOC file when it changes, ask
530      # Ninja to check if the timestamp actually changed to know if downstream
531      # dependencies should be recompiled.
532      restat = true
533
534      # Tell GN about the output files. It will link to the dylib but use the
535      # tocname for dependency management.
536      outputs = [
537        dylib,
538        tocname,
539      ]
540      link_output = dylib
541      depend_output = tocname
542
543      if (_enable_dsyms) {
544        outputs += dsym_output
545      }
546      if (_save_unstripped_output) {
547        outputs += [ _unstripped_output ]
548      }
549    }
550
551    tool("solink_module") {
552      # E.g. "./libfoo.so":
553      sofile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
554      rspfile = sofile + ".rsp"
555      pool = "//build/toolchain:link_pool($default_toolchain)"
556
557      link_command = "$linker_driver_env $linker_driver $linker_driver_args -bundle {{ldflags}} -o \"$sofile\" \"@$rspfile\" {{rlibs}}"
558      link_command += dsym_switch
559      command = link_command
560
561      rspfile_content =
562          "{{inputs}} {{frameworks}} {{swiftmodules}} {{solibs}} {{libs}}"
563
564      description = "SOLINK_MODULE {{output}}"
565
566      # Use this for {{output_extension}} expansions unless a target manually
567      # overrides it (in which case {{output_extension}} will be what the target
568      # specifies).
569      default_output_dir = "{{root_out_dir}}"
570      default_output_extension = ".so"
571
572      outputs = [ sofile ]
573
574      if (_enable_dsyms) {
575        outputs += dsym_output
576      }
577      if (_save_unstripped_output) {
578        outputs += [ _unstripped_output ]
579      }
580    }
581
582    tool("link") {
583      outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
584      rspfile = "$outfile.rsp"
585      pool = "//build/toolchain:link_pool($default_toolchain)"
586
587      command = "$linker_driver_env $linker_driver $linker_driver_args $dsym_switch {{ldflags}} -o \"$outfile\" \"@$rspfile\" {{rlibs}}"
588      description = "LINK $outfile"
589      rspfile_content =
590          "{{inputs}} {{frameworks}} {{swiftmodules}} {{solibs}} {{libs}}"
591      outputs = [ outfile ]
592
593      if (_enable_dsyms) {
594        outputs += dsym_output
595      }
596      if (_save_unstripped_output) {
597        outputs += [ _unstripped_output ]
598      }
599
600      default_output_dir = "{{root_out_dir}}"
601    }
602
603    # These two are really entirely generic, but have to be repeated in
604    # each toolchain because GN doesn't allow a template to be used here.
605    # See //build/toolchain/toolchain.gni for details.
606    tool("stamp") {
607      command = stamp_command
608      description = stamp_description
609    }
610    tool("copy") {
611      command = copy_command
612      description = copy_description
613    }
614
615    tool("copy_bundle_data") {
616      # copy_command use hardlink if possible but this does not work with
617      # directories. Also when running EG2 tests from Xcode, Xcode tries to
618      # copy some files into the application bundle which fails if source
619      # and destination are hardlinked together.
620      #
621      # Instead use clonefile to copy the files which is as efficient as
622      # hardlink but ensure the file have distinct metadata (thus avoid the
623      # error with ditto, see https://crbug.com/1042182).
624      if (host_os == "mac") {
625        command = "rm -rf {{output}} && /bin/cp -Rc {{source}} {{output}}"
626      } else {
627        command = "rm -rf {{output}} && /bin/cp -Rld {{source}} {{output}}"
628      }
629      description = "COPY_BUNDLE_DATA {{source}} {{output}}"
630      pool = "//build/toolchain/apple:bundle_pool($default_toolchain)"
631    }
632
633    # Swift is only used on iOS, not macOS. We want to minimize the number
634    # of Xcode-based tools used by the macOS toolchain, so we intentionally
635    # disallow future uses of Swift on macOS. https://crbug.com/965663.
636    if (toolchain_args.current_os == "ios") {
637      tool("swift") {
638        _tool = rebase_path("//build/toolchain/apple/swiftc.py", root_build_dir)
639
640        depfile = "{{target_out_dir}}/{{module_name}}.d"
641        depsformat = "gcc"
642
643        _header_path = "{{target_gen_dir}}/{{target_output_name}}.h"
644        _output_dir = "{{target_out_dir}}/{{label_name}}"
645
646        outputs = [
647          _header_path,
648          "$_output_dir/{{module_name}}-OutputFileMap.json",
649          "$_output_dir/{{module_name}}.SwiftFileList",
650          "$_output_dir/{{module_name}}.abi.json",
651          "$_output_dir/{{module_name}}.d",
652          "$_output_dir/{{module_name}}.dia",
653          "$_output_dir/{{module_name}}.swiftdoc",
654          "$_output_dir/{{module_name}}.swiftmodule",
655          "$_output_dir/{{module_name}}.swiftsourceinfo",
656        ]
657
658        partial_outputs = [ "$_output_dir/{{source_name_part}}.o" ]
659
660        # The list of outputs and partial_outputs change whether the whole
661        # module optimization is enabled or not.
662        if (swift_whole_module_optimization) {
663          outputs += [
664            "$_output_dir/{{module_name}}.swiftconstvalues",
665            "$_output_dir/{{module_name}}.swiftdeps",
666          ]
667        } else {
668          outputs += [ "$_output_dir/{{module_name}}.priors" ]
669          partial_outputs += [
670            "$_output_dir/{{source_name_part}}.d",
671            "$_output_dir/{{source_name_part}}.dia",
672            "$_output_dir/{{source_name_part}}.swiftdeps",
673            "$_output_dir/{{source_name_part}}.swiftconstvalues",
674          ]
675        }
676
677        # If configured to keep the intermediate build files, pass the flag
678        # to the script and inform gn of the stamp file only (as the other
679        # files have names that cannot be predicted without invoking swiftc).
680        if (swift_keep_intermediate_files) {
681          _derived_data_dir = "$_output_dir/DerivedData"
682          outputs += [ "$_derived_data_dir/{{module_name}}.stamp" ]
683        }
684
685        # Additional flags passed to the wrapper script but that are only
686        # set conditionally.
687        _extra_flags = ""
688
689        # Environment variables passed to the wrapper script. Considered
690        # part of the command-line by ninja (and thus cause the build to
691        # be considered dirty if they change) without having to be parsed
692        # by the script.
693        _env_vars = "TOOL_VERSION=${tool_versions.swiftc} " +
694                    "JSON_VERSION=${tool_versions.swift_const_gather_protocols}"
695
696        # Include the version of the compiler on the command-line. This causes
697        # `ninja` to consider all the compilation output to be dirty when the
698        # version changes.
699        if (defined(swiftc_version)) {
700          _env_vars += " SWIFTC_VERSION=$swiftc_version"
701        }
702
703        # Include the version of Xcode on the command-line (if specified via
704        # toolchain_args). This causes `ninja` to consider all the compilation
705        # outputs to be dirty when the version change.
706        #
707        # This is required because sometimes module dependency changes between
708        # different version of Xcode (e.g. when moving from Xcode 14 beta 6 to
709        # Xcode 14 RC). If the swiftmodule are not rebuilt when the version
710        # changes, they may encode dependency on now non-existing frameworks
711        # causing linker failures ultimately.
712        if (defined(toolchain_args.xcode_build)) {
713          _env_vars += " XCODE_VERSION=${toolchain_args.xcode_build}"
714        }
715
716        if (invoker.sdk_developer_dir != "") {
717          _env_vars += " DEVELOPER_DIR=${toolchain_args.sdk_developer_dir}"
718        }
719
720        if (swift_toolchain_path != "") {
721          _extra_flags += " --swift-toolchain-path " +
722                          rebase_path(swift_toolchain_path, root_build_dir)
723        }
724
725        if (swift_whole_module_optimization) {
726          _extra_flags += " --whole-module-optimization"
727        }
728
729        if (swift_keep_intermediate_files) {
730          _extra_flags += " --swift-keep-intermediate-files" +
731                          " --derived-data-dir $_derived_data_dir"
732        }
733
734        # The Swift compiler assumes that the generated header will be used by
735        # Objective-C code compiled with module support enabled (-fmodules).
736        #
737        # As Chromium code is compiled without support for modules (i.e. the
738        # code is compiled without `-fmodules`), the dependent modules are not
739        # imported from the generated header, which causes compilation failure
740        # if the client code does not first import the required modules (see
741        # https://crbug.com/1316061 for details).
742        #
743        # Secondly, the Swift compiler uses absolute path when importing other
744        # modules' generated headers or Objective-C bridging headers. This
745        # causes issues with the distributed compiler (i.e. reclient or siso)
746        # as they want all paths to be relative to the source directory.
747        #
748        # Instruct swiftc.py to rewrite the generated header use relative
749        # import and to use the old #import syntax for system frameworks.
750        _extra_flags += " --fix-generated-header"
751
752        _src_dir = rebase_path("//", root_build_dir)
753        _gen_dir = rebase_path(root_gen_dir, root_build_dir)
754        _const_gather_protocols_file = rebase_path(
755                "//build/toolchain/apple/swift_const_gather_protocols.json",
756                root_build_dir)
757
758        command =
759            "$_env_vars $python_path $_tool --module-name {{module_name}} " +
760            "--header-path $_header_path --target-out-dir $_output_dir " +
761            "--const-gather-protocols-file $_const_gather_protocols_file " +
762            "--depfile-path $depfile --src-dir $_src_dir --gen-dir $_gen_dir " +
763            "--bridge-header {{bridge_header}} {{include_dirs}} " +
764            "{{module_dirs}} {{swiftflags}} {{inputs}}$_extra_flags"
765
766        description = "SWIFT $_output_dir/{{module_name}}.swiftmodule"
767      }
768    }
769
770    # xcassets are only used on iOS, not macOS. We want to minimize the number
771    # of Xcode-based tools used by the macOS toolchain, so we intentionally
772    # disallow future uses of xcassets on macOS. https://crbug.com/965663.
773    if (toolchain_args.current_os == "ios") {
774      tool("compile_xcassets") {
775        _tool = rebase_path("//build/toolchain/apple/compile_xcassets.py",
776                            root_build_dir)
777
778        _env_vars = "TOOL_VERSION=${tool_versions.compile_xcassets}"
779        if (invoker.sdk_developer_dir != "") {
780          _env_vars += " DEVELOPER_DIR=${toolchain_args.sdk_developer_dir}"
781        }
782
783        command = "$_env_vars $python_path $_tool " +
784                  "-p '${toolchain_args.current_os}' " +
785                  "-e '${invoker.target_environment}' " +
786                  "-t '${invoker.deployment_target}' " +
787                  "-T '{{bundle_product_type}}' " +
788                  "-P '{{bundle_partial_info_plist}}' " +
789                  " {{xcasset_compiler_flags}} " + "-o {{output}} {{inputs}}"
790
791        description = "COMPILE_XCASSETS {{output}}"
792        pool = "//build/toolchain/apple:bundle_pool($default_toolchain)"
793      }
794    }
795
796    tool("action") {
797      pool = "//build/toolchain:action_pool($default_toolchain)"
798    }
799  }
800}
801
802# Make an additional toolchain which is used for making tools that are run
803# on the host machine as part of the build process (such as proc macros
804# and Cargo build scripts). This toolchain uses the prebuilt stdlib that
805# comes with the compiler, so it doesn't have to wait for the stdlib to be
806# built before building other stuff. And this ensures its proc macro
807# outputs have the right ABI to be loaded by the compiler, and it can be
808# used to compile build scripts that are part of the stdlib that is built
809# for the default toolchain.
810template("apple_rust_host_build_tools_toolchain") {
811  single_apple_toolchain(target_name) {
812    assert(defined(invoker.toolchain_args),
813           "Toolchains must declare toolchain_args")
814    forward_variables_from(invoker,
815                           "*",
816                           TESTONLY_AND_VISIBILITY + [ "toolchain_args" ])
817    toolchain_args = {
818      # Populate toolchain args from the invoker.
819      forward_variables_from(invoker.toolchain_args, "*")
820      toolchain_for_rust_host_build_tools = true
821
822      # The host build tools are static release builds to make the Chromium
823      # build faster. They do not need PGO etc, so no official builds.
824      is_debug = false
825      is_component_build = false
826      is_official_build = false
827      use_clang_coverage = false
828      use_sanitizer_coverage = false
829      generate_linker_map = false
830      use_thin_lto = false
831    }
832  }
833}
834
835# If PartitionAlloc is part of the build (even as a transitive dependency), then
836# it replaces the system allocator. If this toolchain is used, that will be
837# overridden and the system allocator will be used regardless. This is important
838# in some third-party binaries outside of Chrome.
839template("apple_system_allocator_toolchain") {
840  single_apple_toolchain(target_name) {
841    assert(defined(invoker.toolchain_args),
842           "Toolchains must declare toolchain_args")
843    forward_variables_from(invoker,
844                           "*",
845                           TESTONLY_AND_VISIBILITY + [ "toolchain_args" ])
846    toolchain_args = {
847      # Populate toolchain args from the invoker.
848      forward_variables_from(invoker.toolchain_args, "*")
849      toolchain_allows_use_partition_alloc_as_malloc = false
850
851      # Disable component build so that we can copy the exes to the
852      # root_build_dir and support the default_toolchain redirection on Windows.
853      # See also the comment in //build/symlink.gni.
854      is_component_build = false
855
856      # Only one toolchain can be configured with MSAN support with our current
857      # GN setup, or they all try to make the instrumented libraries and
858      # collide.
859      is_msan = false
860    }
861  }
862}
863
864# Makes an Apple toolchain for the target, and an equivalent toolchain with the
865# prebuilt Rust stdlib for building proc macros (and other for-build-use
866# artifacts).
867template("apple_toolchain") {
868  single_apple_toolchain(target_name) {
869    assert(defined(invoker.toolchain_args),
870           "Toolchains must declare toolchain_args")
871    forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
872
873    # No need to forward visibility and test_only as they apply to targets not
874    # toolchains, but presubmit checks require that we explicitly exclude them
875  }
876
877  apple_rust_host_build_tools_toolchain(
878      "${target_name}_for_rust_host_build_tools") {
879    assert(defined(invoker.toolchain_args),
880           "Toolchains must declare toolchain_args")
881    forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
882  }
883
884  apple_system_allocator_toolchain(
885      "${target_name}_host_with_system_allocator") {
886    assert(defined(invoker.toolchain_args),
887           "Toolchains must declare toolchain_args")
888    forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
889  }
890  apple_system_allocator_toolchain("${target_name}_with_system_allocator") {
891    assert(defined(invoker.toolchain_args),
892           "Toolchains must declare toolchain_args")
893    forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
894  }
895}
896