blob: 03fb153d5c6f787563f18361464ac6d8b43e30ee [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 Georgib3db79e2011-04-21 18:48:50 +020037export top := $(CURDIR)
38export src := src
Stefan Reinauer2551f592015-06-29 14:28:25 -070039export srck := $(abspath $(top)/../../util/kconfig)
Patrick Georgib3db79e2011-04-21 18:48:50 +020040export obj ?= build
41export objutil ?= $(obj)/util
Nico Huber08c70182014-10-14 23:27:50 +020042export objk := $(objutil)/lp_kconfig
Patrick Georgib3db79e2011-04-21 18:48:50 +020043
Patrick Georgib3db79e2011-04-21 18:48:50 +020044export KCONFIG_AUTOHEADER := $(obj)/config.h
45export KCONFIG_AUTOCONFIG := $(obj)/auto.conf
Stefan Reinauered564992015-06-11 14:57:36 -070046export KCONFIG_DEPENDENCIES := $(obj)/auto.conf.cmd
47export KCONFIG_SPLITCONFIG := $(obj)/config
48export KCONFIG_TRISTATE := $(obj)/tristate.conf
49export KCONFIG_NEGATIVES := 1
50export Kconfig := Kconfig
51export CONFIG_ := CONFIG_LP_
Patrick Georgib3db79e2011-04-21 18:48:50 +020052
53# directory containing the toplevel Makefile.inc
54TOPLEVEL := .
55
Gabe Black1ee2c6d2013-08-09 04:27:35 -070056CONFIG_LP_SHELL := sh
Paul Kocialkowskidb0c0c42015-08-03 14:05:33 +020057KBUILD_DEFCONFIG ?= configs/defconfig
Patrick Georgib3db79e2011-04-21 18:48:50 +020058UNAME_RELEASE := $(shell uname -r)
59DOTCONFIG ?= .config
60KCONFIG_CONFIG = $(DOTCONFIG)
61export KCONFIG_CONFIG
62HAVE_DOTCONFIG := $(wildcard $(DOTCONFIG))
63MAKEFLAGS += -rR --no-print-directory
64
65# Make is silent per default, but 'make V=1' will show all compiler calls.
66Q:=@
67ifneq ($(V),1)
68ifneq ($(Q),)
69.SILENT:
70endif
71endif
72
73CPP:= $(CC) -x assembler-with-cpp -DASSEMBLY -E
Uwe Hermanne55b32a2008-08-08 07:56:07 +000074HOSTCC = gcc
75HOSTCXX = g++
Patrick Georgib3db79e2011-04-21 18:48:50 +020076HOSTCFLAGS := -I$(srck) -I$(objk) -g
77HOSTCXXFLAGS := -I$(srck) -I$(objk)
Jordan Crousef6145c32008-03-19 23:56:58 +000078
Uwe Hermann3b641292008-08-27 12:53:47 +000079DOXYGEN := doxygen
80DOXYGEN_OUTPUT_DIR := doxygen
81
Patrick Georgib3db79e2011-04-21 18:48:50 +020082all: real-all
Jordan Crousec3e728f2008-04-09 23:05:59 +000083
Stefan Reinauerf53dbfa2015-07-09 00:26:49 +020084ifeq ($(INNER_SCANBUILD),y)
85CC:=$(CC_real)
86HOSTCC:=$(CC_real) --hostcc
87HOSTCXX:=$(CC_real) --hostcxx
88endif
89
Uwe Hermannea872452008-10-22 15:49:20 +000090# This include must come _before_ the pattern rules below!
91# Order _does_ matter for pattern rules.
Stefan Reinauer2551f592015-06-29 14:28:25 -070092include $(srck)/Makefile
Uwe Hermannea872452008-10-22 15:49:20 +000093
Stefan Reinauer8af0d032012-12-14 13:05:21 -080094include $(HAVE_DOTCONFIG)
95
Gabe Black51edd542013-09-30 23:00:33 -070096ARCHDIR-$(CONFIG_LP_ARCH_ARM) := arm
Furquan Shaikh8c8c3772014-02-19 11:35:30 -080097ARCHDIR-$(CONFIG_LP_ARCH_ARM64) := arm64
Gabe Black1ee2c6d2013-08-09 04:27:35 -070098ARCHDIR-$(CONFIG_LP_ARCH_X86) := x86
Stefan Reinauer8af0d032012-12-14 13:05:21 -080099
100ARCH-y := $(ARCHDIR-y)
101
Patrick Georgi21fc58b2015-07-06 09:07:11 +0000102# If architecture folder name is different from xcompile architecture name,
Stefan Reinauer8af0d032012-12-14 13:05:21 -0800103# override here.
Gabe Black51edd542013-09-30 23:00:33 -0700104ARCH-$(CONFIG_LP_ARCH_ARM) := arm
Furquan Shaikh8c8c3772014-02-19 11:35:30 -0800105ARCH-$(CONFIG_LP_ARCH_ARM64) := arm64
Patrick Georgi21fc58b2015-07-06 09:07:11 +0000106ARCH-$(CONFIG_LP_ARCH_X86) := x86_32
Stefan Reinauer8af0d032012-12-14 13:05:21 -0800107
Patrick Georgib3db79e2011-04-21 18:48:50 +0200108# Three cases where we don't need fully populated $(obj) lists:
109# 1. when no .config exists
110# 2. when make config (in any flavour) is run
111# 3. when make distclean is run
112# Don't waste time on reading all Makefile.incs in these cases
Uwe Hermannea872452008-10-22 15:49:20 +0000113ifeq ($(strip $(HAVE_DOTCONFIG)),)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200114NOCOMPILE:=1
115endif
116ifneq ($(MAKECMDGOALS),)
Patrick Georgi0ffef882017-01-19 23:20:14 +0100117ifneq ($(filter %config %clean,$(MAKECMDGOALS)),)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200118NOCOMPILE:=1
119endif
120endif
121
122ifeq ($(NOCOMPILE),1)
123include $(TOPLEVEL)/Makefile.inc
124real-all: config
125
Uwe Hermannea872452008-10-22 15:49:20 +0000126else
127
Patrick Georgi0ffef882017-01-19 23:20:14 +0100128# in addition to the dependency below, create the file if it doesn't exist
129# to silence stupid warnings about a file that would be generated anyway.
130$(if $(wildcard .xcompile)$(NOCOMPILE),,$(eval $(shell $(top)/../../util/xcompile/xcompile $(XGCCPATH) > .xcompile)))
131
132.xcompile: $(top)/../../util/xcompile/xcompile
133 $< $(XGCCPATH) > $@.tmp
134 \mv -f $@.tmp $@ 2> /dev/null
135
136-include .xcompile
137
Patrick Georgib144a342017-01-25 14:21:12 +0100138CC := $(CC_$(ARCH-y))
139AS := $(AS_$(ARCH-y))
140LD := $(LD_$(ARCH-y))
141NM := $(NM_$(ARCH-y))
142OBJCOPY := $(OBJCOPY_$(ARCH-y))
143OBJDUMP := $(OBJDUMP_$(ARCH-y))
144READELF := $(READELF_$(ARCH-y))
145STRIP := $(STRIP_$(ARCH-y))
146AR := $(AR_$(ARCH-y))
147
Raul E Rangel50a2a862018-07-13 15:16:58 -0600148CFLAGS += -std=gnu11 $(CFLAGS_$(ARCH-y))
Patrick Georgib144a342017-01-25 14:21:12 +0100149
Patrick Georgib3db79e2011-04-21 18:48:50 +0200150ifneq ($(INNER_SCANBUILD),y)
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700151ifeq ($(CONFIG_LP_COMPILER_LLVM_CLANG),y)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200152CC:=clang -m32
153HOSTCC:=clang
154endif
155endif
156
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700157ifeq ($(CONFIG_LP_CCACHE),y)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200158CCACHE:=$(word 1,$(wildcard $(addsuffix /ccache,$(subst :, ,$(PATH)))))
159ifeq ($(CCACHE),)
160$(error ccache selected, but not found in PATH)
161endif
162CCACHE:=CCACHE_COMPILERCHECK=content CCACHE_BASEDIR=$(top) $(CCACHE)
163CC := $(CCACHE) $(CC)
164HOSTCC := $(CCACHE) $(HOSTCC)
165HOSTCXX := $(CCACHE) $(HOSTCXX)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200166endif
167
168strip_quotes = $(subst ",,$(subst \",,$(1)))
169
170# The primary target needs to be here before we include the
171# other files
172
173ifeq ($(INNER_SCANBUILD),y)
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700174CONFIG_LP_SCANBUILD_ENABLE:=
Patrick Georgib3db79e2011-04-21 18:48:50 +0200175endif
176
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700177ifeq ($(CONFIG_LP_SCANBUILD_ENABLE),y)
178ifneq ($(CONFIG_LP_SCANBUILD_REPORT_LOCATION),)
179CONFIG_LP_SCANBUILD_REPORT_LOCATION:=-o $(CONFIG_LP_SCANBUILD_REPORT_LOCATION)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200180endif
181real-all:
182 echo '#!/bin/sh' > .ccwrap
183 echo 'CC="$(CC)"' >> .ccwrap
184 echo 'if [ "$$1" = "--hostcc" ]; then shift; CC="$(HOSTCC)"; fi' >> .ccwrap
185 echo 'if [ "$$1" = "--hostcxx" ]; then shift; CC="$(HOSTCXX)"; fi' >> .ccwrap
186 echo 'eval $$CC $$*' >> .ccwrap
187 chmod +x .ccwrap
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700188 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 +0200189else
190real-all: real-target
191endif
192
193# must come rather early
194.SECONDEXPANSION:
195
Nico Huber602a82a2015-09-09 13:02:51 +0200196$(KCONFIG_AUTOHEADER): $(KCONFIG_CONFIG)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200197 $(MAKE) oldconfig
198
199# Add a new class of source/object files to the build system
200add-class= \
201 $(eval $(1)-srcs:=) \
202 $(eval $(1)-objs:=) \
203 $(eval classes+=$(1))
204
205# Special classes are managed types with special behaviour
206# On parse time, for each entry in variable $(1)-y
207# a handler $(1)-handler is executed with the arguments:
208# * $(1): directory the parser is in
209# * $(2): current entry
210add-special-class= \
211 $(eval $(1):=) \
212 $(eval special-classes+=$(1))
213
214# Clean -y variables, include Makefile.inc
215# Add paths to files in X-y to X-srcs
216# Add subdirs-y to subdirs
217includemakefiles= \
218 $(foreach class,classes subdirs $(classes) $(special-classes), $(eval $(class)-y:=)) \
219 $(eval -include $(1)) \
220 $(foreach class,$(classes-y), $(call add-class,$(class))) \
Arthur Heymans6c2324a2019-10-29 18:36:46 +0100221 $(foreach special,$(special-classes), \
222 $(foreach item,$($(special)-y), $(call $(special)-handler,$(dir $(1)),$(item)))) \
Patrick Georgib3db79e2011-04-21 18:48:50 +0200223 $(foreach class,$(classes), \
224 $(eval $(class)-srcs+= \
225 $$(subst $(top)/,, \
226 $$(abspath $$(addprefix $(dir $(1)),$$($(class)-y)))))) \
Patrick Georgib3db79e2011-04-21 18:48:50 +0200227 $(eval subdirs+=$$(subst $(CURDIR)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
228
229# For each path in $(subdirs) call includemakefiles
230# Repeat until subdirs is empty
231evaluate_subdirs= \
232 $(eval cursubdirs:=$(subdirs)) \
233 $(eval subdirs:=) \
234 $(foreach dir,$(cursubdirs), \
235 $(eval $(call includemakefiles,$(dir)/Makefile.inc))) \
236 $(if $(subdirs),$(eval $(call evaluate_subdirs)))
237
238# collect all object files eligible for building
239subdirs:=$(TOPLEVEL)
240$(eval $(call evaluate_subdirs))
241
242src-to-obj=$(addsuffix .$(1).o, $(basename $(addprefix $(obj)/, $($(1)-srcs))))
243$(foreach class,$(classes),$(eval $(class)-objs:=$(call src-to-obj,$(class))))
244
245allsrcs:=$(foreach var, $(addsuffix -srcs,$(classes)), $($(var)))
246allobjs:=$(foreach var, $(addsuffix -objs,$(classes)), $($(var)))
247alldirs:=$(sort $(abspath $(dir $(allobjs))))
248
249# macro to define template macros that are used by use_template macro
250define create_cc_template
251# $1 obj class
252# $2 source suffix (c, S)
253# $3 additional compiler flags
254# $4 additional dependencies
255ifn$(EMPTY)def $(1)-objs_$(2)_template
256de$(EMPTY)fine $(1)-objs_$(2)_template
Gabe Blackf06111a2013-03-12 15:50:08 -0700257$(obj)/$$(1).$(1).o: $$(1).$(2) $(obj)/libpayload-config.h $(4)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200258 @printf " CC $$$$(subst $$$$(obj)/,,$$$$(@))\n"
259 $(CC) $(3) -MMD $$$$(CFLAGS) -c -o $$$$@ $$$$<
260en$(EMPTY)def
261end$(EMPTY)if
262endef
263
264filetypes-of-class=$(subst .,,$(sort $(suffix $($(1)-srcs))))
265$(foreach class,$(classes), \
266 $(foreach type,$(call filetypes-of-class,$(class)), \
267 $(eval $(call create_cc_template,$(class),$(type),$($(class)-$(type)-ccopts),$($(class)-$(type)-deps)))))
268
269foreach-src=$(foreach file,$($(1)-srcs),$(eval $(call $(1)-objs_$(subst .,,$(suffix $(file)))_template,$(basename $(file)))))
270$(eval $(foreach class,$(classes),$(call foreach-src,$(class))))
271
272DEPENDENCIES = $(allobjs:.o=.d)
273-include $(DEPENDENCIES)
274
275printall:
276 @$(foreach class,$(classes),echo $(class)-objs:=$($(class)-objs); )
277 @echo alldirs:=$(alldirs)
278 @echo allsrcs=$(allsrcs)
279 @echo DEPENDENCIES=$(DEPENDENCIES)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200280 @$(foreach class,$(special-classes),echo $(class):='$($(class))'; )
Uwe Hermannea872452008-10-22 15:49:20 +0000281
282endif
Jordan Crousef6145c32008-03-19 23:56:58 +0000283
Stefan Reinauered564992015-06-11 14:57:36 -0700284$(shell mkdir -p $(KCONFIG_SPLITCONFIG) $(obj) $(objk)/lxdialog $(additional-dirs) $(alldirs))
Jordan Crousef6145c32008-03-19 23:56:58 +0000285
Patrick Georgib3db79e2011-04-21 18:48:50 +0200286cscope:
287 cscope -bR
288
289doxy: doxygen
290doxygen:
291 $(DOXYGEN) Doxyfile
292
293doxyclean: doxygen-clean
294doxygen-clean:
295 rm -rf $(DOXYGEN_OUTPUT_DIR)
296
297clean-for-update: doxygen-clean clean-for-update-target
298 rm -f $(allobjs) .xcompile
299 rm -f $(DEPENDENCIES)
300 rmdir -p $(alldirs) 2>/dev/null >/dev/null || true
301
302clean: clean-for-update clean-target
Martin Roth6959f5c2016-03-23 16:07:54 -0600303 rm -f .ccwrap junit_config junit_config.old
Patrick Georgib144a342017-01-25 14:21:12 +0100304 rm -rf $(obj)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200305
306clean-cscope:
307 rm -f cscope.out
308
Patrick Georgib144a342017-01-25 14:21:12 +0100309distclean: clean-cscope clean
Martin Roth6959f5c2016-03-23 16:07:54 -0600310 rm -f .config .config.old ..config.tmp .kconfig.d .tmpconfig* .ccwrap .xcompile junit.xml
Patrick Georgib3db79e2011-04-21 18:48:50 +0200311
Paul Kocialkowski5d5fcdd2015-08-03 16:44:01 +0200312.PHONY: $(PHONY) clean clean-cscope cscope distclean doxygen doxy