blob: 2f4764705cf80dd3e25c04e0c84f262d30139726 [file] [log] [blame]
Chris Kayc8a47ba2023-10-20 09:17:33 +00001#
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
18toolchains ?= host $(ARCH)
19
20ifneq ($(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)
32endif
33
34ifneq ($(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))
46endif
47
48ifneq ($(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))
60endif
61
62include $(dir $(lastword $(MAKEFILE_LIST)))build_env.mk
Chris Kay2c09bf62024-04-09 16:30:52 +000063include $(dir $(lastword $(MAKEFILE_LIST)))utilities.mk
64
Chris Kayc8a47ba2023-10-20 09:17:33 +000065include $(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
76tool-classes := cc # C compilers
77tool-classes += cpp # C preprocessors
78
79# Assembly-related tools
80tool-classes += as # Assemblers
81
82# Linking and object-handling tools
83tool-classes += ld # Linkers
84tool-classes += oc # Object copiers
85tool-classes += od # Object dumpers
86tool-classes += ar # Archivers
87
88# Other tools
89tool-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
100tools := arm-clang # armclang
101tools += arm-link # armlink
102tools += arm-ar # armar
103tools += arm-fromelf # fromelf
104
105# LLVM Project
106tools += llvm-clang # clang
107tools += llvm-lld # lld
108tools += llvm-objcopy # llvm-objcopy
109tools += llvm-objdump # llvm-objdump
110tools += llvm-ar # llvm-ar
111
112# GNU Compiler Collection & GNU Binary Utilities
113tools += gnu-gcc # gcc
114tools += gnu-ld # ld
115tools += gnu-objcopy # objcopy
116tools += gnu-objdump # objdump
117tools += gnu-ar # gcc-ar
118
119# Other tools
120tools += 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
132tools-cc := arm-clang llvm-clang gnu-gcc # C compilers
133tools-cpp := arm-clang llvm-clang gnu-gcc # C preprocessors
134
135# Assembly-related tools
136tools-as := arm-clang llvm-clang gnu-gcc # Assemblers
137
138# Linking and object-handling tools
139tools-ld := arm-clang arm-link llvm-clang llvm-lld gnu-gcc gnu-ld # Linkers
140tools-oc := arm-fromelf llvm-objcopy gnu-objcopy # Object copiers
141tools-od := arm-fromelf llvm-objdump gnu-objdump # Object dumpers
142tools-ar := arm-ar llvm-ar gnu-ar # Archivers
143
144# Other tools
145tools-dtc := dtc # Device tree compilers
146
147define 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
153endef
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 Kay0a2f8a02024-02-20 13:13:23 +0000163# configured by the makefile for each toolchain using variables of the form:
Chris Kayc8a47ba2023-10-20 09:17:33 +0000164#
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
174define 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
181endef
182
183define 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))))
188endef
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 Kay2c09bf62024-04-09 16:30:52 +0000227guess-tool-arm-clang = $(shell $(call escape-shell,$(1)) --version 2>&1 <$(nul) | grep -o "Tool: armclang")
228guess-tool-arm-link = $(shell $(call escape-shell,$(1)) --help 2>&1 <$(nul) | grep -o "Tool: armlink")
229guess-tool-arm-fromelf = $(shell $(call escape-shell,$(1)) --help 2>&1 <$(nul) | grep -o "Tool: fromelf")
230guess-tool-arm-ar = $(shell $(call escape-shell,$(1)) --version 2>&1 <$(nul) | grep -o "Tool: armar")
Chris Kayc8a47ba2023-10-20 09:17:33 +0000231
232# LLVM Project
Chris Kay2c09bf62024-04-09 16:30:52 +0000233guess-tool-llvm-clang = $(shell $(call escape-shell,$(1)) -v 2>&1 <$(nul) | grep -o "clang version")
234guess-tool-llvm-lld = $(shell $(call escape-shell,$(1)) --help 2>&1 <$(nul) | grep -o "OVERVIEW: lld")
235guess-tool-llvm-objcopy = $(shell $(call escape-shell,$(1)) --help 2>&1 <$(nul) | grep -o "llvm-objcopy tool")
236guess-tool-llvm-objdump = $(shell $(call escape-shell,$(1)) --help 2>&1 <$(nul) | grep -o "llvm object file dumper")
237guess-tool-llvm-ar = $(shell $(call escape-shell,$(1)) --help 2>&1 <$(nul) | grep -o "LLVM Archiver")
Chris Kayc8a47ba2023-10-20 09:17:33 +0000238
239# GNU Compiler Collection & GNU Binary Utilities
Chris Kay2c09bf62024-04-09 16:30:52 +0000240guess-tool-gnu-gcc = $(shell $(call escape-shell,$(1)) -v 2>&1 <$(nul) | grep -o "gcc version")
241guess-tool-gnu-ld = $(shell $(call escape-shell,$(1)) -v 2>&1 <$(nul) | grep -o "GNU ld")
242guess-tool-gnu-objcopy = $(shell $(call escape-shell,$(1)) --version 2>&1 <$(nul) | grep -o "GNU objcopy")
243guess-tool-gnu-objdump = $(shell $(call escape-shell,$(1)) --version 2>&1 <$(nul) | grep -o "GNU objdump")
244guess-tool-gnu-ar = $(shell $(call escape-shell,$(1)) --version 2>&1 <$(nul) | grep -o "GNU ar")
Chris Kayc8a47ba2023-10-20 09:17:33 +0000245
246# Other tools
Chris Kay2c09bf62024-04-09 16:30:52 +0000247guess-tool-dtc = $(shell $(call escape-shell,$(1)) --version 2>&1 <$(nul) | grep -o "Version: DTC")
Chris Kayc8a47ba2023-10-20 09:17:33 +0000248
249guess-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 Kay2c09bf62024-04-09 16:30:52 +0000276guess-arm-clang-cpp = $(1)
277guess-arm-clang-as = $(1)
Chris Kayc8a47ba2023-10-20 09:17:33 +0000278guess-arm-clang-ld = # Fall back to `$(toolchain)-ld-default`
279guess-arm-clang-oc = # Fall back to `$(toolchain)-oc-default`
280guess-arm-clang-od = # Fall back to `$(toolchain)-od-default`
281guess-arm-clang-ar = # Fall back to `$(toolchain)-ar-default`
282
Chris Kay2c09bf62024-04-09 16:30:52 +0000283guess-llvm-clang-cpp = $(1)
284guess-llvm-clang-as = $(1)
285guess-llvm-clang-ld = $(shell $(call escape-shell,$(1)) --print-prog-name ld.lld 2>$(nul))
286guess-llvm-clang-oc = $(shell $(call escape-shell,$(1)) --print-prog-name llvm-objcopy 2>$(nul))
287guess-llvm-clang-od = $(shell $(call escape-shell,$(1)) --print-prog-name llvm-objdump 2>$(nul))
288guess-llvm-clang-ar = $(shell $(call escape-shell,$(1)) --print-prog-name llvm-ar 2>$(nul))
Chris Kayc8a47ba2023-10-20 09:17:33 +0000289
Chris Kay2c09bf62024-04-09 16:30:52 +0000290guess-gnu-gcc-cpp = $(1)
291guess-gnu-gcc-as = $(1)
292guess-gnu-gcc-ld = $(1)
293guess-gnu-gcc-oc = $(shell $(call escape-shell,$(1)) --print-prog-name objcopy 2>$(nul))
294guess-gnu-gcc-od = $(shell $(call escape-shell,$(1)) --print-prog-name objdump 2>$(nul))
295guess-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 Kayc8a47ba2023-10-20 09:17:33 +0000296
297define locate-toolchain-tool-cc
298 $(eval toolchain := $(1))
299
Chris Kay2c09bf62024-04-09 16:30:52 +0000300 $(toolchain)-cc := $$(or $$($(toolchain)-cc),$$($(toolchain)-cc-default))
301 $(toolchain)-cc-id := $$(call guess-tool,$$(tools-cc),$$($(toolchain)-cc))
Chris Kayc8a47ba2023-10-20 09:17:33 +0000302endef
303
304define locate-toolchain-tool
305 $(eval toolchain := $(1))
306 $(eval tool-class := $(2))
307
308 ifndef $(toolchain)-$(tool-class)
Chris Kay2c09bf62024-04-09 16:30:52 +0000309 $(toolchain)-$(tool-class) := $$(call guess-$$($(toolchain)-cc-id)-$(tool-class),$$($(toolchain)-cc-path))
Chris Kayc8a47ba2023-10-20 09:17:33 +0000310
311 ifeq ($$($(toolchain)-$(tool-class)),)
Chris Kay2c09bf62024-04-09 16:30:52 +0000312 $(toolchain)-$(tool-class) := $$($(toolchain)-$(tool-class)-default)
Chris Kayc8a47ba2023-10-20 09:17:33 +0000313 endif
314 endif
315
Chris Kay2c09bf62024-04-09 16:30:52 +0000316 $(toolchain)-$(tool-class)-id := $$(call guess-tool,$$(tools-$(tool-class)),$$($$(toolchain)-$(tool-class)))
Chris Kayc8a47ba2023-10-20 09:17:33 +0000317endef
318
319define canonicalize-toolchain-tool-path
320 $(eval toolchain := $(1))
321 $(eval tool-class := $(2))
322
Chris Kay2c09bf62024-04-09 16:30:52 +0000323 $(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 Kayc8a47ba2023-10-20 09:17:33 +0000327endef
328
329define 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))))
338endef
339
340$(foreach toolchain,$(toolchains), \
341 $(eval $(call locate-toolchain,$(toolchain))))