blob: 243e18ffb1b5bdae2114781049d36d242ba798ff [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
Chris Kay0adde4e2024-05-14 13:08:31 +000011# - The AArch32 toolchain (`aarch32`) for building Arm AArch32 images
Chris Kayc8a47ba2023-10-20 09:17:33 +000012# - 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
Chris Kay0adde4e2024-05-14 13:08:31 +000018ifndef toolchain-mk
19 toolchain-mk := $(lastword $(MAKEFILE_LIST))
Chris Kayc8a47ba2023-10-20 09:17:33 +000020
Chris Kay87e98c22024-06-03 11:10:11 +000021 include $(dir $(toolchain-mk))build_env.mk
22 include $(dir $(toolchain-mk))utilities.mk
Chris Kay2c09bf62024-04-09 16:30:52 +000023
Chris Kay87e98c22024-06-03 11:10:11 +000024 #
25 # Make assigns generic default values to `CC`, `CPP`, `AS`, etc. if they
26 # are not explicitly assigned values by the user. These are usually okay
27 # for very simple programs when building for the host system, but we
28 # need greater control over the toolchain flow.
29 #
30 # Therefore, we undefine these built-in variables if they have default
31 # values, so that we can provide our own default values later instead.
32 #
33
34 ifeq ($(origin CC),default)
35 undefine CC
36 endif
37
38 ifeq ($(origin CPP),default)
39 undefine CPP
40 endif
Chris Kayc8a47ba2023-10-20 09:17:33 +000041
Chris Kay87e98c22024-06-03 11:10:11 +000042 ifeq ($(origin AS),default)
43 undefine AS
44 endif
Chris Kayc8a47ba2023-10-20 09:17:33 +000045
Chris Kay87e98c22024-06-03 11:10:11 +000046 ifeq ($(origin AR),default)
47 undefine AR
48 endif
49
50 ifeq ($(origin LD),default)
51 undefine LD
52 endif
53
54 #
55 # The full list of toolchains supported by TF-A.
56 #
57 # Each of these toolchains defines a file of the same name in the
58 # `toolchains` directory, which must configure the following variables:
59 #
60 # - <toolchain>-name
61 #
62 # A human-readable name for the toolchain,
63 #
64 # Additionally, for every tool class, it must also define:
65 #
66 # - <toolchain>-<tool-class>-parameter
67 #
68 # The command line or environment variable used to set the tool for
69 # for the given tool class.
70 #
Chris Kay98255ef2024-08-29 15:08:42 +000071 # - <toolchain>-<tool-class>-default-id
Chris Kay87e98c22024-06-03 11:10:11 +000072 #
73 # The default tool identifier used if the tool for the given tool
74 # class cannot be identified.
75 #
Chris Kay98255ef2024-08-29 15:08:42 +000076 # - <toolchain>-<tool-class>-default
77 #
78 # The default commands to try, in the order defined, for the given
79 # tool class if the user does not explicitly provide one, and if the
80 # command could not be derived from the C compiler.
81 #
Chris Kay87e98c22024-06-03 11:10:11 +000082
83 toolchains := host # Used for host targets
84 toolchains += aarch32 # Used for AArch32 targets
85 toolchains += aarch64 # Used for AArch64 targets
86 toolchains += rk3399-m0 # Used for RK3399 Cortex-M0 targets
87
88 include $(toolchains:%=$(dir $(toolchain-mk))toolchains/%.mk)
89
Chris Kay0adde4e2024-05-14 13:08:31 +000090 #
91 # Configure tool classes that we recognize.
92 #
93 # In the context of this build system, a tool class identifies a
94 # specific role or type of tool in the toolchain.
95 #
Chris Kayc8a47ba2023-10-20 09:17:33 +000096
Chris Kayf665ee42024-06-03 02:01:34 +000097 toolchain-tool-classes := cc
98 toolchain-tool-class-name-cc := C compiler
Chris Kayc8a47ba2023-10-20 09:17:33 +000099
Chris Kayf665ee42024-06-03 02:01:34 +0000100 toolchain-tool-classes += cpp
101 toolchain-tool-class-name-cpp := C preprocessor
Chris Kayc8a47ba2023-10-20 09:17:33 +0000102
Chris Kayf665ee42024-06-03 02:01:34 +0000103 toolchain-tool-classes += as
104 toolchain-tool-class-name-as := assembler
Chris Kay24f3fb42024-04-16 17:19:20 +0000105
Chris Kayf665ee42024-06-03 02:01:34 +0000106 toolchain-tool-classes += ld
107 toolchain-tool-class-name-ld := linker
Chris Kay24f3fb42024-04-16 17:19:20 +0000108
Chris Kayf665ee42024-06-03 02:01:34 +0000109 toolchain-tool-classes += oc
110 toolchain-tool-class-name-oc := object copier
Chris Kay24f3fb42024-04-16 17:19:20 +0000111
Chris Kayf665ee42024-06-03 02:01:34 +0000112 toolchain-tool-classes += od
113 toolchain-tool-class-name-od := object dumper
Chris Kay24f3fb42024-04-16 17:19:20 +0000114
Chris Kayf665ee42024-06-03 02:01:34 +0000115 toolchain-tool-classes += ar
116 toolchain-tool-class-name-ar := archiver
Chris Kayc8a47ba2023-10-20 09:17:33 +0000117
Chris Kayf665ee42024-06-03 02:01:34 +0000118 toolchain-tool-classes += dtc
119 toolchain-tool-class-name-dtc := device tree compiler
Chris Kayc8a47ba2023-10-20 09:17:33 +0000120
Chris Kay0adde4e2024-05-14 13:08:31 +0000121 #
122 # Configure tools that we recognize.
123 #
124 # Here we declare the list of specific toolchain tools that we know how
125 # to interact with. We don't organize these into tool classes yet - that
126 # happens further down.
127 #
Chris Kay24f3fb42024-04-16 17:19:20 +0000128
Chris Kay0adde4e2024-05-14 13:08:31 +0000129 # Arm® Compiler for Embedded
Chris Kayf665ee42024-06-03 02:01:34 +0000130 toolchain-tools := arm-clang
131 toolchain-tool-name-arm-clang := Arm® Compiler for Embedded `armclang`
Chris Kay24f3fb42024-04-16 17:19:20 +0000132
Chris Kayf665ee42024-06-03 02:01:34 +0000133 toolchain-tools += arm-link
134 toolchain-tool-name-arm-link := Arm® Compiler for Embedded `armlink`
Chris Kay24f3fb42024-04-16 17:19:20 +0000135
Chris Kayf665ee42024-06-03 02:01:34 +0000136 toolchain-tools += arm-ar
137 toolchain-tool-name-arm-ar := Arm® Compiler for Embedded `armar`
Chris Kayc8a47ba2023-10-20 09:17:33 +0000138
Chris Kayf665ee42024-06-03 02:01:34 +0000139 toolchain-tools += arm-fromelf
140 toolchain-tool-name-arm-fromelf := Arm® Compiler for Embedded `fromelf`
Chris Kay24f3fb42024-04-16 17:19:20 +0000141
Chris Kay0adde4e2024-05-14 13:08:31 +0000142 # LLVM Project
Chris Kayf665ee42024-06-03 02:01:34 +0000143 toolchain-tools += llvm-clang
144 toolchain-tool-name-llvm-clang := LLVM Clang (`clang`)
Chris Kay24f3fb42024-04-16 17:19:20 +0000145
Chris Kayf665ee42024-06-03 02:01:34 +0000146 toolchain-tools += llvm-lld
147 toolchain-tool-name-llvm-lld := LLVM LLD (`lld`)
Chris Kay24f3fb42024-04-16 17:19:20 +0000148
Chris Kayf665ee42024-06-03 02:01:34 +0000149 toolchain-tools += llvm-objcopy
150 toolchain-tool-name-llvm-objcopy := LLVM `llvm-objcopy`
Chris Kay24f3fb42024-04-16 17:19:20 +0000151
Chris Kayf665ee42024-06-03 02:01:34 +0000152 toolchain-tools += llvm-objdump
153 toolchain-tool-name-llvm-objdump := LLVM `llvm-objdump`
Chris Kayc8a47ba2023-10-20 09:17:33 +0000154
Chris Kayf665ee42024-06-03 02:01:34 +0000155 toolchain-tools += llvm-ar
156 toolchain-tool-name-llvm-ar := LLVM `llvm-ar`
Chris Kay24f3fb42024-04-16 17:19:20 +0000157
Chris Kay0adde4e2024-05-14 13:08:31 +0000158 # GNU Compiler Collection & GNU Binary Utilities
Chris Kayf665ee42024-06-03 02:01:34 +0000159 toolchain-tools += gnu-gcc
160 toolchain-tool-name-gnu-gcc := GNU GCC (`gcc`)
Chris Kay24f3fb42024-04-16 17:19:20 +0000161
Chris Kayf665ee42024-06-03 02:01:34 +0000162 toolchain-tools += gnu-ld
163 toolchain-tool-name-gnu-ld := GNU LD (`ld.bfd`)
Chris Kay24f3fb42024-04-16 17:19:20 +0000164
Chris Kayf665ee42024-06-03 02:01:34 +0000165 toolchain-tools += gnu-objcopy
166 toolchain-tool-name-gnu-objcopy := GNU `objcopy`
Chris Kay24f3fb42024-04-16 17:19:20 +0000167
Chris Kayf665ee42024-06-03 02:01:34 +0000168 toolchain-tools += gnu-objdump
169 toolchain-tool-name-gnu-objdump := GNU `objdump`
Chris Kayc8a47ba2023-10-20 09:17:33 +0000170
Chris Kayf665ee42024-06-03 02:01:34 +0000171 toolchain-tools += gnu-ar
172 toolchain-tool-name-gnu-ar := GNU `ar`
Chris Kayc8a47ba2023-10-20 09:17:33 +0000173
Chris Kay0adde4e2024-05-14 13:08:31 +0000174 # Other tools
Chris Kayf665ee42024-06-03 02:01:34 +0000175 toolchain-tools += generic-dtc
176 toolchain-tool-name-generic-dtc := Device Tree Compiler (`dtc`)
Chris Kayc8a47ba2023-10-20 09:17:33 +0000177
Chris Kay0adde4e2024-05-14 13:08:31 +0000178 #
179 # Assign tools to tool classes.
180 #
181 # Multifunctional tools, i.e. tools which can perform multiple roles in
182 # a toolchain, may be specified in multiple tool class lists. For
183 # example, a C compiler which can also perform the role of a linker may
Chris Kayf665ee42024-06-03 02:01:34 +0000184 # be placed in both `toolchain-tools-cc` and `toolchain-tools-ld`.
Chris Kay0adde4e2024-05-14 13:08:31 +0000185 #
Chris Kayc8a47ba2023-10-20 09:17:33 +0000186
Chris Kay0adde4e2024-05-14 13:08:31 +0000187 # C-related tools
Chris Kayf665ee42024-06-03 02:01:34 +0000188 toolchain-tools-cc := arm-clang llvm-clang gnu-gcc # C compilers
189 toolchain-tools-cpp := arm-clang llvm-clang gnu-gcc # C preprocessors
Chris Kayc8a47ba2023-10-20 09:17:33 +0000190
Chris Kay0adde4e2024-05-14 13:08:31 +0000191 # Assembly-related tools
Chris Kayf665ee42024-06-03 02:01:34 +0000192 toolchain-tools-as := arm-clang llvm-clang gnu-gcc # Assemblers
Chris Kayc8a47ba2023-10-20 09:17:33 +0000193
Chris Kay0adde4e2024-05-14 13:08:31 +0000194 # Linking and object-handling tools
Chris Kayf665ee42024-06-03 02:01:34 +0000195 toolchain-tools-ld := arm-clang arm-link llvm-clang llvm-lld gnu-gcc gnu-ld # Linkers
196 toolchain-tools-oc := arm-fromelf llvm-objcopy gnu-objcopy # Object copiers
197 toolchain-tools-od := arm-fromelf llvm-objdump gnu-objdump # Object dumpers
198 toolchain-tools-ar := arm-ar llvm-ar gnu-ar # Archivers
Chris Kayc8a47ba2023-10-20 09:17:33 +0000199
Chris Kay0adde4e2024-05-14 13:08:31 +0000200 # Other tools
Chris Kayf665ee42024-06-03 02:01:34 +0000201 toolchain-tools-dtc := generic-dtc # Device tree compilers
Chris Kayc8a47ba2023-10-20 09:17:33 +0000202
Chris Kay0adde4e2024-05-14 13:08:31 +0000203 #
Chris Kay0adde4e2024-05-14 13:08:31 +0000204 # Helper functions to identify toolchain tools.
205 #
206 # The functions defined in this section return a tool identifier when
207 # given a path to a binary. We generally check a help or version string
208 # to more reliably identify tools than by looking at the path alone
209 # (e.g. `gcc` on macOS is actually Apple Clang).
210 #
Chris Kayf665ee42024-06-03 02:01:34 +0000211 # Each tool-guessing function (`toolchain-guess-tool-$(tool)`) takes a
212 # single argument giving the path to the tool to guess, and returns a
213 # non-empty value if the tool corresponds to the tool identifier
214 # `$(tool)`:
Chris Kay0adde4e2024-05-14 13:08:31 +0000215 #
Chris Kayf665ee42024-06-03 02:01:34 +0000216 # $(call toolchain-guess-tool-llvm-clang,aarch64-none-elf-gcc) # <empty>
217 # $(call toolchain-guess-tool-gnu-gcc,aarch64-none-elf-gcc) # <non-empty>
Chris Kay0adde4e2024-05-14 13:08:31 +0000218 #
Chris Kayf665ee42024-06-03 02:01:34 +0000219 # The `toolchain-guess-tool` function tries to find the corresponding tool
Chris Kay0adde4e2024-05-14 13:08:31 +0000220 # identifier for a tool given its path. It takes two arguments:
221 #
222 # - $(1): a list of candidate tool identifiers to check
223 # - $(2): the path to the tool to identify
224 #
225 # If any of the guess functions corresponding to candidate tool
226 # identifiers return a non-empty value then the tool identifier of the
227 # first function to do so is returned:
228 #
Chris Kayf665ee42024-06-03 02:01:34 +0000229 # $(call toolchain-guess-tool,gnu-gcc llvm-clang,armclang) # <empty>
230 # $(call toolchain-guess-tool,gnu-gcc llvm-clang,clang-14) # llvm-clang
231 # $(call toolchain-guess-tool,gnu-gcc llvm-clang,aarch64-none-elf-gcc-12) # gnu-gcc
Chris Kay0adde4e2024-05-14 13:08:31 +0000232 #
Chris Kayf665ee42024-06-03 02:01:34 +0000233 # Tools are checked in the order that they are provided, and the first
234 # match is returned.
Chris Kay0adde4e2024-05-14 13:08:31 +0000235 #
Chris Kayc8a47ba2023-10-20 09:17:33 +0000236
Chris Kay0adde4e2024-05-14 13:08:31 +0000237 # Arm Compiler for Embedded
Chris Kayf665ee42024-06-03 02:01:34 +0000238 toolchain-guess-tool-arm-clang = $(shell $(1) --version 2>&1 <$(nul) | grep -o "Tool: armclang")
239 toolchain-guess-tool-arm-link = $(shell $(1) --help 2>&1 <$(nul) | grep -o "Tool: armlink")
240 toolchain-guess-tool-arm-fromelf = $(shell $(1) --help 2>&1 <$(nul) | grep -o "Tool: fromelf")
241 toolchain-guess-tool-arm-ar = $(shell $(1) --version 2>&1 <$(nul) | grep -o "Tool: armar")
Chris Kayc8a47ba2023-10-20 09:17:33 +0000242
Chris Kay0adde4e2024-05-14 13:08:31 +0000243 # LLVM Project
Chris Kayf665ee42024-06-03 02:01:34 +0000244 toolchain-guess-tool-llvm-clang = $(shell $(1) -v 2>&1 <$(nul) | grep -o "clang version")
245 toolchain-guess-tool-llvm-lld = $(shell $(1) --help 2>&1 <$(nul) | grep -o "OVERVIEW: lld")
246 toolchain-guess-tool-llvm-objcopy = $(shell $(1) --help 2>&1 <$(nul) | grep -o "llvm-objcopy tool")
247 toolchain-guess-tool-llvm-objdump = $(shell $(1) --help 2>&1 <$(nul) | grep -o "llvm object file dumper")
248 toolchain-guess-tool-llvm-ar = $(shell $(1) --help 2>&1 <$(nul) | grep -o "LLVM Archiver")
Chris Kayc8a47ba2023-10-20 09:17:33 +0000249
Chris Kay0adde4e2024-05-14 13:08:31 +0000250 # GNU Compiler Collection & GNU Binary Utilities
Chris Kayf665ee42024-06-03 02:01:34 +0000251 toolchain-guess-tool-gnu-gcc = $(shell $(1) -v 2>&1 <$(nul) | grep -o "gcc version")
252 toolchain-guess-tool-gnu-ld = $(shell $(1) -v 2>&1 <$(nul) | grep -o "GNU ld")
253 toolchain-guess-tool-gnu-objcopy = $(shell $(1) --version 2>&1 <$(nul) | grep -o "GNU objcopy")
254 toolchain-guess-tool-gnu-objdump = $(shell $(1) --version 2>&1 <$(nul) | grep -o "GNU objdump")
255 toolchain-guess-tool-gnu-ar = $(shell $(1) --version 2>&1 <$(nul) | grep -o "GNU ar")
Chris Kayc8a47ba2023-10-20 09:17:33 +0000256
Chris Kay0adde4e2024-05-14 13:08:31 +0000257 # Other tools
Chris Kayf665ee42024-06-03 02:01:34 +0000258 toolchain-guess-tool-generic-dtc = $(shell $(1) --version 2>&1 <$(nul) | grep -o "Version: DTC")
Chris Kayc8a47ba2023-10-20 09:17:33 +0000259
Chris Kayf665ee42024-06-03 02:01:34 +0000260 toolchain-guess-tool = $(firstword $(foreach candidate,$(1), \
261 $(if $(call toolchain-guess-tool-$(candidate),$(2)),$(candidate))))
Chris Kayc8a47ba2023-10-20 09:17:33 +0000262
Chris Kay0adde4e2024-05-14 13:08:31 +0000263 #
Chris Kay87e98c22024-06-03 11:10:11 +0000264 # Warn the user that a tool could not be identified.
265 #
266 # Parameters:
267 #
268 # - $1: The toolchain that the tool belongs to.
269 # - $2: The tool class that the tool belongs to.
270 #
271
272 define toolchain-warn-unrecognized
273 $(warning )
Chris Kay98255ef2024-08-29 15:08:42 +0000274 $(warning The configured $($(1)-name) $(toolchain-tool-class-name-$(2)) could not be identified:)
Chris Kay87e98c22024-06-03 11:10:11 +0000275 $(warning )
276 $(warning $(space) $($(1)-$(2))$(if $($(1)-$(2)-parameter), (via `$($(1)-$(2)-parameter)`)))
277 $(warning )
Chris Kay98255ef2024-08-29 15:08:42 +0000278 $(warning The following tools were tried, but either did not exist or could not be identified:)
Chris Kay87e98c22024-06-03 11:10:11 +0000279 $(warning )
Chris Kay98255ef2024-08-29 15:08:42 +0000280
281 $(foreach default,$($(1)-$(2)-default), \
282 $(warning $(space) - $(default)))
283
Chris Kay87e98c22024-06-03 11:10:11 +0000284 $(warning )
285 $(warning The following tools are supported:)
286 $(warning )
287
288 $(foreach tool,$(toolchain-tools-$(2)), \
289 $(warning $(space) - $(toolchain-tool-name-$(tool))))
290
291 $(warning )
Chris Kay98255ef2024-08-29 15:08:42 +0000292 $(warning The build system will treat this $(toolchain-tool-class-name-$(2)) as $(toolchain-tool-name-$($(1)-$(2)-default-id)).)
Chris Kay87e98c22024-06-03 11:10:11 +0000293 $(warning )
294 endef
295
296 #
Chris Kay0adde4e2024-05-14 13:08:31 +0000297 # Locate and identify tools belonging to each toolchain.
298 #
299 # Each tool class in each toolchain receives a variable of the form
300 # `$(toolchain)-$(tool)` giving the associated path to the program. For
301 # example:
302 #
303 # - `aarch64-ld` gives the linker for the AArch64 toolchain,
304 # - `aarch32-oc` gives the object copier for the AArch32 toolchain, and
305 # - `host-cc` gives the C compiler for the host toolchain.
306 #
307 # For each of these variables, if no program path is explicitly provided
308 # by the parent Makefile then the C compiler is queried (if supported)
Chris Kayf665ee42024-06-03 02:01:34 +0000309 # for its location.
Chris Kay0adde4e2024-05-14 13:08:31 +0000310 #
Chris Kayf665ee42024-06-03 02:01:34 +0000311 # If the C compiler cannot provide the location (or the tool class *is*
312 # the C compiler), then it is assigned a default value specific for that
313 # toolchain.
Chris Kay0adde4e2024-05-14 13:08:31 +0000314 #
Chris Kayc8a47ba2023-10-20 09:17:33 +0000315
Chris Kayf665ee42024-06-03 02:01:34 +0000316 toolchain-guess-arm-clang-cpp = $(1)
317 toolchain-guess-arm-clang-as = $(1)
318 toolchain-guess-arm-clang-ld = # Fall back to `$(toolchain)-ld-default`
319 toolchain-guess-arm-clang-oc = # Fall back to `$(toolchain)-oc-default`
320 toolchain-guess-arm-clang-od = # Fall back to `$(toolchain)-od-default`
321 toolchain-guess-arm-clang-ar = # Fall back to `$(toolchain)-ar-default`
Chris Kayc8a47ba2023-10-20 09:17:33 +0000322
Chris Kayf665ee42024-06-03 02:01:34 +0000323 toolchain-guess-llvm-clang-cpp = $(1)
324 toolchain-guess-llvm-clang-as = $(1)
325 toolchain-guess-llvm-clang-ld = $(shell $(1) --print-prog-name ld.lld 2>$(nul))
326 toolchain-guess-llvm-clang-oc = $(shell $(1) --print-prog-name llvm-objcopy 2>$(nul))
327 toolchain-guess-llvm-clang-od = $(shell $(1) --print-prog-name llvm-objdump 2>$(nul))
328 toolchain-guess-llvm-clang-ar = $(shell $(1) --print-prog-name llvm-ar 2>$(nul))
Chris Kayc8a47ba2023-10-20 09:17:33 +0000329
Chris Kayf665ee42024-06-03 02:01:34 +0000330 toolchain-guess-gnu-gcc-cpp = $(1)
331 toolchain-guess-gnu-gcc-as = $(1)
332 toolchain-guess-gnu-gcc-ld = $(1)
333 toolchain-guess-gnu-gcc-oc = $(shell $(1) --print-prog-name objcopy 2>$(nul))
334 toolchain-guess-gnu-gcc-od = $(shell $(1) --print-prog-name objdump 2>$(nul))
335 toolchain-guess-gnu-gcc-ar = $(shell $(1) --print-prog-name ar 2>$(nul))
Chris Kay24f3fb42024-04-16 17:19:20 +0000336
Chris Kay87e98c22024-06-03 11:10:11 +0000337 #
338 # Configure a toolchain.
339 #
340 # Parameters:
341 #
342 # - $1: The toolchain to configure.
343 #
344 # This function iterates over all tool classes and configures them for
345 # the provided toolchain. Toolchain tools are initialized lazily and
346 # on-demand based on the first read of the tool path or identifier
347 # variables.
348 #
Chris Kay24f3fb42024-04-16 17:19:20 +0000349
Chris Kay87e98c22024-06-03 11:10:11 +0000350 define toolchain-configure
351 $$(foreach tool-class,$$(toolchain-tool-classes), \
352 $$(eval $$(call toolchain-configure-tool,$1,$$(tool-class))))
Chris Kay0adde4e2024-05-14 13:08:31 +0000353 endef
Chris Kayc8a47ba2023-10-20 09:17:33 +0000354
Chris Kay87e98c22024-06-03 11:10:11 +0000355 #
356 # Configure a specific tool within a toolchain.
357 #
358 # Parameters:
359 #
360 # - $1: The toolchain to configure.
361 # - $2: The tool class to configure.
362 #
Chris Kayc8a47ba2023-10-20 09:17:33 +0000363
Chris Kay87e98c22024-06-03 11:10:11 +0000364 define toolchain-configure-tool
365 $1-$2-configure = $\
366 $$(eval $$(call toolchain-determine-tool,$1,$2))
Chris Kayc8a47ba2023-10-20 09:17:33 +0000367
Chris Kay87e98c22024-06-03 11:10:11 +0000368 #
369 # When either of the following variables are read for the first
370 # time, the appropriate tool is determined and *both* variables
371 # are overwritten with their final values.
372 #
Chris Kay0adde4e2024-05-14 13:08:31 +0000373
Chris Kay87e98c22024-06-03 11:10:11 +0000374 $1-$2 = $$($1-$2-configure)$$($1-$2)
375 $1-$2-id = $$($1-$2-configure)$$($1-$2-id)
376 endef
Chris Kayc8a47ba2023-10-20 09:17:33 +0000377
Chris Kay87e98c22024-06-03 11:10:11 +0000378 #
379 # Determines and identifies a tool.
380 #
381 # Parameters:
382 #
383 # - $1: The toolchain identifier.
384 # - $2: The tool class.
385 #
386 # Tool identification happens by reading the designated tool parameter
387 # to get the user-specified command for the tool (e.g. `CC` or `LD`). If
388 # no tool parameter is defined then try to derive the tool from the C
389 # compiler.
390 #
391 # If all else fails, fall back to the default command defined by the
392 # toolchain makefile.
393 #
Chris Kayc8a47ba2023-10-20 09:17:33 +0000394
Chris Kay87e98c22024-06-03 11:10:11 +0000395 define toolchain-determine-tool
Chris Kay98255ef2024-08-29 15:08:42 +0000396 toolchain-$1-$2-guess-from-cc = $$(if $$(filter-out cc,$2),$\
Chris Kay87e98c22024-06-03 11:10:11 +0000397 $$(call toolchain-guess-$$($1-cc-id)-$2,$$($1-cc)))
Chris Kayc8a47ba2023-10-20 09:17:33 +0000398
Chris Kay87e98c22024-06-03 11:10:11 +0000399 toolchain-$1-$2-shell = $$(or $$($$($1-$2-parameter)),$\
Chris Kay98255ef2024-08-29 15:08:42 +0000400 $$(toolchain-$1-$2-guess-from-cc),$\
401 $$(toolchain-$1-$2-default))
402
403 toolchain-$1-$2-default = $$(firstword $\
404 $$(foreach default,$$($1-$2-default),$\
405 $$(if $$(call which,$$(default)),$$(default))) $\
406 $$($1-$2-default))
Chris Kayc8a47ba2023-10-20 09:17:33 +0000407
Chris Kay87e98c22024-06-03 11:10:11 +0000408 $1-$2 := $(if $(call which,$$(toolchain-$1-$2-shell)),$\
409 $$(call escape-shell,$$(toolchain-$1-$2-shell)),$\
410 $$(toolchain-$1-$2-shell))
411
412 $1-$2-id := $$(or \
413 $$(call toolchain-guess-tool,$$(toolchain-tools-$2),$$($1-$2)),$\
Chris Kay98255ef2024-08-29 15:08:42 +0000414 $$(strip $$(call toolchain-warn-unrecognized,$1,$2)$$($1-$2-default-id)))
Chris Kay0adde4e2024-05-14 13:08:31 +0000415 endef
Chris Kayc8a47ba2023-10-20 09:17:33 +0000416
Chris Kay0adde4e2024-05-14 13:08:31 +0000417 $(foreach toolchain,$(toolchains), \
Chris Kay87e98c22024-06-03 11:10:11 +0000418 $(eval $(call toolchain-configure,$(toolchain))))
Chris Kay0adde4e2024-05-14 13:08:31 +0000419endif