blob: d164bfee166f8c17a4a4d1790a6ac285b02a65c0 [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/toolchain/ccache.gni")
import("//build/toolchain/goma.gni")
# Defines a clang-based toolchain
#
# Parameters
#
# cc
# cxx
# ar
# ld
#
# 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.
#
# strip
# The strip executable which will be run on all shared libraries and
# executables as they're built.
#
# use_goma
# Override the global use_goma setting, useful to opt-out of goma in a
# particular toolchain by setting use_gome = false in it.
#
# deps
# Dependencies of this toolchain.
template("clang_toolchain") {
toolchain(target_name) {
assert(defined(invoker.cc), "clang_toolchain() must specify a \"cc\" value")
assert(defined(invoker.cxx),
"clang_toolchain() must specify a \"cxx\" value")
assert(defined(invoker.ar), "clang_toolchain() must specify a \"ar\" value")
assert(defined(invoker.ld), "clang_toolchain() must specify a \"ld\" value")
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.use_goma)) {
use_goma = invoker.use_goma
}
if (use_goma) {
compiler_prefix = "$goma_dir/gomacc "
} else if (use_ccache) {
compiler_prefix = "ccache "
} else {
compiler_prefix = ""
}
cc = compiler_prefix + invoker.cc
cxx = compiler_prefix + invoker.cxx
forward_variables_from(invoker,
[
"ar",
"deps",
"ld",
])
# 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}}"
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}}"
depsformat = "gcc"
description = "CXX {{output}}"
outputs = [
"{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
]
}
tool("asm") {
depfile = "{{output}}.d"
command = "$cc {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
description = "ASM {{output}}"
outputs = [
"{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
]
}
tool("objc") {
depfile = "{{output}}.d"
command = "$cc -MD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} {{cflags_objc}} -c {{source}} -o {{output}}"
depsformat = "gcc"
description = "OBJC {{output}}"
outputs = [
"{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
]
}
tool("objcxx") {
depfile = "{{output}}.d"
command = "$cxx -MD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} {{cflags_objcc}} -c {{source}} -o {{output}}"
depsformat = "gcc"
description = "OBJCXX {{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"
unstripped_outfile = outfile
if (defined(invoker.strip)) {
unstripped_outfile = "{{root_out_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,-soname=\"$outname\" @\"$rspfile\""
rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive {{libs}}"
default_output_extension = ".so"
}
if (defined(invoker.strip)) {
command +=
" && ${invoker.strip} -s -o \"$outfile\" \"$unstripped_outfile\""
}
description = "SOLINK $outfile"
default_output_dir = "{{root_out_dir}}"
output_prefix = "lib"
outputs = [
outfile,
]
if (outfile != unstripped_outfile) {
outputs += [ unstripped_outfile ]
}
}
tool("solink_module") {
outname = "{{target_output_name}}{{output_extension}}"
outfile = "{{output_dir}}/$outname"
rspfile = "$outfile.rsp"
if (defined(invoker.strip)) {
unstripped_outfile = "{{root_out_dir}}/lib.unstripped/{{target_output_name}}{{output_extension}}"
} else {
unstripped_outfile = outfile
}
if (invoker.toolchain_os == "mac") {
command = "$ld -shared {{ldflags}} -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,-soname=\"$outname\" @\"$rspfile\""
rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive {{libs}}"
default_output_extension = ".so"
}
if (defined(invoker.strip)) {
command +=
" && ${invoker.strip} -s -o \"$outfile\" \"$unstripped_outfile\""
}
description = "SOLINK $outfile"
default_output_dir = "{{root_out_dir}}"
outputs = [
outfile,
]
if (outfile != unstripped_outfile) {
outputs += [ unstripped_outfile ]
}
}
tool("link") {
outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
rspfile = "$outfile.rsp"
unstripped_outfile = outfile
if (defined(invoker.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,--start-group @\"$rspfile\" {{solibs}} -Wl,--end-group {{libs}}"
rspfile_content = "{{inputs}}"
}
if (defined(invoker.strip)) {
command +=
" && ${invoker.strip} -s -o \"$outfile\" \"$unstripped_outfile\""
}
description = "LINK $outfile"
default_output_dir = "{{root_out_dir}}"
outputs = [
outfile,
]
if (outfile != unstripped_outfile) {
outputs += [ unstripped_outfile ]
}
}
tool("stamp") {
command = "touch {{output}}"
description = "STAMP {{output}}"
}
tool("copy") {
# 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.
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_toolchain = target_toolchain
target_os = target_os
target_cpu = target_cpu
is_pic_default = invoker.is_pic_default
}
}
}