blob: 2dd95772eba466b544e3fa612490682f219db5b9 [file] [log] [blame]
Keir Mierle58671912019-11-14 00:15:41 -08001# Copyright 2019 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7# https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15# Generates a host clang toolchain for a specific target.
16#
17# Args:
18# toolchain_cflags: Additional C/C++ compiler flags for the target.
19# toolchain_ldflags: Additional linker flags for the target.
20template("host_clang") {
21 # Toolchain C flags
22 _cflags_list = [
23 # Colorize output. Ninja's Clang invocation disables color by default.
24 "-fdiagnostics-color",
25 ]
26 if (defined(invoker.toolchain_cflags)) {
27 _cflags_list += invoker.toolchain_cflags
28 }
Rob Mohr4b501c62019-12-19 09:11:52 -080029
30 if (host_os == "mac") {
31 _xcode_sysroot = exec_script("$dir_pw_build/py/exec.py",
32 [
Alexei Frolov77af7192019-12-20 11:55:11 -080033 "--",
Rob Mohr4b501c62019-12-19 09:11:52 -080034 "/usr/bin/xcrun",
35 "--show-sdk-path",
36 ],
37 "trim string")
38 _cflags_list += [ "--sysroot=$_xcode_sysroot" ]
39 }
40
Keir Mierle58671912019-11-14 00:15:41 -080041 _toolchain_cflags = string_join(" ", _cflags_list)
42
Wyatt Hepler21192402020-01-15 15:40:51 -080043 # Specify the default C++ version, which targets can override with a config.
44 _toolchain_cflags_cc = "-std=c++17 -Wno-register"
45
Keir Mierle58671912019-11-14 00:15:41 -080046 # Toolchain LD flags
47 _toolchain_ldflags = ""
48 if (defined(invoker.toolchain_ldflags)) {
Wyatt Heplercca0daf2019-11-14 11:29:04 -080049 _toolchain_ldflags = string_join(" ", invoker.toolchain_ldflags)
Keir Mierle58671912019-11-14 00:15:41 -080050 }
51
52 # Note: On macOS, there is no "llvm-ar", only "ar", which happens to be LLVM
53 # ar. This should get updated for linux systems.
54 _ar = "ar"
55 _cc = "clang"
56 _cxx = "clang++"
57
58 toolchain(target_name) {
59 tool("asm") {
60 depfile = "{{output}}.d"
61 command = string_join(" ",
62 [
63 "$_cc",
64 "-MMD -MF $depfile", # Write out dependencies.
Wyatt Hepler21192402020-01-15 15:40:51 -080065 _toolchain_cflags,
Keir Mierle58671912019-11-14 00:15:41 -080066 "{{defines}}",
67 "{{include_dirs}}",
68 "{{asmflags}}",
69 "-c {{source}}",
70 "-o {{output}}",
71 ])
72 depsformat = "gcc"
73 description = "as {{output}}"
74 outputs = [
Wyatt Heplerc3a2d472020-01-09 12:24:24 -080075 # Use {{source_file_part}}, which includes the extension, instead of
76 # {{source_name_part}} so that object files created from <file_name>.c
77 # and <file_name>.cc sources are unique.
78 "{{source_out_dir}}/{{target_output_name}}.{{source_file_part}}.o",
Keir Mierle58671912019-11-14 00:15:41 -080079 ]
80 }
81
82 tool("cc") {
83 depfile = "{{output}}.d"
Wyatt Heplercca0daf2019-11-14 11:29:04 -080084 command = string_join(" ",
Keir Mierle58671912019-11-14 00:15:41 -080085 [
86 "$_cc",
87 "-MMD -MF $depfile", # Write out dependencies.
Wyatt Hepler21192402020-01-15 15:40:51 -080088 _toolchain_cflags,
Keir Mierle58671912019-11-14 00:15:41 -080089 "{{defines}}",
90 "{{include_dirs}}",
91 "{{cflags}}",
92 "{{cflags_c}}",
93 "-c {{source}}",
94 "-o {{output}}",
95 ])
96 depsformat = "gcc"
97 description = "cc {{output}}"
98 outputs = [
Wyatt Heplerc3a2d472020-01-09 12:24:24 -080099 "{{source_out_dir}}/{{target_output_name}}.{{source_file_part}}.o",
Keir Mierle58671912019-11-14 00:15:41 -0800100 ]
101 }
102
103 tool("cxx") {
104 depfile = "{{output}}.d"
105 command = string_join(" ",
106 [
107 "$_cxx",
108 "-MMD -MF $depfile", # Write out dependencies.
Wyatt Hepler21192402020-01-15 15:40:51 -0800109 _toolchain_cflags_cc,
110 _toolchain_cflags,
Keir Mierle58671912019-11-14 00:15:41 -0800111 "{{defines}}",
112 "{{include_dirs}}",
113 "{{cflags}}",
114 "{{cflags_cc}}",
115 "-c {{source}}",
116 "-o {{output}}",
117 ])
118 depsformat = "gcc"
119 description = "c++ {{output}}"
120 outputs = [
Wyatt Heplerc3a2d472020-01-09 12:24:24 -0800121 "{{source_out_dir}}/{{target_output_name}}.{{source_file_part}}.o",
Keir Mierle58671912019-11-14 00:15:41 -0800122 ]
123 }
124
125 tool("alink") {
126 command = "rm -f {{output}} && $_ar rcs {{output}} {{inputs}}"
127 description = "ar {{target_output_name}}{{output_extension}}"
128 outputs = [
129 "{{target_out_dir}}/{{target_output_name}}{{output_extension}}",
130 ]
131 default_output_extension = ".a"
132 }
133
134 lib_switch = "-l"
135 lib_dir_switch = "-L"
136
137 _link_outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
Wyatt Heplercca0daf2019-11-14 11:29:04 -0800138
139 _link_flags = [
140 "$_cxx",
141 "{{ldflags}}",
142
Wyatt Hepler21192402020-01-15 15:40:51 -0800143 _toolchain_cflags,
144 _toolchain_ldflags,
Wyatt Heplercca0daf2019-11-14 11:29:04 -0800145
146 "{{inputs}}",
147 "{{libs}}",
148
149 "-o $_link_outfile",
150 ]
151
Keir Mierle58671912019-11-14 00:15:41 -0800152 _link_mapfile = "{{output_dir}}/{{target_output_name}}.map"
Keir Mierle58671912019-11-14 00:15:41 -0800153
Wyatt Heplercca0daf2019-11-14 11:29:04 -0800154 if (host_os == "linux") {
155 _link_flags += [
156 # Output a map file that shows symbols and their location.
157 "-Wl,-Map,$_link_mapfile",
Keir Mierle58671912019-11-14 00:15:41 -0800158
Wyatt Heplercca0daf2019-11-14 11:29:04 -0800159 # Delete unreferenced sections. Helpful with -ffunction-sections.
160 "-Wl,--gc-sections",
161 ]
162 } else if (host_os == "mac") {
163 _link_flags += [
164 # Output a map file that shows symbols and their location.
165 "-Wl,-map,$_link_mapfile",
Keir Mierle58671912019-11-14 00:15:41 -0800166
Wyatt Heplercca0daf2019-11-14 11:29:04 -0800167 # Delete unreferenced sections. Helpful with -ffunction-sections.
168 "-Wl,-dead_strip",
169 ]
170 }
Keir Mierle58671912019-11-14 00:15:41 -0800171
Wyatt Heplercca0daf2019-11-14 11:29:04 -0800172 _link_command = string_join(" ", _link_flags)
Keir Mierle58671912019-11-14 00:15:41 -0800173
174 tool("link") {
175 command = _link_command
176 description = "ld $_link_outfile"
177 outputs = [
178 _link_outfile,
179 ]
180 default_output_dir = "{{target_out_dir}}"
181 default_output_extension = ""
182 }
183
184 tool("solink") {
185 command = _link_command + " -shared"
186 description = "ld -shared $_link_outfile"
187 outputs = [
188 _link_outfile,
189 ]
190 default_output_dir = "{{target_out_dir}}"
191 default_output_extension = ".so"
192 }
193
194 tool("stamp") {
Wyatt Hepler9bc159f2020-01-15 08:06:11 -0800195 if (host_os == "win") {
196 command = "cmd /c type nul > \"{{output}}\""
197 } else {
198 command = "touch {{output}}"
199 }
Keir Mierle58671912019-11-14 00:15:41 -0800200 description = "stamp {{output}}"
201 }
202
203 tool("copy") {
204 command = "cp -af {{source}} {{output}}"
205 description = "cp {{source}} {{output}}"
206 }
207 }
208}