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 # This value will be inherited in the toolchain below.
6 concurrent_links = exec_script("get_concurrent_links.py", [], "value")
8 # This template defines a toolchain for something that works like gcc
11 # 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 template("gcc_toolchain") {
40 toolchain(target_name) {
41 assert(defined(invoker.cc), "gcc_toolchain() must specify a \"cc\" value")
42 assert(defined(invoker.cxx), "gcc_toolchain() must specify a \"cxx\" value")
43 assert(defined(invoker.ar), "gcc_toolchain() must specify a \"ar\" value")
44 assert(defined(invoker.ld), "gcc_toolchain() must specify a \"ld\" value")
45 assert(defined(invoker.readelf),
46 "gcc_toolchain() must specify a \"readelf\" value")
47 assert(defined(invoker.nm), "gcc_toolchain() must specify a \"nm\" value")
48 assert(defined(invoker.toolchain_cpu),
49 "gcc_toolchain() must specify a \"toolchain_cpu\"")
50 assert(defined(invoker.toolchain_os),
51 "gcc_toolchain() must specify a \"toolchain_os\"")
53 # We can't do string interpolation ($ in strings) on things with dots in
54 # them. To allow us to use $cc below, for example, we create copies of
55 # these values in our scope.
60 readelf = invoker.readelf
63 # Bring these into our scope for string interpolation with default values.
64 if (defined(invoker.libs_section_prefix)) {
65 libs_section_prefix = invoker.libs_section_prefix
67 libs_section_prefix = ""
70 if (defined(invoker.libs_section_postfix)) {
71 libs_section_postfix = invoker.libs_section_postfix
73 libs_section_postfix = ""
76 if (defined(invoker.solink_libs_section_prefix)) {
77 solink_libs_section_prefix = invoker.solink_libs_section_prefix
79 solink_libs_section_prefix = ""
82 if (defined(invoker.solink_libs_section_postfix)) {
83 solink_libs_section_postfix = invoker.solink_libs_section_postfix
85 solink_libs_section_postfix = ""
88 # These library switches can apply to all tools below.
93 depfile = "{{output}}.d"
94 command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
96 description = "CC {{output}}"
98 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
103 depfile = "{{output}}.d"
104 command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
106 description = "CXX {{output}}"
108 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
113 # For GCC we can just use the C compiler to compile assembly.
114 depfile = "{{output}}.d"
115 command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
117 description = "ASM {{output}}"
119 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
124 rspfile = "{{output}}.rsp"
125 command = "rm -f {{output}} && $ar rcs {{output}} @$rspfile"
126 description = "AR {{output}}"
127 rspfile_content = "{{inputs}}"
129 "{{target_out_dir}}/{{target_output_name}}{{output_extension}}",
131 default_output_extension = ".a"
132 output_prefix = "lib"
136 soname = "{{target_output_name}}{{output_extension}}" # e.g. "libfoo.so".
137 sofile = "{{root_out_dir}}/$soname" # Possibly including toolchain dir.
138 rspfile = sofile + ".rsp"
140 # These variables are not built into GN but are helpers that implement
141 # (1) linking to produce a .so, (2) extracting the symbols from that file
142 # to a temporary file, (3) if the temporary file has differences from the
143 # existing .TOC file, overwrite it, otherwise, don't change it.
144 tocfile = sofile + ".TOC"
145 temporary_tocname = sofile + ".tmp"
147 "$ld -shared {{ldflags}} -o $sofile -Wl,-soname=$soname @$rspfile"
148 toc_command = "{ $readelf -d $sofile | grep SONAME ; $nm -gD -f p $sofile | cut -f1-2 -d' '; } > $temporary_tocname"
149 replace_command = "if ! cmp -s $temporary_tocname $tocfile; then mv $temporary_tocname $tocfile; fi"
151 command = "$link_command && $toc_command && $replace_command"
152 if (defined(invoker.postsolink)) {
153 command += " && " + invoker.postsolink
155 rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"
157 description = "SOLINK $sofile"
159 # Use this for {{output_extension}} expansions unless a target manually
160 # overrides it (in which case {{output_extension}} will be what the target
162 default_output_extension = ".so"
163 if (defined(invoker.default_output_extension)) {
164 default_output_extension = invoker.default_output_extension
167 output_prefix = "lib"
169 # Since the above commands only updates the .TOC file when it changes, ask
170 # Ninja to check if the timestamp actually changed to know if downstream
171 # dependencies should be recompiled.
174 # Tell GN about the output files. It will link to the sofile but use the
175 # tocfile for dependency management.
180 if (defined(invoker.solink_outputs)) {
181 outputs += invoker.solink_outputs
184 depend_output = tocfile
188 outfile = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
189 rspfile = "$outfile.rsp"
190 command = "$ld {{ldflags}} -o $outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end-group $libs_section_prefix {{libs}} $libs_section_postfix"
191 if (defined(invoker.postlink)) {
192 command += " && " + invoker.postlink
194 description = "LINK $outfile"
195 rspfile_content = "{{inputs}}"
199 if (defined(invoker.link_outputs)) {
200 outputs += invoker.link_outputs
205 command = "touch {{output}}"
206 description = "STAMP {{output}}"
210 command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
211 description = "COPY {{source}} {{output}}"
214 # When invoking this toolchain not as the default one, these args will be
215 # passed to the build. They are ignored when this is the default toolchain.
217 current_cpu = invoker.toolchain_cpu
218 current_os = invoker.toolchain_os
220 # These values need to be passed through unchanged.
221 target_os = target_os
222 target_cpu = target_cpu
224 if (defined(invoker.is_clang)) {
225 is_clang = invoker.is_clang
229 if (defined(invoker.deps)) {