blob: b976baa1d1e7d08b5dced15ac37f25a19a417735 [file] [log] [blame]
Jordan Crousef6145c32008-03-19 23:56:58 +00001##
Patrick Georgib3db79e2011-04-21 18:48:50 +02002## This file is part of the coreboot project.
Jordan Crousef6145c32008-03-19 23:56:58 +00003##
4## Copyright (C) 2008 Advanced Micro Devices, Inc.
Uwe Hermanne55b32a2008-08-08 07:56:07 +00005## Copyright (C) 2008 Uwe Hermann <uwe@hermann-uwe.de>
Patrick Georgib3db79e2011-04-21 18:48:50 +02006## Copyright (C) 2009-2010 coresystems GmbH
7## Copyright (C) 2011 secunet Security Networks AG
Jordan Crousef6145c32008-03-19 23:56:58 +00008##
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
Patrick Georgib3db79e2011-04-21 18:48:50 +020033ifeq ($(INNER_SCANBUILD),y)
34CC_real:=$(CC)
Jordan Crousef6145c32008-03-19 23:56:58 +000035endif
36
Stefan Reinauer97f546c2010-04-20 17:19:20 +000037$(if $(wildcard .xcompile),,$(eval $(shell bash util/xcompile/xcompile > .xcompile)))
38include .xcompile
39
Patrick Georgib3db79e2011-04-21 18:48:50 +020040ifeq ($(INNER_SCANBUILD),y)
41CC:=$(CC_real)
42HOSTCC:=$(CC_real) --hostcc
43HOSTCXX:=$(CC_real) --hostcxx
44endif
45
46export top := $(CURDIR)
47export src := src
48export srck := $(top)/util/kconfig
49export obj ?= build
50export objutil ?= $(obj)/util
51export objk := $(objutil)/kconfig
52
53
54export KCONFIG_AUTOHEADER := $(obj)/config.h
55export KCONFIG_AUTOCONFIG := $(obj)/auto.conf
56
57# directory containing the toplevel Makefile.inc
58TOPLEVEL := .
59
60CONFIG_SHELL := sh
61KBUILD_DEFCONFIG := configs/defconfig
62UNAME_RELEASE := $(shell uname -r)
63DOTCONFIG ?= .config
64KCONFIG_CONFIG = $(DOTCONFIG)
65export KCONFIG_CONFIG
66HAVE_DOTCONFIG := $(wildcard $(DOTCONFIG))
67MAKEFLAGS += -rR --no-print-directory
68
69# Make is silent per default, but 'make V=1' will show all compiler calls.
70Q:=@
71ifneq ($(V),1)
72ifneq ($(Q),)
73.SILENT:
74endif
75endif
76
77CPP:= $(CC) -x assembler-with-cpp -DASSEMBLY -E
Uwe Hermanne55b32a2008-08-08 07:56:07 +000078HOSTCC = gcc
79HOSTCXX = g++
Patrick Georgib3db79e2011-04-21 18:48:50 +020080HOSTCFLAGS := -I$(srck) -I$(objk) -g
81HOSTCXXFLAGS := -I$(srck) -I$(objk)
82LIBGCC_FILE_NAME := $(shell test -r `$(CC) -print-libgcc-file-name` && $(CC) -print-libgcc-file-name)
Jordan Crousef6145c32008-03-19 23:56:58 +000083
Uwe Hermann3b641292008-08-27 12:53:47 +000084DOXYGEN := doxygen
85DOXYGEN_OUTPUT_DIR := doxygen
86
Patrick Georgib3db79e2011-04-21 18:48:50 +020087all: real-all
Jordan Crousec3e728f2008-04-09 23:05:59 +000088
Uwe Hermannea872452008-10-22 15:49:20 +000089# This include must come _before_ the pattern rules below!
90# Order _does_ matter for pattern rules.
91include util/kconfig/Makefile
92
Patrick Georgib3db79e2011-04-21 18:48:50 +020093# Three cases where we don't need fully populated $(obj) lists:
94# 1. when no .config exists
95# 2. when make config (in any flavour) is run
96# 3. when make distclean is run
97# Don't waste time on reading all Makefile.incs in these cases
Uwe Hermannea872452008-10-22 15:49:20 +000098ifeq ($(strip $(HAVE_DOTCONFIG)),)
Patrick Georgib3db79e2011-04-21 18:48:50 +020099NOCOMPILE:=1
100endif
101ifneq ($(MAKECMDGOALS),)
102ifneq ($(filter %config distclean,$(MAKECMDGOALS)),)
103NOCOMPILE:=1
104endif
105endif
106
107ifeq ($(NOCOMPILE),1)
108include $(TOPLEVEL)/Makefile.inc
109real-all: config
110
Uwe Hermannea872452008-10-22 15:49:20 +0000111else
112
Patrick Georgib3db79e2011-04-21 18:48:50 +0200113include $(HAVE_DOTCONFIG)
Uwe Hermannea872452008-10-22 15:49:20 +0000114
Patrick Georgib3db79e2011-04-21 18:48:50 +0200115ifneq ($(INNER_SCANBUILD),y)
116ifeq ($(CONFIG_COMPILER_LLVM_CLANG),y)
117CC:=clang -m32
118HOSTCC:=clang
119endif
120endif
121
122ifeq ($(CONFIG_CCACHE),y)
123CCACHE:=$(word 1,$(wildcard $(addsuffix /ccache,$(subst :, ,$(PATH)))))
124ifeq ($(CCACHE),)
125$(error ccache selected, but not found in PATH)
126endif
127CCACHE:=CCACHE_COMPILERCHECK=content CCACHE_BASEDIR=$(top) $(CCACHE)
128CC := $(CCACHE) $(CC)
129HOSTCC := $(CCACHE) $(HOSTCC)
130HOSTCXX := $(CCACHE) $(HOSTCXX)
131ROMCC := $(CCACHE) $(ROMCC)
132endif
133
134strip_quotes = $(subst ",,$(subst \",,$(1)))
135
136# The primary target needs to be here before we include the
137# other files
138
139ifeq ($(INNER_SCANBUILD),y)
140CONFIG_SCANBUILD_ENABLE:=
141endif
142
143ifeq ($(CONFIG_SCANBUILD_ENABLE),y)
144ifneq ($(CONFIG_SCANBUILD_REPORT_LOCATION),)
145CONFIG_SCANBUILD_REPORT_LOCATION:=-o $(CONFIG_SCANBUILD_REPORT_LOCATION)
146endif
147real-all:
148 echo '#!/bin/sh' > .ccwrap
149 echo 'CC="$(CC)"' >> .ccwrap
150 echo 'if [ "$$1" = "--hostcc" ]; then shift; CC="$(HOSTCC)"; fi' >> .ccwrap
151 echo 'if [ "$$1" = "--hostcxx" ]; then shift; CC="$(HOSTCXX)"; fi' >> .ccwrap
152 echo 'eval $$CC $$*' >> .ccwrap
153 chmod +x .ccwrap
154 scan-build $(CONFIG_SCANBUILD_REPORT_LOCATION) -analyze-headers --use-cc=$(top)/.ccwrap --use-c++=$(top)/.ccwrap $(MAKE) INNER_SCANBUILD=y
155else
156real-all: real-target
157endif
158
159# must come rather early
160.SECONDEXPANSION:
161
162$(obj)/config.h:
163 $(MAKE) oldconfig
164
165# Add a new class of source/object files to the build system
166add-class= \
167 $(eval $(1)-srcs:=) \
168 $(eval $(1)-objs:=) \
169 $(eval classes+=$(1))
170
171# Special classes are managed types with special behaviour
172# On parse time, for each entry in variable $(1)-y
173# a handler $(1)-handler is executed with the arguments:
174# * $(1): directory the parser is in
175# * $(2): current entry
176add-special-class= \
177 $(eval $(1):=) \
178 $(eval special-classes+=$(1))
179
180# Clean -y variables, include Makefile.inc
181# Add paths to files in X-y to X-srcs
182# Add subdirs-y to subdirs
183includemakefiles= \
184 $(foreach class,classes subdirs $(classes) $(special-classes), $(eval $(class)-y:=)) \
185 $(eval -include $(1)) \
186 $(foreach class,$(classes-y), $(call add-class,$(class))) \
187 $(foreach class,$(classes), \
188 $(eval $(class)-srcs+= \
189 $$(subst $(top)/,, \
190 $$(abspath $$(addprefix $(dir $(1)),$$($(class)-y)))))) \
191 $(foreach special,$(special-classes), \
192 $(foreach item,$($(special)-y), $(call $(special)-handler,$(dir $(1)),$(item)))) \
193 $(eval subdirs+=$$(subst $(CURDIR)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
194
195# For each path in $(subdirs) call includemakefiles
196# Repeat until subdirs is empty
197evaluate_subdirs= \
198 $(eval cursubdirs:=$(subdirs)) \
199 $(eval subdirs:=) \
200 $(foreach dir,$(cursubdirs), \
201 $(eval $(call includemakefiles,$(dir)/Makefile.inc))) \
202 $(if $(subdirs),$(eval $(call evaluate_subdirs)))
203
204# collect all object files eligible for building
205subdirs:=$(TOPLEVEL)
206$(eval $(call evaluate_subdirs))
207
208src-to-obj=$(addsuffix .$(1).o, $(basename $(addprefix $(obj)/, $($(1)-srcs))))
209$(foreach class,$(classes),$(eval $(class)-objs:=$(call src-to-obj,$(class))))
210
211allsrcs:=$(foreach var, $(addsuffix -srcs,$(classes)), $($(var)))
212allobjs:=$(foreach var, $(addsuffix -objs,$(classes)), $($(var)))
213alldirs:=$(sort $(abspath $(dir $(allobjs))))
214
215# macro to define template macros that are used by use_template macro
216define create_cc_template
217# $1 obj class
218# $2 source suffix (c, S)
219# $3 additional compiler flags
220# $4 additional dependencies
221ifn$(EMPTY)def $(1)-objs_$(2)_template
222de$(EMPTY)fine $(1)-objs_$(2)_template
223$(obj)/$$(1).$(1).o: $$(1).$(2) $(obj)/config.h $(4)
224 @printf " CC $$$$(subst $$$$(obj)/,,$$$$(@))\n"
225 $(CC) $(3) -MMD $$$$(CFLAGS) -c -o $$$$@ $$$$<
226en$(EMPTY)def
227end$(EMPTY)if
228endef
229
230filetypes-of-class=$(subst .,,$(sort $(suffix $($(1)-srcs))))
231$(foreach class,$(classes), \
232 $(foreach type,$(call filetypes-of-class,$(class)), \
233 $(eval $(call create_cc_template,$(class),$(type),$($(class)-$(type)-ccopts),$($(class)-$(type)-deps)))))
234
235foreach-src=$(foreach file,$($(1)-srcs),$(eval $(call $(1)-objs_$(subst .,,$(suffix $(file)))_template,$(basename $(file)))))
236$(eval $(foreach class,$(classes),$(call foreach-src,$(class))))
237
238DEPENDENCIES = $(allobjs:.o=.d)
239-include $(DEPENDENCIES)
240
241printall:
242 @$(foreach class,$(classes),echo $(class)-objs:=$($(class)-objs); )
243 @echo alldirs:=$(alldirs)
244 @echo allsrcs=$(allsrcs)
245 @echo DEPENDENCIES=$(DEPENDENCIES)
246 @echo LIBGCC_FILE_NAME=$(LIBGCC_FILE_NAME)
247 @$(foreach class,$(special-classes),echo $(class):='$($(class))'; )
Uwe Hermannea872452008-10-22 15:49:20 +0000248
249endif
Jordan Crousef6145c32008-03-19 23:56:58 +0000250
Patrick Georgib3db79e2011-04-21 18:48:50 +0200251$(shell mkdir -p $(obj) $(objutil)/kconfig/lxdialog $(additional-dirs) $(alldirs))
Jordan Crousef6145c32008-03-19 23:56:58 +0000252
Patrick Georgib3db79e2011-04-21 18:48:50 +0200253cscope:
254 cscope -bR
255
256doxy: doxygen
257doxygen:
258 $(DOXYGEN) Doxyfile
259
260doxyclean: doxygen-clean
261doxygen-clean:
262 rm -rf $(DOXYGEN_OUTPUT_DIR)
263
264clean-for-update: doxygen-clean clean-for-update-target
265 rm -f $(allobjs) .xcompile
266 rm -f $(DEPENDENCIES)
267 rmdir -p $(alldirs) 2>/dev/null >/dev/null || true
268
269clean: clean-for-update clean-target
270 rm -f .ccwrap
271
272clean-cscope:
273 rm -f cscope.out
274
275distclean: clean-cscope
276 rm -rf $(obj)
277 rm -f .config .config.old ..config.tmp .kconfig.d .tmpconfig* .ccwrap .xcompile
278
279.PHONY: $(PHONY) clean clean-cscope cscope distclean doxygen doxy .xcompile