blob: 049d5a90c19a34e8b2584d52bda8cd1094774d5a [file] [log] [blame]
# Copyright 2016 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/config/clang/clang.gni")
import("//build/toolchain/ccache.gni")
import("//build/toolchain/goma.gni")
import("//build/rust/config.gni")
# Each toolchain must define "stamp" and "copy" tools,
# but they are always the same in every toolchain.
stamp_command = "touch {{output}}"
stamp_description = "STAMP {{output}}"
# We use link instead of copy; the way "copy" tool is being used is
# compatible with links since Ninja is tracking changes to the source.
copy_command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
copy_description = "COPY {{source}} {{output}}"
# Defines a Clang-based toolchain.
#
# Parameters
#
# toolchain_cpu
# Required: Value of "current_cpu" inside this toolchain.
#
# toolchain_os
# Required: Value of "current_os" inside this toolchain.
#
# toolchain_args
# Optional: Scope specifying build arguments to override in this
# toolchain context.
# NOTE! Do not use this in the toolchain used as the default toolchain.
# It will have no effect, and you will be confused.
#
# use_strip
# Required: Whether to strip binaries, leaving unstripped ones
# in lib.unstripped and exe.unstripped subdirectories.
#
# prefix
# Optional: The path from which "cc", "cxx", "ar", "ld" and
# "strip" will be found (unless overridden). If not given,
# prefix defaults to $clang_prefix.
#
# cc
# cxx
# ar
# ld
# strip
# Optional: Override default tool names.
#
# use_ccache, use_goma
# Optional: Override the global setting, useful to opt out of
# ccache/goma in a particular toolchain.
#
# deps
# Optional: Dependencies of this toolchain.
#
# propagates_configs
# Optional: public_configs should escape this toolchain via deps
#
template("clang_toolchain") {
toolchain(target_name) {
assert(defined(invoker.toolchain_cpu),
"clang_toolchain() must specify a \"toolchain_cpu\"")
assert(defined(invoker.toolchain_os),
"clang_toolchain() must specify a \"toolchain_os\"")
compiler_prefix = ""
if (defined(invoker.use_goma)) {
use_goma = invoker.use_goma
}
if (use_goma) {
goma_dir = rebase_path(goma_dir)
compiler_prefix = "$goma_dir/gomacc "
} else {
if (defined(invoker.use_ccache)) {
use_ccache = invoker.use_ccache
}
if (use_ccache) {
compiler_prefix = "ccache "
}
}
prefix = clang_prefix
if (defined(invoker.prefix)) {
prefix = invoker.prefix
}
# Set all tools to defaults
cc = "${prefix}/clang"
cxx = "${prefix}/clang++"
ar = "${prefix}/llvm-ar"
ld = cxx
rustc = "${rustc_prefix}/rustc"
forward_variables_from(invoker, [ "use_strip" ])
if (use_strip) {
if (invoker.toolchain_os == "mac") {
strip = "xcrun strip"
} else {
strip = "${prefix}/llvm-objcopy"
buildidtool = rebase_path("//sysroot/bin/buildidtool", root_build_dir)
}
}
# Override any tools as requested by the invoker
if (defined(invoker.cc)) {
cc = invoker.cc
}
if (defined(invoker.cxx)) {
cxx = invoker.cxx
}
forward_variables_from(invoker,
[
"ar",
"deps",
"ld",
"propagates_configs",
"strip",
])
# These library switches can apply to all tools below
lib_switch = "-l"
lib_dir_switch = "-L"
tool("cc") {
depfile = "{{output}}.d"
command = "$cc -MD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
command_launcher = compiler_prefix
depsformat = "gcc"
description = "CC {{output}}"
outputs = [
"{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
]
}
tool("cxx") {
depfile = "{{output}}.d"
command = "$cxx -MD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
command_launcher = compiler_prefix
depsformat = "gcc"
description = "CXX {{output}}"
outputs = [
"{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
]
}
tool("asm") {
depfile = "{{output}}.d"
command = "$cc -MD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
command_launcher = compiler_prefix
depsformat = "gcc"
description = "ASM {{output}}"
outputs = [
"{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
]
}
tool("alink") {
rspfile = "{{output}}.rsp"
command =
"rm -f {{output}} && $ar {{arflags}} rcsD {{output}} @\"$rspfile\""
description = "AR {{output}}"
rspfile_content = "{{inputs}}"
outputs = [
"{{output_dir}}/{{target_output_name}}{{output_extension}}",
]
default_output_dir = "{{target_out_dir}}"
default_output_extension = ".a"
output_prefix = "lib"
}
tool("solink") {
outname = "{{target_output_name}}{{output_extension}}"
outfile = "{{output_dir}}/$outname"
rspfile = "$outfile.rsp"
pool = "//build/toolchain:link($default_toolchain)"
unstripped_outfile = outfile
if (use_strip) {
unstripped_outfile = "{{output_dir}}/lib.unstripped/{{target_output_name}}{{output_extension}}"
}
if (invoker.toolchain_os == "mac") {
command = "$ld -shared {{ldflags}} -Wl,-install_name,@rpath/\"{{target_output_name}}{{output_extension}}\" -o \"$unstripped_outfile\" -Wl,-filelist,\"$rspfile\" {{libs}} {{solibs}}"
rspfile_content = "{{inputs_newline}}"
default_output_extension = ".dylib"
} else {
command = "$ld -shared {{ldflags}} -o \"$unstripped_outfile\" -Wl,--Map=\"$unstripped_outfile.map\" -Wl,-soname=\"$outname\" @\"$rspfile\""
rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive {{libs}}"
default_output_extension = ".so"
}
description = "SOLINK $outfile"
default_output_dir = "{{root_out_dir}}"
output_prefix = "lib"
outputs = [
outfile,
]
if (outfile != unstripped_outfile) {
outputs += [ unstripped_outfile ]
}
if (use_strip) {
if (invoker.toolchain_os == "mac") {
command += " && $strip -x \"$unstripped_outfile\" -o \"$outfile\""
} else {
command +=
" && $strip --strip-all \"$unstripped_outfile\" \"$outfile\""
command +=
" && $buildidtool -build-id-dir .build-id" +
" -stamp \"$outfile.build-id.stamp\"" + " -entry \"=$outfile\"" +
" -entry \".debug=$unstripped_outfile\""
}
}
}
tool("link") {
outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
rspfile = "$outfile.rsp"
pool = "//build/toolchain:link($default_toolchain)"
unstripped_outfile = outfile
if (use_strip) {
unstripped_outfile = "{{root_out_dir}}/exe.unstripped/{{target_output_name}}{{output_extension}}"
}
if (invoker.toolchain_os == "mac") {
command = "$ld {{ldflags}} -o \"$unstripped_outfile\" -Wl,-filelist,\"$rspfile\" {{solibs}} {{libs}}"
rspfile_content = "{{inputs_newline}}"
} else {
command = "$ld {{ldflags}} -o \"$unstripped_outfile\" -Wl,--Map=\"$unstripped_outfile.map\" -Wl,--start-group @\"$rspfile\" {{solibs}} -Wl,--end-group {{libs}}"
rspfile_content = "{{inputs}}"
}
if (use_strip) {
if (invoker.toolchain_os == "mac") {
command += " && $strip \"$unstripped_outfile\" -o \"$outfile\""
} else {
command += " && " + "$strip --strip-sections" +
" \"$unstripped_outfile\" \"$outfile\""
command +=
" && $buildidtool -build-id-dir .build-id" +
" -stamp \"$outfile.build-id.stamp\"" + " -entry \"=$outfile\"" +
" -entry \".debug=$unstripped_outfile\""
}
}
description = "LINK $outfile"
default_output_dir = "{{root_out_dir}}"
outputs = [
outfile,
]
if (outfile != unstripped_outfile) {
outputs += [ unstripped_outfile ]
}
}
tool("rust_bin") {
pool = "//build/rust:pool($default_toolchain)"
outname = "{{target_output_name}}{{output_extension}}"
outfile = "{{output_dir}}/$outname"
depfile = "$outfile.d"
unstripped_outfile = outfile
command = ""
if (use_strip) {
unstripped_outfile = "{{output_dir}}/exe.unstripped/{{target_output_name}}{{output_extension}}"
# TODO: see the comment below
command += "mkdir -p {{output_dir}}/exe.unstripped && "
}
command += "$rustc --color=always --crate-name {{crate_name}} {{source}} --crate-type {{crate_type}} --emit=dep-info=$depfile,link -Zdep-info-omit-d-target {{rustflags}} -o \"$unstripped_outfile\" {{rustdeps}} {{externs}}"
description = "RUST {{output}}"
default_output_dir = "{{root_out_dir}}"
outputs = [
outfile,
]
# TODO: enable this once Ninja supports multiple outputs with depfile:
# https://github.com/ninja-build/ninja/pull/1534
#if (outfile != unstripped_outfile) {
# outputs += [ unstripped_outfile ]
#}
if (use_strip) {
if (invoker.toolchain_os == "mac") {
command += " && $strip \"$unstripped_outfile\" -o \"$outfile\""
} else {
command +=
" && $strip --strip-sections \"$unstripped_outfile\" \"$outfile\""
command +=
" && $buildidtool -build-id-dir .build-id" +
" -stamp \"$outfile.build-id.stamp\"" + " -entry \"=$outfile\"" +
" -entry \".debug=$unstripped_outfile\""
}
}
}
tool("rust_rlib") {
pool = "//build/rust:pool($default_toolchain)"
depfile = "{{output}}.d"
command = "$rustc --color=always --crate-name {{crate_name}} {{source}} --crate-type {{crate_type}} --emit=dep-info=$depfile,link -Zdep-info-omit-d-target {{rustflags}} -o {{output}} {{rustdeps}} {{externs}}"
description = "RUST {{output}}"
outputs = [
"{{output_dir}}/lib{{target_output_name}}{{output_extension}}",
]
default_output_dir = "{{target_out_dir}}"
default_output_extension = ".rlib"
}
tool("rust_staticlib") {
pool = "//build/rust:pool($default_toolchain)"
depfile = "{{output}}.d"
command = "$rustc --color=always --crate-name {{crate_name}} {{source}} --crate-type {{crate_type}} --emit=dep-info=$depfile,link -Zdep-info-omit-d-target {{rustflags}} -o {{output}} {{rustdeps}} {{externs}}"
description = "RUST {{output}}"
outputs = [
"{{output_dir}}/{{target_output_name}}{{output_extension}}",
]
default_output_dir = "{{target_out_dir}}"
default_output_extension = ".a"
output_prefix = "lib"
}
tool("rust_dylib") {
pool = "//build/rust:pool($default_toolchain)"
outname = "{{target_output_name}}{{output_extension}}"
outfile = "{{output_dir}}/$outname"
depfile = "$outfile.d"
command = ""
unstripped_outfile = outfile
if (use_strip) {
unstripped_outfile = "{{output_dir}}/lib.unstripped/{{target_output_name}}{{output_extension}}"
# TODO: see the comment below
command += "mkdir -p {{output_dir}}/exe.unstripped && "
}
command += "$rustc --color=always --crate-name {{crate_name}} {{source}} --crate-type {{crate_type}} --emit=dep-info=$depfile,link -Zdep-info-omit-d-target {{rustflags}} -o \"$unstripped_outfile\" {{rustdeps}} {{externs}}"
description = "RUST {{output}}"
default_output_dir = "{{root_out_dir}}"
if (invoker.toolchain_os == "mac") {
default_output_extension = ".dylib"
} else {
default_output_extension = ".so"
}
output_prefix = "lib"
outputs = [
outfile,
]
# TODO: enable this once Ninja supports multiple outputs with depfile:
# https://github.com/ninja-build/ninja/pull/1534
#if (outfile != unstripped_outfile) {
# outputs += [ unstripped_outfile ]
#}
if (use_strip) {
if (invoker.toolchain_os == "mac") {
command += " && $strip -x \"$unstripped_outfile\" -o \"$outfile\""
} else {
command +=
" && $strip --strip-all \"$unstripped_outfile\" \"$outfile\""
command +=
" && $buildidtool -build-id-dir .build-id" +
" -stamp \"$outfile.build-id.stamp\"" + " -entry \"=$outfile\"" +
" -entry \".debug=$unstripped_outfile\""
}
}
}
tool("rust_macro") {
pool = "//build/rust:pool($default_toolchain)"
depfile = "{{output}}.d"
command = "$rustc --color=always --crate-name {{crate_name}} {{source}} --crate-type {{crate_type}} --emit=dep-info=$depfile,link -Zdep-info-omit-d-target {{rustflags}} -o {{output}} {{rustdeps}} {{externs}}"
description = "RUST {{output}}"
outputs = [
"{{output_dir}}/{{target_output_name}}{{output_extension}}",
]
default_output_dir = "{{root_out_dir}}"
if (invoker.toolchain_os == "mac") {
default_output_extension = ".dylib"
} else {
default_output_extension = ".so"
}
output_prefix = "lib"
}
tool("stamp") {
command = stamp_command
description = stamp_description
}
tool("copy") {
command = copy_command
description = copy_description
}
# 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
if (defined(invoker.toolchain_args)) {
# The invoker isn't allowed to fiddle with the essential settings.
forward_variables_from(invoker.toolchain_args,
"*",
[
"current_cpu",
"current_os",
"target_os",
"target_cpu",
])
}
}
}
}