blob: 0b08c70af7d0a6ebfd1784f13631432884090526 [file] [log] [blame]
Jordan Crousef6145c32008-03-19 23:56:58 +00001##
Jordan Crousef6145c32008-03-19 23:56:58 +00002##
3## Copyright (C) 2008 Advanced Micro Devices, Inc.
Uwe Hermanne55b32a2008-08-08 07:56:07 +00004## Copyright (C) 2008 Uwe Hermann <uwe@hermann-uwe.de>
Patrick Georgib3db79e2011-04-21 18:48:50 +02005## Copyright (C) 2009-2010 coresystems GmbH
6## Copyright (C) 2011 secunet Security Networks AG
Jordan Crousef6145c32008-03-19 23:56:58 +00007##
8## Redistribution and use in source and binary forms, with or without
9## modification, are permitted provided that the following conditions
10## are met:
11## 1. Redistributions of source code must retain the above copyright
12## notice, this list of conditions and the following disclaimer.
13## 2. Redistributions in binary form must reproduce the above copyright
14## notice, this list of conditions and the following disclaimer in the
15## documentation and/or other materials provided with the distribution.
16## 3. The name of the author may not be used to endorse or promote products
17## derived from this software without specific prior written permission.
18##
19## THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20## ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23## FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25## OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27## LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28## OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29## SUCH DAMAGE.
30##
31
Patrick Georgib3db79e2011-04-21 18:48:50 +020032ifeq ($(INNER_SCANBUILD),y)
33CC_real:=$(CC)
Jordan Crousef6145c32008-03-19 23:56:58 +000034endif
35
Patrick Georgib3db79e2011-04-21 18:48:50 +020036export top := $(CURDIR)
37export src := src
Stefan Reinauer2551f592015-06-29 14:28:25 -070038export srck := $(abspath $(top)/../../util/kconfig)
Patrick Georgib3db79e2011-04-21 18:48:50 +020039export obj ?= build
40export objutil ?= $(obj)/util
Nico Huber08c70182014-10-14 23:27:50 +020041export objk := $(objutil)/lp_kconfig
Patrick Georgib3db79e2011-04-21 18:48:50 +020042
Patrick Georgib3db79e2011-04-21 18:48:50 +020043export KCONFIG_AUTOHEADER := $(obj)/config.h
44export KCONFIG_AUTOCONFIG := $(obj)/auto.conf
Stefan Reinauered564992015-06-11 14:57:36 -070045export KCONFIG_DEPENDENCIES := $(obj)/auto.conf.cmd
46export KCONFIG_SPLITCONFIG := $(obj)/config
47export KCONFIG_TRISTATE := $(obj)/tristate.conf
48export KCONFIG_NEGATIVES := 1
49export Kconfig := Kconfig
50export CONFIG_ := CONFIG_LP_
Patrick Georgib3db79e2011-04-21 18:48:50 +020051
52# directory containing the toplevel Makefile.inc
53TOPLEVEL := .
54
Gabe Black1ee2c6d2013-08-09 04:27:35 -070055CONFIG_LP_SHELL := sh
Paul Kocialkowskidb0c0c42015-08-03 14:05:33 +020056KBUILD_DEFCONFIG ?= configs/defconfig
Patrick Georgib3db79e2011-04-21 18:48:50 +020057UNAME_RELEASE := $(shell uname -r)
58DOTCONFIG ?= .config
59KCONFIG_CONFIG = $(DOTCONFIG)
60export KCONFIG_CONFIG
61HAVE_DOTCONFIG := $(wildcard $(DOTCONFIG))
62MAKEFLAGS += -rR --no-print-directory
63
64# Make is silent per default, but 'make V=1' will show all compiler calls.
65Q:=@
66ifneq ($(V),1)
67ifneq ($(Q),)
68.SILENT:
69endif
70endif
71
72CPP:= $(CC) -x assembler-with-cpp -DASSEMBLY -E
Uwe Hermanne55b32a2008-08-08 07:56:07 +000073HOSTCC = gcc
74HOSTCXX = g++
Patrick Georgib3db79e2011-04-21 18:48:50 +020075HOSTCFLAGS := -I$(srck) -I$(objk) -g
76HOSTCXXFLAGS := -I$(srck) -I$(objk)
Jordan Crousef6145c32008-03-19 23:56:58 +000077
Uwe Hermann3b641292008-08-27 12:53:47 +000078DOXYGEN := doxygen
79DOXYGEN_OUTPUT_DIR := doxygen
80
Patrick Georgib3db79e2011-04-21 18:48:50 +020081all: real-all
Jordan Crousec3e728f2008-04-09 23:05:59 +000082
Stefan Reinauerf53dbfa2015-07-09 00:26:49 +020083ifeq ($(INNER_SCANBUILD),y)
84CC:=$(CC_real)
85HOSTCC:=$(CC_real) --hostcc
86HOSTCXX:=$(CC_real) --hostcxx
87endif
88
Uwe Hermannea872452008-10-22 15:49:20 +000089# This include must come _before_ the pattern rules below!
90# Order _does_ matter for pattern rules.
Stefan Reinauer2551f592015-06-29 14:28:25 -070091include $(srck)/Makefile
Uwe Hermannea872452008-10-22 15:49:20 +000092
Stefan Reinauer8af0d032012-12-14 13:05:21 -080093include $(HAVE_DOTCONFIG)
94
Gabe Black51edd542013-09-30 23:00:33 -070095ARCHDIR-$(CONFIG_LP_ARCH_ARM) := arm
Furquan Shaikh8c8c3772014-02-19 11:35:30 -080096ARCHDIR-$(CONFIG_LP_ARCH_ARM64) := arm64
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
Patrick Georgi21fc58b2015-07-06 09:07:11 +0000101# If architecture folder name is different from xcompile architecture name,
Stefan Reinauer8af0d032012-12-14 13:05:21 -0800102# 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
Patrick Georgi21fc58b2015-07-06 09:07:11 +0000105ARCH-$(CONFIG_LP_ARCH_X86) := x86_32
Stefan Reinauer8af0d032012-12-14 13:05:21 -0800106
Patrick Georgib3db79e2011-04-21 18:48:50 +0200107# Three cases where we don't need fully populated $(obj) lists:
108# 1. when no .config exists
109# 2. when make config (in any flavour) is run
110# 3. when make distclean is run
111# Don't waste time on reading all Makefile.incs in these cases
Uwe Hermannea872452008-10-22 15:49:20 +0000112ifeq ($(strip $(HAVE_DOTCONFIG)),)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200113NOCOMPILE:=1
114endif
115ifneq ($(MAKECMDGOALS),)
Patrick Georgi0ffef882017-01-19 23:20:14 +0100116ifneq ($(filter %config %clean,$(MAKECMDGOALS)),)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200117NOCOMPILE:=1
118endif
119endif
120
Nico Huber1823d532020-11-16 22:50:49 +0100121xcompile ?= $(obj)/xcompile
122$(xcompile): $(top)/../../util/xcompile/xcompile
123 $< $(XGCCPATH) > $@.tmp
124 \mv -f $@.tmp $@ 2> /dev/null
125
Patrick Georgib3db79e2011-04-21 18:48:50 +0200126ifeq ($(NOCOMPILE),1)
127include $(TOPLEVEL)/Makefile.inc
128real-all: config
129
Uwe Hermannea872452008-10-22 15:49:20 +0000130else
131
Patrick Georgi0ffef882017-01-19 23:20:14 +0100132# in addition to the dependency below, create the file if it doesn't exist
133# to silence stupid warnings about a file that would be generated anyway.
Nico Huber1823d532020-11-16 22:50:49 +0100134$(if $(wildcard $(xcompile)),,$(shell \
135 mkdir -p $(dir $(xcompile)) && \
136 $(top)/../../util/xcompile/xcompile $(XGCCPATH) > $(xcompile) || rm -f $(xcompile)))
Patrick Georgi0ffef882017-01-19 23:20:14 +0100137
Nico Huber1823d532020-11-16 22:50:49 +0100138include $(xcompile)
Patrick Georgi0ffef882017-01-19 23:20:14 +0100139
Nico Huber1823d532020-11-16 22:50:49 +0100140ifneq ($(XCOMPILE_COMPLETE),1)
141$(shell rm -f $(xcompile))
142$(error $(xcompile) deleted because it's invalid. \
143 Restarting the build should fix that, or explain the problem)
144endif
Patrick Georgi0ffef882017-01-19 23:20:14 +0100145
Patrick Georgib144a342017-01-25 14:21:12 +0100146CC := $(CC_$(ARCH-y))
147AS := $(AS_$(ARCH-y))
148LD := $(LD_$(ARCH-y))
149NM := $(NM_$(ARCH-y))
150OBJCOPY := $(OBJCOPY_$(ARCH-y))
151OBJDUMP := $(OBJDUMP_$(ARCH-y))
152READELF := $(READELF_$(ARCH-y))
153STRIP := $(STRIP_$(ARCH-y))
154AR := $(AR_$(ARCH-y))
155
Raul E Rangel50a2a862018-07-13 15:16:58 -0600156CFLAGS += -std=gnu11 $(CFLAGS_$(ARCH-y))
Patrick Georgib144a342017-01-25 14:21:12 +0100157
Patrick Georgib3db79e2011-04-21 18:48:50 +0200158ifneq ($(INNER_SCANBUILD),y)
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700159ifeq ($(CONFIG_LP_COMPILER_LLVM_CLANG),y)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200160CC:=clang -m32
161HOSTCC:=clang
162endif
163endif
164
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700165ifeq ($(CONFIG_LP_CCACHE),y)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200166CCACHE:=$(word 1,$(wildcard $(addsuffix /ccache,$(subst :, ,$(PATH)))))
167ifeq ($(CCACHE),)
168$(error ccache selected, but not found in PATH)
169endif
170CCACHE:=CCACHE_COMPILERCHECK=content CCACHE_BASEDIR=$(top) $(CCACHE)
171CC := $(CCACHE) $(CC)
172HOSTCC := $(CCACHE) $(HOSTCC)
173HOSTCXX := $(CCACHE) $(HOSTCXX)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200174endif
175
176strip_quotes = $(subst ",,$(subst \",,$(1)))
177
178# The primary target needs to be here before we include the
179# other files
180
181ifeq ($(INNER_SCANBUILD),y)
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700182CONFIG_LP_SCANBUILD_ENABLE:=
Patrick Georgib3db79e2011-04-21 18:48:50 +0200183endif
184
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700185ifeq ($(CONFIG_LP_SCANBUILD_ENABLE),y)
186ifneq ($(CONFIG_LP_SCANBUILD_REPORT_LOCATION),)
187CONFIG_LP_SCANBUILD_REPORT_LOCATION:=-o $(CONFIG_LP_SCANBUILD_REPORT_LOCATION)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200188endif
189real-all:
190 echo '#!/bin/sh' > .ccwrap
191 echo 'CC="$(CC)"' >> .ccwrap
192 echo 'if [ "$$1" = "--hostcc" ]; then shift; CC="$(HOSTCC)"; fi' >> .ccwrap
193 echo 'if [ "$$1" = "--hostcxx" ]; then shift; CC="$(HOSTCXX)"; fi' >> .ccwrap
194 echo 'eval $$CC $$*' >> .ccwrap
195 chmod +x .ccwrap
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700196 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 +0200197else
198real-all: real-target
199endif
200
201# must come rather early
202.SECONDEXPANSION:
203
Nico Huber602a82a2015-09-09 13:02:51 +0200204$(KCONFIG_AUTOHEADER): $(KCONFIG_CONFIG)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200205 $(MAKE) oldconfig
206
207# Add a new class of source/object files to the build system
208add-class= \
209 $(eval $(1)-srcs:=) \
210 $(eval $(1)-objs:=) \
211 $(eval classes+=$(1))
212
213# Special classes are managed types with special behaviour
214# On parse time, for each entry in variable $(1)-y
215# a handler $(1)-handler is executed with the arguments:
216# * $(1): directory the parser is in
217# * $(2): current entry
218add-special-class= \
219 $(eval $(1):=) \
220 $(eval special-classes+=$(1))
221
222# Clean -y variables, include Makefile.inc
223# Add paths to files in X-y to X-srcs
224# Add subdirs-y to subdirs
225includemakefiles= \
226 $(foreach class,classes subdirs $(classes) $(special-classes), $(eval $(class)-y:=)) \
227 $(eval -include $(1)) \
228 $(foreach class,$(classes-y), $(call add-class,$(class))) \
Arthur Heymans6c2324a2019-10-29 18:36:46 +0100229 $(foreach special,$(special-classes), \
230 $(foreach item,$($(special)-y), $(call $(special)-handler,$(dir $(1)),$(item)))) \
Patrick Georgib3db79e2011-04-21 18:48:50 +0200231 $(foreach class,$(classes), \
232 $(eval $(class)-srcs+= \
233 $$(subst $(top)/,, \
234 $$(abspath $$(addprefix $(dir $(1)),$$($(class)-y)))))) \
Patrick Georgib3db79e2011-04-21 18:48:50 +0200235 $(eval subdirs+=$$(subst $(CURDIR)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
236
237# For each path in $(subdirs) call includemakefiles
238# Repeat until subdirs is empty
239evaluate_subdirs= \
240 $(eval cursubdirs:=$(subdirs)) \
241 $(eval subdirs:=) \
242 $(foreach dir,$(cursubdirs), \
243 $(eval $(call includemakefiles,$(dir)/Makefile.inc))) \
244 $(if $(subdirs),$(eval $(call evaluate_subdirs)))
245
246# collect all object files eligible for building
247subdirs:=$(TOPLEVEL)
248$(eval $(call evaluate_subdirs))
249
250src-to-obj=$(addsuffix .$(1).o, $(basename $(addprefix $(obj)/, $($(1)-srcs))))
251$(foreach class,$(classes),$(eval $(class)-objs:=$(call src-to-obj,$(class))))
252
253allsrcs:=$(foreach var, $(addsuffix -srcs,$(classes)), $($(var)))
254allobjs:=$(foreach var, $(addsuffix -objs,$(classes)), $($(var)))
255alldirs:=$(sort $(abspath $(dir $(allobjs))))
256
257# macro to define template macros that are used by use_template macro
258define create_cc_template
259# $1 obj class
260# $2 source suffix (c, S)
261# $3 additional compiler flags
262# $4 additional dependencies
263ifn$(EMPTY)def $(1)-objs_$(2)_template
264de$(EMPTY)fine $(1)-objs_$(2)_template
Gabe Blackf06111a2013-03-12 15:50:08 -0700265$(obj)/$$(1).$(1).o: $$(1).$(2) $(obj)/libpayload-config.h $(4)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200266 @printf " CC $$$$(subst $$$$(obj)/,,$$$$(@))\n"
267 $(CC) $(3) -MMD $$$$(CFLAGS) -c -o $$$$@ $$$$<
268en$(EMPTY)def
269end$(EMPTY)if
270endef
271
272filetypes-of-class=$(subst .,,$(sort $(suffix $($(1)-srcs))))
273$(foreach class,$(classes), \
274 $(foreach type,$(call filetypes-of-class,$(class)), \
275 $(eval $(call create_cc_template,$(class),$(type),$($(class)-$(type)-ccopts),$($(class)-$(type)-deps)))))
276
277foreach-src=$(foreach file,$($(1)-srcs),$(eval $(call $(1)-objs_$(subst .,,$(suffix $(file)))_template,$(basename $(file)))))
278$(eval $(foreach class,$(classes),$(call foreach-src,$(class))))
279
280DEPENDENCIES = $(allobjs:.o=.d)
281-include $(DEPENDENCIES)
282
283printall:
284 @$(foreach class,$(classes),echo $(class)-objs:=$($(class)-objs); )
285 @echo alldirs:=$(alldirs)
286 @echo allsrcs=$(allsrcs)
287 @echo DEPENDENCIES=$(DEPENDENCIES)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200288 @$(foreach class,$(special-classes),echo $(class):='$($(class))'; )
Uwe Hermannea872452008-10-22 15:49:20 +0000289
290endif
Jordan Crousef6145c32008-03-19 23:56:58 +0000291
Stefan Reinauered564992015-06-11 14:57:36 -0700292$(shell mkdir -p $(KCONFIG_SPLITCONFIG) $(obj) $(objk)/lxdialog $(additional-dirs) $(alldirs))
Jordan Crousef6145c32008-03-19 23:56:58 +0000293
Patrick Georgib3db79e2011-04-21 18:48:50 +0200294cscope:
295 cscope -bR
296
297doxy: doxygen
298doxygen:
299 $(DOXYGEN) Doxyfile
300
301doxyclean: doxygen-clean
302doxygen-clean:
303 rm -rf $(DOXYGEN_OUTPUT_DIR)
304
305clean-for-update: doxygen-clean clean-for-update-target
Nico Huber1823d532020-11-16 22:50:49 +0100306 rm -f $(allobjs) $(xcompile)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200307 rm -f $(DEPENDENCIES)
308 rmdir -p $(alldirs) 2>/dev/null >/dev/null || true
309
310clean: clean-for-update clean-target
Martin Roth6959f5c2016-03-23 16:07:54 -0600311 rm -f .ccwrap junit_config junit_config.old
Patrick Georgib144a342017-01-25 14:21:12 +0100312 rm -rf $(obj)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200313
314clean-cscope:
315 rm -f cscope.out
316
Patrick Georgib144a342017-01-25 14:21:12 +0100317distclean: clean-cscope clean
Martin Roth6959f5c2016-03-23 16:07:54 -0600318 rm -f .config .config.old ..config.tmp .kconfig.d .tmpconfig* .ccwrap .xcompile junit.xml
Patrick Georgib3db79e2011-04-21 18:48:50 +0200319
Paul Kocialkowski5d5fcdd2015-08-03 16:44:01 +0200320.PHONY: $(PHONY) clean clean-cscope cscope distclean doxygen doxy