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