treewide: more SPDX header work
[coreboot.git] / toolchain.inc
blob2a4a91dafdcae69b3a0a4a6bf054c92c60b04298
1 # This file is part of the coreboot project.
2 # SPDX-License-Identifier: GPL-2.0-only
4 # ccache integration
5 ifeq ($(CONFIG_CCACHE),y)
7 CCACHE:=$(word 1,$(wildcard $(addsuffix /ccache,$(subst :, ,$(PATH)))))
8 ifeq ($(CCACHE),)
9 $(error ccache selected, but not found in PATH)
10 endif
12 export CCACHE_COMPILERCHECK=content
13 export CCACHE_BASEDIR=$(top)
15 $(foreach arch,$(ARCH_SUPPORTED), \
16         $(eval CC_$(arch):=$(CCACHE) $(CC_$(arch))))
18 HOSTCC:=$(CCACHE) $(HOSTCC)
19 HOSTCXX:=$(CCACHE) $(HOSTCXX)
20 endif
22 # scan-build integration
23 ifneq ($(CCC_ANALYZER_OUTPUT_FORMAT),)
25 ifeq ($(CCC_ANALYZER_ANALYSIS),)
26 export CCC_ANALYZER_ANALYSIS := -analyzer-opt-analyze-headers
27 endif
29 $(foreach arch,$(ARCH_SUPPORTED), \
30         $(eval CC_$(arch):=CCC_CC="$(CC_$(arch))" $(CC) ))
32 HOSTCC:=CCC_CC="$(HOSTCC)" $(CC)
33 HOSTCXX:=CCC_CXX="$(HOSTCXX)" $(CXX)
34 endif
36 COREBOOT_STANDARD_STAGES := decompressor bootblock verstage romstage ramstage
37 MAP-decompressor := bootblock
39 ARCHDIR-i386    := x86
40 ARCHDIR-x86_32  := x86
41 ARCHDIR-x86_64  := x86
42 ARCHDIR-arm     := arm
43 ARCHDIR-arm64   := arm64
44 ARCHDIR-riscv   := riscv
45 ARCHDIR-ppc64   := ppc64
47 CFLAGS_arm      +=
48 CFLAGS_arm64    += -mgeneral-regs-only
49 CFLAGS_riscv    +=
50 CFLAGS_x86_32   +=
51 CFLAGS_x86_64   += -mcmodel=large -mno-red-zone
52 CFLAGS_ppc64    +=
54 # Some boards only provide 2K stacks, so storing lots of data there leads to
55 # problems. Since C rules don't allow us to statically determine the maximum
56 # stack use, we use 1.5K as heuristic, assuming that we typically have lots
57 # of tiny stack frames and the odd large one.
59 # Store larger buffers in BSS, use MAYBE_STATIC_BSS to share data in cache-as-ram
60 # on x86.
61 # Since GCCs detection of dynamic array bounds unfortunately seems to be
62 # very basic, you'll sometimes have to use a static upper bound for the
63 # size and an assert() to make sure it's honored (see gpio_base3_value()
64 # for an example).
65 # (If you absolutely need a larger stack frame and are 100% sure it cannot
66 # cause problems, you can whitelist it with #pragma diagnostic.)
67 ifeq ($(CONFIG_COMPILER_GCC),y)
68 CFLAGS_arm      += -Wstack-usage=1536
69 CFLAGS_arm64    += -Wstack-usage=1536
70 CFLAGS_riscv    += -Wstack-usage=1536
71 CFLAGS_ppc64    += -Wstack-usage=1536
72 endif
74 toolchain_to_dir = \
75         $(foreach arch,$(ARCH_SUPPORTED),\
76         $(eval CPPFLAGS_$(arch) += \
77         -Isrc/arch/$(ARCHDIR-$(arch))/include))
79 # set_stage_toolchain: Decides the toolchain to be used by every stage
80 # E.g.: If bootblock is x86_32, it sets ARCH-BOOTBLOCK-y = x86_32, whereas
81 # ARCH-BOOTBLOCK-n = armv7. Then, ARCH-BOOTBLOCK-y can be used anywhere to
82 # decide the compiler toolchain for bootblock stage
83 # This step is essential for initializing the toolchain for coreboot standard
84 # stages i.e. bootblock, romstage and ramstage, since it acts as the second
85 # parameter to create_class_compiler below in init_standard_toolchain
86 map_stage = $(strip $(if $(MAP-$(1)),$(MAP-$(1)),$(1)))
87 set_stage_toolchain= \
88         $(foreach arch,$(ARCH_SUPPORTED), \
89         $(eval ARCH-$(1)-$($(shell \
90                 echo CONFIG_ARCH_$(call map_stage,$(1))_$(arch) | \
91                 tr '[:lower:]' '[:upper:]')) := $(arch)))
93 # standard-archs: Tell which architectures are used by the standard stages.
94 standard-archs = $(sort $(foreach stagearch, \
95                           $(patsubst %,ARCH-%-y,$(COREBOOT_STANDARD_STAGES)), \
96                           $($(stagearch))))
98 # create_class_compiler: Used to create compiler tool set for
99 # special classes
100 # @1: special class
101 # @2: compiler set to be used
102 # e.g.: smm special class uses i386 as compiler set
103 define create_class_compiler
104 $(if $(2),,$(warning *** The toolchain architecture for $(1) is unknown.) \
105         $(error Check your .config file for CONFIG_ARCH_$(1)_* settings))
106 CC_$(1) := $(CC_$(2))
107 GCC_$(1) := $(GCC_CC_$(2))
108 LD_$(1) := $(LD_$(2))
109 NM_$(1) := $(NM_$(2))
110 AR_$(1) := $(AR_$(2))
111 GNATBIND_$(1) := $(GNATBIND_$(2))
112 OBJCOPY_$(1) := $(OBJCOPY_$(2))
113 OBJDUMP_$(1) := $(OBJDUMP_$(2))
114 STRIP_$(1) := $(STRIP_$(2))
115 READELF_$(1) := $(READELF_$(2))
116 CFLAGS_$(1) = $$(CFLAGS_common) $$(CFLAGS_$(2))
117 ADAFLAGS_$(1) = --RTS=$$(obj)/libgnat-$(2)/ $$(ADAFLAGS_common) $$(GCC_ADAFLAGS_$(2))
118 CPPFLAGS_$(1) = $$(CPPFLAGS_common) $$(CPPFLAGS_$(2)) -D__ARCH_$(2)__
119 COMPILER_RT_$(1) := $$(COMPILER_RT_$(2))
120 COMPILER_RT_FLAGS_$(1) := $$(COMPILER_RT_FLAGS_$(2))
121 LDFLAGS_$(1) = $$(LDFLAGS_common) $$(LDFLAGS_$(2))
122 endef
124 # define_class: Allows defining any program as dynamic class and compiler tool
125 # set for the same based on the architecture for which the program is to be
126 # compiled
127 # @1: program (class name)
128 # @2: architecture for which the program needs to be compiled
129 # IMP: Ensure that define_class is called before any .c or .S files are added to
130 # the class of the program. Check subdirs-y for order of subdirectory inclusions
131 define define_class
132 classes-y += $(1)
133 $(eval $(call create_class_compiler,$(1),$(2)))
134 endef
136 # initialize standard toolchain (CC,AS and others) for give stage
137 # @1 : stage for which the toolchain is to be initialized
138 init_standard_toolchain = \
139         $(eval $(call set_stage_toolchain,$(1))) \
140         $(eval $(call create_class_compiler,$(1),$(ARCH-$(1)-y)))
142 init_stages = \
143         $(foreach stage,$(COREBOOT_STANDARD_STAGES), \
144         $(eval $(call init_standard_toolchain,$(stage))))
146 $(eval $(call toolchain_to_dir))
148 $(call init_stages)
150 # Test for coreboot toolchain (except when explicitly not requested)
151 ifneq ($(NOCOMPILE),1)
152 # Only run if we're doing a build (not for tests, kconfig, ...)
153 # rationale: gcc versions by Linux distributions tend to be quite messed up
154 # llvm/clang also needs patches supplied by the coreboot build
155 COMPILERFAIL:=0
156 IASLFAIL:=0
158 ifneq ($(CONFIG_ANY_TOOLCHAIN),y)
159 # Verify that the coreboot toolchain is being used.
160 $(foreach arch,$(sort $(foreach stage,\
161         $(COREBOOT_STANDARD_STAGES),$(ARCH-$(stage)-y))), \
162         $(if $(shell if [ -n "$(CC_$(arch))" ]; then \
163                 $(CC_$(arch)) -v 2>&1 | grep -q "coreboot toolchain" || \
164                 echo not-coreboot; else echo not-coreboot; fi), \
165                 $(eval COMPILERFAIL:=1)\
166                 $(warning The coreboot toolchain for '$(arch)'\
167                         architecture was not found.)))
168 # If iasl doesn't match the current coreboot version, fail the test
169 # TODO: Figure out if iasl is even needed for the build.
170 $(if $(shell if [ -n "$(IASL)" ]; then \
171         $(IASL) -v 2>&1 | grep -q "coreboot toolchain" || \
172         echo not-coreboot; else echo not-coreboot; fi), \
173         $(eval COMPILERFAIL:=1)$(eval IASLFAIL:=1)\
174         $(warning The coreboot toolchain version of iasl \
175                 '$(shell util/crossgcc/buildgcc -s iasl)' was not found))
176 else #$(CONFIG_ANY_TOOLCHAIN)
177 # If the coreboot toolchain isn't being used, verify that there is A toolchain
178 $(foreach arch,$(sort \
179         $(foreach stage,$(COREBOOT_STANDARD_STAGES),$(ARCH-$(stage)-y))), \
180         $(if $(CC_$(arch)),, $(eval COMPILERFAIL:=1) \
181         $(warning No compiler found for '$(arch)' architecture. \
182                 Install one or use the coreboot toolchain?)) )
183 # If iasl isn't present, fail
184 # TODO: Figure out if iasl is even needed for the build.
185 $(if $(IASL),, $(eval COMPILERFAIL:=1)$(eval IASLFAIL:=1) \
186         $(warning iasl not found. \
187                 Please install it or use the coreboot toolchain.))
188 endif
190 # If the compiler check failed, print out warnings
191 ifeq ($(COMPILERFAIL),1)
192 ifneq ($(XGCCPATH),)
193 $(warning )
194 $(warning Path to your toolchain is currently set to '$(XGCCPATH)')
195 endif
196 $(warning )
197 $(warning To build the entire coreboot toolchain: run 'make crossgcc')
198 ifeq ($(IASLFAIL),1)
199 $(warning To build just IASL: run 'make iasl')
200 endif #($(IASLFAIL),1)
201 $(warning For more toolchain build targets: run 'make help_toolchain')
202 $(warning )
203 ifneq ($(CONFIG_ANY_TOOLCHAIN),y)
204 $(warning To try to use any toolchain in your path, \
205         run 'make menuconfig', then select)
206 $(warning the config option: 'General setup', \
207         and 'Allow building with any toolchain')
208 $(warning Note that this is NOT supported.  \
209         Using it means you're on your own.)
210 $(warning )
211 endif #($(CONFIG_ANY_TOOLCHAIN),y)
212 $(error Halting the build)
213 endif #($(COMPILERFAIL),1)
215 endif #($(NOCOMPILE),1)
217 # Run the toolchain version checks if the requested target is 'test-toolchain'
218 # Checks the versions of GCC, binutils, clang, and IASL
219 ifneq ($(MAKECMDGOALS),)
220 ifneq ($(filter test-toolchain,$(MAKECMDGOALS)),)
221 $(foreach arch, $(ARCH_SUPPORTED), \
222         $(if $(shell if [ -n "$(GCC_CC_$(arch))" ]; then \
223                 $(GCC_CC_$(arch)) -v 2>&1 | \
224                 grep -q "$(shell util/crossgcc/buildgcc -s gcc)" || \
225                 echo not-current; fi), \
226                 $(eval COMPILER_OUT_OF_DATE:=1) \
227                 $(warning The coreboot toolchain version of gcc for '$(arch)' \
228                         architecture is not the current version.)) \
229         $(if $(shell if [ -n "$(CLANG_CC_$(arch))" ]; then \
230                 $(CLANG_CC_$(arch)) -v 2>&1 | \
231                 grep -q "$(shell util/crossgcc/buildgcc -s clang)" || \
232                 echo not-current; fi), \
233                 $(eval COMPILER_OUT_OF_DATE:=1)\
234                 $(warning The coreboot toolchain version of clang for \
235                         '$(arch)' architecture is not the current version.)) \
236         $(if $(shell if [ "$(OBJDUMP_$(arch))" != "invalidobjdump" ]; then \
237         $(OBJDUMP_$(arch)) -v 2>&1 | \
238         grep -q "$(shell util/crossgcc/buildgcc -s binutils)" || \
239         echo not-current; fi), \
240                 $(eval COMPILER_OUT_OF_DATE:=1)\
241                 $(warning The coreboot toolchain version of binutils for \
242                         '$(arch)' architecture is not the current version.)) \
244 $(if $(shell if [ -n "$(IASL)" ]; then $(IASL) -v 2>&1 | \
245         grep -q "$(shell util/crossgcc/buildgcc -s iasl)" || \
246         echo not-coreboot; fi), \
247         $(eval COMPILER_OUT_OF_DATE:=1)\
248         $(warning The coreboot toolchain version of iasl \
249                 is not the current version))
250 endif #($(filter crossgcc_check%,$(MAKECMDGOALS)),)
251 endif #($(MAKECMDGOALS),)