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