From 3789c3c0009028bd7730c6cead64ef3f7d071bd6 Mon Sep 17 00:00:00 2001 From: Chris Kay Date: Mon, 3 Jun 2024 11:10:11 +0000 Subject: [PATCH] build: determine toolchain tools dynamically Since the introduction of the toolchain detection framework into the build system, we have done determination and identification of the toolchain(s) used for the build at the initialization of the build system. This incurs a large cost to the build every time - for every toolchain that has been requested by the current makefile, we try to identify each tool in the list of known tool classes, even if that tool doesn't actually see any use. For the clean and check-like targets we worked around this by disabling most of the toolchains if we detect these targets, but this is inflexible and not very reliable, and it still means that when building normal targets we are incurring that cost for all tools whether they are used or not. This change instead modifies the toolchain detection framework to only initialize a tool for a given toolchain when it is first used. This does mean that we can no longer warn about an incorrectly-configured toolchain at the beginning of build system invocation, but it has the advantage of substantially reducing build time and the complexity of *using* the framework (at the cost of an increase in complexity in the framework itself). Change-Id: I7f3d06b2eb58c1b26a846791a13b0037f32c8013 Signed-off-by: Chris Kay --- Makefile | 11 -- lib/romlib/Makefile | 6 - make_helpers/toolchain.mk | 240 +++++++++++++++-------- make_helpers/toolchains/aarch32.mk | 16 +- make_helpers/toolchains/aarch64.mk | 16 +- make_helpers/toolchains/host.mk | 16 +- plat/rockchip/rk3399/drivers/m0/Makefile | 2 - tools/amlogic/Makefile | 2 - tools/cert_create/Makefile | 2 - tools/encrypt_fw/Makefile | 2 - tools/fiptool/Makefile | 2 - tools/marvell/doimage/Makefile | 2 - tools/nxp/create_pbl/Makefile | 2 - tools/sptool/Makefile | 2 - tools/stm32image/Makefile | 2 - 15 files changed, 186 insertions(+), 137 deletions(-) diff --git a/Makefile b/Makefile index cb88758f5..b3f103f23 100644 --- a/Makefile +++ b/Makefile @@ -37,17 +37,6 @@ include ${MAKE_HELPERS_DIRECTORY}defaults.mk # Configure the toolchains used to build TF-A and its tools ################################################################################ -# -# The clean and check targets do not behave correctly if the user's environment -# does not appropriately configure a toolchain. While we try to find a permanent -# solution to this, do not try to detect any toolchains if we are building -# exclusively with targets which do not use any toolchain tools. -# - -ifeq ($(filter-out check% %clean doc %tool,$(or $(MAKECMDGOALS),all)),) - toolchains := -endif - include ${MAKE_HELPERS_DIRECTORY}toolchain.mk # Assertions enabled for DEBUG builds by default diff --git a/lib/romlib/Makefile b/lib/romlib/Makefile index 29fbf7839..3d2b85026 100644 --- a/lib/romlib/Makefile +++ b/lib/romlib/Makefile @@ -4,12 +4,6 @@ # SPDX-License-Identifier: BSD-3-Clause # -ifeq ($(filter-out clean,$(or $(MAKECMDGOALS),all)),) - toolchains := -else - toolchains := aarch64 -endif - include ../../make_helpers/build-rules.mk include ../../make_helpers/common.mk include ../../make_helpers/toolchain.mk diff --git a/make_helpers/toolchain.mk b/make_helpers/toolchain.mk index 68be6a12e..6a58af149 100644 --- a/make_helpers/toolchain.mk +++ b/make_helpers/toolchain.mk @@ -18,13 +18,74 @@ ifndef toolchain-mk toolchain-mk := $(lastword $(MAKEFILE_LIST)) - toolchains ?= host $(ARCH) + include $(dir $(toolchain-mk))build_env.mk + include $(dir $(toolchain-mk))utilities.mk - include $(dir $(lastword $(MAKEFILE_LIST)))build_env.mk - include $(dir $(lastword $(MAKEFILE_LIST)))utilities.mk + # + # Make assigns generic default values to `CC`, `CPP`, `AS`, etc. if they + # are not explicitly assigned values by the user. These are usually okay + # for very simple programs when building for the host system, but we + # need greater control over the toolchain flow. + # + # Therefore, we undefine these built-in variables if they have default + # values, so that we can provide our own default values later instead. + # - include $(addprefix $(dir $(lastword $(MAKEFILE_LIST)))toolchains/, \ - $(addsuffix .mk,$(toolchains))) + ifeq ($(origin CC),default) + undefine CC + endif + + ifeq ($(origin CPP),default) + undefine CPP + endif + + ifeq ($(origin AS),default) + undefine AS + endif + + ifeq ($(origin AR),default) + undefine AR + endif + + ifeq ($(origin LD),default) + undefine LD + endif + + # + # The full list of toolchains supported by TF-A. + # + # Each of these toolchains defines a file of the same name in the + # `toolchains` directory, which must configure the following variables: + # + # - -name + # + # A human-readable name for the toolchain, + # + # Additionally, for every tool class, it must also define: + # + # - --parameter + # + # The command line or environment variable used to set the tool for + # for the given tool class. + # + # - --default + # + # The default command to use for the given tool class if the user + # does not explicitly provide one, and if the command could not be + # derived from the C compiler. + # + # - --id-default + # + # The default tool identifier used if the tool for the given tool + # class cannot be identified. + # + + toolchains := host # Used for host targets + toolchains += aarch32 # Used for AArch32 targets + toolchains += aarch64 # Used for AArch64 targets + toolchains += rk3399-m0 # Used for RK3399 Cortex-M0 targets + + include $(toolchains:%=$(dir $(toolchain-mk))toolchains/%.mk) # # Configure tool classes that we recognize. @@ -139,37 +200,6 @@ ifndef toolchain-mk # Other tools toolchain-tools-dtc := generic-dtc # Device tree compilers - # - # Default tools for each toolchain. - # - # Toolchains can specify a default path to any given tool with a tool - # class. These values are used in the absence of user-specified values, - # and are configured by the makefile for each toolchain using variables - # of the form: - # - # - $(toolchain)-$(tool-class)-default - # - # For example, the default C compiler for the AArch32 and AArch64 - # toolchains could be configured with: - # - # - aarch32-cc-default - # - aarch64-cc-default - # - - define toolchain-check-tool-class-default - ifndef $(1)-$(tool-class)-default - $$(error no default value specified for tool class `$(2)` of toolchain `$(1)`) - endif - endef - - define toolchain-check-tool-class-defaults - $(foreach tool-class,$(toolchain-tool-classes), \ - $(eval $(call toolchain-check-tool-class-default,$(1),$(tool-class)))) - endef - - $(foreach toolchain,$(toolchains), \ - $(eval $(call toolchain-check-tool-class-defaults,$(toolchain)))) - # # Helper functions to identify toolchain tools. # @@ -230,6 +260,36 @@ ifndef toolchain-mk toolchain-guess-tool = $(firstword $(foreach candidate,$(1), \ $(if $(call toolchain-guess-tool-$(candidate),$(2)),$(candidate)))) + # + # Warn the user that a tool could not be identified. + # + # Parameters: + # + # - $1: The toolchain that the tool belongs to. + # - $2: The tool class that the tool belongs to. + # + + define toolchain-warn-unrecognized + $(warning ) + $(warning The configured $($(1)-name) $(toolchain-tool-class-name-$(2)) could not be identified and may not be supported:) + $(warning ) + $(warning $(space) $($(1)-$(2))$(if $($(1)-$(2)-parameter), (via `$($(1)-$(2)-parameter)`))) + $(warning ) + $(warning The default $($(1)-name) $(toolchain-tool-class-name-$(2)) is:) + $(warning ) + $(warning $(space) $($(1)-$(2)-default)) + $(warning ) + $(warning The following tools are supported:) + $(warning ) + + $(foreach tool,$(toolchain-tools-$(2)), \ + $(warning $(space) - $(toolchain-tool-name-$(tool)))) + + $(warning ) + $(warning The build system will treat this $(toolchain-tool-class-name-$(2)) as $(toolchain-tool-name-$($(1)-$(2)-id-default)).) + $(warning ) + endef + # # Locate and identify tools belonging to each toolchain. # @@ -271,56 +331,80 @@ ifndef toolchain-mk toolchain-guess-gnu-gcc-od = $(shell $(1) --print-prog-name objdump 2>$(nul)) toolchain-guess-gnu-gcc-ar = $(shell $(1) --print-prog-name ar 2>$(nul)) - define toolchain-warn-unrecognized - $$(warning ) - $$(warning The configured $$($(1)-name) $$(toolchain-tool-class-name-$(2)) could not be identified and may not be supported:) - $$(warning ) - $$(warning $$(space) $$($(1)-$(2))) - $$(warning ) - $$(warning The default $$($(1)-name) $$(toolchain-tool-class-name-$(2)) is:) - $$(warning ) - $$(warning $$(space) $$($(1)-$(2)-default)) - $$(warning ) - $$(warning The following tools are supported:) - $$(warning ) + # + # Configure a toolchain. + # + # Parameters: + # + # - $1: The toolchain to configure. + # + # This function iterates over all tool classes and configures them for + # the provided toolchain. Toolchain tools are initialized lazily and + # on-demand based on the first read of the tool path or identifier + # variables. + # - $$(foreach tool,$$(toolchain-tools-$(2)), \ - $$(warning $$(space) - $$(toolchain-tool-name-$$(tool)))) - - $$(warning ) - $$(warning The build system will treat this $$(toolchain-tool-class-name-$(2)) as $$(toolchain-tool-name-$$($(1)-$(2)-id-default)).) - $$(warning ) + define toolchain-configure + $$(foreach tool-class,$$(toolchain-tool-classes), \ + $$(eval $$(call toolchain-configure-tool,$1,$$(tool-class)))) endef + # + # Configure a specific tool within a toolchain. + # + # Parameters: + # + # - $1: The toolchain to configure. + # - $2: The tool class to configure. + # + + define toolchain-configure-tool + $1-$2-configure = $\ + $$(eval $$(call toolchain-determine-tool,$1,$2)) + + # + # When either of the following variables are read for the first + # time, the appropriate tool is determined and *both* variables + # are overwritten with their final values. + # + + $1-$2 = $$($1-$2-configure)$$($1-$2) + $1-$2-id = $$($1-$2-configure)$$($1-$2-id) + endef + + # + # Determines and identifies a tool. + # + # Parameters: + # + # - $1: The toolchain identifier. + # - $2: The tool class. + # + # Tool identification happens by reading the designated tool parameter + # to get the user-specified command for the tool (e.g. `CC` or `LD`). If + # no tool parameter is defined then try to derive the tool from the C + # compiler. + # + # If all else fails, fall back to the default command defined by the + # toolchain makefile. + # + define toolchain-determine-tool - $(1)-$(2)-guess = $$(if $$(filter-out cc,$(2)),$\ - $$(call toolchain-guess-$$($(1)-cc-id)-$(2),$$($(1)-cc))) + toolchain-$1-$2-guess = $$(if $$(filter-out cc,$2),$\ + $$(call toolchain-guess-$$($1-cc-id)-$2,$$($1-cc))) - $(1)-$(2) := $$(or $$($(1)-$(2)),$$($(1)-$(2)-guess)) - $(1)-$(2) := $$(or $$($(1)-$(2)),$$($(1)-$(2)-default)) + toolchain-$1-$2-shell = $$(or $$($$($1-$2-parameter)),$\ + $$(toolchain-$1-$2-guess),$$($1-$2-default)) - ifneq ($$(call which,$$($(1)-$(2))),) - # If we can resolve this tool to a program on the `PATH` - # then escape it for use in a shell, which allows us to - # preserve spaces. + $1-$2 := $(if $(call which,$$(toolchain-$1-$2-shell)),$\ + $$(call escape-shell,$$(toolchain-$1-$2-shell)),$\ + $$(toolchain-$1-$2-shell)) - $(1)-$(2) := $$(call escape-shell,$$($(1)-$(2))) - endif - - $(1)-$(2)-id := $$(call toolchain-guess-tool,$$(toolchain-tools-$(2)),$$($(1)-$(2))) - - ifndef $(1)-$(2)-id - $(1)-$(2)-id := $$($(1)-$(2)-id-default) - - $$(eval $$(call toolchain-warn-unrecognized,$(1),$(2))) - endif - endef - - define toolchain-determine - $$(foreach tool-class,$$(toolchain-tool-classes), \ - $$(eval $$(call toolchain-determine-tool,$(1),$$(tool-class)))) + $1-$2-id := $$(or \ + $$(call toolchain-guess-tool,$$(toolchain-tools-$2),$$($1-$2)),$\ + $$(strip $$(call toolchain-warn-unrecognized,$1,$2)$$($1-$2-id-default))) endef $(foreach toolchain,$(toolchains), \ - $(eval $(call toolchain-determine,$(toolchain)))) + $(eval $(call toolchain-configure,$(toolchain)))) endif diff --git a/make_helpers/toolchains/aarch32.mk b/make_helpers/toolchains/aarch32.mk index ff00a5339..4e25eaa7d 100644 --- a/make_helpers/toolchains/aarch32.mk +++ b/make_helpers/toolchains/aarch32.mk @@ -6,34 +6,34 @@ aarch32-name := AArch32 -aarch32-cc := $(if $(filter-out default,$(origin CC)),$(CC)) +aarch32-cc-parameter := CC aarch32-cc-default := $(or $(CROSS_COMPILE),arm-none-eabi-)gcc aarch32-cc-id-default := gnu-gcc -aarch32-cpp := $(if $(filter-out default,$(origin CPP)),$(CPP)) +aarch32-cpp-parameter := CPP aarch32-cpp-default := $(or $(CROSS_COMPILE),arm-none-eabi-)gcc aarch32-cpp-id-default := gnu-gcc -aarch32-as := $(if $(filter-out default,$(origin AS)),$(AS)) +aarch32-as-parameter := AS aarch32-as-default := $(or $(CROSS_COMPILE),arm-none-eabi-)gcc aarch32-as-id-default := gnu-gcc -aarch32-ld := $(if $(filter-out default,$(origin LD)),$(LD)) +aarch32-ld-parameter := LD aarch32-ld-default := $(or $(CROSS_COMPILE),arm-none-eabi-)gcc aarch32-ld-id-default := gnu-gcc -aarch32-oc := $(if $(filter-out default,$(origin OC)),$(OC)) +aarch32-oc-parameter := OC aarch32-oc-default := $(or $(CROSS_COMPILE),arm-none-eabi-)objcopy aarch32-oc-id-default := gnu-objcopy -aarch32-od := $(if $(filter-out default,$(origin OD)),$(OD)) +aarch32-od-parameter := OD aarch32-od-default := $(or $(CROSS_COMPILE),arm-none-eabi-)objdump aarch32-od-id-default := gnu-objdump -aarch32-ar := $(if $(filter-out default,$(origin AR)),$(AR)) +aarch32-ar-parameter := AR aarch32-ar-default := $(or $(CROSS_COMPILE),arm-none-eabi-)gcc-ar aarch32-ar-id-default := gnu-ar -aarch32-dtc := $(if $(filter-out default,$(origin DTC)),$(DTC)) +aarch32-dtc-parameter := DTC aarch32-dtc-default := dtc aarch32-dtc-id-default := generic-dtc diff --git a/make_helpers/toolchains/aarch64.mk b/make_helpers/toolchains/aarch64.mk index 407f06818..afefe3eb3 100644 --- a/make_helpers/toolchains/aarch64.mk +++ b/make_helpers/toolchains/aarch64.mk @@ -6,34 +6,34 @@ aarch64-name := AArch64 -aarch64-cc := $(if $(filter-out default,$(origin CC)),$(CC)) +aarch64-cc-parameter := CC aarch64-cc-default := $(or $(CROSS_COMPILE),aarch64-none-elf-)gcc aarch64-cc-id-default := gnu-gcc -aarch64-cpp := $(if $(filter-out default,$(origin CPP)),$(CPP)) +aarch64-cpp-parameter := CPP aarch64-cpp-default := $(or $(CROSS_COMPILE),aarch64-none-elf-)gcc aarch64-cpp-id-default := gnu-gcc -aarch64-as := $(if $(filter-out default,$(origin AS)),$(AS)) +aarch64-as-parameter := AS aarch64-as-default := $(or $(CROSS_COMPILE),aarch64-none-elf-)gcc aarch64-as-id-default := gnu-gcc -aarch64-ld := $(if $(filter-out default,$(origin LD)),$(LD)) +aarch64-ld-parameter := LD aarch64-ld-default := $(or $(CROSS_COMPILE),aarch64-none-elf-)gcc aarch64-ld-id-default := gnu-gcc -aarch64-oc := $(if $(filter-out default,$(origin OC)),$(OC)) +aarch64-oc-parameter := OC aarch64-oc-default := $(or $(CROSS_COMPILE),aarch64-none-elf-)objcopy aarch64-oc-id-default := gnu-objcopy -aarch64-od := $(if $(filter-out default,$(origin OD)),$(OD)) +aarch64-od-parameter := OD aarch64-od-default := $(or $(CROSS_COMPILE),aarch64-none-elf-)objdump aarch64-od-id-default := gnu-objdump -aarch64-ar := $(if $(filter-out default,$(origin AR)),$(AR)) +aarch64-ar-parameter := AR aarch64-ar-default := $(or $(CROSS_COMPILE),aarch64-none-elf-)gcc-ar aarch64-ar-id-default := gnu-ar -aarch64-dtc := $(if $(filter-out default,$(origin DTC)),$(DTC)) +aarch64-dtc-parameter := DTC aarch64-dtc-default := dtc aarch64-dtc-id-default := generic-dtc diff --git a/make_helpers/toolchains/host.mk b/make_helpers/toolchains/host.mk index 733c289b7..ddf022f53 100644 --- a/make_helpers/toolchains/host.mk +++ b/make_helpers/toolchains/host.mk @@ -6,34 +6,34 @@ host-name := host -host-cc := $(HOSTCC) +host-cc-parameter := HOSTCC host-cc-default := gcc host-cc-id-default := gnu-gcc -host-cpp := $(HOSTCPP) +host-cpp-parameter := HOSTCPP host-cpp-default := gcc host-cpp-id-default := gnu-gcc -host-as := $(HOSTAS) +host-as-parameter := HOSTAS host-as-default := gcc host-as-id-default := gnu-gcc -host-ld := $(HOSTLD) +host-ld-parameter := HOSTLD host-ld-default := gcc host-ld-id-default := gnu-gcc -host-oc := $(HOSTOC) +host-oc-parameter := HOSTOC host-oc-default := objcopy host-oc-id-default := gnu-objcopy -host-od := $(HOSTOD) +host-od-parameter := HOSTOD host-od-default := objdump host-od-id-default := gnu-objdump -host-ar := $(HOSTAR) +host-ar-parameter := HOSTAR host-ar-default := gcc-ar host-ar-id-default := gnu-ar -host-dtc := $(HOSTDTC) +host-dtc-parameter := HOSTDTC host-dtc-default := dtc host-dtc-id-default := generic-dtc diff --git a/plat/rockchip/rk3399/drivers/m0/Makefile b/plat/rockchip/rk3399/drivers/m0/Makefile index e742591f3..32446efb0 100644 --- a/plat/rockchip/rk3399/drivers/m0/Makefile +++ b/plat/rockchip/rk3399/drivers/m0/Makefile @@ -4,8 +4,6 @@ # SPDX-License-Identifier: BSD-3-Clause # -toolchains := rk3399-m0 - include ../../../../../make_helpers/common.mk include ../../../../../make_helpers/toolchain.mk diff --git a/tools/amlogic/Makefile b/tools/amlogic/Makefile index 7a5343754..7bfee7d7c 100644 --- a/tools/amlogic/Makefile +++ b/tools/amlogic/Makefile @@ -5,8 +5,6 @@ # https://spdx.org/licenses # -toolchains := host - MAKE_HELPERS_DIRECTORY := ../../make_helpers/ include ${MAKE_HELPERS_DIRECTORY}build_macros.mk include ${MAKE_HELPERS_DIRECTORY}build_env.mk diff --git a/tools/cert_create/Makefile b/tools/cert_create/Makefile index 16f4aa3c9..ce12a6605 100644 --- a/tools/cert_create/Makefile +++ b/tools/cert_create/Makefile @@ -10,8 +10,6 @@ CRTTOOL ?= cert_create${BIN_EXT} BINARY := $(notdir ${CRTTOOL}) COT := tbbr -toolchains := host - MAKE_HELPERS_DIRECTORY := ../../make_helpers/ include ${MAKE_HELPERS_DIRECTORY}build_macros.mk include ${MAKE_HELPERS_DIRECTORY}build_env.mk diff --git a/tools/encrypt_fw/Makefile b/tools/encrypt_fw/Makefile index 0210c36e7..50b0fa2f9 100644 --- a/tools/encrypt_fw/Makefile +++ b/tools/encrypt_fw/Makefile @@ -11,8 +11,6 @@ ENCTOOL ?= encrypt_fw${BIN_EXT} BINARY := $(notdir ${ENCTOOL}) OPENSSL_DIR := /usr -toolchains := host - MAKE_HELPERS_DIRECTORY := ../../make_helpers/ include ${MAKE_HELPERS_DIRECTORY}build_macros.mk include ${MAKE_HELPERS_DIRECTORY}build_env.mk diff --git a/tools/fiptool/Makefile b/tools/fiptool/Makefile index 23c8e6489..54dee87e1 100644 --- a/tools/fiptool/Makefile +++ b/tools/fiptool/Makefile @@ -4,8 +4,6 @@ # SPDX-License-Identifier: BSD-3-Clause # -toolchains := host - MAKE_HELPERS_DIRECTORY := ../../make_helpers/ include ${MAKE_HELPERS_DIRECTORY}build_macros.mk include ${MAKE_HELPERS_DIRECTORY}build_env.mk diff --git a/tools/marvell/doimage/Makefile b/tools/marvell/doimage/Makefile index 488b768e8..a4f7a1d89 100644 --- a/tools/marvell/doimage/Makefile +++ b/tools/marvell/doimage/Makefile @@ -4,8 +4,6 @@ # SPDX-License-Identifier: BSD-3-Clause # https://spdx.org/licenses -toolchains := host - include ../../../make_helpers/common.mk include ../../../make_helpers/toolchain.mk diff --git a/tools/nxp/create_pbl/Makefile b/tools/nxp/create_pbl/Makefile index 7648b7fc0..22aa921d0 100644 --- a/tools/nxp/create_pbl/Makefile +++ b/tools/nxp/create_pbl/Makefile @@ -4,8 +4,6 @@ # SPDX-License-Identifier: BSD-3-Clause # -toolchains := host - MAKE_HELPERS_DIRECTORY := ../../../make_helpers/ include ${MAKE_HELPERS_DIRECTORY}build_macros.mk include ${MAKE_HELPERS_DIRECTORY}build_env.mk diff --git a/tools/sptool/Makefile b/tools/sptool/Makefile index e336a0c54..0da5c09c3 100644 --- a/tools/sptool/Makefile +++ b/tools/sptool/Makefile @@ -4,8 +4,6 @@ # SPDX-License-Identifier: BSD-3-Clause # -toolchains := host - MAKE_HELPERS_DIRECTORY := ../../make_helpers/ include ${MAKE_HELPERS_DIRECTORY}build_macros.mk include ${MAKE_HELPERS_DIRECTORY}build_env.mk diff --git a/tools/stm32image/Makefile b/tools/stm32image/Makefile index 2b34ef8dd..453daaeba 100644 --- a/tools/stm32image/Makefile +++ b/tools/stm32image/Makefile @@ -4,8 +4,6 @@ # SPDX-License-Identifier: BSD-3-Clause # -toolchains := host - MAKE_HELPERS_DIRECTORY := ../../make_helpers/ include ${MAKE_HELPERS_DIRECTORY}build_macros.mk include ${MAKE_HELPERS_DIRECTORY}build_env.mk