blob: b052222b787a6d71abe3189a311ade94088f97c8 [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")
if (host_cpu == "arm64") {
_clang_cpu = "aarch64"
} else if (host_cpu == "x64") {
_clang_cpu = "x86_64"
} else {
assert(false, "Host CPU not supported")
}
if (host_os == "linux") {
_clang_os = "linux"
} else if (host_os == "mac") {
_clang_os = "darwin"
} else {
assert(false, "Host OS not supported")
}
_clang_name = "clang+llvm-$_clang_cpu-$_clang_os"
_clang_path = "//buildtools/toolchain/$_clang_name"
declare_args() {
# The default clang toolchain provided by the buildtools. This variable is
# additionally consumed by the go toolchain.
clang_prefix = rebase_path("$_clang_path/bin", root_build_dir)
}
# Defines a clang-based toolchain
#
# Parameters
#
# prefix
# If given 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
#
# 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.
#
# use_goma
# Override the global use_goma setting, useful to opt-out of goma in a
# particular toolchain by setting use_goma = false in it.
#
# deps
# Dependencies of this toolchain.
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\"")
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 = ""
}
prefix = clang_prefix
if (defined(invoker.prefix)) {
prefix = invoker.prefix
}
# Set all tools to defaults
cc = compiler_prefix + "${prefix}/clang"
cxx = compiler_prefix + "${prefix}/clang++"
ar = "${prefix}/llvm-ar"
ld = cxx
forward_variables_from(invoker, ["use_strip"])
if (use_strip) {
clang_strip = "${prefix}/strip"
strip = clang_strip
}
# Override any tools as requested by the invoker
if (defined(invoker.cc)) {
cc = compiler_prefix + invoker.cc
}
if (defined(invoker.cxx)) {
cxx = compiler_prefix + invoker.cxx
}
forward_variables_from(invoker,
[
"ar",
"deps",
"ld",
"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}}"
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 (use_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 (use_strip) {
command +=
" && $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 (use_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 (use_strip) {
command +=
" && $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 (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,--start-group @\"$rspfile\" {{solibs}} -Wl,--end-group {{libs}}"
rspfile_content = "{{inputs}}"
}
if (use_strip) {
command +=
" && $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
}
}
}