Introduce stage-specific architecture for coreboot
[coreboot.git] / Makefile
blob56d3a5aae14df2460b545ae8c7e277449d276976
1 ##
2 ## This file is part of the coreboot project.
3 ##
4 ## Copyright (C) 2008 Advanced Micro Devices, Inc.
5 ## Copyright (C) 2008 Uwe Hermann <uwe@hermann-uwe.de>
6 ## Copyright (C) 2009-2010 coresystems GmbH
7 ## Copyright (C) 2011 secunet Security Networks AG
8 ##
9 ## Redistribution and use in source and binary forms, with or without
10 ## modification, are permitted provided that the following conditions
11 ## are met:
12 ## 1. Redistributions of source code must retain the above copyright
13 ## notice, this list of conditions and the following disclaimer.
14 ## 2. Redistributions in binary form must reproduce the above copyright
15 ## notice, this list of conditions and the following disclaimer in the
16 ## documentation and/or other materials provided with the distribution.
17 ## 3. The name of the author may not be used to endorse or promote products
18 ## derived from this software without specific prior written permission.
20 ## THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 ## ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 ## FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 ## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 ## OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 ## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 ## LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 ## OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 ## SUCH DAMAGE.
33 ifeq ($(INNER_SCANBUILD),y)
34 CC_real:=$(CC)
35 endif
37 $(if $(wildcard .xcompile),,$(eval $(shell bash util/xcompile/xcompile $(XGCCPATH) > .xcompile)))
38 include .xcompile
40 ifeq ($(INNER_SCANBUILD),y)
41 CC:=$(CC_real)
42 HOSTCC:=$(CC_real) --hostcc
43 HOSTCXX:=$(CC_real) --hostcxx
44 endif
46 export top := $(CURDIR)
47 export src := src
48 export srck := $(top)/util/kconfig
49 export obj ?= build
50 export objutil ?= $(obj)/util
51 export objk := $(objutil)/kconfig
54 export KCONFIG_AUTOHEADER := $(obj)/config.h
55 export KCONFIG_AUTOCONFIG := $(obj)/auto.conf
57 # directory containing the toplevel Makefile.inc
58 TOPLEVEL := .
60 CONFIG_SHELL := sh
61 KBUILD_DEFCONFIG := configs/defconfig
62 UNAME_RELEASE := $(shell uname -r)
63 DOTCONFIG ?= .config
64 KCONFIG_CONFIG = $(DOTCONFIG)
65 export KCONFIG_CONFIG
66 HAVE_DOTCONFIG := $(wildcard $(DOTCONFIG))
67 MAKEFLAGS += -rR --no-print-directory
69 # Make is silent per default, but 'make V=1' will show all compiler calls.
70 Q:=@
71 ifneq ($(V),1)
72 ifneq ($(Q),)
73 .SILENT:
74 endif
75 endif
77 HOSTCC = gcc
78 HOSTCXX = g++
79 HOSTCFLAGS := -g
80 HOSTCXXFLAGS := -g
82 DOXYGEN := doxygen
83 DOXYGEN_OUTPUT_DIR := doxygen
85 all: real-all
87 # This include must come _before_ the pattern rules below!
88 # Order _does_ matter for pattern rules.
89 include util/kconfig/Makefile
91 # Three cases where we don't need fully populated $(obj) lists:
92 # 1. when no .config exists
93 # 2. when make config (in any flavour) is run
94 # 3. when make distclean is run
95 # Don't waste time on reading all Makefile.incs in these cases
96 ifeq ($(strip $(HAVE_DOTCONFIG)),)
97 NOCOMPILE:=1
98 endif
99 ifneq ($(MAKECMDGOALS),)
100 ifneq ($(filter %config %clean cross%,$(MAKECMDGOALS)),)
101 NOCOMPILE:=1
102 endif
103 ifeq ($(MAKECMDGOALS), %clean)
104 NOMKDIR:=1
105 endif
106 endif
108 ifeq ($(NOCOMPILE),1)
109 include $(TOPLEVEL)/Makefile.inc
110 real-all: config
112 else
114 include $(HAVE_DOTCONFIG)
116 include toolchain.inc
118 ifneq ($(INNER_SCANBUILD),y)
119 ifeq ($(CONFIG_COMPILER_LLVM_CLANG),y)
120 CC:=clang -m32 -mno-mmx -mno-sse
121 HOSTCC:=clang
122 endif
123 endif
125 ifeq ($(CONFIG_CCACHE),y)
126 CCACHE:=$(word 1,$(wildcard $(addsuffix /ccache,$(subst :, ,$(PATH)))))
127 ifeq ($(CCACHE),)
128 $(error ccache selected, but not found in PATH)
129 endif
130 CCACHE:=CCACHE_COMPILERCHECK=content CCACHE_BASEDIR=$(top) $(CCACHE)
131 CC := $(CCACHE) $(CC)
132 HOSTCC := $(CCACHE) $(HOSTCC)
133 HOSTCXX := $(CCACHE) $(HOSTCXX)
134 ROMCC := $(CCACHE) $(ROMCC)
135 endif
137 strip_quotes = $(subst ",,$(subst \",,$(1)))
139 # The primary target needs to be here before we include the
140 # other files
142 ifeq ($(INNER_SCANBUILD),y)
143 CONFIG_SCANBUILD_ENABLE:=
144 endif
146 ifeq ($(CONFIG_SCANBUILD_ENABLE),y)
147 ifneq ($(CONFIG_SCANBUILD_REPORT_LOCATION),)
148 CONFIG_SCANBUILD_REPORT_LOCATION:=-o $(CONFIG_SCANBUILD_REPORT_LOCATION)
149 endif
150 real-all:
151 echo '#!/bin/sh' > .ccwrap
152 echo 'CC="$(CC)"' >> .ccwrap
153 echo 'if [ "$$1" = "--hostcc" ]; then shift; CC="$(HOSTCC)"; fi' >> .ccwrap
154 echo 'if [ "$$1" = "--hostcxx" ]; then shift; CC="$(HOSTCXX)"; fi' >> .ccwrap
155 echo 'eval $$CC $$*' >> .ccwrap
156 chmod +x .ccwrap
157 scan-build $(CONFIG_SCANBUILD_REPORT_LOCATION) -analyze-headers --use-cc=$(top)/.ccwrap --use-c++=$(top)/.ccwrap $(MAKE) INNER_SCANBUILD=y
158 else
159 real-all: real-target
160 endif
162 # must come rather early
163 .SECONDEXPANSION:
165 $(obj)/config.h:
166 $(MAKE) oldconfig
168 # Add a new class of source/object files to the build system
169 add-class= \
170 $(eval $(1)-srcs:=) \
171 $(eval $(1)-objs:=) \
172 $(eval classes+=$(1))
174 # Special classes are managed types with special behaviour
175 # On parse time, for each entry in variable $(1)-y
176 # a handler $(1)-handler is executed with the arguments:
177 # * $(1): directory the parser is in
178 # * $(2): current entry
179 add-special-class= \
180 $(eval $(1):=) \
181 $(eval special-classes+=$(1))
183 # Clean -y variables, include Makefile.inc
184 # Add paths to files in X-y to X-srcs
185 # Add subdirs-y to subdirs
186 includemakefiles= \
187 $(foreach class,classes subdirs $(classes) $(special-classes), $(eval $(class)-y:=)) \
188 $(eval -include $(1)) \
189 $(foreach class,$(classes-y), $(call add-class,$(class))) \
190 $(foreach class,$(classes), \
191 $(eval $(class)-srcs+= \
192 $$(subst $(top)/,, \
193 $$(abspath $$(subst $(dir $(1))/,/,$$(addprefix $(dir $(1)),$$($(class)-y))))))) \
194 $(foreach special,$(special-classes), \
195 $(foreach item,$($(special)-y), $(call $(special)-handler,$(dir $(1)),$(item)))) \
196 $(eval subdirs+=$$(subst $(CURDIR)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
198 # For each path in $(subdirs) call includemakefiles
199 # Repeat until subdirs is empty
200 evaluate_subdirs= \
201 $(eval cursubdirs:=$(subdirs)) \
202 $(eval subdirs:=) \
203 $(foreach dir,$(cursubdirs), \
204 $(eval $(call includemakefiles,$(dir)/Makefile.inc))) \
205 $(if $(subdirs),$(eval $(call evaluate_subdirs)))
207 # collect all object files eligible for building
208 subdirs:=$(TOPLEVEL)
209 $(eval $(call evaluate_subdirs))
210 ifeq ($(FAILBUILD),1)
211 $(error cannot continue build)
212 endif
214 # Eliminate duplicate mentions of source files in a class
215 $(foreach class,$(classes),$(eval $(class)-srcs:=$(sort $($(class)-srcs))))
217 src-to-obj=$(addsuffix .$(1).o, $(basename $(patsubst src/%, $(obj)/%, $($(1)-srcs))))
218 $(foreach class,$(classes),$(eval $(class)-objs:=$(call src-to-obj,$(class))))
220 # Save all objs before processing them (for dependency inclusion)
221 originalobjs:=$(foreach var, $(addsuffix -objs,$(classes)), $($(var)))
223 # Call post-processors if they're defined
224 $(foreach class,$(classes),\
225 $(if $(value $(class)-postprocess),$(eval $(call $(class)-postprocess,$($(class)-objs)))))
227 allsrcs:=$(foreach var, $(addsuffix -srcs,$(classes)), $($(var)))
228 allobjs:=$(foreach var, $(addsuffix -objs,$(classes)), $($(var)))
229 alldirs:=$(sort $(abspath $(dir $(allobjs))))
231 # macro to define template macros that are used by use_template macro
232 define create_cc_template
233 # $1 obj class
234 # $2 source suffix (c, S)
235 # $3 additional compiler flags
236 # $4 additional dependencies
237 ifn$(EMPTY)def $(1)-objs_$(2)_template
238 de$(EMPTY)fine $(1)-objs_$(2)_template
239 $(obj)/$$(1).$(1).o: src/$$(1).$(2) $(obj)/config.h $(4)
240 @printf " CC $$$$(subst $$$$(obj)/,,$$$$(@))\n"
241 $(CC_$(1)) $(3) -MMD $$$$(CFLAGS_$(1)) -c -o $$$$@ $$$$<
242 en$(EMPTY)def
243 end$(EMPTY)if
244 endef
246 filetypes-of-class=$(subst .,,$(sort $(suffix $($(1)-srcs))))
247 $(foreach class,$(classes), \
248 $(foreach type,$(call filetypes-of-class,$(class)), \
249 $(eval $(call create_cc_template,$(class),$(type),$($(class)-$(type)-ccopts),$($(class)-$(type)-deps)))))
251 foreach-src=$(foreach file,$($(1)-srcs),$(eval $(call $(1)-objs_$(subst .,,$(suffix $(file)))_template,$(subst src/,,$(basename $(file))))))
252 $(eval $(foreach class,$(classes),$(call foreach-src,$(class))))
254 DEPENDENCIES = $(originalobjs:.o=.d)
255 -include $(DEPENDENCIES)
257 printall:
258 @$(foreach class,$(classes),echo $(class)-objs:=$($(class)-objs); )
259 @echo alldirs:=$(alldirs)
260 @echo allsrcs=$(allsrcs)
261 @echo DEPENDENCIES=$(DEPENDENCIES)
262 @echo LIBGCC_FILE_NAME=$(LIBGCC_FILE_NAME_$(class))
263 @$(foreach class,$(special-classes),echo $(class):='$($(class))'; )
265 endif
267 ifndef NOMKDIR
268 $(shell mkdir -p $(obj) $(objutil)/kconfig/lxdialog $(additional-dirs) $(alldirs))
269 endif
271 cscope:
272 cscope -bR
274 doxy: doxygen
275 doxygen:
276 $(DOXYGEN) documentation/Doxyfile.coreboot
278 doxyclean: doxygen-clean
279 doxygen-clean:
280 rm -rf $(DOXYGEN_OUTPUT_DIR)
282 clean-for-update: doxygen-clean clean-for-update-target
283 rm -rf $(obj) .xcompile
285 clean: clean-for-update clean-target
286 rm -f .ccwrap
288 clean-cscope:
289 rm -f cscope.out
291 distclean: clean
292 rm -f .config .config.old ..config.tmp .kconfig.d .tmpconfig* .ccwrap .xcompile
294 .PHONY: $(PHONY) clean clean-for-update clean-cscope cscope distclean doxygen doxy .xcompile