2019-11-14 08:15:41 +00:00
|
|
|
# Copyright 2019 The Pigweed Authors
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
# use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
# the License at
|
|
|
|
#
|
|
|
|
# https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations under
|
|
|
|
# the License.
|
|
|
|
|
|
|
|
# Generates a host clang toolchain for a specific target.
|
|
|
|
#
|
|
|
|
# Args:
|
|
|
|
# toolchain_cflags: Additional C/C++ compiler flags for the target.
|
|
|
|
# toolchain_ldflags: Additional linker flags for the target.
|
|
|
|
template("host_clang") {
|
|
|
|
# Toolchain C flags
|
|
|
|
_cflags_list = [
|
|
|
|
# Colorize output. Ninja's Clang invocation disables color by default.
|
|
|
|
"-fdiagnostics-color",
|
|
|
|
]
|
|
|
|
if (defined(invoker.toolchain_cflags)) {
|
|
|
|
_cflags_list += invoker.toolchain_cflags
|
|
|
|
}
|
2019-12-19 17:11:52 +00:00
|
|
|
|
|
|
|
if (host_os == "mac") {
|
|
|
|
_xcode_sysroot = exec_script("$dir_pw_build/py/exec.py",
|
|
|
|
[
|
2019-12-20 19:55:11 +00:00
|
|
|
"--",
|
2019-12-19 17:11:52 +00:00
|
|
|
"/usr/bin/xcrun",
|
|
|
|
"--show-sdk-path",
|
|
|
|
],
|
|
|
|
"trim string")
|
|
|
|
_cflags_list += [ "--sysroot=$_xcode_sysroot" ]
|
|
|
|
}
|
|
|
|
|
2019-11-14 08:15:41 +00:00
|
|
|
_toolchain_cflags = string_join(" ", _cflags_list)
|
|
|
|
|
2020-01-15 23:40:51 +00:00
|
|
|
# Specify the default C++ version, which targets can override with a config.
|
|
|
|
_toolchain_cflags_cc = "-std=c++17 -Wno-register"
|
|
|
|
|
2019-11-14 08:15:41 +00:00
|
|
|
# Toolchain LD flags
|
|
|
|
_toolchain_ldflags = ""
|
|
|
|
if (defined(invoker.toolchain_ldflags)) {
|
2019-11-14 19:29:04 +00:00
|
|
|
_toolchain_ldflags = string_join(" ", invoker.toolchain_ldflags)
|
2019-11-14 08:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Note: On macOS, there is no "llvm-ar", only "ar", which happens to be LLVM
|
|
|
|
# ar. This should get updated for linux systems.
|
|
|
|
_ar = "ar"
|
|
|
|
_cc = "clang"
|
|
|
|
_cxx = "clang++"
|
|
|
|
|
|
|
|
toolchain(target_name) {
|
|
|
|
tool("asm") {
|
|
|
|
depfile = "{{output}}.d"
|
|
|
|
command = string_join(" ",
|
|
|
|
[
|
|
|
|
"$_cc",
|
|
|
|
"-MMD -MF $depfile", # Write out dependencies.
|
2020-01-15 23:40:51 +00:00
|
|
|
_toolchain_cflags,
|
2019-11-14 08:15:41 +00:00
|
|
|
"{{defines}}",
|
|
|
|
"{{include_dirs}}",
|
|
|
|
"{{asmflags}}",
|
|
|
|
"-c {{source}}",
|
|
|
|
"-o {{output}}",
|
|
|
|
])
|
|
|
|
depsformat = "gcc"
|
|
|
|
description = "as {{output}}"
|
|
|
|
outputs = [
|
2020-01-09 20:24:24 +00:00
|
|
|
# Use {{source_file_part}}, which includes the extension, instead of
|
|
|
|
# {{source_name_part}} so that object files created from <file_name>.c
|
|
|
|
# and <file_name>.cc sources are unique.
|
|
|
|
"{{source_out_dir}}/{{target_output_name}}.{{source_file_part}}.o",
|
2019-11-14 08:15:41 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
tool("cc") {
|
|
|
|
depfile = "{{output}}.d"
|
2019-11-14 19:29:04 +00:00
|
|
|
command = string_join(" ",
|
2019-11-14 08:15:41 +00:00
|
|
|
[
|
|
|
|
"$_cc",
|
|
|
|
"-MMD -MF $depfile", # Write out dependencies.
|
2020-01-15 23:40:51 +00:00
|
|
|
_toolchain_cflags,
|
2019-11-14 08:15:41 +00:00
|
|
|
"{{defines}}",
|
|
|
|
"{{include_dirs}}",
|
|
|
|
"{{cflags}}",
|
|
|
|
"{{cflags_c}}",
|
|
|
|
"-c {{source}}",
|
|
|
|
"-o {{output}}",
|
|
|
|
])
|
|
|
|
depsformat = "gcc"
|
|
|
|
description = "cc {{output}}"
|
2020-02-27 19:43:49 +00:00
|
|
|
outputs =
|
|
|
|
[ "{{source_out_dir}}/{{target_output_name}}.{{source_file_part}}.o" ]
|
2019-11-14 08:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tool("cxx") {
|
|
|
|
depfile = "{{output}}.d"
|
|
|
|
command = string_join(" ",
|
|
|
|
[
|
|
|
|
"$_cxx",
|
|
|
|
"-MMD -MF $depfile", # Write out dependencies.
|
2020-01-15 23:40:51 +00:00
|
|
|
_toolchain_cflags_cc,
|
|
|
|
_toolchain_cflags,
|
2019-11-14 08:15:41 +00:00
|
|
|
"{{defines}}",
|
|
|
|
"{{include_dirs}}",
|
|
|
|
"{{cflags}}",
|
|
|
|
"{{cflags_cc}}",
|
|
|
|
"-c {{source}}",
|
|
|
|
"-o {{output}}",
|
|
|
|
])
|
|
|
|
depsformat = "gcc"
|
|
|
|
description = "c++ {{output}}"
|
2020-02-27 19:43:49 +00:00
|
|
|
outputs =
|
|
|
|
[ "{{source_out_dir}}/{{target_output_name}}.{{source_file_part}}.o" ]
|
2019-11-14 08:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tool("alink") {
|
|
|
|
command = "rm -f {{output}} && $_ar rcs {{output}} {{inputs}}"
|
|
|
|
description = "ar {{target_output_name}}{{output_extension}}"
|
2020-02-27 19:43:49 +00:00
|
|
|
outputs =
|
|
|
|
[ "{{target_out_dir}}/{{target_output_name}}{{output_extension}}" ]
|
2019-11-14 08:15:41 +00:00
|
|
|
default_output_extension = ".a"
|
|
|
|
}
|
|
|
|
|
|
|
|
lib_switch = "-l"
|
|
|
|
lib_dir_switch = "-L"
|
|
|
|
|
|
|
|
_link_outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
|
|
|
|
|
2019-11-14 19:29:04 +00:00
|
|
|
_link_flags = [
|
|
|
|
"$_cxx",
|
|
|
|
"{{ldflags}}",
|
|
|
|
|
2020-01-15 23:40:51 +00:00
|
|
|
_toolchain_cflags,
|
|
|
|
_toolchain_ldflags,
|
2019-11-14 19:29:04 +00:00
|
|
|
|
|
|
|
"{{inputs}}",
|
|
|
|
"{{libs}}",
|
|
|
|
|
|
|
|
"-o $_link_outfile",
|
|
|
|
]
|
2019-11-14 08:15:41 +00:00
|
|
|
|
2019-11-14 19:29:04 +00:00
|
|
|
_link_mapfile = "{{output_dir}}/{{target_output_name}}.map"
|
|
|
|
|
2020-01-22 20:41:36 +00:00
|
|
|
if (host_os == "mac") {
|
2019-11-14 19:29:04 +00:00
|
|
|
_link_flags += [
|
|
|
|
# Output a map file that shows symbols and their location.
|
2020-01-22 20:41:36 +00:00
|
|
|
"-Wl,-map,$_link_mapfile",
|
2019-11-14 08:15:41 +00:00
|
|
|
|
2019-11-14 19:29:04 +00:00
|
|
|
# Delete unreferenced sections. Helpful with -ffunction-sections.
|
2020-01-22 20:41:36 +00:00
|
|
|
"-Wl,-dead_strip",
|
2019-11-14 19:29:04 +00:00
|
|
|
]
|
2020-01-22 20:41:36 +00:00
|
|
|
} else {
|
2019-11-14 19:29:04 +00:00
|
|
|
_link_flags += [
|
|
|
|
# Output a map file that shows symbols and their location.
|
2020-01-22 20:41:36 +00:00
|
|
|
"-Wl,-Map,$_link_mapfile",
|
2019-11-14 08:15:41 +00:00
|
|
|
|
2019-11-14 19:29:04 +00:00
|
|
|
# Delete unreferenced sections. Helpful with -ffunction-sections.
|
2020-01-22 20:41:36 +00:00
|
|
|
"-Wl,--gc-sections",
|
2019-11-14 19:29:04 +00:00
|
|
|
]
|
|
|
|
}
|
2019-11-14 08:15:41 +00:00
|
|
|
|
2019-11-14 19:29:04 +00:00
|
|
|
_link_command = string_join(" ", _link_flags)
|
2019-11-14 08:15:41 +00:00
|
|
|
|
|
|
|
tool("link") {
|
|
|
|
command = _link_command
|
|
|
|
description = "ld $_link_outfile"
|
2020-02-27 19:43:49 +00:00
|
|
|
outputs = [ _link_outfile ]
|
2019-11-14 08:15:41 +00:00
|
|
|
default_output_dir = "{{target_out_dir}}"
|
|
|
|
default_output_extension = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
tool("solink") {
|
|
|
|
command = _link_command + " -shared"
|
|
|
|
description = "ld -shared $_link_outfile"
|
2020-02-27 19:43:49 +00:00
|
|
|
outputs = [ _link_outfile ]
|
2019-11-14 08:15:41 +00:00
|
|
|
default_output_dir = "{{target_out_dir}}"
|
|
|
|
default_output_extension = ".so"
|
|
|
|
}
|
|
|
|
|
|
|
|
tool("stamp") {
|
2020-01-15 16:06:11 +00:00
|
|
|
if (host_os == "win") {
|
|
|
|
command = "cmd /c type nul > \"{{output}}\""
|
|
|
|
} else {
|
|
|
|
command = "touch {{output}}"
|
|
|
|
}
|
2019-11-14 08:15:41 +00:00
|
|
|
description = "stamp {{output}}"
|
|
|
|
}
|
|
|
|
|
|
|
|
tool("copy") {
|
|
|
|
command = "cp -af {{source}} {{output}}"
|
|
|
|
description = "cp {{source}} {{output}}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|