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.
56 # Bring these into our scope for string interpolation with default values.
57 if (defined(invoker.libs_section_prefix)) {
58 libs_section_prefix = invoker.libs_section_prefix
60 libs_section_prefix = ""
63 if (defined(invoker.libs_section_postfix)) {
64 libs_section_postfix = invoker.libs_section_postfix
66 libs_section_postfix = ""
69 if (defined(invoker.solink_libs_section_prefix)) {
70 solink_libs_section_prefix = invoker.solink_libs_section_prefix
72 solink_libs_section_prefix = ""
75 if (defined(invoker.solink_libs_section_postfix)) {
76 solink_libs_section_postfix = invoker.solink_libs_section_postfix
78 solink_libs_section_postfix = ""
81 # These library switches can apply to all tools below.
86 depfile = "{{output}}.d"
87 command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
89 description = "CC {{output}}"
91 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
96 depfile = "{{output}}.d"
97 command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
99 description = "CXX {{output}}"
101 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
106 # For GCC we can just use the C compiler to compile assembly.
107 depfile = "{{output}}.d"
108 command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
110 description = "ASM {{output}}"
112 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
117 rspfile = "{{output}}.rsp"
118 command = "rm -f {{output}} && $ar rcs {{output}} @$rspfile"
119 description = "AR {{output}}"
120 rspfile_content = "{{inputs}}"
122 "{{target_out_dir}}/{{target_output_name}}{{output_extension}}",
124 default_output_extension = ".a"
125 output_prefix = "lib"
129 soname = "{{target_output_name}}{{output_extension}}" # e.g. "libfoo.so".
130 sofile = "{{root_out_dir}}/$soname" # Possibly including toolchain dir.
131 rspfile = sofile + ".rsp"
133 # These variables are not built into GN but are helpers that implement
134 # (1) linking to produce a .so, (2) extracting the symbols from that file
135 # to a temporary file, (3) if the temporary file has differences from the
136 # existing .TOC file, overwrite it, otherwise, don't change it.
137 tocfile = sofile + ".TOC"
138 temporary_tocname = sofile + ".tmp"
140 "$ld -shared {{ldflags}} -o $sofile -Wl,-soname=$soname @$rspfile"
141 toc_command = "{ readelf -d $sofile | grep SONAME ; nm -gD -f p $sofile | cut -f1-2 -d' '; } > $temporary_tocname"
142 replace_command = "if ! cmp -s $temporary_tocname $tocfile; then mv $temporary_tocname $tocfile; fi"
144 command = "$link_command && $toc_command && $replace_command"
145 if (defined(invoker.postsolink)) {
146 command += " && " + invoker.postsolink
148 rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"
150 description = "SOLINK $sofile"
152 # Use this for {{output_extension}} expansions unless a target manually
153 # overrides it (in which case {{output_extension}} will be what the target
155 default_output_extension = ".so"
157 output_prefix = "lib"
159 # Since the above commands only updates the .TOC file when it changes, ask
160 # Ninja to check if the timestamp actually changed to know if downstream
161 # dependencies should be recompiled.
164 # Tell GN about the output files. It will link to the sofile but use the
165 # tocfile for dependency management.
170 if (defined(invoker.solink_outputs)) {
171 outputs += invoker.solink_outputs
174 depend_output = tocfile
178 outfile = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
179 rspfile = "$outfile.rsp"
180 command = "$ld {{ldflags}} -o $outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end-group $libs_section_prefix {{libs}} $libs_section_postfix"
181 if (defined(invoker.postlink)) {
182 command += " && " + invoker.postlink
184 description = "LINK $outfile"
185 rspfile_content = "{{inputs}}"
189 if (defined(invoker.link_outputs)) {
190 outputs += invoker.link_outputs
195 command = "touch {{output}}"
196 description = "STAMP {{output}}"
200 command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
201 description = "COPY {{source}} {{output}}"
204 # When invoking this toolchain not as the default one, these args will be
205 # passed to the build. They are ignored when this is the default toolchain.
207 current_cpu = invoker.toolchain_cpu
208 current_os = invoker.toolchain_os
210 # These values need to be passed through unchanged.
211 target_os = target_os
212 target_cpu = target_cpu
214 if (defined(invoker.is_clang)) {
215 is_clang = invoker.is_clang
219 if (defined(invoker.deps)) {