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 template defines a toolchain for something that works like gcc
8 # It requires the following variables specifying the executables to run:
13 # and the following which is used in the toolchain_args
14 # - toolchain_cpu_arch (What "cpu_arch" should be set to when invoking a
15 # build using this toolchain.)
16 # - toolchain_os (What "os" should be set to when invoking a build using this
19 # Optional parameters:
20 # - libs_section_prefix
21 # - libs_section_postfix
22 # The contents of these strings, if specified, will be placed around
23 # the libs section of the linker line. It allows one to inject libraries
24 # at the beginning and end for all targets in a toolchain.
25 # - solink_libs_section_prefix
26 # - solink_libs_section_postfix
27 # Same as libs_section_{pre,post}fix except used for solink instead of link.
29 # The content of this string, if specified, will be appended to the solink
32 # Just forwarded to the toolchain definition.
34 template("gcc_toolchain") {
35 toolchain(target_name) {
36 assert(defined(invoker.cc), "gcc_toolchain() must specify a \"cc\" value")
37 assert(defined(invoker.cxx), "gcc_toolchain() must specify a \"cxx\" value")
38 assert(defined(invoker.ar), "gcc_toolchain() must specify a \"ar\" value")
39 assert(defined(invoker.ld), "gcc_toolchain() must specify a \"ld\" value")
40 assert(defined(invoker.toolchain_cpu_arch),
41 "gcc_toolchain() must specify a \"toolchain_cpu_arch\"")
42 assert(defined(invoker.toolchain_os),
43 "gcc_toolchain() must specify a \"toolchain_os\"")
45 # We can't do string interpolation ($ in strings) on things with dots in
46 # them. To allow us to use $cc below, for example, we create copies of
47 # these values in our scope.
53 # Bring these into our scope for string interpolation with default values.
54 if (defined(invoker.libs_section_prefix)) {
55 libs_section_prefix = invoker.libs_section_prefix
57 libs_section_prefix = ""
60 if (defined(invoker.libs_section_postfix)) {
61 libs_section_postfix = invoker.libs_section_postfix
63 libs_section_postfix = ""
66 if (defined(invoker.solink_libs_section_prefix)) {
67 solink_libs_section_prefix = invoker.solink_libs_section_prefix
69 solink_libs_section_prefix = ""
72 if (defined(invoker.solink_libs_section_postfix)) {
73 solink_libs_section_postfix = invoker.solink_libs_section_postfix
75 solink_libs_section_postfix = ""
78 # These library switches can apply to all tools below.
83 depfile = "{{output}}.d"
84 command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
86 description = "CC {{output}}"
88 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
93 depfile = "{{output}}.d"
94 command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
96 description = "CXX {{output}}"
98 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
103 # For GCC we can just use the C compiler to compile assembly.
104 depfile = "{{output}}.d"
105 command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
107 description = "ASM {{output}}"
109 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
114 rspfile = "{{output}}.rsp"
115 command = "rm -f {{output}} && $ar rcs {{output}} @$rspfile"
116 description = "AR {{output}}"
117 rspfile_content = "{{inputs}}"
119 "{{target_out_dir}}/{{target_output_name}}{{output_extension}}"
121 default_output_extension = ".a"
122 output_prefix = "lib"
126 soname = "{{target_output_name}}{{output_extension}}" # e.g. "libfoo.so".
127 sofile = "{{root_out_dir}}/$soname" # Possibly including toolchain dir.
128 rspfile = sofile + ".rsp"
130 # These variables are not built into GN but are helpers that implement
131 # (1) linking to produce a .so, (2) extracting the symbols from that file
132 # to a temporary file, (3) if the temporary file has differences from the
133 # existing .TOC file, overwrite it, otherwise, don't change it.
134 tocfile = sofile + ".TOC"
135 temporary_tocname = sofile + ".tmp"
136 link_command = "$ld -shared {{ldflags}} -o $sofile -Wl,-soname=$soname @$rspfile"
137 toc_command = "{ readelf -d $sofile | grep SONAME ; nm -gD -f p $soname | cut -f1-2 -d' '; } > $temporary_tocname"
138 replace_command = "if ! cmp -s $temporary_tocname $tocfile; then mv $temporary_tocname $tocfile; fi"
140 command = "$link_command && $toc_command && $replace_command"
141 if (defined(invoker.postsolink)) {
142 command += " && " + invoker.postsolink
144 rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"
146 description = "SOLINK $sofile"
148 # Use this for {{output_extension}} expansions unless a target manually
149 # overrides it (in which case {{output_extension}} will be what the target
151 default_output_extension = ".so"
153 output_prefix = "lib"
155 # Since the above commands only updates the .TOC file when it changes, ask
156 # Ninja to check if the timestamp actually changed to know if downstream
157 # dependencies should be recompiled.
160 # Tell GN about the output files. It will link to the sofile but use the
161 # tocfile for dependency management.
167 depend_output = tocfile
171 outfile = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
172 rspfile = "$outfile.rsp"
173 command = "$ld {{ldflags}} -o $outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end-group $libs_section_prefix {{libs}} $libs_section_postfix"
174 description = "LINK $outfile"
175 rspfile_content = "{{inputs}}"
176 outputs = [ outfile ]
180 command = "touch {{output}}"
181 description = "STAMP {{output}}"
185 command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
186 description = "COPY {{source}} {{output}}"
189 # When invoking this toolchain not as the default one, these args will be
190 # passed to the build. They are ignored when this is the default toolchain.
192 cpu_arch = invoker.toolchain_cpu_arch
193 os = invoker.toolchain_os
194 if (defined(invoker.is_clang)) {
195 is_clang = invoker.is_clang
199 if (defined(invoker.deps)) {