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/toolchain/toolchain.gni")
7 # This value will be inherited in the toolchain below.
8 concurrent_links = exec_script("get_concurrent_links.py", [], "value")
10 # This template defines a toolchain for something that works like gcc
13 # It requires the following variables specifying the executables to run:
18 # and the following which is used in the toolchain_args
19 # - toolchain_cpu (What "current_cpu" should be set to when invoking a
20 # build using this toolchain.)
21 # - toolchain_os (What "current_os" should be set to when invoking a
22 # build using this toolchain.)
24 # Optional parameters:
25 # - libs_section_prefix
26 # - libs_section_postfix
27 # The contents of these strings, if specified, will be placed around
28 # the libs section of the linker line. It allows one to inject libraries
29 # at the beginning and end for all targets in a toolchain.
30 # - solink_libs_section_prefix
31 # - solink_libs_section_postfix
32 # Same as libs_section_{pre,post}fix except used for solink instead of link.
34 # The content of this array, if specified, will be added to the list of
35 # outputs from the link command. This can be useful in conjunction with
36 # the post_link parameter.
38 # The content of this string, if specified, will be run as a separate
39 # command following the the link command.
41 # Just forwarded to the toolchain definition.
42 # - executable_extension
43 # If this string is specified it will be used for the file extension
44 # for an executable, rather than using no extension; targets will
45 # still be able to override the extension using the output_extension
48 # Whether to use clang instead of gcc.
49 # - is_component_build
50 # Whether to forcibly enable or disable component builds for this
51 # toolchain; if not specified, the toolchain will inherit the
54 # The contents of this string, if specified, will be passed as a #define
55 # to the toolchain. It can be used to force recompiles whenever a
56 # toolchain is updated.
58 # If this string is specified it will be used for the file extension
59 # for a shared library, rather than default value specified in
62 # Location of the strip executable. When specified, strip will be run on
63 # all shared libraries and executables as they are built. The pre-stripped
64 # artifacts will be put in lib.stripped/ and exe.stripped/.
65 template("gcc_toolchain") {
66 toolchain(target_name) {
67 assert(defined(invoker.cc), "gcc_toolchain() must specify a \"cc\" value")
68 assert(defined(invoker.cxx), "gcc_toolchain() must specify a \"cxx\" value")
69 assert(defined(invoker.ar), "gcc_toolchain() must specify a \"ar\" value")
70 assert(defined(invoker.ld), "gcc_toolchain() must specify a \"ld\" value")
71 assert(defined(invoker.toolchain_cpu),
72 "gcc_toolchain() must specify a \"toolchain_cpu\"")
73 assert(defined(invoker.toolchain_os),
74 "gcc_toolchain() must specify a \"toolchain_os\"")
76 # This define changes when the toolchain changes, forcing a rebuild.
77 # Nothing should ever use this define.
78 if (defined(invoker.rebuild_define)) {
79 rebuild_string = "-D" + invoker.rebuild_define + " "
84 # We can't do string interpolation ($ in strings) on things with dots in
85 # them. To allow us to use $cc below, for example, we create copies of
86 # these values in our scope.
91 if (defined(invoker.readelf)) {
92 readelf = invoker.readelf
96 if (defined(invoker.nm)) {
102 if (defined(invoker.shlib_extension)) {
103 default_shlib_extension = invoker.shlib_extension
105 default_shlib_extension = shlib_extension
108 if (defined(invoker.executable_extension)) {
109 default_executable_extension = invoker.executable_extension
111 default_executable_extension = ""
114 # Bring these into our scope for string interpolation with default values.
115 if (defined(invoker.libs_section_prefix)) {
116 libs_section_prefix = invoker.libs_section_prefix
118 libs_section_prefix = ""
121 if (defined(invoker.libs_section_postfix)) {
122 libs_section_postfix = invoker.libs_section_postfix
124 libs_section_postfix = ""
127 if (defined(invoker.solink_libs_section_prefix)) {
128 solink_libs_section_prefix = invoker.solink_libs_section_prefix
130 solink_libs_section_prefix = ""
133 if (defined(invoker.solink_libs_section_postfix)) {
134 solink_libs_section_postfix = invoker.solink_libs_section_postfix
136 solink_libs_section_postfix = ""
139 # These library switches can apply to all tools below.
141 lib_dir_switch = "-L"
144 depfile = "{{output}}.d"
145 command = "$cc -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
147 description = "CC {{output}}"
149 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
154 depfile = "{{output}}.d"
155 command = "$cxx -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
157 description = "CXX {{output}}"
159 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
164 # For GCC we can just use the C compiler to compile assembly.
165 depfile = "{{output}}.d"
166 command = "$cc -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
168 description = "ASM {{output}}"
170 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
175 rspfile = "{{output}}.rsp"
176 command = "rm -f {{output}} && $ar rcs {{output}} @$rspfile"
177 description = "AR {{output}}"
178 rspfile_content = "{{inputs}}"
180 "{{target_out_dir}}/{{target_output_name}}{{output_extension}}",
182 default_output_extension = ".a"
183 output_prefix = "lib"
187 soname = "{{target_output_name}}{{output_extension}}" # e.g. "libfoo.so".
188 sofile = "{{root_out_dir}}/$soname" # Possibly including toolchain dir.
189 if (shlib_subdir != ".") {
190 sofile = "{{root_out_dir}}/$shlib_subdir/$soname"
192 rspfile = sofile + ".rsp"
194 unstripped_sofile = sofile
195 if (defined(invoker.strip)) {
196 unstripped_sofile = "{{root_out_dir}}/lib.unstripped/$soname"
199 # These variables are not built into GN but are helpers that implement
200 # (1) linking to produce a .so, (2) extracting the symbols from that file
201 # to a temporary file, (3) if the temporary file has differences from the
202 # existing .TOC file, overwrite it, otherwise, don't change it.
203 tocfile = sofile + ".TOC"
204 temporary_tocname = sofile + ".tmp"
206 link_command = "$ld -shared {{ldflags}} -o $unstripped_sofile -Wl,-soname=$soname @$rspfile"
207 assert(defined(readelf), "to solink you must have a readelf")
208 assert(defined(nm), "to solink you must have an nm")
209 toc_command = "{ $readelf -d $unstripped_sofile | grep SONAME ; $nm -gD -f p $unstripped_sofile | cut -f1-2 -d' '; } > $temporary_tocname"
210 replace_command = "if ! cmp -s $temporary_tocname $tocfile; then mv $temporary_tocname $tocfile; fi"
212 command = "$link_command && $toc_command && $replace_command"
213 if (defined(invoker.strip)) {
215 "${invoker.strip} --strip-unneeded -o $sofile $unstripped_sofile"
216 command += " && " + strip_command
218 rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"
220 description = "SOLINK $sofile"
222 # Use this for {{output_extension}} expansions unless a target manually
223 # overrides it (in which case {{output_extension}} will be what the target
225 default_output_extension = default_shlib_extension
227 output_prefix = "lib"
229 # Since the above commands only updates the .TOC file when it changes, ask
230 # Ninja to check if the timestamp actually changed to know if downstream
231 # dependencies should be recompiled.
234 # Tell GN about the output files. It will link to the sofile but use the
235 # tocfile for dependency management.
240 if (sofile != unstripped_sofile) {
241 outputs += [ unstripped_sofile ]
244 depend_output = tocfile
248 exename = "{{target_output_name}}{{output_extension}}"
249 outfile = "{{root_out_dir}}/$exename"
250 rspfile = "$outfile.rsp"
251 unstripped_outfile = outfile
253 # Use this for {{output_extension}} expansions unless a target manually
254 # overrides it (in which case {{output_extension}} will be what the target
256 default_output_extension = default_executable_extension
258 if (defined(invoker.strip)) {
259 unstripped_outfile = "{{root_out_dir}}/exe.unstripped/$exename"
262 command = "$ld {{ldflags}} -o $unstripped_outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end-group $libs_section_prefix {{libs}} $libs_section_postfix"
263 if (defined(invoker.strip)) {
265 "${invoker.strip} --strip-unneeded -o $outfile $unstripped_outfile"
266 command += " && " + strip_command
268 if (defined(invoker.postlink)) {
269 command += " && " + invoker.postlink
271 description = "LINK $outfile"
272 rspfile_content = "{{inputs}}"
276 if (outfile != unstripped_outfile) {
277 outputs += [ unstripped_outfile ]
279 if (defined(invoker.link_outputs)) {
280 outputs += invoker.link_outputs
285 command = "touch {{output}}"
286 description = "STAMP {{output}}"
290 command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
291 description = "COPY {{source}} {{output}}"
294 # When invoking this toolchain not as the default one, these args will be
295 # passed to the build. They are ignored when this is the default toolchain.
297 current_cpu = invoker.toolchain_cpu
298 current_os = invoker.toolchain_os
300 # These values need to be passed through unchanged.
301 target_os = target_os
302 target_cpu = target_cpu
304 if (defined(invoker.is_clang)) {
305 is_clang = invoker.is_clang
307 if (defined(invoker.is_component_build)) {
308 is_component_build = invoker.is_component_build
312 if (defined(invoker.deps)) {