xref: /aosp_15_r20/external/angle/build/toolchain/apple/toolchain.gni (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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}} -c {{source}} -o {{output}}"
408      depsformat = "gcc"
409      description = "CXX {{output}}"
410      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
411    }
412
413    tool("asm") {
414      # For GCC we can just use the C compiler to compile assembly.
415      depfile = "{{output}}.d"
416      command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
417      depsformat = "gcc"
418      description = "ASM {{output}}"
419      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
420    }
421
422    tool("objc") {
423      depfile = "{{output}}.d"
424      precompiled_header_type = "gcc"
425      command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{framework_dirs}} {{cflags}} {{cflags_objc}} -c {{source}} -o {{output}}"
426      depsformat = "gcc"
427      description = "OBJC {{output}}"
428      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
429    }
430
431    tool("objcxx") {
432      depfile = "{{output}}.d"
433      precompiled_header_type = "gcc"
434      command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{framework_dirs}} {{cflags}} {{cflags_objcc}} -c {{source}} -o {{output}}"
435      depsformat = "gcc"
436      description = "OBJCXX {{output}}"
437      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
438    }
439
440    tool("alink") {
441      rspfile = "{{output}}.rsp"
442      rspfile_content = "{{inputs}}"
443
444      if (!toolchain_uses_lld) {
445        script = rebase_path("//build/toolchain/apple/filter_libtool.py",
446                             root_build_dir)
447
448        # Specify explicit path for libtool.
449        libtool = invoker.bin_path + "libtool"
450        command = "rm -f {{output}} && TOOL_VERSION=${tool_versions.filter_libtool} $python_path $script $libtool -static -D {{arflags}} -o {{output}} @$rspfile"
451        description = "LIBTOOL-STATIC {{output}}"
452      } else {
453        ar = "${prefix}llvm-ar"
454        command = "\"$ar\" {{arflags}} -r -c -s -D {{output}} @$rspfile"
455
456        # Remove the output file first so that ar doesn't try to modify the
457        # existing file.
458        command = "rm -f {{output}} && $command"
459        description = "AR {{output}}"
460      }
461      outputs = [ "{{output_dir}}/{{target_output_name}}{{output_extension}}" ]
462      default_output_dir = "{{target_out_dir}}"
463      default_output_extension = ".a"
464      output_prefix = "lib"
465    }
466
467    tool("solink") {
468      # E.g. "./libfoo.dylib":
469      dylib = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
470      rspfile = dylib + ".rsp"
471      pool = "//build/toolchain:link_pool($default_toolchain)"
472
473      # These variables are not built into GN but are helpers that implement
474      # (1) linking to produce a .dylib, (2) extracting the symbols from that
475      # file to a temporary file, (3) if the temporary file has differences from
476      # the existing .TOC file, overwrite it, otherwise, don't change it.
477      #
478      # As a special case, if the library reexports symbols from other dynamic
479      # libraries, we always update the .TOC and skip the temporary file and
480      # diffing steps, since that library always needs to be re-linked.
481      tocname = dylib + ".TOC"
482      temporary_tocname = dylib + ".tmp"
483
484      # Use explicit paths to binaries. The binaries present on the default
485      # search path in /usr/bin are thin wrappers around xcrun, which requires a
486      # full CommandLineTools or Xcode install, and still may not choose the
487      # appropriate binary if there are multiple installs.
488      if (host_os == "mac") {
489        nm = invoker.bin_path + "nm"
490        otool = invoker.bin_path + "otool"
491      } else {
492        nm = "${prefix}llvm-nm"
493        otool = "${prefix}llvm-otool"
494      }
495
496      does_reexport_command = "[ ! -e \"$dylib\" -o ! -e \"$tocname\" ] || $otool -l \"$dylib\" | grep -q LC_REEXPORT_DYLIB"
497
498      link_command =
499          "$linker_driver_env $linker_driver $linker_driver_args -shared "
500      if (toolchain_is_component_build) {
501        link_command += " -Wl,-install_name,@rpath/\"{{target_output_name}}{{output_extension}}\" "
502      }
503      link_command += dsym_switch
504      link_command += "{{ldflags}} -o \"$dylib\" \"@$rspfile\" {{rlibs}}"
505
506      replace_command = "if ! cmp -s \"$temporary_tocname\" \"$tocname\"; then mv \"$temporary_tocname\" \"$tocname\""
507      extract_toc_command = "{ $otool -l \"$dylib\" | grep LC_ID_DYLIB -A 5; $nm -gPp \"$dylib\" | cut -f1-2 -d' ' | grep -v U\$\$; true; }"
508
509      command = "if $does_reexport_command ; then $link_command && $extract_toc_command > \"$tocname\"; else $link_command && $extract_toc_command > \"$temporary_tocname\" && $replace_command ; fi; fi"
510
511      rspfile_content =
512          "{{inputs}} {{frameworks}} {{swiftmodules}} {{solibs}} {{libs}}"
513
514      description = "SOLINK {{output}}"
515
516      # Use this for {{output_extension}} expansions unless a target manually
517      # overrides it (in which case {{output_extension}} will be what the target
518      # specifies).
519      default_output_dir = "{{root_out_dir}}"
520      default_output_extension = ".dylib"
521
522      output_prefix = "lib"
523
524      # Since the above commands only updates the .TOC file when it changes, ask
525      # Ninja to check if the timestamp actually changed to know if downstream
526      # dependencies should be recompiled.
527      restat = true
528
529      # Tell GN about the output files. It will link to the dylib but use the
530      # tocname for dependency management.
531      outputs = [
532        dylib,
533        tocname,
534      ]
535      link_output = dylib
536      depend_output = tocname
537
538      if (_enable_dsyms) {
539        outputs += dsym_output
540      }
541      if (_save_unstripped_output) {
542        outputs += [ _unstripped_output ]
543      }
544    }
545
546    tool("solink_module") {
547      # E.g. "./libfoo.so":
548      sofile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
549      rspfile = sofile + ".rsp"
550      pool = "//build/toolchain:link_pool($default_toolchain)"
551
552      link_command = "$linker_driver_env $linker_driver $linker_driver_args -bundle {{ldflags}} -o \"$sofile\" \"@$rspfile\" {{rlibs}}"
553      link_command += dsym_switch
554      command = link_command
555
556      rspfile_content =
557          "{{inputs}} {{frameworks}} {{swiftmodules}} {{solibs}} {{libs}}"
558
559      description = "SOLINK_MODULE {{output}}"
560
561      # Use this for {{output_extension}} expansions unless a target manually
562      # overrides it (in which case {{output_extension}} will be what the target
563      # specifies).
564      default_output_dir = "{{root_out_dir}}"
565      default_output_extension = ".so"
566
567      outputs = [ sofile ]
568
569      if (_enable_dsyms) {
570        outputs += dsym_output
571      }
572      if (_save_unstripped_output) {
573        outputs += [ _unstripped_output ]
574      }
575    }
576
577    tool("link") {
578      outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
579      rspfile = "$outfile.rsp"
580      pool = "//build/toolchain:link_pool($default_toolchain)"
581
582      command = "$linker_driver_env $linker_driver $linker_driver_args $dsym_switch {{ldflags}} -o \"$outfile\" \"@$rspfile\" {{rlibs}}"
583      description = "LINK $outfile"
584      rspfile_content =
585          "{{inputs}} {{frameworks}} {{swiftmodules}} {{solibs}} {{libs}}"
586      outputs = [ outfile ]
587
588      if (_enable_dsyms) {
589        outputs += dsym_output
590      }
591      if (_save_unstripped_output) {
592        outputs += [ _unstripped_output ]
593      }
594
595      default_output_dir = "{{root_out_dir}}"
596    }
597
598    # These two are really entirely generic, but have to be repeated in
599    # each toolchain because GN doesn't allow a template to be used here.
600    # See //build/toolchain/toolchain.gni for details.
601    tool("stamp") {
602      command = stamp_command
603      description = stamp_description
604    }
605    tool("copy") {
606      command = copy_command
607      description = copy_description
608    }
609
610    tool("copy_bundle_data") {
611      # copy_command use hardlink if possible but this does not work with
612      # directories. Also when running EG2 tests from Xcode, Xcode tries to
613      # copy some files into the application bundle which fails if source
614      # and destination are hardlinked together.
615      #
616      # Instead use clonefile to copy the files which is as efficient as
617      # hardlink but ensure the file have distinct metadata (thus avoid the
618      # error with ditto, see https://crbug.com/1042182).
619      if (host_os == "mac") {
620        command = "rm -rf {{output}} && /bin/cp -Rc {{source}} {{output}}"
621      } else {
622        command = "rm -rf {{output}} && /bin/cp -Rld {{source}} {{output}}"
623      }
624      description = "COPY_BUNDLE_DATA {{source}} {{output}}"
625      pool = "//build/toolchain/apple:bundle_pool($default_toolchain)"
626    }
627
628    # Swift is only used on iOS, not macOS. We want to minimize the number
629    # of Xcode-based tools used by the macOS toolchain, so we intentionally
630    # disallow future uses of Swift on macOS. https://crbug.com/965663.
631    if (toolchain_args.current_os == "ios") {
632      tool("swift") {
633        _tool = rebase_path("//build/toolchain/apple/swiftc.py", root_build_dir)
634
635        depfile = "{{target_out_dir}}/{{module_name}}.d"
636        depsformat = "gcc"
637
638        _header_path = "{{target_gen_dir}}/{{target_output_name}}.h"
639        _output_dir = "{{target_out_dir}}/{{label_name}}"
640
641        outputs = [
642          _header_path,
643          "$_output_dir/{{module_name}}-OutputFileMap.json",
644          "$_output_dir/{{module_name}}.SwiftFileList",
645          "$_output_dir/{{module_name}}.abi.json",
646          "$_output_dir/{{module_name}}.d",
647          "$_output_dir/{{module_name}}.dia",
648          "$_output_dir/{{module_name}}.swiftdoc",
649          "$_output_dir/{{module_name}}.swiftmodule",
650          "$_output_dir/{{module_name}}.swiftsourceinfo",
651        ]
652
653        partial_outputs = [ "$_output_dir/{{source_name_part}}.o" ]
654
655        # The list of outputs and partial_outputs change whether the whole
656        # module optimization is enabled or not.
657        if (swift_whole_module_optimization) {
658          outputs += [
659            "$_output_dir/{{module_name}}.swiftconstvalues",
660            "$_output_dir/{{module_name}}.swiftdeps",
661          ]
662        } else {
663          outputs += [ "$_output_dir/{{module_name}}.priors" ]
664          partial_outputs += [
665            "$_output_dir/{{source_name_part}}.d",
666            "$_output_dir/{{source_name_part}}.dia",
667            "$_output_dir/{{source_name_part}}.swiftdeps",
668            "$_output_dir/{{source_name_part}}.swiftconstvalues",
669          ]
670        }
671
672        # If configured to keep the intermediate build files, pass the flag
673        # to the script and inform gn of the stamp file only (as the other
674        # files have names that cannot be predicted without invoking swiftc).
675        if (swift_keep_intermediate_files) {
676          _derived_data_dir = "$_output_dir/DerivedData"
677          outputs += [ "$_derived_data_dir/{{module_name}}.stamp" ]
678        }
679
680        # Additional flags passed to the wrapper script but that are only
681        # set conditionally.
682        _extra_flags = ""
683
684        # Environment variables passed to the wrapper script. Considered
685        # part of the command-line by ninja (and thus cause the build to
686        # be considered dirty if they change) without having to be parsed
687        # by the script.
688        _env_vars = "TOOL_VERSION=${tool_versions.swiftc} " +
689                    "JSON_VERSION=${tool_versions.swift_const_gather_protocols}"
690
691        # Include the version of the compiler on the command-line. This causes
692        # `ninja` to consider all the compilation output to be dirty when the
693        # version changes.
694        if (defined(swiftc_version)) {
695          _env_vars += " SWIFTC_VERSION=$swiftc_version"
696        }
697
698        # Include the version of Xcode on the command-line (if specified via
699        # toolchain_args). This causes `ninja` to consider all the compilation
700        # outputs to be dirty when the version change.
701        #
702        # This is required because sometimes module dependency changes between
703        # different version of Xcode (e.g. when moving from Xcode 14 beta 6 to
704        # Xcode 14 RC). If the swiftmodule are not rebuilt when the version
705        # changes, they may encode dependency on now non-existing frameworks
706        # causing linker failures ultimately.
707        if (defined(toolchain_args.xcode_build)) {
708          _env_vars += " XCODE_VERSION=${toolchain_args.xcode_build}"
709        }
710
711        if (invoker.sdk_developer_dir != "") {
712          _env_vars += " DEVELOPER_DIR=${toolchain_args.sdk_developer_dir}"
713        }
714
715        if (swift_toolchain_path != "") {
716          _extra_flags += " --swift-toolchain-path " +
717                          rebase_path(swift_toolchain_path, root_build_dir)
718        }
719
720        if (swift_whole_module_optimization) {
721          _extra_flags += " --whole-module-optimization"
722        }
723
724        if (swift_keep_intermediate_files) {
725          _extra_flags += " --derived-data-dir $_derived_data_dir"
726        }
727
728        # The Swift compiler assumes that the generated header will be used by
729        # Objective-C code compiled with module support enabled (-fmodules).
730        #
731        # As Chromium code is compiled without support for modules (i.e. the
732        # code is compiled without `-fmodules`), the dependent modules are not
733        # imported from the generated header, which causes compilation failure
734        # if the client code does not first import the required modules (see
735        # https://crbug.com/1316061 for details).
736        #
737        # Secondly, the Swift compiler uses absolute path when importing other
738        # modules' generated headers or Objective-C bridging headers. This
739        # causes issues with the distributed compiler (i.e. reclient or siso)
740        # as they want all paths to be relative to the source directory.
741        #
742        # Instruct swiftc.py to rewrite the generated header use relative
743        # import and to use the old #import syntax for system frameworks.
744        _extra_flags += " --fix-generated-header"
745
746        _src_dir = rebase_path("//", root_build_dir)
747        _gen_dir = rebase_path(root_gen_dir, root_build_dir)
748        _const_gather_protocols_file = rebase_path(
749                "//build/toolchain/apple/swift_const_gather_protocols.json",
750                root_build_dir)
751
752        command =
753            "$_env_vars $python_path $_tool --module-name {{module_name}} " +
754            "--header-path $_header_path --target-out-dir $_output_dir " +
755            "--const-gather-protocols-file $_const_gather_protocols_file " +
756            "--depfile-path $depfile --src-dir $_src_dir --gen-dir $_gen_dir " +
757            "--bridge-header {{bridge_header}} {{include_dirs}} " +
758            "{{module_dirs}} {{swiftflags}} {{inputs}}$_extra_flags"
759
760        description = "SWIFT $_output_dir/{{module_name}}.swiftmodule"
761      }
762    }
763
764    # xcassets are only used on iOS, not macOS. We want to minimize the number
765    # of Xcode-based tools used by the macOS toolchain, so we intentionally
766    # disallow future uses of xcassets on macOS. https://crbug.com/965663.
767    if (toolchain_args.current_os == "ios") {
768      tool("compile_xcassets") {
769        _tool = rebase_path("//build/toolchain/apple/compile_xcassets.py",
770                            root_build_dir)
771
772        _env_vars = "TOOL_VERSION=${tool_versions.compile_xcassets}"
773        if (invoker.sdk_developer_dir != "") {
774          _env_vars += " DEVELOPER_DIR=${toolchain_args.sdk_developer_dir}"
775        }
776
777        command =
778            "$_env_vars $python_path $_tool " +
779            "-p '${toolchain_args.current_os}' " +
780            "-e '${invoker.target_environment}' " +
781            "-t '${invoker.deployment_target}' " +
782            "-T '{{bundle_product_type}}' " +
783            "-P '{{bundle_partial_info_plist}}' " + "-o {{output}} {{inputs}}"
784
785        description = "COMPILE_XCASSETS {{output}}"
786        pool = "//build/toolchain/apple:bundle_pool($default_toolchain)"
787      }
788    }
789
790    tool("action") {
791      pool = "//build/toolchain:action_pool($default_toolchain)"
792    }
793  }
794}
795
796# Make an additional toolchain which is used for making tools that are run
797# on the host machine as part of the build process (such as proc macros
798# and Cargo build scripts). This toolchain uses the prebuilt stdlib that
799# comes with the compiler, so it doesn't have to wait for the stdlib to be
800# built before building other stuff. And this ensures its proc macro
801# outputs have the right ABI to be loaded by the compiler, and it can be
802# used to compile build scripts that are part of the stdlib that is built
803# for the default toolchain.
804template("apple_rust_host_build_tools_toolchain") {
805  single_apple_toolchain(target_name) {
806    assert(defined(invoker.toolchain_args),
807           "Toolchains must declare toolchain_args")
808    forward_variables_from(invoker,
809                           "*",
810                           TESTONLY_AND_VISIBILITY + [ "toolchain_args" ])
811    toolchain_args = {
812      # Populate toolchain args from the invoker.
813      forward_variables_from(invoker.toolchain_args, "*")
814      toolchain_for_rust_host_build_tools = true
815
816      # The host build tools are static release builds to make the Chromium
817      # build faster. They do not need PGO etc, so no official builds.
818      is_debug = false
819      is_component_build = false
820      is_official_build = false
821      use_clang_coverage = false
822      use_sanitizer_coverage = false
823      generate_linker_map = false
824      use_thin_lto = false
825    }
826  }
827}
828
829# If PartitionAlloc is part of the build (even as a transitive dependency), then
830# it replaces the system allocator. If this toolchain is used, that will be
831# overridden and the system allocator will be used regardless. This is important
832# in some third-party binaries outside of Chrome.
833template("apple_system_allocator_toolchain") {
834  single_apple_toolchain(target_name) {
835    assert(defined(invoker.toolchain_args),
836           "Toolchains must declare toolchain_args")
837    forward_variables_from(invoker,
838                           "*",
839                           TESTONLY_AND_VISIBILITY + [ "toolchain_args" ])
840    toolchain_args = {
841      # Populate toolchain args from the invoker.
842      forward_variables_from(invoker.toolchain_args, "*")
843      toolchain_allows_use_partition_alloc_as_malloc = false
844
845      # Disable component build so that we can copy the exes to the
846      # root_build_dir and support the default_toolchain redirection on Windows.
847      # See also the comment in //build/symlink.gni.
848      is_component_build = false
849
850      # Only one toolchain can be configured with MSAN support with our current
851      # GN setup, or they all try to make the instrumented libraries and
852      # collide.
853      is_msan = false
854    }
855  }
856}
857
858# Makes an Apple toolchain for the target, and an equivalent toolchain with the
859# prebuilt Rust stdlib for building proc macros (and other for-build-use
860# artifacts).
861template("apple_toolchain") {
862  single_apple_toolchain(target_name) {
863    assert(defined(invoker.toolchain_args),
864           "Toolchains must declare toolchain_args")
865    forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
866
867    # No need to forward visibility and test_only as they apply to targets not
868    # toolchains, but presubmit checks require that we explicitly exclude them
869  }
870
871  apple_rust_host_build_tools_toolchain(
872      "${target_name}_for_rust_host_build_tools") {
873    assert(defined(invoker.toolchain_args),
874           "Toolchains must declare toolchain_args")
875    forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
876  }
877
878  apple_system_allocator_toolchain(
879      "${target_name}_host_with_system_allocator") {
880    assert(defined(invoker.toolchain_args),
881           "Toolchains must declare toolchain_args")
882    forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
883  }
884  apple_system_allocator_toolchain("${target_name}_with_system_allocator") {
885    assert(defined(invoker.toolchain_args),
886           "Toolchains must declare toolchain_args")
887    forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
888  }
889}
890