blob: 3b6bbb457b1c849aa9046c6bc6f9d53437df7a46 [file] [log] [blame]
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# This template defines a toolchain for something that works like gcc
# (including clang).
#
# It requires the following variables specifying the executables to run:
# - cc
# - cxx
# - ar
# - ld
# and the following which is used in the toolchain_args
# - toolchain_cpu (What "current_cpu" should be set to when invoking a
# build using this toolchain.)
# - toolchain_os (What "current_os" should be set to when invoking a
# build using this toolchain.)
#
# Optional parameters that control the tools:
#
# - libs_section_prefix
# - libs_section_postfix
# The contents of these strings, if specified, will be placed around
# the libs section of the linker line. It allows one to inject libraries
# at the beginning and end for all targets in a toolchain.
# - solink_libs_section_prefix
# - solink_libs_section_postfix
# Same as libs_section_{pre,post}fix except used for solink instead of link.
# - post_solink
# The content of this string, if specified, will be appended to the solink
# command.
# - deps
# Just forwarded to the toolchain definition.
#
# Optional build argument contols.
#
# - is_clang
# Whether to use clang instead of gcc.
# - is_component_build
# Whether to forcibly enable or disable component builds for this
# toolchain; if not specified, the toolchain will inherit the
# default setting.
# - use_gold
# Override the global use_gold setting, useful if the particular
# toolchain has a custom link step that is not actually using Gold.
template("gcc_toolchain") {
toolchain(target_name) {
assert(defined(invoker.cc), "gcc_toolchain() must specify a \"cc\" value")
assert(defined(invoker.cxx), "gcc_toolchain() must specify a \"cxx\" value")
assert(defined(invoker.ar), "gcc_toolchain() must specify a \"ar\" value")
assert(defined(invoker.ld), "gcc_toolchain() must specify a \"ld\" value")
assert(defined(invoker.toolchain_cpu),
"gcc_toolchain() must specify a \"toolchain_cpu\"")
assert(defined(invoker.toolchain_os),
"gcc_toolchain() must specify a \"toolchain_os\"")
compiler_prefix = ""
# This define changes when the toolchain changes, forcing a rebuild.
# Nothing should ever use this define.
if (defined(invoker.rebuild_define)) {
rebuild_string = "-D" + invoker.rebuild_define + " "
} else {
rebuild_string = ""
}
# We can't do string interpolation ($ in strings) on things with dots in
# them. To allow us to use $cc below, for example, we create copies of
# these values in our scope.
cc = compiler_prefix + invoker.cc
cxx = compiler_prefix + invoker.cxx
ar = invoker.ar
ld = invoker.ld
if (defined(invoker.readelf)) {
readelf = invoker.readelf
} else {
readelf = "readelf"
}
if (defined(invoker.nm)) {
nm = invoker.nm
} else {
nm = "nm"
}
if (defined(invoker.executable_extension)) {
default_executable_extension = invoker.executable_extension
} else {
default_executable_extension = ""
}
# Bring these into our scope for string interpolation with default values.
if (defined(invoker.libs_section_prefix)) {
libs_section_prefix = invoker.libs_section_prefix
} else {
libs_section_prefix = ""
}
if (defined(invoker.libs_section_postfix)) {
libs_section_postfix = invoker.libs_section_postfix
} else {
libs_section_postfix = ""
}
if (defined(invoker.solink_libs_section_prefix)) {
solink_libs_section_prefix = invoker.solink_libs_section_prefix
} else {
solink_libs_section_prefix = ""
}
if (defined(invoker.solink_libs_section_postfix)) {
solink_libs_section_postfix = invoker.solink_libs_section_postfix
} else {
solink_libs_section_postfix = ""
}
# These library switches can apply to all tools below.
lib_switch = "-l"
lib_dir_switch = "-L"
tool("cc") {
depfile = "{{output}}.d"
command = "$cc -g -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
depsformat = "gcc"
description = "CC {{output}}"
outputs = [
"{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
]
}
tool("cxx") {
depfile = "{{output}}.d"
command = "$cxx -g -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
depsformat = "gcc"
description = "CXX {{output}}"
outputs = [
"{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
]
}
tool("asm") {
# We can just use the C compiler to compile assembly.
command = "$cc ${rebuild_string}{{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
description = "ASM {{output}}"
outputs = [
"{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
]
}
tool("alink") {
rspfile = "{{output}}.rsp"
command = "rm -f {{output}} && $ar rcs {{output}} @$rspfile"
description = "AR {{output}}"
rspfile_content = "{{inputs}}"
outputs = [
"{{target_out_dir}}/{{target_output_name}}{{output_extension}}",
]
default_output_extension = ".a"
output_prefix = "lib"
}
tool("solink") {
soname = "{{target_output_name}}{{output_extension}}" # e.g. "libfoo.so".
sofile = "{{root_out_dir}}/$soname" # Possibly including toolchain dir.
rspfile = sofile + ".rsp"
# These variables are not built into GN but are helpers that implement
# (1) linking to produce a .so, (2) extracting the symbols from that file
# to a temporary file, (3) if the temporary file has differences from the
# existing .TOC file, overwrite it, otherwise, don't change it.
tocfile = sofile + ".TOC"
temporary_tocname = sofile + ".tmp"
link_command =
"$ld -shared {{ldflags}} -o $sofile -Wl,-soname=$soname @$rspfile"
toc_command = "{ $readelf -d $sofile | grep SONAME ; $nm -gD -f p $sofile | cut -f1-2 -d' '; } > $temporary_tocname"
replace_command = "if ! cmp -s $temporary_tocname $tocfile; then mv $temporary_tocname $tocfile; fi"
command = "$link_command && $toc_command && $replace_command"
if (defined(invoker.postsolink)) {
command += " && " + invoker.postsolink
}
rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"
description = "SOLINK $sofile"
# Use this for {{output_extension}} expansions unless a target manually
# overrides it (in which case {{output_extension}} will be what the target
# specifies).
default_output_extension = ".so"
output_prefix = "lib"
# Since the above commands only updates the .TOC file when it changes, ask
# Ninja to check if the timestamp actually changed to know if downstream
# dependencies should be recompiled.
restat = true
# Tell GN about the output files. It will link to the sofile but use the
# tocfile for dependency management.
outputs = [
sofile,
tocfile,
]
if (defined(invoker.solink_outputs)) {
outputs += invoker.solink_outputs
}
link_output = sofile
depend_output = tocfile
}
tool("link") {
outfile = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
rspfile = "$outfile.rsp"
command = "$ld {{ldflags}} -g -o $outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end-group $libs_section_prefix {{libs}} $libs_section_postfix"
default_output_extension = default_executable_extension
if (defined(invoker.postlink)) {
command += " && " + invoker.postlink
}
description = "LINK $outfile"
rspfile_content = "{{inputs}}"
outputs = [
outfile,
]
if (defined(invoker.link_outputs)) {
outputs += invoker.link_outputs
}
}
tool("stamp") {
command = "touch {{output}}"
description = "STAMP {{output}}"
}
tool("copy") {
command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
description = "COPY {{source}} {{output}}"
}
# When invoking this toolchain not as the default one, these args will be
# passed to the build. They are ignored when this is the default toolchain.
toolchain_args() {
current_cpu = invoker.toolchain_cpu
current_os = invoker.toolchain_os
# These values need to be passed through unchanged.
target_os = target_os
target_cpu = target_cpu
forward_variables_from(invoker,
[
"is_clang",
"is_component_build",
"use_gold",
"symbol_level",
])
}
forward_variables_from(invoker, [ "deps" ])
}
}
# This is a shorthand for gcc_toolchain instances based on the
# Chromium-built version of Clang. Only the toolchain_cpu and
# toolchain_os variables need to be specified by the invoker, and
# optionally toolprefix if it's a cross-compile case. Note that for
# a cross-compile case this toolchain requires a config to pass the
# appropriate -target option, or else it will actually just be doing
# a native compile. The invoker can optionally override use_gold too.
template("clang_toolchain") {
assert(defined(invoker.toolchain_cpu),
"clang_toolchain() must specify a \"toolchain_cpu\"")
assert(defined(invoker.toolchain_os),
"clang_toolchain() must specify a \"toolchain_os\"")
if (defined(invoker.toolprefix)) {
toolprefix = invoker.toolprefix
} else {
toolprefix = ""
}
gcc_toolchain(target_name) {
prefix = rebase_path("//third_party/llvm-build/Release+Asserts/bin",
root_build_dir)
cc = "$prefix/clang"
cxx = "$prefix/clang++"
ld = cxx
is_clang = true
readelf = "${toolprefix}readelf"
ar = "${toolprefix}ar"
nm = "${toolprefix}nm"
forward_variables_from(invoker,
[
"toolchain_cpu",
"toolchain_os",
"use_gold",
])
}
}