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 string, if specified, will be appended to the solink
37 # Just forwarded to the toolchain definition.
39 # Whether to use clang instead of gcc.
41 # Location of the strip executable. When specified, strip will be run on
42 # all shared libraries and executables as they are built. The pre-stripped
43 # artifacts will be put in lib.stripped/ and exe.stripped/.
44 template("gcc_toolchain") {
45 toolchain(target_name) {
46 assert(defined(invoker.cc), "gcc_toolchain() must specify a \"cc\" value")
47 assert(defined(invoker.cxx), "gcc_toolchain() must specify a \"cxx\" value")
48 assert(defined(invoker.ar), "gcc_toolchain() must specify a \"ar\" value")
49 assert(defined(invoker.ld), "gcc_toolchain() must specify a \"ld\" value")
50 assert(defined(invoker.toolchain_cpu),
51 "gcc_toolchain() must specify a \"toolchain_cpu\"")
52 assert(defined(invoker.toolchain_os),
53 "gcc_toolchain() must specify a \"toolchain_os\"")
55 # We can't do string interpolation ($ in strings) on things with dots in
56 # them. To allow us to use $cc below, for example, we create copies of
57 # these values in our scope.
62 if (defined(invoker.readelf)) {
63 readelf = invoker.readelf
67 if (defined(invoker.nm)) {
73 # Bring these into our scope for string interpolation with default values.
74 if (defined(invoker.libs_section_prefix)) {
75 libs_section_prefix = invoker.libs_section_prefix
77 libs_section_prefix = ""
80 if (defined(invoker.libs_section_postfix)) {
81 libs_section_postfix = invoker.libs_section_postfix
83 libs_section_postfix = ""
86 if (defined(invoker.solink_libs_section_prefix)) {
87 solink_libs_section_prefix = invoker.solink_libs_section_prefix
89 solink_libs_section_prefix = ""
92 if (defined(invoker.solink_libs_section_postfix)) {
93 solink_libs_section_postfix = invoker.solink_libs_section_postfix
95 solink_libs_section_postfix = ""
98 # These library switches can apply to all tools below.
100 lib_dir_switch = "-L"
103 depfile = "{{output}}.d"
104 command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
106 description = "CC {{output}}"
108 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
113 depfile = "{{output}}.d"
114 command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
116 description = "CXX {{output}}"
118 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
123 # For GCC we can just use the C compiler to compile assembly.
124 depfile = "{{output}}.d"
125 command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
127 description = "ASM {{output}}"
129 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
134 rspfile = "{{output}}.rsp"
135 command = "rm -f {{output}} && $ar rcs {{output}} @$rspfile"
136 description = "AR {{output}}"
137 rspfile_content = "{{inputs}}"
139 "{{target_out_dir}}/{{target_output_name}}{{output_extension}}",
141 default_output_extension = ".a"
142 output_prefix = "lib"
146 soname = "{{target_output_name}}{{output_extension}}" # e.g. "libfoo.so".
147 sofile = "{{root_out_dir}}/$soname" # Possibly including toolchain dir.
148 if (shlib_subdir != ".") {
149 sofile = "{{root_out_dir}}/$shlib_subdir/$soname"
151 rspfile = sofile + ".rsp"
153 unstripped_sofile = sofile
154 if (defined(invoker.strip)) {
155 unstripped_sofile = "{{root_out_dir}}/lib.unstripped/$soname"
158 # These variables are not built into GN but are helpers that implement
159 # (1) linking to produce a .so, (2) extracting the symbols from that file
160 # to a temporary file, (3) if the temporary file has differences from the
161 # existing .TOC file, overwrite it, otherwise, don't change it.
162 tocfile = sofile + ".TOC"
163 temporary_tocname = sofile + ".tmp"
165 link_command = "$ld -shared {{ldflags}} -o $unstripped_sofile -Wl,-soname=$soname @$rspfile"
166 assert(defined(readelf), "to solink you must have a readelf")
167 assert(defined(nm), "to solink you must have an nm")
168 toc_command = "{ $readelf -d $unstripped_sofile | grep SONAME ; $nm -gD -f p $unstripped_sofile | cut -f1-2 -d' '; } > $temporary_tocname"
169 replace_command = "if ! cmp -s $temporary_tocname $tocfile; then mv $temporary_tocname $tocfile; fi"
171 command = "$link_command && $toc_command && $replace_command"
172 if (defined(invoker.strip)) {
174 "${invoker.strip} --strip-unneeded -o $sofile $unstripped_sofile"
175 command += " && " + strip_command
177 rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"
179 description = "SOLINK $sofile"
181 # Use this for {{output_extension}} expansions unless a target manually
182 # overrides it (in which case {{output_extension}} will be what the target
184 default_output_extension = shlib_extension
185 if (defined(invoker.default_output_extension)) {
186 default_output_extension = invoker.default_output_extension
189 output_prefix = "lib"
191 # Since the above commands only updates the .TOC file when it changes, ask
192 # Ninja to check if the timestamp actually changed to know if downstream
193 # dependencies should be recompiled.
196 # Tell GN about the output files. It will link to the sofile but use the
197 # tocfile for dependency management.
202 if (sofile != unstripped_sofile) {
203 outputs += [ unstripped_sofile ]
206 depend_output = tocfile
210 exename = "{{target_output_name}}{{output_extension}}"
211 outfile = "{{root_out_dir}}/$exename"
212 rspfile = "$outfile.rsp"
213 unstripped_outfile = outfile
214 if (defined(invoker.strip)) {
215 unstripped_outfile = "{{root_out_dir}}/exe.unstripped/$exename"
218 command = "$ld {{ldflags}} -o $unstripped_outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end-group $libs_section_prefix {{libs}} $libs_section_postfix"
219 if (defined(invoker.strip)) {
221 "${invoker.strip} --strip-unneeded -o $outfile $unstripped_outfile"
222 command += " && " + strip_command
224 description = "LINK $outfile"
225 rspfile_content = "{{inputs}}"
229 if (outfile != unstripped_outfile) {
230 outputs += [ unstripped_outfile ]
235 command = "touch {{output}}"
236 description = "STAMP {{output}}"
240 command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
241 description = "COPY {{source}} {{output}}"
244 # When invoking this toolchain not as the default one, these args will be
245 # passed to the build. They are ignored when this is the default toolchain.
247 current_cpu = invoker.toolchain_cpu
248 current_os = invoker.toolchain_os
250 # These values need to be passed through unchanged.
251 target_os = target_os
252 target_cpu = target_cpu
254 if (defined(invoker.is_clang)) {
255 is_clang = invoker.is_clang
259 if (defined(invoker.deps)) {