blob: 1097ce50141c601f8a9a1801ac5fe0971d27f67d [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
Nico Huber08c70182014-10-14 23:27:50 +020051export objk := $(objutil)/lp_kconfig
Patrick Georgib3db79e2011-04-21 18:48:50 +020052
53
54export KCONFIG_AUTOHEADER := $(obj)/config.h
55export KCONFIG_AUTOCONFIG := $(obj)/auto.conf
56
57# directory containing the toplevel Makefile.inc
58TOPLEVEL := .
59
Gabe Black1ee2c6d2013-08-09 04:27:35 -070060CONFIG_LP_SHELL := sh
Patrick Georgib3db79e2011-04-21 18:48:50 +020061KBUILD_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
Gabe Black51edd542013-09-30 23:00:33 -070094ARCHDIR-$(CONFIG_LP_ARCH_ARM) := arm
Furquan Shaikh8c8c3772014-02-19 11:35:30 -080095ARCHDIR-$(CONFIG_LP_ARCH_ARM64) := arm64
Ionela Voinescuce22c022014-09-24 17:05:33 +010096ARCHDIR-$(CONFIG_LP_ARCH_MIPS) := mips
Gabe Black1ee2c6d2013-08-09 04:27:35 -070097ARCHDIR-$(CONFIG_LP_ARCH_X86) := x86
Stefan Reinauer8af0d032012-12-14 13:05:21 -080098
99ARCH-y := $(ARCHDIR-y)
100
101# If architecture folder name is different from GCC binutils architecture name,
102# override here.
Gabe Black51edd542013-09-30 23:00:33 -0700103ARCH-$(CONFIG_LP_ARCH_ARM) := arm
Furquan Shaikh8c8c3772014-02-19 11:35:30 -0800104ARCH-$(CONFIG_LP_ARCH_ARM64) := arm64
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700105ARCH-$(CONFIG_LP_ARCH_X86) := i386
Ionela Voinescuce22c022014-09-24 17:05:33 +0100106ARCH-$(CONFIG_LP_ARCH_MIPS) := mipsel
Stefan Reinauer8af0d032012-12-14 13:05:21 -0800107
108CC := $(CC_$(ARCH-y))
109AS := $(AS_$(ARCH-y))
110LD := $(LD_$(ARCH-y))
111NM := $(NM_$(ARCH-y))
112OBJCOPY := $(OBJCOPY_$(ARCH-y))
113OBJDUMP := $(OBJDUMP_$(ARCH-y))
114READELF := $(READELF_$(ARCH-y))
115STRIP := $(STRIP_$(ARCH-y))
116AR := $(AR_$(ARCH-y))
117
118CFLAGS += $(CFLAGS_$(ARCH-y))
119
120LIBGCC_FILE_NAME := $(shell test -r `$(CC) -print-libgcc-file-name` && \
121 $(CC) -print-libgcc-file-name)
122
Patrick Georgib3db79e2011-04-21 18:48:50 +0200123# Three cases where we don't need fully populated $(obj) lists:
124# 1. when no .config exists
125# 2. when make config (in any flavour) is run
126# 3. when make distclean is run
127# Don't waste time on reading all Makefile.incs in these cases
Uwe Hermannea872452008-10-22 15:49:20 +0000128ifeq ($(strip $(HAVE_DOTCONFIG)),)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200129NOCOMPILE:=1
130endif
131ifneq ($(MAKECMDGOALS),)
132ifneq ($(filter %config distclean,$(MAKECMDGOALS)),)
133NOCOMPILE:=1
134endif
135endif
136
137ifeq ($(NOCOMPILE),1)
138include $(TOPLEVEL)/Makefile.inc
139real-all: config
140
Uwe Hermannea872452008-10-22 15:49:20 +0000141else
142
Patrick Georgib3db79e2011-04-21 18:48:50 +0200143ifneq ($(INNER_SCANBUILD),y)
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700144ifeq ($(CONFIG_LP_COMPILER_LLVM_CLANG),y)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200145CC:=clang -m32
146HOSTCC:=clang
147endif
148endif
149
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700150ifeq ($(CONFIG_LP_CCACHE),y)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200151CCACHE:=$(word 1,$(wildcard $(addsuffix /ccache,$(subst :, ,$(PATH)))))
152ifeq ($(CCACHE),)
153$(error ccache selected, but not found in PATH)
154endif
155CCACHE:=CCACHE_COMPILERCHECK=content CCACHE_BASEDIR=$(top) $(CCACHE)
156CC := $(CCACHE) $(CC)
157HOSTCC := $(CCACHE) $(HOSTCC)
158HOSTCXX := $(CCACHE) $(HOSTCXX)
159ROMCC := $(CCACHE) $(ROMCC)
160endif
161
162strip_quotes = $(subst ",,$(subst \",,$(1)))
163
164# The primary target needs to be here before we include the
165# other files
166
167ifeq ($(INNER_SCANBUILD),y)
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700168CONFIG_LP_SCANBUILD_ENABLE:=
Patrick Georgib3db79e2011-04-21 18:48:50 +0200169endif
170
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700171ifeq ($(CONFIG_LP_SCANBUILD_ENABLE),y)
172ifneq ($(CONFIG_LP_SCANBUILD_REPORT_LOCATION),)
173CONFIG_LP_SCANBUILD_REPORT_LOCATION:=-o $(CONFIG_LP_SCANBUILD_REPORT_LOCATION)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200174endif
175real-all:
176 echo '#!/bin/sh' > .ccwrap
177 echo 'CC="$(CC)"' >> .ccwrap
178 echo 'if [ "$$1" = "--hostcc" ]; then shift; CC="$(HOSTCC)"; fi' >> .ccwrap
179 echo 'if [ "$$1" = "--hostcxx" ]; then shift; CC="$(HOSTCXX)"; fi' >> .ccwrap
180 echo 'eval $$CC $$*' >> .ccwrap
181 chmod +x .ccwrap
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700182 scan-build $(CONFIG_LP_SCANBUILD_REPORT_LOCATION) -analyze-headers --use-cc=$(top)/.ccwrap --use-c++=$(top)/.ccwrap $(MAKE) INNER_SCANBUILD=y
Patrick Georgib3db79e2011-04-21 18:48:50 +0200183else
184real-all: real-target
185endif
186
187# must come rather early
188.SECONDEXPANSION:
189
190$(obj)/config.h:
191 $(MAKE) oldconfig
192
193# Add a new class of source/object files to the build system
194add-class= \
195 $(eval $(1)-srcs:=) \
196 $(eval $(1)-objs:=) \
197 $(eval classes+=$(1))
198
199# Special classes are managed types with special behaviour
200# On parse time, for each entry in variable $(1)-y
201# a handler $(1)-handler is executed with the arguments:
202# * $(1): directory the parser is in
203# * $(2): current entry
204add-special-class= \
205 $(eval $(1):=) \
206 $(eval special-classes+=$(1))
207
208# Clean -y variables, include Makefile.inc
209# Add paths to files in X-y to X-srcs
210# Add subdirs-y to subdirs
211includemakefiles= \
212 $(foreach class,classes subdirs $(classes) $(special-classes), $(eval $(class)-y:=)) \
213 $(eval -include $(1)) \
214 $(foreach class,$(classes-y), $(call add-class,$(class))) \
215 $(foreach class,$(classes), \
216 $(eval $(class)-srcs+= \
217 $$(subst $(top)/,, \
218 $$(abspath $$(addprefix $(dir $(1)),$$($(class)-y)))))) \
219 $(foreach special,$(special-classes), \
220 $(foreach item,$($(special)-y), $(call $(special)-handler,$(dir $(1)),$(item)))) \
221 $(eval subdirs+=$$(subst $(CURDIR)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
222
223# For each path in $(subdirs) call includemakefiles
224# Repeat until subdirs is empty
225evaluate_subdirs= \
226 $(eval cursubdirs:=$(subdirs)) \
227 $(eval subdirs:=) \
228 $(foreach dir,$(cursubdirs), \
229 $(eval $(call includemakefiles,$(dir)/Makefile.inc))) \
230 $(if $(subdirs),$(eval $(call evaluate_subdirs)))
231
232# collect all object files eligible for building
233subdirs:=$(TOPLEVEL)
234$(eval $(call evaluate_subdirs))
235
236src-to-obj=$(addsuffix .$(1).o, $(basename $(addprefix $(obj)/, $($(1)-srcs))))
237$(foreach class,$(classes),$(eval $(class)-objs:=$(call src-to-obj,$(class))))
238
239allsrcs:=$(foreach var, $(addsuffix -srcs,$(classes)), $($(var)))
240allobjs:=$(foreach var, $(addsuffix -objs,$(classes)), $($(var)))
241alldirs:=$(sort $(abspath $(dir $(allobjs))))
242
243# macro to define template macros that are used by use_template macro
244define create_cc_template
245# $1 obj class
246# $2 source suffix (c, S)
247# $3 additional compiler flags
248# $4 additional dependencies
249ifn$(EMPTY)def $(1)-objs_$(2)_template
250de$(EMPTY)fine $(1)-objs_$(2)_template
Gabe Blackf06111a2013-03-12 15:50:08 -0700251$(obj)/$$(1).$(1).o: $$(1).$(2) $(obj)/libpayload-config.h $(4)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200252 @printf " CC $$$$(subst $$$$(obj)/,,$$$$(@))\n"
253 $(CC) $(3) -MMD $$$$(CFLAGS) -c -o $$$$@ $$$$<
254en$(EMPTY)def
255end$(EMPTY)if
256endef
257
258filetypes-of-class=$(subst .,,$(sort $(suffix $($(1)-srcs))))
259$(foreach class,$(classes), \
260 $(foreach type,$(call filetypes-of-class,$(class)), \
261 $(eval $(call create_cc_template,$(class),$(type),$($(class)-$(type)-ccopts),$($(class)-$(type)-deps)))))
262
263foreach-src=$(foreach file,$($(1)-srcs),$(eval $(call $(1)-objs_$(subst .,,$(suffix $(file)))_template,$(basename $(file)))))
264$(eval $(foreach class,$(classes),$(call foreach-src,$(class))))
265
266DEPENDENCIES = $(allobjs:.o=.d)
267-include $(DEPENDENCIES)
268
269printall:
270 @$(foreach class,$(classes),echo $(class)-objs:=$($(class)-objs); )
271 @echo alldirs:=$(alldirs)
272 @echo allsrcs=$(allsrcs)
273 @echo DEPENDENCIES=$(DEPENDENCIES)
274 @echo LIBGCC_FILE_NAME=$(LIBGCC_FILE_NAME)
275 @$(foreach class,$(special-classes),echo $(class):='$($(class))'; )
Uwe Hermannea872452008-10-22 15:49:20 +0000276
277endif
Jordan Crousef6145c32008-03-19 23:56:58 +0000278
Nico Huber08c70182014-10-14 23:27:50 +0200279$(shell mkdir -p $(obj) $(objk)/lxdialog $(additional-dirs) $(alldirs))
Jordan Crousef6145c32008-03-19 23:56:58 +0000280
Patrick Georgib3db79e2011-04-21 18:48:50 +0200281cscope:
282 cscope -bR
283
284doxy: doxygen
285doxygen:
286 $(DOXYGEN) Doxyfile
287
288doxyclean: doxygen-clean
289doxygen-clean:
290 rm -rf $(DOXYGEN_OUTPUT_DIR)
291
292clean-for-update: doxygen-clean clean-for-update-target
293 rm -f $(allobjs) .xcompile
294 rm -f $(DEPENDENCIES)
295 rmdir -p $(alldirs) 2>/dev/null >/dev/null || true
296
297clean: clean-for-update clean-target
298 rm -f .ccwrap
299
300clean-cscope:
301 rm -f cscope.out
302
303distclean: clean-cscope
304 rm -rf $(obj)
305 rm -f .config .config.old ..config.tmp .kconfig.d .tmpconfig* .ccwrap .xcompile
306
307.PHONY: $(PHONY) clean clean-cscope cscope distclean doxygen doxy .xcompile