blob: 92efd7362ad6ac705392158ed04c399c33857d34 [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
Patrick Georgidb032552015-07-06 09:05:48 +000037# in addition to the dependency below, create the file if it doesn't exist
38# to silence stupid warnings about a file that would be generated anyway.
39$(if $(wildcard .xcompile),,$(eval $(shell util/xcompile/xcompile $(XGCCPATH) > .xcompile)))
40
41.xcompile: util/xcompile/xcompile
42 $< $(XGCCPATH) > $@.tmp
43 \mv -f $@.tmp $@ 2> /dev/null
Stefan Reinauer97f546c2010-04-20 17:19:20 +000044
Patrick Georgib3db79e2011-04-21 18:48:50 +020045ifeq ($(INNER_SCANBUILD),y)
46CC:=$(CC_real)
47HOSTCC:=$(CC_real) --hostcc
48HOSTCXX:=$(CC_real) --hostcxx
49endif
50
51export top := $(CURDIR)
52export src := src
Stefan Reinauer2551f592015-06-29 14:28:25 -070053export srck := $(abspath $(top)/../../util/kconfig)
Patrick Georgib3db79e2011-04-21 18:48:50 +020054export obj ?= build
55export objutil ?= $(obj)/util
Nico Huber08c70182014-10-14 23:27:50 +020056export objk := $(objutil)/lp_kconfig
Patrick Georgib3db79e2011-04-21 18:48:50 +020057
Patrick Georgib3db79e2011-04-21 18:48:50 +020058export KCONFIG_AUTOHEADER := $(obj)/config.h
59export KCONFIG_AUTOCONFIG := $(obj)/auto.conf
Stefan Reinauered564992015-06-11 14:57:36 -070060export KCONFIG_DEPENDENCIES := $(obj)/auto.conf.cmd
61export KCONFIG_SPLITCONFIG := $(obj)/config
62export KCONFIG_TRISTATE := $(obj)/tristate.conf
63export KCONFIG_NEGATIVES := 1
64export Kconfig := Kconfig
65export CONFIG_ := CONFIG_LP_
Patrick Georgib3db79e2011-04-21 18:48:50 +020066
67# directory containing the toplevel Makefile.inc
68TOPLEVEL := .
69
Gabe Black1ee2c6d2013-08-09 04:27:35 -070070CONFIG_LP_SHELL := sh
Patrick Georgib3db79e2011-04-21 18:48:50 +020071KBUILD_DEFCONFIG := configs/defconfig
72UNAME_RELEASE := $(shell uname -r)
73DOTCONFIG ?= .config
74KCONFIG_CONFIG = $(DOTCONFIG)
75export KCONFIG_CONFIG
76HAVE_DOTCONFIG := $(wildcard $(DOTCONFIG))
77MAKEFLAGS += -rR --no-print-directory
78
79# Make is silent per default, but 'make V=1' will show all compiler calls.
80Q:=@
81ifneq ($(V),1)
82ifneq ($(Q),)
83.SILENT:
84endif
85endif
86
87CPP:= $(CC) -x assembler-with-cpp -DASSEMBLY -E
Uwe Hermanne55b32a2008-08-08 07:56:07 +000088HOSTCC = gcc
89HOSTCXX = g++
Patrick Georgib3db79e2011-04-21 18:48:50 +020090HOSTCFLAGS := -I$(srck) -I$(objk) -g
91HOSTCXXFLAGS := -I$(srck) -I$(objk)
Jordan Crousef6145c32008-03-19 23:56:58 +000092
Uwe Hermann3b641292008-08-27 12:53:47 +000093DOXYGEN := doxygen
94DOXYGEN_OUTPUT_DIR := doxygen
95
Patrick Georgib3db79e2011-04-21 18:48:50 +020096all: real-all
Jordan Crousec3e728f2008-04-09 23:05:59 +000097
Uwe Hermannea872452008-10-22 15:49:20 +000098# This include must come _before_ the pattern rules below!
99# Order _does_ matter for pattern rules.
Stefan Reinauer2551f592015-06-29 14:28:25 -0700100include $(srck)/Makefile
Uwe Hermannea872452008-10-22 15:49:20 +0000101
Stefan Reinauer8af0d032012-12-14 13:05:21 -0800102include $(HAVE_DOTCONFIG)
103
Patrick Georgidb032552015-07-06 09:05:48 +0000104include .xcompile
105
Gabe Black51edd542013-09-30 23:00:33 -0700106ARCHDIR-$(CONFIG_LP_ARCH_ARM) := arm
Furquan Shaikh8c8c3772014-02-19 11:35:30 -0800107ARCHDIR-$(CONFIG_LP_ARCH_ARM64) := arm64
Ionela Voinescuce22c022014-09-24 17:05:33 +0100108ARCHDIR-$(CONFIG_LP_ARCH_MIPS) := mips
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700109ARCHDIR-$(CONFIG_LP_ARCH_X86) := x86
Stefan Reinauer8af0d032012-12-14 13:05:21 -0800110
111ARCH-y := $(ARCHDIR-y)
112
113# If architecture folder name is different from GCC binutils architecture name,
114# override here.
Gabe Black51edd542013-09-30 23:00:33 -0700115ARCH-$(CONFIG_LP_ARCH_ARM) := arm
Furquan Shaikh8c8c3772014-02-19 11:35:30 -0800116ARCH-$(CONFIG_LP_ARCH_ARM64) := arm64
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700117ARCH-$(CONFIG_LP_ARCH_X86) := i386
Ionela Voinescuce22c022014-09-24 17:05:33 +0100118ARCH-$(CONFIG_LP_ARCH_MIPS) := mipsel
Stefan Reinauer8af0d032012-12-14 13:05:21 -0800119
120CC := $(CC_$(ARCH-y))
121AS := $(AS_$(ARCH-y))
122LD := $(LD_$(ARCH-y))
123NM := $(NM_$(ARCH-y))
124OBJCOPY := $(OBJCOPY_$(ARCH-y))
125OBJDUMP := $(OBJDUMP_$(ARCH-y))
126READELF := $(READELF_$(ARCH-y))
127STRIP := $(STRIP_$(ARCH-y))
128AR := $(AR_$(ARCH-y))
129
130CFLAGS += $(CFLAGS_$(ARCH-y))
131
Patrick Georgib3db79e2011-04-21 18:48:50 +0200132# Three cases where we don't need fully populated $(obj) lists:
133# 1. when no .config exists
134# 2. when make config (in any flavour) is run
135# 3. when make distclean is run
136# Don't waste time on reading all Makefile.incs in these cases
Uwe Hermannea872452008-10-22 15:49:20 +0000137ifeq ($(strip $(HAVE_DOTCONFIG)),)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200138NOCOMPILE:=1
139endif
140ifneq ($(MAKECMDGOALS),)
141ifneq ($(filter %config distclean,$(MAKECMDGOALS)),)
142NOCOMPILE:=1
143endif
144endif
145
146ifeq ($(NOCOMPILE),1)
147include $(TOPLEVEL)/Makefile.inc
148real-all: config
149
Uwe Hermannea872452008-10-22 15:49:20 +0000150else
151
Patrick Georgib3db79e2011-04-21 18:48:50 +0200152ifneq ($(INNER_SCANBUILD),y)
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700153ifeq ($(CONFIG_LP_COMPILER_LLVM_CLANG),y)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200154CC:=clang -m32
155HOSTCC:=clang
156endif
157endif
158
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700159ifeq ($(CONFIG_LP_CCACHE),y)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200160CCACHE:=$(word 1,$(wildcard $(addsuffix /ccache,$(subst :, ,$(PATH)))))
161ifeq ($(CCACHE),)
162$(error ccache selected, but not found in PATH)
163endif
164CCACHE:=CCACHE_COMPILERCHECK=content CCACHE_BASEDIR=$(top) $(CCACHE)
165CC := $(CCACHE) $(CC)
166HOSTCC := $(CCACHE) $(HOSTCC)
167HOSTCXX := $(CCACHE) $(HOSTCXX)
168ROMCC := $(CCACHE) $(ROMCC)
169endif
170
171strip_quotes = $(subst ",,$(subst \",,$(1)))
172
173# The primary target needs to be here before we include the
174# other files
175
176ifeq ($(INNER_SCANBUILD),y)
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700177CONFIG_LP_SCANBUILD_ENABLE:=
Patrick Georgib3db79e2011-04-21 18:48:50 +0200178endif
179
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700180ifeq ($(CONFIG_LP_SCANBUILD_ENABLE),y)
181ifneq ($(CONFIG_LP_SCANBUILD_REPORT_LOCATION),)
182CONFIG_LP_SCANBUILD_REPORT_LOCATION:=-o $(CONFIG_LP_SCANBUILD_REPORT_LOCATION)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200183endif
184real-all:
185 echo '#!/bin/sh' > .ccwrap
186 echo 'CC="$(CC)"' >> .ccwrap
187 echo 'if [ "$$1" = "--hostcc" ]; then shift; CC="$(HOSTCC)"; fi' >> .ccwrap
188 echo 'if [ "$$1" = "--hostcxx" ]; then shift; CC="$(HOSTCXX)"; fi' >> .ccwrap
189 echo 'eval $$CC $$*' >> .ccwrap
190 chmod +x .ccwrap
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700191 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 +0200192else
193real-all: real-target
194endif
195
196# must come rather early
197.SECONDEXPANSION:
198
199$(obj)/config.h:
200 $(MAKE) oldconfig
201
202# Add a new class of source/object files to the build system
203add-class= \
204 $(eval $(1)-srcs:=) \
205 $(eval $(1)-objs:=) \
206 $(eval classes+=$(1))
207
208# Special classes are managed types with special behaviour
209# On parse time, for each entry in variable $(1)-y
210# a handler $(1)-handler is executed with the arguments:
211# * $(1): directory the parser is in
212# * $(2): current entry
213add-special-class= \
214 $(eval $(1):=) \
215 $(eval special-classes+=$(1))
216
217# Clean -y variables, include Makefile.inc
218# Add paths to files in X-y to X-srcs
219# Add subdirs-y to subdirs
220includemakefiles= \
221 $(foreach class,classes subdirs $(classes) $(special-classes), $(eval $(class)-y:=)) \
222 $(eval -include $(1)) \
223 $(foreach class,$(classes-y), $(call add-class,$(class))) \
224 $(foreach class,$(classes), \
225 $(eval $(class)-srcs+= \
226 $$(subst $(top)/,, \
227 $$(abspath $$(addprefix $(dir $(1)),$$($(class)-y)))))) \
228 $(foreach special,$(special-classes), \
229 $(foreach item,$($(special)-y), $(call $(special)-handler,$(dir $(1)),$(item)))) \
230 $(eval subdirs+=$$(subst $(CURDIR)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
231
232# For each path in $(subdirs) call includemakefiles
233# Repeat until subdirs is empty
234evaluate_subdirs= \
235 $(eval cursubdirs:=$(subdirs)) \
236 $(eval subdirs:=) \
237 $(foreach dir,$(cursubdirs), \
238 $(eval $(call includemakefiles,$(dir)/Makefile.inc))) \
239 $(if $(subdirs),$(eval $(call evaluate_subdirs)))
240
241# collect all object files eligible for building
242subdirs:=$(TOPLEVEL)
243$(eval $(call evaluate_subdirs))
244
245src-to-obj=$(addsuffix .$(1).o, $(basename $(addprefix $(obj)/, $($(1)-srcs))))
246$(foreach class,$(classes),$(eval $(class)-objs:=$(call src-to-obj,$(class))))
247
248allsrcs:=$(foreach var, $(addsuffix -srcs,$(classes)), $($(var)))
249allobjs:=$(foreach var, $(addsuffix -objs,$(classes)), $($(var)))
250alldirs:=$(sort $(abspath $(dir $(allobjs))))
251
252# macro to define template macros that are used by use_template macro
253define create_cc_template
254# $1 obj class
255# $2 source suffix (c, S)
256# $3 additional compiler flags
257# $4 additional dependencies
258ifn$(EMPTY)def $(1)-objs_$(2)_template
259de$(EMPTY)fine $(1)-objs_$(2)_template
Gabe Blackf06111a2013-03-12 15:50:08 -0700260$(obj)/$$(1).$(1).o: $$(1).$(2) $(obj)/libpayload-config.h $(4)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200261 @printf " CC $$$$(subst $$$$(obj)/,,$$$$(@))\n"
262 $(CC) $(3) -MMD $$$$(CFLAGS) -c -o $$$$@ $$$$<
263en$(EMPTY)def
264end$(EMPTY)if
265endef
266
267filetypes-of-class=$(subst .,,$(sort $(suffix $($(1)-srcs))))
268$(foreach class,$(classes), \
269 $(foreach type,$(call filetypes-of-class,$(class)), \
270 $(eval $(call create_cc_template,$(class),$(type),$($(class)-$(type)-ccopts),$($(class)-$(type)-deps)))))
271
272foreach-src=$(foreach file,$($(1)-srcs),$(eval $(call $(1)-objs_$(subst .,,$(suffix $(file)))_template,$(basename $(file)))))
273$(eval $(foreach class,$(classes),$(call foreach-src,$(class))))
274
275DEPENDENCIES = $(allobjs:.o=.d)
276-include $(DEPENDENCIES)
277
278printall:
279 @$(foreach class,$(classes),echo $(class)-objs:=$($(class)-objs); )
280 @echo alldirs:=$(alldirs)
281 @echo allsrcs=$(allsrcs)
282 @echo DEPENDENCIES=$(DEPENDENCIES)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200283 @$(foreach class,$(special-classes),echo $(class):='$($(class))'; )
Uwe Hermannea872452008-10-22 15:49:20 +0000284
285endif
Jordan Crousef6145c32008-03-19 23:56:58 +0000286
Stefan Reinauered564992015-06-11 14:57:36 -0700287$(shell mkdir -p $(KCONFIG_SPLITCONFIG) $(obj) $(objk)/lxdialog $(additional-dirs) $(alldirs))
Jordan Crousef6145c32008-03-19 23:56:58 +0000288
Patrick Georgib3db79e2011-04-21 18:48:50 +0200289cscope:
290 cscope -bR
291
292doxy: doxygen
293doxygen:
294 $(DOXYGEN) Doxyfile
295
296doxyclean: doxygen-clean
297doxygen-clean:
298 rm -rf $(DOXYGEN_OUTPUT_DIR)
299
300clean-for-update: doxygen-clean clean-for-update-target
301 rm -f $(allobjs) .xcompile
302 rm -f $(DEPENDENCIES)
303 rmdir -p $(alldirs) 2>/dev/null >/dev/null || true
304
305clean: clean-for-update clean-target
306 rm -f .ccwrap
307
308clean-cscope:
309 rm -f cscope.out
310
311distclean: clean-cscope
312 rm -rf $(obj)
313 rm -f .config .config.old ..config.tmp .kconfig.d .tmpconfig* .ccwrap .xcompile
314
315.PHONY: $(PHONY) clean clean-cscope cscope distclean doxygen doxy .xcompile