1 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import("//build/config/sanitizers/sanitizers.gni")
6 import("//build/toolchain/toolchain.gni")
8 # This value will be inherited in the toolchain below.
9 concurrent_links = exec_script("get_concurrent_links.py", [], "value")
11 # This template defines a toolchain for something that works like gcc
14 # It requires the following variables specifying the executables to run:
19 # and the following which is used in the toolchain_args
20 # - toolchain_cpu (What "current_cpu" should be set to when invoking a
21 # build using this toolchain.)
22 # - toolchain_os (What "current_os" should be set to when invoking a
23 # build using this toolchain.)
25 # Optional parameters:
26 # - libs_section_prefix
27 # - libs_section_postfix
28 # The contents of these strings, if specified, will be placed around
29 # the libs section of the linker line. It allows one to inject libraries
30 # at the beginning and end for all targets in a toolchain.
31 # - solink_libs_section_prefix
32 # - solink_libs_section_postfix
33 # Same as libs_section_{pre,post}fix except used for solink instead of link.
35 # The content of this array, if specified, will be added to the list of
36 # outputs from the link command. This can be useful in conjunction with
37 # the post_link parameter.
39 # The content of this string, if specified, will be run as a separate
40 # command following the the link command.
42 # Just forwarded to the toolchain definition.
43 # - executable_extension
44 # If this string is specified it will be used for the file extension
45 # for an executable, rather than using no extension; targets will
46 # still be able to override the extension using the output_extension
49 # Whether to use clang instead of gcc.
50 # - is_component_build
51 # Whether to forcibly enable or disable component builds for this
52 # toolchain; if not specified, the toolchain will inherit the
55 # The contents of this string, if specified, will be passed as a #define
56 # to the toolchain. It can be used to force recompiles whenever a
57 # toolchain is updated.
59 # If this string is specified it will be used for the file extension
60 # for a shared library, rather than default value specified in
63 # Location of the strip executable. When specified, strip will be run on
64 # all shared libraries and executables as they are built. The pre-stripped
65 # artifacts will be put in lib.stripped/ and exe.stripped/.
66 template("gcc_toolchain") {
67 toolchain(target_name) {
68 assert(defined(invoker.cc), "gcc_toolchain() must specify a \"cc\" value")
69 assert(defined(invoker.cxx), "gcc_toolchain() must specify a \"cxx\" value")
70 assert(defined(invoker.ar), "gcc_toolchain() must specify a \"ar\" value")
71 assert(defined(invoker.ld), "gcc_toolchain() must specify a \"ld\" value")
72 assert(defined(invoker.toolchain_cpu),
73 "gcc_toolchain() must specify a \"toolchain_cpu\"")
74 assert(defined(invoker.toolchain_os),
75 "gcc_toolchain() must specify a \"toolchain_os\"")
77 # This define changes when the toolchain changes, forcing a rebuild.
78 # Nothing should ever use this define.
79 if (defined(invoker.rebuild_define)) {
80 rebuild_string = "-D" + invoker.rebuild_define + " "
85 # We can't do string interpolation ($ in strings) on things with dots in
86 # them. To allow us to use $cc below, for example, we create copies of
87 # these values in our scope.
92 if (defined(invoker.readelf)) {
93 readelf = invoker.readelf
97 if (defined(invoker.nm)) {
103 if (defined(invoker.shlib_extension)) {
104 default_shlib_extension = invoker.shlib_extension
106 default_shlib_extension = shlib_extension
109 if (defined(invoker.executable_extension)) {
110 default_executable_extension = invoker.executable_extension
112 default_executable_extension = ""
115 # Bring these into our scope for string interpolation with default values.
116 if (defined(invoker.libs_section_prefix)) {
117 libs_section_prefix = invoker.libs_section_prefix
119 libs_section_prefix = ""
122 if (defined(invoker.libs_section_postfix)) {
123 libs_section_postfix = invoker.libs_section_postfix
125 libs_section_postfix = ""
128 if (defined(invoker.solink_libs_section_prefix)) {
129 solink_libs_section_prefix = invoker.solink_libs_section_prefix
131 solink_libs_section_prefix = ""
134 if (defined(invoker.solink_libs_section_postfix)) {
135 solink_libs_section_postfix = invoker.solink_libs_section_postfix
137 solink_libs_section_postfix = ""
140 # These library switches can apply to all tools below.
142 lib_dir_switch = "-L"
145 depfile = "{{output}}.d"
146 command = "$cc -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
148 description = "CC {{output}}"
150 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
155 depfile = "{{output}}.d"
156 command = "$cxx -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
158 description = "CXX {{output}}"
160 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
165 # For GCC we can just use the C compiler to compile assembly.
166 depfile = "{{output}}.d"
167 command = "$cc -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
169 description = "ASM {{output}}"
171 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
176 rspfile = "{{output}}.rsp"
178 if (is_cfi && invoker.toolchain_os != "nacl") {
179 gold_plugin_path = rebase_path(
180 "//third_party/llvm-build/Release+Asserts/lib/LLVMgold.so",
182 arflags = "--plugin $gold_plugin_path"
184 command = "rm -f {{output}} && $ar rcs $arflags {{output}} @$rspfile"
185 description = "AR {{output}}"
186 rspfile_content = "{{inputs}}"
188 "{{target_out_dir}}/{{target_output_name}}{{output_extension}}",
190 default_output_extension = ".a"
191 output_prefix = "lib"
195 soname = "{{target_output_name}}{{output_extension}}" # e.g. "libfoo.so".
196 sofile = "{{root_out_dir}}/$soname" # Possibly including toolchain dir.
197 if (shlib_subdir != ".") {
198 sofile = "{{root_out_dir}}/$shlib_subdir/$soname"
200 rspfile = sofile + ".rsp"
202 unstripped_sofile = sofile
203 if (defined(invoker.strip)) {
204 unstripped_sofile = "{{root_out_dir}}/lib.unstripped/$soname"
207 # These variables are not built into GN but are helpers that implement
208 # (1) linking to produce a .so, (2) extracting the symbols from that file
209 # to a temporary file, (3) if the temporary file has differences from the
210 # existing .TOC file, overwrite it, otherwise, don't change it.
211 tocfile = sofile + ".TOC"
212 temporary_tocname = sofile + ".tmp"
214 link_command = "$ld -shared {{ldflags}} -o $unstripped_sofile -Wl,-soname=$soname @$rspfile"
215 assert(defined(readelf), "to solink you must have a readelf")
216 assert(defined(nm), "to solink you must have an nm")
217 toc_command = "{ $readelf -d $unstripped_sofile | grep SONAME ; $nm -gD -f p $unstripped_sofile | cut -f1-2 -d' '; } > $temporary_tocname"
218 replace_command = "if ! cmp -s $temporary_tocname $tocfile; then mv $temporary_tocname $tocfile; fi"
220 command = "$link_command && $toc_command && $replace_command"
221 if (defined(invoker.strip)) {
223 "${invoker.strip} --strip-unneeded -o $sofile $unstripped_sofile"
224 command += " && " + strip_command
226 rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"
228 description = "SOLINK $sofile"
230 # Use this for {{output_extension}} expansions unless a target manually
231 # overrides it (in which case {{output_extension}} will be what the target
233 default_output_extension = default_shlib_extension
235 output_prefix = "lib"
237 # Since the above commands only updates the .TOC file when it changes, ask
238 # Ninja to check if the timestamp actually changed to know if downstream
239 # dependencies should be recompiled.
242 # Tell GN about the output files. It will link to the sofile but use the
243 # tocfile for dependency management.
248 if (sofile != unstripped_sofile) {
249 outputs += [ unstripped_sofile ]
252 depend_output = tocfile
256 exename = "{{target_output_name}}{{output_extension}}"
257 outfile = "{{root_out_dir}}/$exename"
258 rspfile = "$outfile.rsp"
259 unstripped_outfile = outfile
261 # Use this for {{output_extension}} expansions unless a target manually
262 # overrides it (in which case {{output_extension}} will be what the target
264 default_output_extension = default_executable_extension
266 if (defined(invoker.strip)) {
267 unstripped_outfile = "{{root_out_dir}}/exe.unstripped/$exename"
270 command = "$ld {{ldflags}} -o $unstripped_outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end-group $libs_section_prefix {{libs}} $libs_section_postfix"
271 if (defined(invoker.strip)) {
273 "${invoker.strip} --strip-unneeded -o $outfile $unstripped_outfile"
274 command += " && " + strip_command
276 if (defined(invoker.postlink)) {
277 command += " && " + invoker.postlink
279 description = "LINK $outfile"
280 rspfile_content = "{{inputs}}"
284 if (outfile != unstripped_outfile) {
285 outputs += [ unstripped_outfile ]
287 if (defined(invoker.link_outputs)) {
288 outputs += invoker.link_outputs
293 command = "touch {{output}}"
294 description = "STAMP {{output}}"
298 command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
299 description = "COPY {{source}} {{output}}"
302 # When invoking this toolchain not as the default one, these args will be
303 # passed to the build. They are ignored when this is the default toolchain.
305 current_cpu = invoker.toolchain_cpu
306 current_os = invoker.toolchain_os
308 # These values need to be passed through unchanged.
309 target_os = target_os
310 target_cpu = target_cpu
312 if (defined(invoker.is_clang)) {
313 is_clang = invoker.is_clang
315 if (defined(invoker.is_component_build)) {
316 is_component_build = invoker.is_component_build
320 if (defined(invoker.deps)) {