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