06847b61a0b0818dfa1a6cdab366f51de9fe19af
[coreboot.git] / Makefile
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 ## This program is free software; you can redistribute it and/or modify
10 ## it under the terms of the GNU General Public License as published by
11 ## the Free Software Foundation; version 2 of the License.
12 ##
13 ## This program is distributed in the hope that it will be useful,
14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ## GNU General Public License for more details.
17 ##
18 ## You should have received a copy of the GNU General Public License
19 ## along with this program; if not, write to the Free Software
20 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21 ##
22
23 ifeq ($(INNER_SCANBUILD),y)
24 CC_real:=$(CC)
25 endif
26
27 $(if $(wildcard .xcompile),,$(eval $(shell bash util/xcompile/xcompile > .xcompile)))
28 include .xcompile
29
30 ifeq ($(INNER_SCANBUILD),y)
31 CC:=$(CC_real)
32 HOSTCC:=$(CC_real) --hostcc
33 HOSTCXX:=$(CC_real) --hostcxx
34 endif
35
36 export top := $(CURDIR)
37 export src := src
38 export srck := $(top)/util/kconfig
39 export obj ?= build
40 export objutil ?= $(obj)/util
41 export objk := $(objutil)/kconfig
42
43
44 export KCONFIG_AUTOHEADER := $(obj)/config.h
45 export KCONFIG_AUTOCONFIG := $(obj)/auto.conf
46
47 # directory containing the toplevel Makefile.inc
48 TOPLEVEL := .
49
50 CONFIG_SHELL := sh
51 KBUILD_DEFCONFIG := configs/defconfig
52 UNAME_RELEASE := $(shell uname -r)
53 DOTCONFIG ?= .config
54 KCONFIG_CONFIG = $(DOTCONFIG)
55 export KCONFIG_CONFIG
56 HAVE_DOTCONFIG := $(wildcard $(DOTCONFIG))
57 MAKEFLAGS += -rR --no-print-directory
58
59 # Make is silent per default, but 'make V=1' will show all compiler calls.
60 Q:=@
61 ifneq ($(V),1)
62 ifneq ($(Q),)
63 .SILENT:
64 endif
65 endif
66
67 CPP:= $(CC) -x assembler-with-cpp -DASSEMBLY -E
68 HOSTCC = gcc
69 HOSTCXX = g++
70 HOSTCFLAGS := -I$(srck) -I$(objk) -g
71 HOSTCXXFLAGS := -I$(srck) -I$(objk)
72 LIBGCC_FILE_NAME := $(shell test -r `$(CC) -print-libgcc-file-name` && $(CC) -print-libgcc-file-name)
73
74 DOXYGEN := doxygen
75 DOXYGEN_OUTPUT_DIR := doxygen
76
77 all: real-all
78
79 # This include must come _before_ the pattern rules below!
80 # Order _does_ matter for pattern rules.
81 include util/kconfig/Makefile
82
83 # Three cases where we don't need fully populated $(obj) lists:
84 # 1. when no .config exists
85 # 2. when make config (in any flavour) is run
86 # 3. when make distclean is run
87 # Don't waste time on reading all Makefile.incs in these cases
88 ifeq ($(strip $(HAVE_DOTCONFIG)),)
89 NOCOMPILE:=1
90 endif
91 ifneq ($(MAKECMDGOALS),)
92 ifneq ($(filter %config distclean,$(MAKECMDGOALS)),)
93 NOCOMPILE:=1
94 endif
95 endif
96
97 ifeq ($(NOCOMPILE),1)
98 include $(TOPLEVEL)/Makefile.inc
99 real-all: config
100
101 else
102
103 include $(HAVE_DOTCONFIG)
104
105 ifneq ($(INNER_SCANBUILD),y)
106 ifeq ($(CONFIG_COMPILER_LLVM_CLANG),y)
107 CC:=clang -m32
108 HOSTCC:=clang
109 endif
110 endif
111
112 ifeq ($(CONFIG_CCACHE),y)
113 CCACHE:=$(wildcard $(addsuffix /ccache,$(subst :, ,$(PATH))))
114 ifeq ($(CCACHE),)
115 $(error ccache selected, but not found in PATH)
116 endif
117 CCACHE:=CCACHE_COMPILERCHECK=content CCACHE_BASEDIR=$(top) $(CCACHE)
118 CC := $(CCACHE) $(CC)
119 HOSTCC := $(CCACHE) $(HOSTCC)
120 HOSTCXX := $(CCACHE) $(HOSTCXX)
121 ROMCC := $(CCACHE) $(ROMCC)
122 endif
123
124 strip_quotes = $(subst ",,$(subst \",,$(1)))
125
126 # The primary target needs to be here before we include the
127 # other files
128
129 ifeq ($(INNER_SCANBUILD),y)
130 CONFIG_SCANBUILD_ENABLE:=
131 endif
132
133 ifeq ($(CONFIG_SCANBUILD_ENABLE),y)
134 ifneq ($(CONFIG_SCANBUILD_REPORT_LOCATION),)
135 CONFIG_SCANBUILD_REPORT_LOCATION:=-o $(CONFIG_SCANBUILD_REPORT_LOCATION)
136 endif
137 real-all:
138         echo '#!/bin/sh' > .ccwrap
139         echo 'CC="$(CC)"' >> .ccwrap
140         echo 'if [ "$$1" = "--hostcc" ]; then shift; CC="$(HOSTCC)"; fi' >> .ccwrap
141         echo 'if [ "$$1" = "--hostcxx" ]; then shift; CC="$(HOSTCXX)"; fi' >> .ccwrap
142         echo 'eval $$CC $$*' >> .ccwrap
143         chmod +x .ccwrap
144         scan-build $(CONFIG_SCANBUILD_REPORT_LOCATION) -analyze-headers --use-cc=$(top)/.ccwrap --use-c++=$(top)/.ccwrap $(MAKE) INNER_SCANBUILD=y
145 else
146 real-all: real-target
147 endif
148
149 # must come rather early
150 .SECONDEXPANSION:
151
152 $(obj)/config.h:
153         $(MAKE) oldconfig
154
155 # Add a new class of source/object files to the build system
156 add-class= \
157         $(eval $(1)-srcs:=) \
158         $(eval $(1)-objs:=) \
159         $(eval classes+=$(1))
160
161 # Special classes are managed types with special behaviour
162 # On parse time, for each entry in variable $(1)-y
163 # a handler $(1)-handler is executed with the arguments:
164 # * $(1): directory the parser is in
165 # * $(2): current entry
166 add-special-class= \
167         $(eval $(1):=) \
168         $(eval special-classes+=$(1))
169
170 # Clean -y variables, include Makefile.inc
171 # Add paths to files in X-y to X-srcs
172 # Add subdirs-y to subdirs
173 includemakefiles= \
174         $(foreach class,classes subdirs $(classes) $(special-classes), $(eval $(class)-y:=)) \
175         $(eval -include $(1)) \
176         $(foreach class,$(classes-y), $(call add-class,$(class))) \
177         $(foreach class,$(classes), \
178                 $(eval $(class)-srcs+= \
179                         $$(subst $(top)/,, \
180                         $$(abspath $$(addprefix $(dir $(1)),$$($(class)-y)))))) \
181         $(foreach special,$(special-classes), \
182                 $(foreach item,$($(special)-y), $(call $(special)-handler,$(dir $(1)),$(item)))) \
183         $(eval subdirs+=$$(subst $(CURDIR)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
184
185 # For each path in $(subdirs) call includemakefiles
186 # Repeat until subdirs is empty
187 evaluate_subdirs= \
188         $(eval cursubdirs:=$(subdirs)) \
189         $(eval subdirs:=) \
190         $(foreach dir,$(cursubdirs), \
191                 $(eval $(call includemakefiles,$(dir)/Makefile.inc))) \
192         $(if $(subdirs),$(eval $(call evaluate_subdirs)))
193
194 # collect all object files eligible for building
195 subdirs:=$(TOPLEVEL)
196 $(eval $(call evaluate_subdirs))
197
198 src-to-obj=$(addsuffix .$(1).o, $(basename $(patsubst src/%, $(obj)/%, $($(1)-srcs))))
199 $(foreach class,$(classes),$(eval $(class)-objs:=$(call src-to-obj,$(class))))
200
201 allsrcs:=$(foreach var, $(addsuffix -srcs,$(classes)), $($(var)))
202 allobjs:=$(foreach var, $(addsuffix -objs,$(classes)), $($(var)))
203 alldirs:=$(sort $(abspath $(dir $(allobjs))))
204
205 # macro to define template macros that are used by use_template macro
206 define create_cc_template
207 # $1 obj class
208 # $2 source suffix (c, S)
209 # $3 additional compiler flags
210 # $4 additional dependencies
211 ifn$(EMPTY)def $(1)-objs_$(2)_template
212 de$(EMPTY)fine $(1)-objs_$(2)_template
213 $(obj)/$$(1).$(1).o: src/$$(1).$(2) $(obj)/config.h $(4)
214         @printf "    CC         $$$$(subst $$$$(obj)/,,$$$$(@))\n"
215         $(CC) $(3) -MMD $$$$(CFLAGS) -c -o $$$$@ $$$$<
216 en$(EMPTY)def
217 end$(EMPTY)if
218 endef
219
220 filetypes-of-class=$(subst .,,$(sort $(suffix $($(1)-srcs))))
221 $(foreach class,$(classes), \
222         $(foreach type,$(call filetypes-of-class,$(class)), \
223                 $(eval $(call create_cc_template,$(class),$(type),$($(class)-$(type)-ccopts),$($(class)-$(type)-deps)))))
224
225 foreach-src=$(foreach file,$($(1)-srcs),$(eval $(call $(1)-objs_$(subst .,,$(suffix $(file)))_template,$(subst src/,,$(basename $(file))))))
226 $(eval $(foreach class,$(classes),$(call foreach-src,$(class))))
227
228 DEPENDENCIES = $(allobjs:.o=.d)
229 -include $(DEPENDENCIES)
230
231 printall:
232         @$(foreach class,$(classes),echo $(class)-objs:=$($(class)-objs); )
233         @echo alldirs:=$(alldirs)
234         @echo allsrcs=$(allsrcs)
235         @echo DEPENDENCIES=$(DEPENDENCIES)
236         @echo LIBGCC_FILE_NAME=$(LIBGCC_FILE_NAME)
237         @$(foreach class,$(special-classes),echo $(class):='$($(class))'; )
238
239 endif
240
241 $(shell mkdir -p $(obj) $(objutil)/kconfig/lxdialog $(additional-dirs) $(alldirs))
242
243 cscope:
244         cscope -bR
245
246 doxy: doxygen
247 doxygen:
248         $(DOXYGEN) documentation/Doxyfile.coreboot
249
250 doxyclean: doxygen-clean
251 doxygen-clean:
252         rm -rf $(DOXYGEN_OUTPUT_DIR)
253
254 clean-for-update: doxygen-clean clean-for-update-target
255         rm -f $(allobjs) .xcompile
256         rm -f $(DEPENDENCIES)
257         rmdir -p $(alldirs) 2>/dev/null >/dev/null || true
258
259 clean: clean-for-update clean-target
260         rm -f .ccwrap
261
262 clean-cscope:
263         rm -f cscope.out
264
265 distclean: clean-cscope
266         rm -rf $(obj)
267         rm -f .config .config.old ..config.tmp .kconfig.d .tmpconfig* .ccwrap .xcompile
268
269 .PHONY: $(PHONY) clean clean-cscope cscope distclean doxygen doxy .xcompile