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 that control the tools:
27 # - libs_section_prefix
28 # - libs_section_postfix
29 # The contents of these strings, if specified, will be placed around
30 # the libs section of the linker line. It allows one to inject libraries
31 # at the beginning and end for all targets in a toolchain.
32 # - solink_libs_section_prefix
33 # - solink_libs_section_postfix
34 # Same as libs_section_{pre,post}fix except used for solink instead of link.
36 # The content of this array, if specified, will be added to the list of
37 # outputs from the link command. This can be useful in conjunction with
38 # the post_link parameter.
40 # The content of this string, if specified, will be run as a separate
41 # command following the the link command.
43 # Just forwarded to the toolchain definition.
44 # - executable_extension
45 # If this string is specified it will be used for the file extension
46 # for an executable, rather than using no extension; targets will
47 # still be able to override the extension using the output_extension
50 # The contents of this string, if specified, will be passed as a #define
51 # to the toolchain. It can be used to force recompiles whenever a
52 # toolchain is updated.
54 # If this string is specified it will be used for the file extension
55 # for a shared library, rather than default value specified in
58 # Location of the strip executable. When specified, strip will be run on
59 # all shared libraries and executables as they are built. The pre-stripped
60 # artifacts will be put in lib.stripped/ and exe.stripped/.
62 # Optional build argument contols.
65 # When set to true, is_asan, is_msan, etc.will all be set to false. Often
66 # secondary toolchains do not want to run with sanitizers.
68 # Whether to use clang instead of gcc.
69 # - is_component_build
70 # Whether to forcibly enable or disable component builds for this
71 # toolchain; if not specified, the toolchain will inherit the
73 template("gcc_toolchain") {
74 toolchain(target_name) {
75 assert(defined(invoker.cc), "gcc_toolchain() must specify a \"cc\" value")
76 assert(defined(invoker.cxx), "gcc_toolchain() must specify a \"cxx\" value")
77 assert(defined(invoker.ar), "gcc_toolchain() must specify a \"ar\" value")
78 assert(defined(invoker.ld), "gcc_toolchain() must specify a \"ld\" value")
79 assert(defined(invoker.toolchain_cpu),
80 "gcc_toolchain() must specify a \"toolchain_cpu\"")
81 assert(defined(invoker.toolchain_os),
82 "gcc_toolchain() must specify a \"toolchain_os\"")
84 # This define changes when the toolchain changes, forcing a rebuild.
85 # Nothing should ever use this define.
86 if (defined(invoker.rebuild_define)) {
87 rebuild_string = "-D" + invoker.rebuild_define + " "
92 # We can't do string interpolation ($ in strings) on things with dots in
93 # them. To allow us to use $cc below, for example, we create copies of
94 # these values in our scope.
99 if (defined(invoker.readelf)) {
100 readelf = invoker.readelf
104 if (defined(invoker.nm)) {
110 if (defined(invoker.shlib_extension)) {
111 default_shlib_extension = invoker.shlib_extension
113 default_shlib_extension = shlib_extension
116 if (defined(invoker.executable_extension)) {
117 default_executable_extension = invoker.executable_extension
119 default_executable_extension = ""
122 # Bring these into our scope for string interpolation with default values.
123 if (defined(invoker.libs_section_prefix)) {
124 libs_section_prefix = invoker.libs_section_prefix
126 libs_section_prefix = ""
129 if (defined(invoker.libs_section_postfix)) {
130 libs_section_postfix = invoker.libs_section_postfix
132 libs_section_postfix = ""
135 if (defined(invoker.solink_libs_section_prefix)) {
136 solink_libs_section_prefix = invoker.solink_libs_section_prefix
138 solink_libs_section_prefix = ""
141 if (defined(invoker.solink_libs_section_postfix)) {
142 solink_libs_section_postfix = invoker.solink_libs_section_postfix
144 solink_libs_section_postfix = ""
147 # These library switches can apply to all tools below.
149 lib_dir_switch = "-L"
152 depfile = "{{output}}.d"
153 command = "$cc -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
155 description = "CC {{output}}"
157 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
162 depfile = "{{output}}.d"
163 command = "$cxx -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
165 description = "CXX {{output}}"
167 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
172 # For GCC we can just use the C compiler to compile assembly.
173 depfile = "{{output}}.d"
174 command = "$cc -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
176 description = "ASM {{output}}"
178 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
183 rspfile = "{{output}}.rsp"
185 if (is_cfi && invoker.toolchain_os != "nacl") {
186 gold_plugin_path = rebase_path(
187 "//third_party/llvm-build/Release+Asserts/lib/LLVMgold.so",
189 arflags = "--plugin $gold_plugin_path"
191 command = "rm -f {{output}} && $ar rcs $arflags {{output}} @$rspfile"
192 description = "AR {{output}}"
193 rspfile_content = "{{inputs}}"
195 "{{target_out_dir}}/{{target_output_name}}{{output_extension}}",
197 default_output_extension = ".a"
198 output_prefix = "lib"
202 soname = "{{target_output_name}}{{output_extension}}" # e.g. "libfoo.so".
203 sofile = "{{root_out_dir}}/$soname" # Possibly including toolchain dir.
204 if (shlib_subdir != ".") {
205 sofile = "{{root_out_dir}}/$shlib_subdir/$soname"
207 rspfile = sofile + ".rsp"
209 unstripped_sofile = sofile
210 if (defined(invoker.strip)) {
211 unstripped_sofile = "{{root_out_dir}}/lib.unstripped/$soname"
214 # These variables are not built into GN but are helpers that implement
215 # (1) linking to produce a .so, (2) extracting the symbols from that file
216 # to a temporary file, (3) if the temporary file has differences from the
217 # existing .TOC file, overwrite it, otherwise, don't change it.
218 tocfile = sofile + ".TOC"
219 temporary_tocname = sofile + ".tmp"
221 link_command = "$ld -shared {{ldflags}} -o $unstripped_sofile -Wl,-soname=$soname @$rspfile"
222 assert(defined(readelf), "to solink you must have a readelf")
223 assert(defined(nm), "to solink you must have an nm")
224 toc_command = "{ $readelf -d $unstripped_sofile | grep SONAME ; $nm -gD -f p $unstripped_sofile | cut -f1-2 -d' '; } > $temporary_tocname"
225 replace_command = "if ! cmp -s $temporary_tocname $tocfile; then mv $temporary_tocname $tocfile; fi"
227 command = "$link_command && $toc_command && $replace_command"
228 if (defined(invoker.strip)) {
230 "${invoker.strip} --strip-unneeded -o $sofile $unstripped_sofile"
231 command += " && " + strip_command
233 rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"
235 description = "SOLINK $sofile"
237 # Use this for {{output_extension}} expansions unless a target manually
238 # overrides it (in which case {{output_extension}} will be what the target
240 default_output_extension = default_shlib_extension
242 output_prefix = "lib"
244 # Since the above commands only updates the .TOC file when it changes, ask
245 # Ninja to check if the timestamp actually changed to know if downstream
246 # dependencies should be recompiled.
249 # Tell GN about the output files. It will link to the sofile but use the
250 # tocfile for dependency management.
255 if (sofile != unstripped_sofile) {
256 outputs += [ unstripped_sofile ]
259 depend_output = tocfile
263 exename = "{{target_output_name}}{{output_extension}}"
264 outfile = "{{root_out_dir}}/$exename"
265 rspfile = "$outfile.rsp"
266 unstripped_outfile = outfile
268 # Use this for {{output_extension}} expansions unless a target manually
269 # overrides it (in which case {{output_extension}} will be what the target
271 default_output_extension = default_executable_extension
273 if (defined(invoker.strip)) {
274 unstripped_outfile = "{{root_out_dir}}/exe.unstripped/$exename"
277 command = "$ld {{ldflags}} -o $unstripped_outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end-group $libs_section_prefix {{libs}} $libs_section_postfix"
278 if (defined(invoker.strip)) {
280 "${invoker.strip} --strip-unneeded -o $outfile $unstripped_outfile"
281 command += " && " + strip_command
283 if (defined(invoker.postlink)) {
284 command += " && " + invoker.postlink
286 description = "LINK $outfile"
287 rspfile_content = "{{inputs}}"
291 if (outfile != unstripped_outfile) {
292 outputs += [ unstripped_outfile ]
294 if (defined(invoker.link_outputs)) {
295 outputs += invoker.link_outputs
300 command = "touch {{output}}"
301 description = "STAMP {{output}}"
305 command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
306 description = "COPY {{source}} {{output}}"
309 # When invoking this toolchain not as the default one, these args will be
310 # passed to the build. They are ignored when this is the default toolchain.
312 current_cpu = invoker.toolchain_cpu
313 current_os = invoker.toolchain_os
315 # These values need to be passed through unchanged.
316 target_os = target_os
317 target_cpu = target_cpu
319 forward_variables_from(invoker,
322 "is_component_build",
325 if (defined(invoker.clear_sanitizers) && invoker.clear_sanitizers) {
335 forward_variables_from(invoker, [ "deps" ])