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