blob: bfa1abf4812faf3140a11594284a3b5d7d48a7b5 [file] [log] [blame]
Jordan Crousef6145c32008-03-19 23:56:58 +00001##
Patrick Georgic0458e62011-07-02 00:29:09 +02002## This file is part of the libpayload 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)
Jordan Crousef6145c32008-03-19 23:56:58 +000082
Uwe Hermann3b641292008-08-27 12:53:47 +000083DOXYGEN := doxygen
84DOXYGEN_OUTPUT_DIR := doxygen
85
Patrick Georgib3db79e2011-04-21 18:48:50 +020086all: real-all
Jordan Crousec3e728f2008-04-09 23:05:59 +000087
Uwe Hermannea872452008-10-22 15:49:20 +000088# This include must come _before_ the pattern rules below!
89# Order _does_ matter for pattern rules.
90include util/kconfig/Makefile
91
Stefan Reinauer8af0d032012-12-14 13:05:21 -080092include $(HAVE_DOTCONFIG)
93
94ARCHDIR-$(CONFIG_ARCH_ARMV7) := armv7
95ARCHDIR-$(CONFIG_ARCH_POWERPC) := powerpc
96ARCHDIR-$(CONFIG_ARCH_X86) := x86
97
98ARCH-y := $(ARCHDIR-y)
99
100# If architecture folder name is different from GCC binutils architecture name,
101# override here.
102ARCH-$(CONFIG_ARCH_ARMV7) := armv7
Gabe Black49ff3c52013-01-16 03:18:02 -0800103ARCH-$(CONFIG_ARCH_POWERPC) := powerpc
Stefan Reinauer8af0d032012-12-14 13:05:21 -0800104ARCH-$(CONFIG_ARCH_X86) := i386
105
106CC := $(CC_$(ARCH-y))
107AS := $(AS_$(ARCH-y))
108LD := $(LD_$(ARCH-y))
109NM := $(NM_$(ARCH-y))
110OBJCOPY := $(OBJCOPY_$(ARCH-y))
111OBJDUMP := $(OBJDUMP_$(ARCH-y))
112READELF := $(READELF_$(ARCH-y))
113STRIP := $(STRIP_$(ARCH-y))
114AR := $(AR_$(ARCH-y))
115
116CFLAGS += $(CFLAGS_$(ARCH-y))
117
118LIBGCC_FILE_NAME := $(shell test -r `$(CC) -print-libgcc-file-name` && \
119 $(CC) -print-libgcc-file-name)
120
Patrick Georgib3db79e2011-04-21 18:48:50 +0200121# Three cases where we don't need fully populated $(obj) lists:
122# 1. when no .config exists
123# 2. when make config (in any flavour) is run
124# 3. when make distclean is run
125# Don't waste time on reading all Makefile.incs in these cases
Uwe Hermannea872452008-10-22 15:49:20 +0000126ifeq ($(strip $(HAVE_DOTCONFIG)),)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200127NOCOMPILE:=1
128endif
129ifneq ($(MAKECMDGOALS),)
130ifneq ($(filter %config distclean,$(MAKECMDGOALS)),)
131NOCOMPILE:=1
132endif
133endif
134
135ifeq ($(NOCOMPILE),1)
136include $(TOPLEVEL)/Makefile.inc
137real-all: config
138
Uwe Hermannea872452008-10-22 15:49:20 +0000139else
140
Patrick Georgib3db79e2011-04-21 18:48:50 +0200141ifneq ($(INNER_SCANBUILD),y)
142ifeq ($(CONFIG_COMPILER_LLVM_CLANG),y)
143CC:=clang -m32
144HOSTCC:=clang
145endif
146endif
147
148ifeq ($(CONFIG_CCACHE),y)
149CCACHE:=$(word 1,$(wildcard $(addsuffix /ccache,$(subst :, ,$(PATH)))))
150ifeq ($(CCACHE),)
151$(error ccache selected, but not found in PATH)
152endif
153CCACHE:=CCACHE_COMPILERCHECK=content CCACHE_BASEDIR=$(top) $(CCACHE)
154CC := $(CCACHE) $(CC)
155HOSTCC := $(CCACHE) $(HOSTCC)
156HOSTCXX := $(CCACHE) $(HOSTCXX)
157ROMCC := $(CCACHE) $(ROMCC)
158endif
159
160strip_quotes = $(subst ",,$(subst \",,$(1)))
161
162# The primary target needs to be here before we include the
163# other files
164
165ifeq ($(INNER_SCANBUILD),y)
166CONFIG_SCANBUILD_ENABLE:=
167endif
168
169ifeq ($(CONFIG_SCANBUILD_ENABLE),y)
170ifneq ($(CONFIG_SCANBUILD_REPORT_LOCATION),)
171CONFIG_SCANBUILD_REPORT_LOCATION:=-o $(CONFIG_SCANBUILD_REPORT_LOCATION)
172endif
173real-all:
174 echo '#!/bin/sh' > .ccwrap
175 echo 'CC="$(CC)"' >> .ccwrap
176 echo 'if [ "$$1" = "--hostcc" ]; then shift; CC="$(HOSTCC)"; fi' >> .ccwrap
177 echo 'if [ "$$1" = "--hostcxx" ]; then shift; CC="$(HOSTCXX)"; fi' >> .ccwrap
178 echo 'eval $$CC $$*' >> .ccwrap
179 chmod +x .ccwrap
180 scan-build $(CONFIG_SCANBUILD_REPORT_LOCATION) -analyze-headers --use-cc=$(top)/.ccwrap --use-c++=$(top)/.ccwrap $(MAKE) INNER_SCANBUILD=y
181else
182real-all: real-target
183endif
184
185# must come rather early
186.SECONDEXPANSION:
187
188$(obj)/config.h:
189 $(MAKE) oldconfig
190
191# Add a new class of source/object files to the build system
192add-class= \
193 $(eval $(1)-srcs:=) \
194 $(eval $(1)-objs:=) \
195 $(eval classes+=$(1))
196
197# Special classes are managed types with special behaviour
198# On parse time, for each entry in variable $(1)-y
199# a handler $(1)-handler is executed with the arguments:
200# * $(1): directory the parser is in
201# * $(2): current entry
202add-special-class= \
203 $(eval $(1):=) \
204 $(eval special-classes+=$(1))
205
206# Clean -y variables, include Makefile.inc
207# Add paths to files in X-y to X-srcs
208# Add subdirs-y to subdirs
209includemakefiles= \
210 $(foreach class,classes subdirs $(classes) $(special-classes), $(eval $(class)-y:=)) \
211 $(eval -include $(1)) \
212 $(foreach class,$(classes-y), $(call add-class,$(class))) \
213 $(foreach class,$(classes), \
214 $(eval $(class)-srcs+= \
215 $$(subst $(top)/,, \
216 $$(abspath $$(addprefix $(dir $(1)),$$($(class)-y)))))) \
217 $(foreach special,$(special-classes), \
218 $(foreach item,$($(special)-y), $(call $(special)-handler,$(dir $(1)),$(item)))) \
219 $(eval subdirs+=$$(subst $(CURDIR)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
220
221# For each path in $(subdirs) call includemakefiles
222# Repeat until subdirs is empty
223evaluate_subdirs= \
224 $(eval cursubdirs:=$(subdirs)) \
225 $(eval subdirs:=) \
226 $(foreach dir,$(cursubdirs), \
227 $(eval $(call includemakefiles,$(dir)/Makefile.inc))) \
228 $(if $(subdirs),$(eval $(call evaluate_subdirs)))
229
230# collect all object files eligible for building
231subdirs:=$(TOPLEVEL)
232$(eval $(call evaluate_subdirs))
233
234src-to-obj=$(addsuffix .$(1).o, $(basename $(addprefix $(obj)/, $($(1)-srcs))))
235$(foreach class,$(classes),$(eval $(class)-objs:=$(call src-to-obj,$(class))))
236
237allsrcs:=$(foreach var, $(addsuffix -srcs,$(classes)), $($(var)))
238allobjs:=$(foreach var, $(addsuffix -objs,$(classes)), $($(var)))
239alldirs:=$(sort $(abspath $(dir $(allobjs))))
240
241# macro to define template macros that are used by use_template macro
242define create_cc_template
243# $1 obj class
244# $2 source suffix (c, S)
245# $3 additional compiler flags
246# $4 additional dependencies
247ifn$(EMPTY)def $(1)-objs_$(2)_template
248de$(EMPTY)fine $(1)-objs_$(2)_template
Gabe Blackf06111a2013-03-12 15:50:08 -0700249$(obj)/$$(1).$(1).o: $$(1).$(2) $(obj)/libpayload-config.h $(4)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200250 @printf " CC $$$$(subst $$$$(obj)/,,$$$$(@))\n"
251 $(CC) $(3) -MMD $$$$(CFLAGS) -c -o $$$$@ $$$$<
252en$(EMPTY)def
253end$(EMPTY)if
254endef
255
256filetypes-of-class=$(subst .,,$(sort $(suffix $($(1)-srcs))))
257$(foreach class,$(classes), \
258 $(foreach type,$(call filetypes-of-class,$(class)), \
259 $(eval $(call create_cc_template,$(class),$(type),$($(class)-$(type)-ccopts),$($(class)-$(type)-deps)))))
260
261foreach-src=$(foreach file,$($(1)-srcs),$(eval $(call $(1)-objs_$(subst .,,$(suffix $(file)))_template,$(basename $(file)))))
262$(eval $(foreach class,$(classes),$(call foreach-src,$(class))))
263
264DEPENDENCIES = $(allobjs:.o=.d)
265-include $(DEPENDENCIES)
266
267printall:
268 @$(foreach class,$(classes),echo $(class)-objs:=$($(class)-objs); )
269 @echo alldirs:=$(alldirs)
270 @echo allsrcs=$(allsrcs)
271 @echo DEPENDENCIES=$(DEPENDENCIES)
272 @echo LIBGCC_FILE_NAME=$(LIBGCC_FILE_NAME)
273 @$(foreach class,$(special-classes),echo $(class):='$($(class))'; )
Uwe Hermannea872452008-10-22 15:49:20 +0000274
275endif
Jordan Crousef6145c32008-03-19 23:56:58 +0000276
Patrick Georgib3db79e2011-04-21 18:48:50 +0200277$(shell mkdir -p $(obj) $(objutil)/kconfig/lxdialog $(additional-dirs) $(alldirs))
Jordan Crousef6145c32008-03-19 23:56:58 +0000278
Patrick Georgib3db79e2011-04-21 18:48:50 +0200279cscope:
280 cscope -bR
281
282doxy: doxygen
283doxygen:
284 $(DOXYGEN) Doxyfile
285
286doxyclean: doxygen-clean
287doxygen-clean:
288 rm -rf $(DOXYGEN_OUTPUT_DIR)
289
290clean-for-update: doxygen-clean clean-for-update-target
291 rm -f $(allobjs) .xcompile
292 rm -f $(DEPENDENCIES)
293 rmdir -p $(alldirs) 2>/dev/null >/dev/null || true
294
295clean: clean-for-update clean-target
296 rm -f .ccwrap
297
298clean-cscope:
299 rm -f cscope.out
300
301distclean: clean-cscope
302 rm -rf $(obj)
303 rm -f .config .config.old ..config.tmp .kconfig.d .tmpconfig* .ccwrap .xcompile
304
305.PHONY: $(PHONY) clean clean-cscope cscope distclean doxygen doxy .xcompile