Chris Kay | c8a47ba | 2023-10-20 09:17:33 +0000 | [diff] [blame] | 1 | # |
| 2 | # Copyright (c) 2023-2024, Arm Limited and Contributors. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | |
| 7 | # |
| 8 | # TF-A uses three toolchains: |
| 9 | # |
| 10 | # - The host toolchain (`host`) for building native tools |
| 11 | # - The AArch32 toolchain (`aarch32`) for building Arm AArch32 images |
| 12 | # - The AArch64 toolchain (`aarch64`) for building Arm AArch64 images |
| 13 | # |
| 14 | # In the main Makefile only one of the two Arm toolchains is enabled in any |
| 15 | # given build, but individual tools and libraries may need access to both. |
| 16 | # |
| 17 | |
| 18 | toolchains ?= host $(ARCH) |
| 19 | |
| 20 | ifneq ($(filter host,$(toolchains)),) |
| 21 | host-cc := $(HOSTCC) |
| 22 | host-cpp := $(HOSTCPP) |
| 23 | |
| 24 | host-as := $(HOSTAS) |
| 25 | |
| 26 | host-ld := $(HOSTLD) |
| 27 | host-oc := $(HOSTOC) |
| 28 | host-od := $(HOSTOD) |
| 29 | host-ar := $(HOSTAR) |
| 30 | |
| 31 | host-dtc := $(HOSTDTC) |
| 32 | endif |
| 33 | |
| 34 | ifneq ($(filter aarch32,$(toolchains)),) |
| 35 | aarch32-cc := $(if $(filter-out default,$(origin CC)),$(CC)) |
| 36 | aarch32-cpp := $(if $(filter-out default,$(origin CPP)),$(CPP)) |
| 37 | |
| 38 | aarch32-as := $(if $(filter-out default,$(origin AS)),$(AS)) |
| 39 | |
| 40 | aarch32-ld := $(if $(filter-out default,$(origin LD)),$(LD)) |
| 41 | aarch32-oc := $(if $(filter-out default,$(origin OC)),$(OC)) |
| 42 | aarch32-od := $(if $(filter-out default,$(origin OD)),$(OD)) |
| 43 | aarch32-ar := $(if $(filter-out default,$(origin AR)),$(AR)) |
| 44 | |
| 45 | aarch32-dtc := $(if $(filter-out default,$(origin DTC)),$(DTC)) |
| 46 | endif |
| 47 | |
| 48 | ifneq ($(filter aarch64,$(toolchains)),) |
| 49 | aarch64-cc := $(if $(filter-out default,$(origin CC)),$(CC)) |
| 50 | aarch64-cpp := $(if $(filter-out default,$(origin CPP)),$(CPP)) |
| 51 | |
| 52 | aarch64-as := $(if $(filter-out default,$(origin AS)),$(AS)) |
| 53 | |
| 54 | aarch64-ld := $(if $(filter-out default,$(origin LD)),$(LD)) |
| 55 | aarch64-oc := $(if $(filter-out default,$(origin OC)),$(OC)) |
| 56 | aarch64-od := $(if $(filter-out default,$(origin OD)),$(OD)) |
| 57 | aarch64-ar := $(if $(filter-out default,$(origin AR)),$(AR)) |
| 58 | |
| 59 | aarch64-dtc := $(if $(filter-out default,$(origin DTC)),$(DTC)) |
| 60 | endif |
| 61 | |
| 62 | include $(dir $(lastword $(MAKEFILE_LIST)))build_env.mk |
Chris Kay | 2c09bf6 | 2024-04-09 16:30:52 +0000 | [diff] [blame] | 63 | include $(dir $(lastword $(MAKEFILE_LIST)))utilities.mk |
| 64 | |
Chris Kay | c8a47ba | 2023-10-20 09:17:33 +0000 | [diff] [blame] | 65 | include $(addprefix $(dir $(lastword $(MAKEFILE_LIST)))toolchains/, \ |
| 66 | $(addsuffix .mk,$(toolchains))) |
| 67 | |
| 68 | # |
| 69 | # Configure tool classes that we recognize. |
| 70 | # |
| 71 | # In the context of this build system, a tool class identifies a specific role |
| 72 | # or type of tool in the toolchain. |
| 73 | # |
| 74 | |
| 75 | # C-related tools |
| 76 | tool-classes := cc # C compilers |
| 77 | tool-classes += cpp # C preprocessors |
| 78 | |
| 79 | # Assembly-related tools |
| 80 | tool-classes += as # Assemblers |
| 81 | |
| 82 | # Linking and object-handling tools |
| 83 | tool-classes += ld # Linkers |
| 84 | tool-classes += oc # Object copiers |
| 85 | tool-classes += od # Object dumpers |
| 86 | tool-classes += ar # Archivers |
| 87 | |
| 88 | # Other tools |
| 89 | tool-classes += dtc # Device tree compilers |
| 90 | |
| 91 | # |
| 92 | # Configure tools that we recognize. |
| 93 | # |
| 94 | # Here we declare the list of specific toolchain tools that we know how to |
| 95 | # interact with. We don't organize these into tool classes yet - that happens |
| 96 | # further down. |
| 97 | # |
| 98 | |
| 99 | # Arm Compiler for Embedded |
| 100 | tools := arm-clang # armclang |
| 101 | tools += arm-link # armlink |
| 102 | tools += arm-ar # armar |
| 103 | tools += arm-fromelf # fromelf |
| 104 | |
| 105 | # LLVM Project |
| 106 | tools += llvm-clang # clang |
| 107 | tools += llvm-lld # lld |
| 108 | tools += llvm-objcopy # llvm-objcopy |
| 109 | tools += llvm-objdump # llvm-objdump |
| 110 | tools += llvm-ar # llvm-ar |
| 111 | |
| 112 | # GNU Compiler Collection & GNU Binary Utilities |
| 113 | tools += gnu-gcc # gcc |
| 114 | tools += gnu-ld # ld |
| 115 | tools += gnu-objcopy # objcopy |
| 116 | tools += gnu-objdump # objdump |
| 117 | tools += gnu-ar # gcc-ar |
| 118 | |
| 119 | # Other tools |
| 120 | tools += dtc # Device Tree Compiler |
| 121 | |
| 122 | # |
| 123 | # Assign tools to tool classes. |
| 124 | # |
| 125 | # Multifunctional tools, i.e. tools which can perform multiple roles in a |
| 126 | # toolchain, may be specified in multiple tool class lists. For example, a C |
| 127 | # compiler which can also perform the role of a linker may be placed in both |
| 128 | # `tools-cc` and `tools-ld`. |
| 129 | # |
| 130 | |
| 131 | # C-related tools |
| 132 | tools-cc := arm-clang llvm-clang gnu-gcc # C compilers |
| 133 | tools-cpp := arm-clang llvm-clang gnu-gcc # C preprocessors |
| 134 | |
| 135 | # Assembly-related tools |
| 136 | tools-as := arm-clang llvm-clang gnu-gcc # Assemblers |
| 137 | |
| 138 | # Linking and object-handling tools |
| 139 | tools-ld := arm-clang arm-link llvm-clang llvm-lld gnu-gcc gnu-ld # Linkers |
| 140 | tools-oc := arm-fromelf llvm-objcopy gnu-objcopy # Object copiers |
| 141 | tools-od := arm-fromelf llvm-objdump gnu-objdump # Object dumpers |
| 142 | tools-ar := arm-ar llvm-ar gnu-ar # Archivers |
| 143 | |
| 144 | # Other tools |
| 145 | tools-dtc := dtc # Device tree compilers |
| 146 | |
| 147 | define check-tool-class-tools |
| 148 | $(eval tool-class := $(1)) |
| 149 | |
| 150 | ifndef tools-$(tool-class) |
| 151 | $$(error no tools registered to handle tool class `$(tool-class)`) |
| 152 | endif |
| 153 | endef |
| 154 | |
| 155 | $(foreach tool-class,$(tool-classes), \ |
| 156 | $(eval $(call check-tool-class-tools,$(tool-class)))) |
| 157 | |
| 158 | # |
| 159 | # Default tools for each toolchain. |
| 160 | # |
| 161 | # Toolchains can specify a default path to any given tool with a tool class. |
| 162 | # These values are used in the absence of user-specified values, and are |
Chris Kay | 0a2f8a0 | 2024-02-20 13:13:23 +0000 | [diff] [blame] | 163 | # configured by the makefile for each toolchain using variables of the form: |
Chris Kay | c8a47ba | 2023-10-20 09:17:33 +0000 | [diff] [blame] | 164 | # |
| 165 | # - $(toolchain)-$(tool-class)-default |
| 166 | # |
| 167 | # For example, the default C compiler for the AArch32 and AArch64 toolchains |
| 168 | # could be configured with: |
| 169 | # |
| 170 | # - aarch32-cc-default |
| 171 | # - aarch64-cc-default |
| 172 | # |
| 173 | |
| 174 | define check-toolchain-tool-class-default |
| 175 | $(eval toolchain := $(1)) |
| 176 | $(eval tool-class := $(2)) |
| 177 | |
| 178 | ifndef $(toolchain)-$(tool-class)-default |
| 179 | $$(error no default value specified for tool class `$(tool-class)` of toolchain `$(toolchain)`) |
| 180 | endif |
| 181 | endef |
| 182 | |
| 183 | define check-toolchain-tool-class-defaults |
| 184 | $(eval toolchain := $(1)) |
| 185 | |
| 186 | $(foreach tool-class,$(tool-classes), \ |
| 187 | $(eval $(call check-toolchain-tool-class-default,$(toolchain),$(tool-class)))) |
| 188 | endef |
| 189 | |
| 190 | $(foreach toolchain,$(toolchains), \ |
| 191 | $(eval $(call check-toolchain-tool-class-defaults,$(toolchain)))) |
| 192 | |
| 193 | # |
| 194 | # Helper functions to identify toolchain tools. |
| 195 | # |
| 196 | # The functions defined in this section return a tool identifier when given a |
| 197 | # path to a binary. We generally check a help or version string to more reliably |
| 198 | # identify tools than by looking at the path alone (e.g. `gcc` on macOS is |
| 199 | # actually Apple Clang). |
| 200 | # |
| 201 | # Each tool-guessing function (`guess-tool-$(tool)`) takes a single argument |
| 202 | # giving the path to the tool to guess, and returns a non-empty value if the |
| 203 | # tool corresponds to the tool identifier `$(tool)`: |
| 204 | # |
| 205 | # $(call guess-tool-llvm-clang,aarch64-none-elf-gcc) # <empty> |
| 206 | # $(call guess-tool-gnu-gcc,aarch64-none-elf-gcc) # <non-empty> |
| 207 | # |
| 208 | # The `guess-tool` function tries to find the corresponding tool identifier |
| 209 | # for a tool given its path. It takes two arguments: |
| 210 | # |
| 211 | # - $(1): a list of candidate tool identifiers to check |
| 212 | # - $(2): the path to the tool to identify |
| 213 | # |
| 214 | # If any of the guess functions corresponding to candidate tool identifiers |
| 215 | # return a non-empty value then the tool identifier of the first function to do |
| 216 | # so is returned: |
| 217 | # |
| 218 | # $(call guess-tool,gnu-gcc llvm-clang,armclang) # <empty> |
| 219 | # $(call guess-tool,gnu-gcc llvm-clang,clang-14) # llvm-clang |
| 220 | # $(call guess-tool,gnu-gcc llvm-clang,aarch64-none-elf-gcc-12) # gnu-gcc |
| 221 | # |
| 222 | # Tools are checked in the order that they appear in `tools-$(tool-class)`, and |
| 223 | # the first match is returned. |
| 224 | # |
| 225 | |
| 226 | # Arm Compiler for Embedded |
Chris Kay | 2c09bf6 | 2024-04-09 16:30:52 +0000 | [diff] [blame] | 227 | guess-tool-arm-clang = $(shell $(call escape-shell,$(1)) --version 2>&1 <$(nul) | grep -o "Tool: armclang") |
| 228 | guess-tool-arm-link = $(shell $(call escape-shell,$(1)) --help 2>&1 <$(nul) | grep -o "Tool: armlink") |
| 229 | guess-tool-arm-fromelf = $(shell $(call escape-shell,$(1)) --help 2>&1 <$(nul) | grep -o "Tool: fromelf") |
| 230 | guess-tool-arm-ar = $(shell $(call escape-shell,$(1)) --version 2>&1 <$(nul) | grep -o "Tool: armar") |
Chris Kay | c8a47ba | 2023-10-20 09:17:33 +0000 | [diff] [blame] | 231 | |
| 232 | # LLVM Project |
Chris Kay | 2c09bf6 | 2024-04-09 16:30:52 +0000 | [diff] [blame] | 233 | guess-tool-llvm-clang = $(shell $(call escape-shell,$(1)) -v 2>&1 <$(nul) | grep -o "clang version") |
| 234 | guess-tool-llvm-lld = $(shell $(call escape-shell,$(1)) --help 2>&1 <$(nul) | grep -o "OVERVIEW: lld") |
| 235 | guess-tool-llvm-objcopy = $(shell $(call escape-shell,$(1)) --help 2>&1 <$(nul) | grep -o "llvm-objcopy tool") |
| 236 | guess-tool-llvm-objdump = $(shell $(call escape-shell,$(1)) --help 2>&1 <$(nul) | grep -o "llvm object file dumper") |
| 237 | guess-tool-llvm-ar = $(shell $(call escape-shell,$(1)) --help 2>&1 <$(nul) | grep -o "LLVM Archiver") |
Chris Kay | c8a47ba | 2023-10-20 09:17:33 +0000 | [diff] [blame] | 238 | |
| 239 | # GNU Compiler Collection & GNU Binary Utilities |
Chris Kay | 2c09bf6 | 2024-04-09 16:30:52 +0000 | [diff] [blame] | 240 | guess-tool-gnu-gcc = $(shell $(call escape-shell,$(1)) -v 2>&1 <$(nul) | grep -o "gcc version") |
| 241 | guess-tool-gnu-ld = $(shell $(call escape-shell,$(1)) -v 2>&1 <$(nul) | grep -o "GNU ld") |
| 242 | guess-tool-gnu-objcopy = $(shell $(call escape-shell,$(1)) --version 2>&1 <$(nul) | grep -o "GNU objcopy") |
| 243 | guess-tool-gnu-objdump = $(shell $(call escape-shell,$(1)) --version 2>&1 <$(nul) | grep -o "GNU objdump") |
| 244 | guess-tool-gnu-ar = $(shell $(call escape-shell,$(1)) --version 2>&1 <$(nul) | grep -o "GNU ar") |
Chris Kay | c8a47ba | 2023-10-20 09:17:33 +0000 | [diff] [blame] | 245 | |
| 246 | # Other tools |
Chris Kay | 2c09bf6 | 2024-04-09 16:30:52 +0000 | [diff] [blame] | 247 | guess-tool-dtc = $(shell $(call escape-shell,$(1)) --version 2>&1 <$(nul) | grep -o "Version: DTC") |
Chris Kay | c8a47ba | 2023-10-20 09:17:33 +0000 | [diff] [blame] | 248 | |
| 249 | guess-tool = $(firstword $(foreach candidate,$(1), \ |
| 250 | $(if $(call guess-tool-$(candidate),$(2)),$(candidate)))) |
| 251 | |
| 252 | # |
| 253 | # Locate and identify tools belonging to each toolchain. |
| 254 | # |
| 255 | # Each tool class in each toolchain receives a variable of the form |
| 256 | # `$(toolchain)-$(tool)` giving the associated path to the program. For example: |
| 257 | # |
| 258 | # - `aarch64-ld` gives the linker for the AArch64 toolchain, |
| 259 | # - `aarch32-oc` gives the object copier for the AArch32 toolchain, and |
| 260 | # - `host-cc` gives the C compiler for the host toolchain. |
| 261 | # |
| 262 | # For each of these variables, if no program path is explicitly provided by the |
| 263 | # parent Makefile then the C compiler is queried (if supported) for its |
| 264 | # location. This is done via the `guess-$(tool)-$(tool-class)` set of functions. |
| 265 | # For example: |
| 266 | # |
| 267 | # - `guess-arm-clang-ld` guesses the linker via Arm Clang, |
| 268 | # - `guess-llvm-clang-as` guesses the assembler via LLVM Clang, and |
| 269 | # - `guess-gnu-gcc-od` guesses the object dumper via GNU GCC. |
| 270 | # |
| 271 | # If the C compiler cannot provide the location (or the tool class is the C |
| 272 | # compiler), then it is assigned the value of the `$(toolchain)-$(tool)-default` |
| 273 | # variable. |
| 274 | # |
| 275 | |
Chris Kay | 2c09bf6 | 2024-04-09 16:30:52 +0000 | [diff] [blame] | 276 | guess-arm-clang-cpp = $(1) |
| 277 | guess-arm-clang-as = $(1) |
Chris Kay | c8a47ba | 2023-10-20 09:17:33 +0000 | [diff] [blame] | 278 | guess-arm-clang-ld = # Fall back to `$(toolchain)-ld-default` |
| 279 | guess-arm-clang-oc = # Fall back to `$(toolchain)-oc-default` |
| 280 | guess-arm-clang-od = # Fall back to `$(toolchain)-od-default` |
| 281 | guess-arm-clang-ar = # Fall back to `$(toolchain)-ar-default` |
| 282 | |
Chris Kay | 2c09bf6 | 2024-04-09 16:30:52 +0000 | [diff] [blame] | 283 | guess-llvm-clang-cpp = $(1) |
| 284 | guess-llvm-clang-as = $(1) |
| 285 | guess-llvm-clang-ld = $(shell $(call escape-shell,$(1)) --print-prog-name ld.lld 2>$(nul)) |
| 286 | guess-llvm-clang-oc = $(shell $(call escape-shell,$(1)) --print-prog-name llvm-objcopy 2>$(nul)) |
| 287 | guess-llvm-clang-od = $(shell $(call escape-shell,$(1)) --print-prog-name llvm-objdump 2>$(nul)) |
| 288 | guess-llvm-clang-ar = $(shell $(call escape-shell,$(1)) --print-prog-name llvm-ar 2>$(nul)) |
Chris Kay | c8a47ba | 2023-10-20 09:17:33 +0000 | [diff] [blame] | 289 | |
Chris Kay | 2c09bf6 | 2024-04-09 16:30:52 +0000 | [diff] [blame] | 290 | guess-gnu-gcc-cpp = $(1) |
| 291 | guess-gnu-gcc-as = $(1) |
| 292 | guess-gnu-gcc-ld = $(1) |
| 293 | guess-gnu-gcc-oc = $(shell $(call escape-shell,$(1)) --print-prog-name objcopy 2>$(nul)) |
| 294 | guess-gnu-gcc-od = $(shell $(call escape-shell,$(1)) --print-prog-name objdump 2>$(nul)) |
| 295 | guess-gnu-gcc-ar = $(call which,$(call decompat-path,$(patsubst %$(call file-name,$(1)),%$(subst gcc,gcc-ar,$(call file-name,$(1))),$(call compat-path,$(1))))) |
Chris Kay | c8a47ba | 2023-10-20 09:17:33 +0000 | [diff] [blame] | 296 | |
| 297 | define locate-toolchain-tool-cc |
| 298 | $(eval toolchain := $(1)) |
| 299 | |
Chris Kay | 2c09bf6 | 2024-04-09 16:30:52 +0000 | [diff] [blame] | 300 | $(toolchain)-cc := $$(or $$($(toolchain)-cc),$$($(toolchain)-cc-default)) |
| 301 | $(toolchain)-cc-id := $$(call guess-tool,$$(tools-cc),$$($(toolchain)-cc)) |
Chris Kay | c8a47ba | 2023-10-20 09:17:33 +0000 | [diff] [blame] | 302 | endef |
| 303 | |
| 304 | define locate-toolchain-tool |
| 305 | $(eval toolchain := $(1)) |
| 306 | $(eval tool-class := $(2)) |
| 307 | |
| 308 | ifndef $(toolchain)-$(tool-class) |
Chris Kay | 2c09bf6 | 2024-04-09 16:30:52 +0000 | [diff] [blame] | 309 | $(toolchain)-$(tool-class) := $$(call guess-$$($(toolchain)-cc-id)-$(tool-class),$$($(toolchain)-cc-path)) |
Chris Kay | c8a47ba | 2023-10-20 09:17:33 +0000 | [diff] [blame] | 310 | |
| 311 | ifeq ($$($(toolchain)-$(tool-class)),) |
Chris Kay | 2c09bf6 | 2024-04-09 16:30:52 +0000 | [diff] [blame] | 312 | $(toolchain)-$(tool-class) := $$($(toolchain)-$(tool-class)-default) |
Chris Kay | c8a47ba | 2023-10-20 09:17:33 +0000 | [diff] [blame] | 313 | endif |
| 314 | endif |
| 315 | |
Chris Kay | 2c09bf6 | 2024-04-09 16:30:52 +0000 | [diff] [blame] | 316 | $(toolchain)-$(tool-class)-id := $$(call guess-tool,$$(tools-$(tool-class)),$$($$(toolchain)-$(tool-class))) |
Chris Kay | c8a47ba | 2023-10-20 09:17:33 +0000 | [diff] [blame] | 317 | endef |
| 318 | |
| 319 | define canonicalize-toolchain-tool-path |
| 320 | $(eval toolchain := $(1)) |
| 321 | $(eval tool-class := $(2)) |
| 322 | |
Chris Kay | 2c09bf6 | 2024-04-09 16:30:52 +0000 | [diff] [blame] | 323 | $(toolchain)-$(tool-class)-path := $$(call absolute-path,$$(call which,$$($(toolchain)-$(tool-class)))) |
| 324 | $(toolchain)-$(tool-class)-path := $$(or $$($(toolchain)-$(tool-class)-path),$$($(toolchain)-$(tool-class))) |
| 325 | |
| 326 | $(toolchain)-$(tool-class) := $(call escape-shell,$$($(toolchain)-$(tool-class)-path)) |
Chris Kay | c8a47ba | 2023-10-20 09:17:33 +0000 | [diff] [blame] | 327 | endef |
| 328 | |
| 329 | define locate-toolchain |
| 330 | $(eval toolchain := $(1)) |
| 331 | |
| 332 | $$(eval $$(call locate-toolchain-tool-cc,$(toolchain))) |
| 333 | $$(eval $$(call canonicalize-toolchain-tool-path,$(toolchain),cc)) |
| 334 | |
| 335 | $$(foreach tool-class,$$(filter-out cc,$$(tool-classes)), \ |
| 336 | $$(eval $$(call locate-toolchain-tool,$(toolchain),$$(tool-class))) \ |
| 337 | $$(eval $$(call canonicalize-toolchain-tool-path,$(toolchain),$$(tool-class)))) |
| 338 | endef |
| 339 | |
| 340 | $(foreach toolchain,$(toolchains), \ |
| 341 | $(eval $(call locate-toolchain,$(toolchain)))) |