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 "{{source_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 "{{source_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 "{{source_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"
164 output_prefix = "lib"
166 # Since the above commands only updates the .TOC file when it changes, ask
167 # Ninja to check if the timestamp actually changed to know if downstream
168 # dependencies should be recompiled.
171 # Tell GN about the output files. It will link to the sofile but use the
172 # tocfile for dependency management.
177 if (defined(invoker.solink_outputs)) {
178 outputs += invoker.solink_outputs
181 depend_output = tocfile
185 outfile = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
186 rspfile = "$outfile.rsp"
187 command = "$ld {{ldflags}} -o $outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end-group $libs_section_prefix {{libs}} $libs_section_postfix"
188 if (defined(invoker.postlink)) {
189 command += " && " + invoker.postlink
191 description = "LINK $outfile"
192 rspfile_content = "{{inputs}}"
196 if (defined(invoker.link_outputs)) {
197 outputs += invoker.link_outputs
202 command = "touch {{output}}"
203 description = "STAMP {{output}}"
207 command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
208 description = "COPY {{source}} {{output}}"
211 # When invoking this toolchain not as the default one, these args will be
212 # passed to the build. They are ignored when this is the default toolchain.
214 current_cpu = invoker.toolchain_cpu
215 current_os = invoker.toolchain_os
217 # These values need to be passed through unchanged.
218 target_os = target_os
219 target_cpu = target_cpu
221 if (defined(invoker.is_clang)) {
222 is_clang = invoker.is_clang
226 if (defined(invoker.deps)) {