44a1d63198077dede020918fedc52e1bb3da00cb
[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 ## 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.
19 ##
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.
31 ##
32
33 ifeq ($(INNER_SCANBUILD),y)
34 CC_real:=$(CC)
35 endif
36
37 $(if $(wildcard .xcompile),,$(eval $(shell bash util/xcompile/xcompile > .xcompile)))
38 include .xcompile
39
40 ifeq ($(INNER_SCANBUILD),y)
41 CC:=$(CC_real)
42 HOSTCC:=$(CC_real) --hostcc
43 HOSTCXX:=$(CC_real) --hostcxx
44 endif
45
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
52
53
54 export KCONFIG_AUTOHEADER := $(obj)/config.h
55 export KCONFIG_AUTOCONFIG := $(obj)/auto.conf
56
57 # directory containing the toplevel Makefile.inc
58 TOPLEVEL := .
59
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
68
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
76
77 HOSTCC = gcc
78 HOSTCXX = g++
79 HOSTCFLAGS := -I$(srck) -I$(objk) -g
80 HOSTCXXFLAGS := -I$(srck) -I$(objk)
81 LIBGCC_FILE_NAME := $(shell test -r `$(CC) -print-libgcc-file-name` && $(CC) -print-libgcc-file-name)
82
83 DOXYGEN := doxygen
84 DOXYGEN_OUTPUT_DIR := doxygen
85
86 all: real-all
87
88 # This include must come _before_ the pattern rules below!
89 # Order _does_ matter for pattern rules.
90 include util/kconfig/Makefile
91
92 # Three cases where we don't need fully populated $(obj) lists:
93 # 1. when no .config exists
94 # 2. when make config (in any flavour) is run
95 # 3. when make distclean is run
96 # Don't waste time on reading all Makefile.incs in these cases
97 ifeq ($(strip $(HAVE_DOTCONFIG)),)
98 NOCOMPILE:=1
99 endif
100 ifneq ($(MAKECMDGOALS),)
101 ifneq ($(filter %config distclean,$(MAKECMDGOALS)),)
102 NOCOMPILE:=1
103 endif
104 endif
105
106 ifeq ($(NOCOMPILE),1)
107 include $(TOPLEVEL)/Makefile.inc
108 real-all: config
109
110 else
111
112 include $(HAVE_DOTCONFIG)
113
114 ifneq ($(INNER_SCANBUILD),y)
115 ifeq ($(CONFIG_COMPILER_LLVM_CLANG),y)
116 CC:=clang -m32
117 HOSTCC:=clang
118 endif
119 endif
120
121 ifeq ($(CONFIG_CCACHE),y)
122 CCACHE:=$(word 1,$(wildcard $(addsuffix /ccache,$(subst :, ,$(PATH)))))
123 ifeq ($(CCACHE),)
124 $(error ccache selected, but not found in PATH)
125 endif
126 CCACHE:=CCACHE_COMPILERCHECK=content CCACHE_BASEDIR=$(top) $(CCACHE)
127 CC := $(CCACHE) $(CC)
128 HOSTCC := $(CCACHE) $(HOSTCC)
129 HOSTCXX := $(CCACHE) $(HOSTCXX)
130 ROMCC := $(CCACHE) $(ROMCC)
131 endif
132
133 strip_quotes = $(subst ",,$(subst \",,$(1)))
134
135 # The primary target needs to be here before we include the
136 # other files
137
138 ifeq ($(INNER_SCANBUILD),y)
139 CONFIG_SCANBUILD_ENABLE:=
140 endif
141
142 ifeq ($(CONFIG_SCANBUILD_ENABLE),y)
143 ifneq ($(CONFIG_SCANBUILD_REPORT_LOCATION),)
144 CONFIG_SCANBUILD_REPORT_LOCATION:=-o $(CONFIG_SCANBUILD_REPORT_LOCATION)
145 endif
146 real-all:
147         echo '#!/bin/sh' > .ccwrap
148         echo 'CC="$(CC)"' >> .ccwrap
149         echo 'if [ "$$1" = "--hostcc" ]; then shift; CC="$(HOSTCC)"; fi' >> .ccwrap
150         echo 'if [ "$$1" = "--hostcxx" ]; then shift; CC="$(HOSTCXX)"; fi' >> .ccwrap
151         echo 'eval $$CC $$*' >> .ccwrap
152         chmod +x .ccwrap
153         scan-build $(CONFIG_SCANBUILD_REPORT_LOCATION) -analyze-headers --use-cc=$(top)/.ccwrap --use-c++=$(top)/.ccwrap $(MAKE) INNER_SCANBUILD=y
154 else
155 real-all: real-target
156 endif
157
158 # must come rather early
159 .SECONDEXPANSION:
160
161 $(obj)/config.h:
162         $(MAKE) oldconfig
163
164 # Add a new class of source/object files to the build system
165 add-class= \
166         $(eval $(1)-srcs:=) \
167         $(eval $(1)-objs:=) \
168         $(eval classes+=$(1))
169
170 # Special classes are managed types with special behaviour
171 # On parse time, for each entry in variable $(1)-y
172 # a handler $(1)-handler is executed with the arguments:
173 # * $(1): directory the parser is in
174 # * $(2): current entry
175 add-special-class= \
176         $(eval $(1):=) \
177         $(eval special-classes+=$(1))
178
179 # Clean -y variables, include Makefile.inc
180 # Add paths to files in X-y to X-srcs
181 # Add subdirs-y to subdirs
182 includemakefiles= \
183         $(foreach class,classes subdirs $(classes) $(special-classes), $(eval $(class)-y:=)) \
184         $(eval -include $(1)) \
185         $(foreach class,$(classes-y), $(call add-class,$(class))) \
186         $(foreach class,$(classes), \
187                 $(eval $(class)-srcs+= \
188                         $$(subst $(top)/,, \
189                         $$(abspath $$(subst $(dir $(1))/,/,$$(addprefix $(dir $(1)),$$($(class)-y))))))) \
190         $(foreach special,$(special-classes), \
191                 $(foreach item,$($(special)-y), $(call $(special)-handler,$(dir $(1)),$(item)))) \
192         $(eval subdirs+=$$(subst $(CURDIR)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
193
194 # For each path in $(subdirs) call includemakefiles
195 # Repeat until subdirs is empty
196 evaluate_subdirs= \
197         $(eval cursubdirs:=$(subdirs)) \
198         $(eval subdirs:=) \
199         $(foreach dir,$(cursubdirs), \
200                 $(eval $(call includemakefiles,$(dir)/Makefile.inc))) \
201         $(if $(subdirs),$(eval $(call evaluate_subdirs)))
202
203 # collect all object files eligible for building
204 subdirs:=$(TOPLEVEL)
205 $(eval $(call evaluate_subdirs))
206
207 src-to-obj=$(addsuffix .$(1).o, $(basename $(patsubst src/%, $(obj)/%, $($(1)-srcs))))
208 $(foreach class,$(classes),$(eval $(class)-objs:=$(call src-to-obj,$(class))))
209
210 allsrcs:=$(foreach var, $(addsuffix -srcs,$(classes)), $($(var)))
211 allobjs:=$(foreach var, $(addsuffix -objs,$(classes)), $($(var)))
212 alldirs:=$(sort $(abspath $(dir $(allobjs))))
213
214 # macro to define template macros that are used by use_template macro
215 define create_cc_template
216 # $1 obj class
217 # $2 source suffix (c, S)
218 # $3 additional compiler flags
219 # $4 additional dependencies
220 ifn$(EMPTY)def $(1)-objs_$(2)_template
221 de$(EMPTY)fine $(1)-objs_$(2)_template
222 $(obj)/$$(1).$(1).o: src/$$(1).$(2) $(obj)/config.h $(4)
223         @printf "    CC         $$$$(subst $$$$(obj)/,,$$$$(@))\n"
224         $(CC) $(3) -MMD $$$$(CFLAGS) -c -o $$$$@ $$$$<
225 en$(EMPTY)def
226 end$(EMPTY)if
227 endef
228
229 filetypes-of-class=$(subst .,,$(sort $(suffix $($(1)-srcs))))
230 $(foreach class,$(classes), \
231         $(foreach type,$(call filetypes-of-class,$(class)), \
232                 $(eval $(call create_cc_template,$(class),$(type),$($(class)-$(type)-ccopts),$($(class)-$(type)-deps)))))
233
234 foreach-src=$(foreach file,$($(1)-srcs),$(eval $(call $(1)-objs_$(subst .,,$(suffix $(file)))_template,$(subst src/,,$(basename $(file))))))
235 $(eval $(foreach class,$(classes),$(call foreach-src,$(class))))
236
237 DEPENDENCIES = $(allobjs:.o=.d)
238 -include $(DEPENDENCIES)
239
240 printall:
241         @$(foreach class,$(classes),echo $(class)-objs:=$($(class)-objs); )
242         @echo alldirs:=$(alldirs)
243         @echo allsrcs=$(allsrcs)
244         @echo DEPENDENCIES=$(DEPENDENCIES)
245         @echo LIBGCC_FILE_NAME=$(LIBGCC_FILE_NAME)
246         @$(foreach class,$(special-classes),echo $(class):='$($(class))'; )
247
248 endif
249
250 ifndef NOMKDIR
251 $(shell mkdir -p $(obj) $(objutil)/kconfig/lxdialog $(additional-dirs) $(alldirs))
252 endif
253
254 cscope:
255         cscope -bR
256
257 doxy: doxygen
258 doxygen:
259         $(DOXYGEN) documentation/Doxyfile.coreboot
260
261 doxyclean: doxygen-clean
262 doxygen-clean:
263         rm -rf $(DOXYGEN_OUTPUT_DIR)
264
265 clean-for-update: doxygen-clean clean-for-update-target
266         rm -f $(allobjs) .xcompile
267         rm -f $(DEPENDENCIES)
268         rmdir -p $(alldirs) 2>/dev/null >/dev/null || true
269
270 clean: clean-for-update clean-target
271         rm -f .ccwrap
272
273 clean-cscope:
274         rm -f cscope.out
275
276 distclean: clean-cscope
277         rm -rf $(obj)
278         rm -f .config .config.old ..config.tmp .kconfig.d .tmpconfig* .ccwrap .xcompile
279
280 .PHONY: $(PHONY) clean clean-cscope cscope distclean doxygen doxy .xcompile