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