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