xref: /rk3399_ARM-atf/make_helpers/toolchain.mk (revision 3789c3c0009028bd7730c6cead64ef3f7d071bd6)
1cc277de8SChris Kay#
2cc277de8SChris Kay# Copyright (c) 2023-2024, Arm Limited and Contributors. All rights reserved.
3cc277de8SChris Kay#
4cc277de8SChris Kay# SPDX-License-Identifier: BSD-3-Clause
5cc277de8SChris Kay#
6cc277de8SChris Kay
7cc277de8SChris Kay#
8cc277de8SChris Kay# TF-A uses three toolchains:
9cc277de8SChris Kay#
10cc277de8SChris Kay#   - The host toolchain (`host`) for building native tools
11291e7182SChris Kay#   - The AArch32 toolchain (`aarch32`) for building Arm AArch32 images
12cc277de8SChris Kay#   - The AArch64 toolchain (`aarch64`) for building Arm AArch64 images
13cc277de8SChris Kay#
14cc277de8SChris Kay# In the main Makefile only one of the two Arm toolchains is enabled in any
15cc277de8SChris Kay# given build, but individual tools and libraries may need access to both.
16cc277de8SChris Kay#
17cc277de8SChris Kay
18291e7182SChris Kayifndef toolchain-mk
19291e7182SChris Kay        toolchain-mk := $(lastword $(MAKEFILE_LIST))
20291e7182SChris Kay
21*3789c3c0SChris Kay        include $(dir $(toolchain-mk))build_env.mk
22*3789c3c0SChris Kay        include $(dir $(toolchain-mk))utilities.mk
23cc277de8SChris Kay
24*3789c3c0SChris Kay        #
25*3789c3c0SChris Kay        # Make assigns generic default values to `CC`, `CPP`, `AS`, etc. if they
26*3789c3c0SChris Kay        # are not explicitly assigned values by the user. These are usually okay
27*3789c3c0SChris Kay        # for very simple programs when building for the host system, but we
28*3789c3c0SChris Kay        # need greater control over the toolchain flow.
29*3789c3c0SChris Kay        #
30*3789c3c0SChris Kay        # Therefore, we undefine these built-in variables if they have default
31*3789c3c0SChris Kay        # values, so that we can provide our own default values later instead.
32*3789c3c0SChris Kay        #
334731c00bSChris Kay
34*3789c3c0SChris Kay        ifeq ($(origin CC),default)
35*3789c3c0SChris Kay                undefine CC
36*3789c3c0SChris Kay        endif
37*3789c3c0SChris Kay
38*3789c3c0SChris Kay        ifeq ($(origin CPP),default)
39*3789c3c0SChris Kay                undefine CPP
40*3789c3c0SChris Kay        endif
41*3789c3c0SChris Kay
42*3789c3c0SChris Kay        ifeq ($(origin AS),default)
43*3789c3c0SChris Kay                undefine AS
44*3789c3c0SChris Kay        endif
45*3789c3c0SChris Kay
46*3789c3c0SChris Kay        ifeq ($(origin AR),default)
47*3789c3c0SChris Kay                undefine AR
48*3789c3c0SChris Kay        endif
49*3789c3c0SChris Kay
50*3789c3c0SChris Kay        ifeq ($(origin LD),default)
51*3789c3c0SChris Kay                undefine LD
52*3789c3c0SChris Kay        endif
53*3789c3c0SChris Kay
54*3789c3c0SChris Kay        #
55*3789c3c0SChris Kay        # The full list of toolchains supported by TF-A.
56*3789c3c0SChris Kay        #
57*3789c3c0SChris Kay        # Each of these toolchains defines a file of the same name in the
58*3789c3c0SChris Kay        # `toolchains` directory, which must configure the following variables:
59*3789c3c0SChris Kay        #
60*3789c3c0SChris Kay        #   - <toolchain>-name
61*3789c3c0SChris Kay        #
62*3789c3c0SChris Kay        #     A human-readable name for the toolchain,
63*3789c3c0SChris Kay        #
64*3789c3c0SChris Kay        # Additionally, for every tool class, it must also define:
65*3789c3c0SChris Kay        #
66*3789c3c0SChris Kay        #   - <toolchain>-<tool-class>-parameter
67*3789c3c0SChris Kay        #
68*3789c3c0SChris Kay        #     The command line or environment variable used to set the tool for
69*3789c3c0SChris Kay        #     for the given tool class.
70*3789c3c0SChris Kay        #
71*3789c3c0SChris Kay        #   - <toolchain>-<tool-class>-default
72*3789c3c0SChris Kay        #
73*3789c3c0SChris Kay        #     The default command to use for the given tool class if the user
74*3789c3c0SChris Kay        #     does not explicitly provide one, and if the command could not be
75*3789c3c0SChris Kay        #     derived from the C compiler.
76*3789c3c0SChris Kay        #
77*3789c3c0SChris Kay        #   - <toolchain>-<tool-class>-id-default
78*3789c3c0SChris Kay        #
79*3789c3c0SChris Kay        #     The default tool identifier used if the tool for the given tool
80*3789c3c0SChris Kay        #     class cannot be identified.
81*3789c3c0SChris Kay        #
82*3789c3c0SChris Kay
83*3789c3c0SChris Kay        toolchains := host # Used for host targets
84*3789c3c0SChris Kay        toolchains += aarch32 # Used for AArch32 targets
85*3789c3c0SChris Kay        toolchains += aarch64 # Used for AArch64 targets
86*3789c3c0SChris Kay        toolchains += rk3399-m0 # Used for RK3399 Cortex-M0 targets
87*3789c3c0SChris Kay
88*3789c3c0SChris Kay        include $(toolchains:%=$(dir $(toolchain-mk))toolchains/%.mk)
89cc277de8SChris Kay
90cc277de8SChris Kay        #
91cc277de8SChris Kay        # Configure tool classes that we recognize.
92cc277de8SChris Kay        #
93291e7182SChris Kay        # In the context of this build system, a tool class identifies a
94291e7182SChris Kay        # specific role or type of tool in the toolchain.
95cc277de8SChris Kay        #
96cc277de8SChris Kay
9714260dbfSChris Kay        toolchain-tool-classes := cc
9814260dbfSChris Kay        toolchain-tool-class-name-cc := C compiler
99cc277de8SChris Kay
10014260dbfSChris Kay        toolchain-tool-classes += cpp
10114260dbfSChris Kay        toolchain-tool-class-name-cpp := C preprocessor
102cc277de8SChris Kay
10314260dbfSChris Kay        toolchain-tool-classes += as
10414260dbfSChris Kay        toolchain-tool-class-name-as := assembler
105cc277de8SChris Kay
10614260dbfSChris Kay        toolchain-tool-classes += ld
10714260dbfSChris Kay        toolchain-tool-class-name-ld := linker
1083d6c7e59SChris Kay
10914260dbfSChris Kay        toolchain-tool-classes += oc
11014260dbfSChris Kay        toolchain-tool-class-name-oc := object copier
1113d6c7e59SChris Kay
11214260dbfSChris Kay        toolchain-tool-classes += od
11314260dbfSChris Kay        toolchain-tool-class-name-od := object dumper
1143d6c7e59SChris Kay
11514260dbfSChris Kay        toolchain-tool-classes += ar
11614260dbfSChris Kay        toolchain-tool-class-name-ar := archiver
1173d6c7e59SChris Kay
11814260dbfSChris Kay        toolchain-tool-classes += dtc
11914260dbfSChris Kay        toolchain-tool-class-name-dtc := device tree compiler
120cc277de8SChris Kay
121cc277de8SChris Kay        #
122cc277de8SChris Kay        # Configure tools that we recognize.
123cc277de8SChris Kay        #
124291e7182SChris Kay        # Here we declare the list of specific toolchain tools that we know how
125291e7182SChris Kay        # to interact with. We don't organize these into tool classes yet - that
126291e7182SChris Kay        # happens further down.
127cc277de8SChris Kay        #
128cc277de8SChris Kay
129291e7182SChris Kay        # Arm® Compiler for Embedded
13014260dbfSChris Kay        toolchain-tools := arm-clang
13114260dbfSChris Kay        toolchain-tool-name-arm-clang := Arm® Compiler for Embedded `armclang`
1323d6c7e59SChris Kay
13314260dbfSChris Kay        toolchain-tools += arm-link
13414260dbfSChris Kay        toolchain-tool-name-arm-link := Arm® Compiler for Embedded `armlink`
1353d6c7e59SChris Kay
13614260dbfSChris Kay        toolchain-tools += arm-ar
13714260dbfSChris Kay        toolchain-tool-name-arm-ar := Arm® Compiler for Embedded `armar`
1383d6c7e59SChris Kay
13914260dbfSChris Kay        toolchain-tools += arm-fromelf
14014260dbfSChris Kay        toolchain-tool-name-arm-fromelf := Arm® Compiler for Embedded `fromelf`
141cc277de8SChris Kay
142cc277de8SChris Kay        # LLVM Project
14314260dbfSChris Kay        toolchain-tools += llvm-clang
14414260dbfSChris Kay        toolchain-tool-name-llvm-clang := LLVM Clang (`clang`)
1453d6c7e59SChris Kay
14614260dbfSChris Kay        toolchain-tools += llvm-lld
14714260dbfSChris Kay        toolchain-tool-name-llvm-lld := LLVM LLD (`lld`)
1483d6c7e59SChris Kay
14914260dbfSChris Kay        toolchain-tools += llvm-objcopy
15014260dbfSChris Kay        toolchain-tool-name-llvm-objcopy := LLVM `llvm-objcopy`
1513d6c7e59SChris Kay
15214260dbfSChris Kay        toolchain-tools += llvm-objdump
15314260dbfSChris Kay        toolchain-tool-name-llvm-objdump := LLVM `llvm-objdump`
1543d6c7e59SChris Kay
15514260dbfSChris Kay        toolchain-tools += llvm-ar
15614260dbfSChris Kay        toolchain-tool-name-llvm-ar := LLVM `llvm-ar`
157cc277de8SChris Kay
158cc277de8SChris Kay        # GNU Compiler Collection & GNU Binary Utilities
15914260dbfSChris Kay        toolchain-tools += gnu-gcc
16014260dbfSChris Kay        toolchain-tool-name-gnu-gcc := GNU GCC (`gcc`)
1613d6c7e59SChris Kay
16214260dbfSChris Kay        toolchain-tools += gnu-ld
16314260dbfSChris Kay        toolchain-tool-name-gnu-ld := GNU LD (`ld.bfd`)
1643d6c7e59SChris Kay
16514260dbfSChris Kay        toolchain-tools += gnu-objcopy
16614260dbfSChris Kay        toolchain-tool-name-gnu-objcopy := GNU `objcopy`
1673d6c7e59SChris Kay
16814260dbfSChris Kay        toolchain-tools += gnu-objdump
16914260dbfSChris Kay        toolchain-tool-name-gnu-objdump := GNU `objdump`
1703d6c7e59SChris Kay
17114260dbfSChris Kay        toolchain-tools += gnu-ar
17214260dbfSChris Kay        toolchain-tool-name-gnu-ar := GNU `ar`
173cc277de8SChris Kay
174cc277de8SChris Kay        # Other tools
17514260dbfSChris Kay        toolchain-tools += generic-dtc
17614260dbfSChris Kay        toolchain-tool-name-generic-dtc := Device Tree Compiler (`dtc`)
177cc277de8SChris Kay
178cc277de8SChris Kay        #
179cc277de8SChris Kay        # Assign tools to tool classes.
180cc277de8SChris Kay        #
181291e7182SChris Kay        # Multifunctional tools, i.e. tools which can perform multiple roles in
182291e7182SChris Kay        # a toolchain, may be specified in multiple tool class lists. For
183291e7182SChris Kay        # example, a C compiler which can also perform the role of a linker may
18414260dbfSChris Kay        # be placed in both `toolchain-tools-cc` and `toolchain-tools-ld`.
185cc277de8SChris Kay        #
186cc277de8SChris Kay
187cc277de8SChris Kay        # C-related tools
18814260dbfSChris Kay        toolchain-tools-cc := arm-clang llvm-clang gnu-gcc # C compilers
18914260dbfSChris Kay        toolchain-tools-cpp := arm-clang llvm-clang gnu-gcc # C preprocessors
190cc277de8SChris Kay
191cc277de8SChris Kay        # Assembly-related tools
19214260dbfSChris Kay        toolchain-tools-as := arm-clang llvm-clang gnu-gcc # Assemblers
193cc277de8SChris Kay
194cc277de8SChris Kay        # Linking and object-handling tools
19514260dbfSChris Kay        toolchain-tools-ld := arm-clang arm-link llvm-clang llvm-lld gnu-gcc gnu-ld # Linkers
19614260dbfSChris Kay        toolchain-tools-oc := arm-fromelf llvm-objcopy gnu-objcopy # Object copiers
19714260dbfSChris Kay        toolchain-tools-od := arm-fromelf llvm-objdump gnu-objdump # Object dumpers
19814260dbfSChris Kay        toolchain-tools-ar := arm-ar llvm-ar gnu-ar # Archivers
199cc277de8SChris Kay
200cc277de8SChris Kay        # Other tools
20114260dbfSChris Kay        toolchain-tools-dtc := generic-dtc # Device tree compilers
202cc277de8SChris Kay
203cc277de8SChris Kay        #
204cc277de8SChris Kay        # Helper functions to identify toolchain tools.
205cc277de8SChris Kay        #
206291e7182SChris Kay        # The functions defined in this section return a tool identifier when
207291e7182SChris Kay        # given a path to a binary. We generally check a help or version string
208291e7182SChris Kay        # to more reliably identify tools than by looking at the path alone
209291e7182SChris Kay        # (e.g. `gcc` on macOS is actually Apple Clang).
210cc277de8SChris Kay        #
21114260dbfSChris Kay        # Each tool-guessing function (`toolchain-guess-tool-$(tool)`) takes a
21214260dbfSChris Kay        # single argument giving the path to the tool to guess, and returns a
21314260dbfSChris Kay        # non-empty value if the tool corresponds to the tool identifier
21414260dbfSChris Kay        # `$(tool)`:
215cc277de8SChris Kay        #
21614260dbfSChris Kay        #     $(call toolchain-guess-tool-llvm-clang,aarch64-none-elf-gcc) # <empty>
21714260dbfSChris Kay        #     $(call toolchain-guess-tool-gnu-gcc,aarch64-none-elf-gcc) # <non-empty>
218cc277de8SChris Kay        #
21914260dbfSChris Kay        # The `toolchain-guess-tool` function tries to find the corresponding tool
220291e7182SChris Kay        # identifier for a tool given its path. It takes two arguments:
221cc277de8SChris Kay        #
222cc277de8SChris Kay        #   - $(1): a list of candidate tool identifiers to check
223cc277de8SChris Kay        #   - $(2): the path to the tool to identify
224cc277de8SChris Kay        #
225291e7182SChris Kay        # If any of the guess functions corresponding to candidate tool
226291e7182SChris Kay        # identifiers return a non-empty value then the tool identifier of the
227291e7182SChris Kay        # first function to do so is returned:
228cc277de8SChris Kay        #
22914260dbfSChris Kay        #     $(call toolchain-guess-tool,gnu-gcc llvm-clang,armclang) # <empty>
23014260dbfSChris Kay        #     $(call toolchain-guess-tool,gnu-gcc llvm-clang,clang-14) # llvm-clang
23114260dbfSChris Kay        #     $(call toolchain-guess-tool,gnu-gcc llvm-clang,aarch64-none-elf-gcc-12) # gnu-gcc
232cc277de8SChris Kay        #
23314260dbfSChris Kay        # Tools are checked in the order that they are provided, and the first
23414260dbfSChris Kay        # match is returned.
235cc277de8SChris Kay        #
236cc277de8SChris Kay
237cc277de8SChris Kay        # Arm Compiler for Embedded
23814260dbfSChris Kay        toolchain-guess-tool-arm-clang = $(shell $(1) --version 2>&1 <$(nul) | grep -o "Tool: armclang")
23914260dbfSChris Kay        toolchain-guess-tool-arm-link = $(shell $(1) --help 2>&1 <$(nul) | grep -o "Tool: armlink")
24014260dbfSChris Kay        toolchain-guess-tool-arm-fromelf = $(shell $(1) --help 2>&1 <$(nul) | grep -o "Tool: fromelf")
24114260dbfSChris Kay        toolchain-guess-tool-arm-ar = $(shell $(1) --version 2>&1 <$(nul) | grep -o "Tool: armar")
242cc277de8SChris Kay
243cc277de8SChris Kay        # LLVM Project
24414260dbfSChris Kay        toolchain-guess-tool-llvm-clang = $(shell $(1) -v 2>&1 <$(nul) | grep -o "clang version")
24514260dbfSChris Kay        toolchain-guess-tool-llvm-lld = $(shell $(1) --help 2>&1 <$(nul) | grep -o "OVERVIEW: lld")
24614260dbfSChris Kay        toolchain-guess-tool-llvm-objcopy = $(shell $(1) --help 2>&1 <$(nul) | grep -o "llvm-objcopy tool")
24714260dbfSChris Kay        toolchain-guess-tool-llvm-objdump = $(shell $(1) --help 2>&1 <$(nul) | grep -o "llvm object file dumper")
24814260dbfSChris Kay        toolchain-guess-tool-llvm-ar = $(shell $(1) --help 2>&1 <$(nul) | grep -o "LLVM Archiver")
249cc277de8SChris Kay
250cc277de8SChris Kay        # GNU Compiler Collection & GNU Binary Utilities
25114260dbfSChris Kay        toolchain-guess-tool-gnu-gcc = $(shell $(1) -v 2>&1 <$(nul) | grep -o "gcc version")
25214260dbfSChris Kay        toolchain-guess-tool-gnu-ld = $(shell $(1) -v 2>&1 <$(nul) | grep -o "GNU ld")
25314260dbfSChris Kay        toolchain-guess-tool-gnu-objcopy = $(shell $(1) --version 2>&1 <$(nul) | grep -o "GNU objcopy")
25414260dbfSChris Kay        toolchain-guess-tool-gnu-objdump = $(shell $(1) --version 2>&1 <$(nul) | grep -o "GNU objdump")
25514260dbfSChris Kay        toolchain-guess-tool-gnu-ar = $(shell $(1) --version 2>&1 <$(nul) | grep -o "GNU ar")
256cc277de8SChris Kay
257cc277de8SChris Kay        # Other tools
25814260dbfSChris Kay        toolchain-guess-tool-generic-dtc = $(shell $(1) --version 2>&1 <$(nul) | grep -o "Version: DTC")
259cc277de8SChris Kay
26014260dbfSChris Kay        toolchain-guess-tool = $(firstword $(foreach candidate,$(1), \
26114260dbfSChris Kay                $(if $(call toolchain-guess-tool-$(candidate),$(2)),$(candidate))))
262cc277de8SChris Kay
263cc277de8SChris Kay        #
264*3789c3c0SChris Kay        # Warn the user that a tool could not be identified.
265*3789c3c0SChris Kay        #
266*3789c3c0SChris Kay        # Parameters:
267*3789c3c0SChris Kay        #
268*3789c3c0SChris Kay        # - $1: The toolchain that the tool belongs to.
269*3789c3c0SChris Kay        # - $2: The tool class that the tool belongs to.
270*3789c3c0SChris Kay        #
271*3789c3c0SChris Kay
272*3789c3c0SChris Kay        define toolchain-warn-unrecognized
273*3789c3c0SChris Kay                $(warning )
274*3789c3c0SChris Kay                $(warning The configured $($(1)-name) $(toolchain-tool-class-name-$(2)) could not be identified and may not be supported:)
275*3789c3c0SChris Kay                $(warning )
276*3789c3c0SChris Kay                $(warning $(space)   $($(1)-$(2))$(if $($(1)-$(2)-parameter), (via `$($(1)-$(2)-parameter)`)))
277*3789c3c0SChris Kay                $(warning )
278*3789c3c0SChris Kay                $(warning The default $($(1)-name) $(toolchain-tool-class-name-$(2)) is:)
279*3789c3c0SChris Kay                $(warning )
280*3789c3c0SChris Kay                $(warning $(space)   $($(1)-$(2)-default))
281*3789c3c0SChris Kay                $(warning )
282*3789c3c0SChris Kay                $(warning The following tools are supported:)
283*3789c3c0SChris Kay                $(warning )
284*3789c3c0SChris Kay
285*3789c3c0SChris Kay                $(foreach tool,$(toolchain-tools-$(2)), \
286*3789c3c0SChris Kay                        $(warning $(space) - $(toolchain-tool-name-$(tool))))
287*3789c3c0SChris Kay
288*3789c3c0SChris Kay                $(warning )
289*3789c3c0SChris Kay                $(warning The build system will treat this $(toolchain-tool-class-name-$(2)) as $(toolchain-tool-name-$($(1)-$(2)-id-default)).)
290*3789c3c0SChris Kay                $(warning )
291*3789c3c0SChris Kay        endef
292*3789c3c0SChris Kay
293*3789c3c0SChris Kay        #
294cc277de8SChris Kay        # Locate and identify tools belonging to each toolchain.
295cc277de8SChris Kay        #
296cc277de8SChris Kay        # Each tool class in each toolchain receives a variable of the form
297291e7182SChris Kay        # `$(toolchain)-$(tool)` giving the associated path to the program. For
298291e7182SChris Kay        # example:
299cc277de8SChris Kay        #
300cc277de8SChris Kay        #   - `aarch64-ld` gives the linker for the AArch64 toolchain,
301cc277de8SChris Kay        #   - `aarch32-oc` gives the object copier for the AArch32 toolchain, and
302cc277de8SChris Kay        #   - `host-cc` gives the C compiler for the host toolchain.
303cc277de8SChris Kay        #
304291e7182SChris Kay        # For each of these variables, if no program path is explicitly provided
305291e7182SChris Kay        # by the parent Makefile then the C compiler is queried (if supported)
30614260dbfSChris Kay        # for its location.
307cc277de8SChris Kay        #
30814260dbfSChris Kay        # If the C compiler cannot provide the location (or the tool class *is*
30914260dbfSChris Kay        # the C compiler), then it is assigned a default value specific for that
31014260dbfSChris Kay        # toolchain.
311cc277de8SChris Kay        #
312cc277de8SChris Kay
31314260dbfSChris Kay        toolchain-guess-arm-clang-cpp = $(1)
31414260dbfSChris Kay        toolchain-guess-arm-clang-as = $(1)
31514260dbfSChris Kay        toolchain-guess-arm-clang-ld = # Fall back to `$(toolchain)-ld-default`
31614260dbfSChris Kay        toolchain-guess-arm-clang-oc = # Fall back to `$(toolchain)-oc-default`
31714260dbfSChris Kay        toolchain-guess-arm-clang-od = # Fall back to `$(toolchain)-od-default`
31814260dbfSChris Kay        toolchain-guess-arm-clang-ar = # Fall back to `$(toolchain)-ar-default`
319cc277de8SChris Kay
32014260dbfSChris Kay        toolchain-guess-llvm-clang-cpp = $(1)
32114260dbfSChris Kay        toolchain-guess-llvm-clang-as = $(1)
32214260dbfSChris Kay        toolchain-guess-llvm-clang-ld = $(shell $(1) --print-prog-name ld.lld 2>$(nul))
32314260dbfSChris Kay        toolchain-guess-llvm-clang-oc = $(shell $(1) --print-prog-name llvm-objcopy 2>$(nul))
32414260dbfSChris Kay        toolchain-guess-llvm-clang-od = $(shell $(1) --print-prog-name llvm-objdump 2>$(nul))
32514260dbfSChris Kay        toolchain-guess-llvm-clang-ar = $(shell $(1) --print-prog-name llvm-ar 2>$(nul))
326cc277de8SChris Kay
32714260dbfSChris Kay        toolchain-guess-gnu-gcc-cpp = $(1)
32814260dbfSChris Kay        toolchain-guess-gnu-gcc-as = $(1)
32914260dbfSChris Kay        toolchain-guess-gnu-gcc-ld = $(1)
33014260dbfSChris Kay        toolchain-guess-gnu-gcc-oc = $(shell $(1) --print-prog-name objcopy 2>$(nul))
33114260dbfSChris Kay        toolchain-guess-gnu-gcc-od = $(shell $(1) --print-prog-name objdump 2>$(nul))
33214260dbfSChris Kay        toolchain-guess-gnu-gcc-ar = $(shell $(1) --print-prog-name ar 2>$(nul))
333cc277de8SChris Kay
334*3789c3c0SChris Kay        #
335*3789c3c0SChris Kay        # Configure a toolchain.
336*3789c3c0SChris Kay        #
337*3789c3c0SChris Kay        # Parameters:
338*3789c3c0SChris Kay        #
339*3789c3c0SChris Kay        #   - $1: The toolchain to configure.
340*3789c3c0SChris Kay        #
341*3789c3c0SChris Kay        # This function iterates over all tool classes and configures them for
342*3789c3c0SChris Kay        # the provided toolchain. Toolchain tools are initialized lazily and
343*3789c3c0SChris Kay        # on-demand based on the first read of the tool path or identifier
344*3789c3c0SChris Kay        # variables.
345*3789c3c0SChris Kay        #
3463d6c7e59SChris Kay
347*3789c3c0SChris Kay        define toolchain-configure
348*3789c3c0SChris Kay                $$(foreach tool-class,$$(toolchain-tool-classes), \
349*3789c3c0SChris Kay                        $$(eval $$(call toolchain-configure-tool,$1,$$(tool-class))))
3503d6c7e59SChris Kay        endef
3513d6c7e59SChris Kay
352*3789c3c0SChris Kay        #
353*3789c3c0SChris Kay        # Configure a specific tool within a toolchain.
354*3789c3c0SChris Kay        #
355*3789c3c0SChris Kay        # Parameters:
356*3789c3c0SChris Kay        #
357*3789c3c0SChris Kay        #   - $1: The toolchain to configure.
358*3789c3c0SChris Kay        #   - $2: The tool class to configure.
359*3789c3c0SChris Kay        #
360*3789c3c0SChris Kay
361*3789c3c0SChris Kay        define toolchain-configure-tool
362*3789c3c0SChris Kay                $1-$2-configure = $\
363*3789c3c0SChris Kay                        $$(eval $$(call toolchain-determine-tool,$1,$2))
364*3789c3c0SChris Kay
365*3789c3c0SChris Kay                #
366*3789c3c0SChris Kay                # When either of the following variables are read for the first
367*3789c3c0SChris Kay                # time, the appropriate tool is determined and *both* variables
368*3789c3c0SChris Kay                # are overwritten with their final values.
369*3789c3c0SChris Kay                #
370*3789c3c0SChris Kay
371*3789c3c0SChris Kay                $1-$2 = $$($1-$2-configure)$$($1-$2)
372*3789c3c0SChris Kay                $1-$2-id = $$($1-$2-configure)$$($1-$2-id)
373*3789c3c0SChris Kay        endef
374*3789c3c0SChris Kay
375*3789c3c0SChris Kay        #
376*3789c3c0SChris Kay        # Determines and identifies a tool.
377*3789c3c0SChris Kay        #
378*3789c3c0SChris Kay        # Parameters:
379*3789c3c0SChris Kay        #
380*3789c3c0SChris Kay        #   - $1: The toolchain identifier.
381*3789c3c0SChris Kay        #   - $2: The tool class.
382*3789c3c0SChris Kay        #
383*3789c3c0SChris Kay        # Tool identification happens by reading the designated tool parameter
384*3789c3c0SChris Kay        # to get the user-specified command for the tool (e.g. `CC` or `LD`). If
385*3789c3c0SChris Kay        # no tool parameter is defined then try to derive the tool from the C
386*3789c3c0SChris Kay        # compiler.
387*3789c3c0SChris Kay        #
388*3789c3c0SChris Kay        # If all else fails, fall back to the default command defined by the
389*3789c3c0SChris Kay        # toolchain makefile.
390*3789c3c0SChris Kay        #
391*3789c3c0SChris Kay
392e01c7126SChris Kay        define toolchain-determine-tool
393*3789c3c0SChris Kay                toolchain-$1-$2-guess = $$(if $$(filter-out cc,$2),$\
394*3789c3c0SChris Kay                        $$(call toolchain-guess-$$($1-cc-id)-$2,$$($1-cc)))
395cc277de8SChris Kay
396*3789c3c0SChris Kay                toolchain-$1-$2-shell = $$(or $$($$($1-$2-parameter)),$\
397*3789c3c0SChris Kay                        $$(toolchain-$1-$2-guess),$$($1-$2-default))
3983d6c7e59SChris Kay
399*3789c3c0SChris Kay                $1-$2 := $(if $(call which,$$(toolchain-$1-$2-shell)),$\
400*3789c3c0SChris Kay                        $$(call escape-shell,$$(toolchain-$1-$2-shell)),$\
401*3789c3c0SChris Kay                        $$(toolchain-$1-$2-shell))
4023d6c7e59SChris Kay
403*3789c3c0SChris Kay                $1-$2-id := $$(or \
404*3789c3c0SChris Kay                        $$(call toolchain-guess-tool,$$(toolchain-tools-$2),$$($1-$2)),$\
405*3789c3c0SChris Kay                        $$(strip $$(call toolchain-warn-unrecognized,$1,$2)$$($1-$2-id-default)))
406cc277de8SChris Kay        endef
407cc277de8SChris Kay
408cc277de8SChris Kay        $(foreach toolchain,$(toolchains), \
409*3789c3c0SChris Kay                $(eval $(call toolchain-configure,$(toolchain))))
410291e7182SChris Kayendif
411