Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 1 | # 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. |
| 20 | template("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 Mohr | 4b501c6 | 2019-12-19 09:11:52 -0800 | [diff] [blame] | 29 | |
| 30 | if (host_os == "mac") { |
| 31 | _xcode_sysroot = exec_script("$dir_pw_build/py/exec.py", |
| 32 | [ |
Alexei Frolov | 77af719 | 2019-12-20 11:55:11 -0800 | [diff] [blame] | 33 | "--", |
Rob Mohr | 4b501c6 | 2019-12-19 09:11:52 -0800 | [diff] [blame] | 34 | "/usr/bin/xcrun", |
| 35 | "--show-sdk-path", |
| 36 | ], |
| 37 | "trim string") |
| 38 | _cflags_list += [ "--sysroot=$_xcode_sysroot" ] |
| 39 | } |
| 40 | |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 41 | _toolchain_cflags = string_join(" ", _cflags_list) |
| 42 | |
Wyatt Hepler | 2119240 | 2020-01-15 15:40:51 -0800 | [diff] [blame] | 43 | # Specify the default C++ version, which targets can override with a config. |
| 44 | _toolchain_cflags_cc = "-std=c++17 -Wno-register" |
| 45 | |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 46 | # Toolchain LD flags |
| 47 | _toolchain_ldflags = "" |
| 48 | if (defined(invoker.toolchain_ldflags)) { |
Wyatt Hepler | cca0daf | 2019-11-14 11:29:04 -0800 | [diff] [blame] | 49 | _toolchain_ldflags = string_join(" ", invoker.toolchain_ldflags) |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 50 | } |
| 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 Hepler | 2119240 | 2020-01-15 15:40:51 -0800 | [diff] [blame] | 65 | _toolchain_cflags, |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 66 | "{{defines}}", |
| 67 | "{{include_dirs}}", |
| 68 | "{{asmflags}}", |
| 69 | "-c {{source}}", |
| 70 | "-o {{output}}", |
| 71 | ]) |
| 72 | depsformat = "gcc" |
| 73 | description = "as {{output}}" |
| 74 | outputs = [ |
Wyatt Hepler | c3a2d47 | 2020-01-09 12:24:24 -0800 | [diff] [blame] | 75 | # 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 Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 79 | ] |
| 80 | } |
| 81 | |
| 82 | tool("cc") { |
| 83 | depfile = "{{output}}.d" |
Wyatt Hepler | cca0daf | 2019-11-14 11:29:04 -0800 | [diff] [blame] | 84 | command = string_join(" ", |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 85 | [ |
| 86 | "$_cc", |
| 87 | "-MMD -MF $depfile", # Write out dependencies. |
Wyatt Hepler | 2119240 | 2020-01-15 15:40:51 -0800 | [diff] [blame] | 88 | _toolchain_cflags, |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 89 | "{{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 Hepler | c3a2d47 | 2020-01-09 12:24:24 -0800 | [diff] [blame] | 99 | "{{source_out_dir}}/{{target_output_name}}.{{source_file_part}}.o", |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 100 | ] |
| 101 | } |
| 102 | |
| 103 | tool("cxx") { |
| 104 | depfile = "{{output}}.d" |
| 105 | command = string_join(" ", |
| 106 | [ |
| 107 | "$_cxx", |
| 108 | "-MMD -MF $depfile", # Write out dependencies. |
Wyatt Hepler | 2119240 | 2020-01-15 15:40:51 -0800 | [diff] [blame] | 109 | _toolchain_cflags_cc, |
| 110 | _toolchain_cflags, |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 111 | "{{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 Hepler | c3a2d47 | 2020-01-09 12:24:24 -0800 | [diff] [blame] | 121 | "{{source_out_dir}}/{{target_output_name}}.{{source_file_part}}.o", |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 122 | ] |
| 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 Hepler | cca0daf | 2019-11-14 11:29:04 -0800 | [diff] [blame] | 138 | |
| 139 | _link_flags = [ |
| 140 | "$_cxx", |
| 141 | "{{ldflags}}", |
| 142 | |
Wyatt Hepler | 2119240 | 2020-01-15 15:40:51 -0800 | [diff] [blame] | 143 | _toolchain_cflags, |
| 144 | _toolchain_ldflags, |
Wyatt Hepler | cca0daf | 2019-11-14 11:29:04 -0800 | [diff] [blame] | 145 | |
| 146 | "{{inputs}}", |
| 147 | "{{libs}}", |
| 148 | |
| 149 | "-o $_link_outfile", |
| 150 | ] |
| 151 | |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 152 | _link_mapfile = "{{output_dir}}/{{target_output_name}}.map" |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 153 | |
Wyatt Hepler | cca0daf | 2019-11-14 11:29:04 -0800 | [diff] [blame] | 154 | if (host_os == "linux") { |
| 155 | _link_flags += [ |
| 156 | # Output a map file that shows symbols and their location. |
| 157 | "-Wl,-Map,$_link_mapfile", |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 158 | |
Wyatt Hepler | cca0daf | 2019-11-14 11:29:04 -0800 | [diff] [blame] | 159 | # 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 Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 166 | |
Wyatt Hepler | cca0daf | 2019-11-14 11:29:04 -0800 | [diff] [blame] | 167 | # Delete unreferenced sections. Helpful with -ffunction-sections. |
| 168 | "-Wl,-dead_strip", |
| 169 | ] |
| 170 | } |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 171 | |
Wyatt Hepler | cca0daf | 2019-11-14 11:29:04 -0800 | [diff] [blame] | 172 | _link_command = string_join(" ", _link_flags) |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 173 | |
| 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 Hepler | 9bc159f | 2020-01-15 08:06:11 -0800 | [diff] [blame] | 195 | if (host_os == "win") { |
| 196 | command = "cmd /c type nul > \"{{output}}\"" |
| 197 | } else { |
| 198 | command = "touch {{output}}" |
| 199 | } |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 200 | description = "stamp {{output}}" |
| 201 | } |
| 202 | |
| 203 | tool("copy") { |
| 204 | command = "cp -af {{source}} {{output}}" |
| 205 | description = "cp {{source}} {{output}}" |
| 206 | } |
| 207 | } |
| 208 | } |