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