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:
16 # and the following which is used in the toolchain_args
17 # - toolchain_cpu (What "current_cpu" should be set to when invoking a
18 # build using this toolchain.)
19 # - toolchain_os (What "current_os" should be set to when invoking a
20 # build using this toolchain.)
22 # Optional parameters:
23 # - libs_section_prefix
24 # - libs_section_postfix
25 # The contents of these strings, if specified, will be placed around
26 # the libs section of the linker line. It allows one to inject libraries
27 # at the beginning and end for all targets in a toolchain.
28 # - solink_libs_section_prefix
29 # - solink_libs_section_postfix
30 # Same as libs_section_{pre,post}fix except used for solink instead of link.
32 # The content of this string, if specified, will be appended to the solink
35 # Just forwarded to the toolchain definition.
37 template("gcc_toolchain") {
38 toolchain(target_name) {
39 assert(defined(invoker.cc), "gcc_toolchain() must specify a \"cc\" value")
40 assert(defined(invoker.cxx), "gcc_toolchain() must specify a \"cxx\" value")
41 assert(defined(invoker.ar), "gcc_toolchain() must specify a \"ar\" value")
42 assert(defined(invoker.ld), "gcc_toolchain() must specify a \"ld\" value")
43 assert(defined(invoker.toolchain_cpu),
44 "gcc_toolchain() must specify a \"toolchain_cpu\"")
45 assert(defined(invoker.toolchain_os),
46 "gcc_toolchain() must specify a \"toolchain_os\"")
48 # We can't do string interpolation ($ in strings) on things with dots in
49 # them. To allow us to use $cc below, for example, we create copies of
50 # these values in our scope.
55 if (defined(invoker.readelf)) {
56 readelf = invoker.readelf
60 if (defined(invoker.nm)) {
66 # Bring these into our scope for string interpolation with default values.
67 if (defined(invoker.libs_section_prefix)) {
68 libs_section_prefix = invoker.libs_section_prefix
70 libs_section_prefix = ""
73 if (defined(invoker.libs_section_postfix)) {
74 libs_section_postfix = invoker.libs_section_postfix
76 libs_section_postfix = ""
79 if (defined(invoker.solink_libs_section_prefix)) {
80 solink_libs_section_prefix = invoker.solink_libs_section_prefix
82 solink_libs_section_prefix = ""
85 if (defined(invoker.solink_libs_section_postfix)) {
86 solink_libs_section_postfix = invoker.solink_libs_section_postfix
88 solink_libs_section_postfix = ""
91 # These library switches can apply to all tools below.
96 depfile = "{{output}}.d"
97 command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
99 description = "CC {{output}}"
101 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
106 depfile = "{{output}}.d"
107 command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
109 description = "CXX {{output}}"
111 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
116 # For GCC we can just use the C compiler to compile assembly.
117 depfile = "{{output}}.d"
118 command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
120 description = "ASM {{output}}"
122 "{{target_out_dir}}/{{target_output_name}}/{{source_name_part}}.o",
127 rspfile = "{{output}}.rsp"
128 command = "rm -f {{output}} && $ar rcs {{output}} @$rspfile"
129 description = "AR {{output}}"
130 rspfile_content = "{{inputs}}"
132 "{{target_out_dir}}/{{target_output_name}}{{output_extension}}",
134 default_output_extension = ".a"
135 output_prefix = "lib"
139 soname = "{{target_output_name}}{{output_extension}}" # e.g. "libfoo.so".
140 sofile = "{{root_out_dir}}/$soname" # Possibly including toolchain dir.
141 rspfile = sofile + ".rsp"
143 # These variables are not built into GN but are helpers that implement
144 # (1) linking to produce a .so, (2) extracting the symbols from that file
145 # to a temporary file, (3) if the temporary file has differences from the
146 # existing .TOC file, overwrite it, otherwise, don't change it.
147 tocfile = sofile + ".TOC"
148 temporary_tocname = sofile + ".tmp"
150 "$ld -shared {{ldflags}} -o $sofile -Wl,-soname=$soname @$rspfile"
151 assert(defined(readelf), "to solink you must have a readelf")
152 assert(defined(nm), "to solink you must have an nm")
153 toc_command = "{ $readelf -d $sofile | grep SONAME ; $nm -gD -f p $sofile | cut -f1-2 -d' '; } > $temporary_tocname"
154 replace_command = "if ! cmp -s $temporary_tocname $tocfile; then mv $temporary_tocname $tocfile; fi"
156 command = "$link_command && $toc_command && $replace_command"
157 if (defined(invoker.postsolink)) {
158 command += " && " + invoker.postsolink
160 rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"
162 description = "SOLINK $sofile"
164 # Use this for {{output_extension}} expansions unless a target manually
165 # overrides it (in which case {{output_extension}} will be what the target
167 default_output_extension = ".so"
168 if (defined(invoker.default_output_extension)) {
169 default_output_extension = invoker.default_output_extension
172 output_prefix = "lib"
174 # Since the above commands only updates the .TOC file when it changes, ask
175 # Ninja to check if the timestamp actually changed to know if downstream
176 # dependencies should be recompiled.
179 # Tell GN about the output files. It will link to the sofile but use the
180 # tocfile for dependency management.
185 if (defined(invoker.solink_outputs)) {
186 outputs += invoker.solink_outputs
189 depend_output = tocfile
193 outfile = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
194 rspfile = "$outfile.rsp"
195 command = "$ld {{ldflags}} -o $outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end-group $libs_section_prefix {{libs}} $libs_section_postfix"
196 if (defined(invoker.postlink)) {
197 command += " && " + invoker.postlink
199 description = "LINK $outfile"
200 rspfile_content = "{{inputs}}"
204 if (defined(invoker.link_outputs)) {
205 outputs += invoker.link_outputs
210 command = "touch {{output}}"
211 description = "STAMP {{output}}"
215 command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
216 description = "COPY {{source}} {{output}}"
219 # When invoking this toolchain not as the default one, these args will be
220 # passed to the build. They are ignored when this is the default toolchain.
222 current_cpu = invoker.toolchain_cpu
223 current_os = invoker.toolchain_os
225 # These values need to be passed through unchanged.
226 target_os = target_os
227 target_cpu = target_cpu
229 if (defined(invoker.is_clang)) {
230 is_clang = invoker.is_clang
234 if (defined(invoker.deps)) {