blob: b5687c3859b439574b4fe4704442effbe14119ec [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
121ifeq ($(NOCOMPILE),1)
122include $(TOPLEVEL)/Makefile.inc
123real-all: config
124
Uwe Hermannea872452008-10-22 15:49:20 +0000125else
126
Patrick Georgi0ffef882017-01-19 23:20:14 +0100127# in addition to the dependency below, create the file if it doesn't exist
128# to silence stupid warnings about a file that would be generated anyway.
129$(if $(wildcard .xcompile)$(NOCOMPILE),,$(eval $(shell $(top)/../../util/xcompile/xcompile $(XGCCPATH) > .xcompile)))
130
131.xcompile: $(top)/../../util/xcompile/xcompile
132 $< $(XGCCPATH) > $@.tmp
133 \mv -f $@.tmp $@ 2> /dev/null
134
135-include .xcompile
136
Patrick Georgib144a342017-01-25 14:21:12 +0100137CC := $(CC_$(ARCH-y))
138AS := $(AS_$(ARCH-y))
139LD := $(LD_$(ARCH-y))
140NM := $(NM_$(ARCH-y))
141OBJCOPY := $(OBJCOPY_$(ARCH-y))
142OBJDUMP := $(OBJDUMP_$(ARCH-y))
143READELF := $(READELF_$(ARCH-y))
144STRIP := $(STRIP_$(ARCH-y))
145AR := $(AR_$(ARCH-y))
146
Raul E Rangel50a2a862018-07-13 15:16:58 -0600147CFLAGS += -std=gnu11 $(CFLAGS_$(ARCH-y))
Patrick Georgib144a342017-01-25 14:21:12 +0100148
Patrick Georgib3db79e2011-04-21 18:48:50 +0200149ifneq ($(INNER_SCANBUILD),y)
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700150ifeq ($(CONFIG_LP_COMPILER_LLVM_CLANG),y)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200151CC:=clang -m32
152HOSTCC:=clang
153endif
154endif
155
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700156ifeq ($(CONFIG_LP_CCACHE),y)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200157CCACHE:=$(word 1,$(wildcard $(addsuffix /ccache,$(subst :, ,$(PATH)))))
158ifeq ($(CCACHE),)
159$(error ccache selected, but not found in PATH)
160endif
161CCACHE:=CCACHE_COMPILERCHECK=content CCACHE_BASEDIR=$(top) $(CCACHE)
162CC := $(CCACHE) $(CC)
163HOSTCC := $(CCACHE) $(HOSTCC)
164HOSTCXX := $(CCACHE) $(HOSTCXX)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200165endif
166
167strip_quotes = $(subst ",,$(subst \",,$(1)))
168
169# The primary target needs to be here before we include the
170# other files
171
172ifeq ($(INNER_SCANBUILD),y)
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700173CONFIG_LP_SCANBUILD_ENABLE:=
Patrick Georgib3db79e2011-04-21 18:48:50 +0200174endif
175
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700176ifeq ($(CONFIG_LP_SCANBUILD_ENABLE),y)
177ifneq ($(CONFIG_LP_SCANBUILD_REPORT_LOCATION),)
178CONFIG_LP_SCANBUILD_REPORT_LOCATION:=-o $(CONFIG_LP_SCANBUILD_REPORT_LOCATION)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200179endif
180real-all:
181 echo '#!/bin/sh' > .ccwrap
182 echo 'CC="$(CC)"' >> .ccwrap
183 echo 'if [ "$$1" = "--hostcc" ]; then shift; CC="$(HOSTCC)"; fi' >> .ccwrap
184 echo 'if [ "$$1" = "--hostcxx" ]; then shift; CC="$(HOSTCXX)"; fi' >> .ccwrap
185 echo 'eval $$CC $$*' >> .ccwrap
186 chmod +x .ccwrap
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700187 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 +0200188else
189real-all: real-target
190endif
191
192# must come rather early
193.SECONDEXPANSION:
194
Nico Huber602a82a2015-09-09 13:02:51 +0200195$(KCONFIG_AUTOHEADER): $(KCONFIG_CONFIG)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200196 $(MAKE) oldconfig
197
198# Add a new class of source/object files to the build system
199add-class= \
200 $(eval $(1)-srcs:=) \
201 $(eval $(1)-objs:=) \
202 $(eval classes+=$(1))
203
204# Special classes are managed types with special behaviour
205# On parse time, for each entry in variable $(1)-y
206# a handler $(1)-handler is executed with the arguments:
207# * $(1): directory the parser is in
208# * $(2): current entry
209add-special-class= \
210 $(eval $(1):=) \
211 $(eval special-classes+=$(1))
212
213# Clean -y variables, include Makefile.inc
214# Add paths to files in X-y to X-srcs
215# Add subdirs-y to subdirs
216includemakefiles= \
217 $(foreach class,classes subdirs $(classes) $(special-classes), $(eval $(class)-y:=)) \
218 $(eval -include $(1)) \
219 $(foreach class,$(classes-y), $(call add-class,$(class))) \
Arthur Heymans6c2324a2019-10-29 18:36:46 +0100220 $(foreach special,$(special-classes), \
221 $(foreach item,$($(special)-y), $(call $(special)-handler,$(dir $(1)),$(item)))) \
Patrick Georgib3db79e2011-04-21 18:48:50 +0200222 $(foreach class,$(classes), \
223 $(eval $(class)-srcs+= \
224 $$(subst $(top)/,, \
225 $$(abspath $$(addprefix $(dir $(1)),$$($(class)-y)))))) \
Patrick Georgib3db79e2011-04-21 18:48:50 +0200226 $(eval subdirs+=$$(subst $(CURDIR)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
227
228# For each path in $(subdirs) call includemakefiles
229# Repeat until subdirs is empty
230evaluate_subdirs= \
231 $(eval cursubdirs:=$(subdirs)) \
232 $(eval subdirs:=) \
233 $(foreach dir,$(cursubdirs), \
234 $(eval $(call includemakefiles,$(dir)/Makefile.inc))) \
235 $(if $(subdirs),$(eval $(call evaluate_subdirs)))
236
237# collect all object files eligible for building
238subdirs:=$(TOPLEVEL)
239$(eval $(call evaluate_subdirs))
240
241src-to-obj=$(addsuffix .$(1).o, $(basename $(addprefix $(obj)/, $($(1)-srcs))))
242$(foreach class,$(classes),$(eval $(class)-objs:=$(call src-to-obj,$(class))))
243
244allsrcs:=$(foreach var, $(addsuffix -srcs,$(classes)), $($(var)))
245allobjs:=$(foreach var, $(addsuffix -objs,$(classes)), $($(var)))
246alldirs:=$(sort $(abspath $(dir $(allobjs))))
247
248# macro to define template macros that are used by use_template macro
249define create_cc_template
250# $1 obj class
251# $2 source suffix (c, S)
252# $3 additional compiler flags
253# $4 additional dependencies
254ifn$(EMPTY)def $(1)-objs_$(2)_template
255de$(EMPTY)fine $(1)-objs_$(2)_template
Gabe Blackf06111a2013-03-12 15:50:08 -0700256$(obj)/$$(1).$(1).o: $$(1).$(2) $(obj)/libpayload-config.h $(4)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200257 @printf " CC $$$$(subst $$$$(obj)/,,$$$$(@))\n"
258 $(CC) $(3) -MMD $$$$(CFLAGS) -c -o $$$$@ $$$$<
259en$(EMPTY)def
260end$(EMPTY)if
261endef
262
263filetypes-of-class=$(subst .,,$(sort $(suffix $($(1)-srcs))))
264$(foreach class,$(classes), \
265 $(foreach type,$(call filetypes-of-class,$(class)), \
266 $(eval $(call create_cc_template,$(class),$(type),$($(class)-$(type)-ccopts),$($(class)-$(type)-deps)))))
267
268foreach-src=$(foreach file,$($(1)-srcs),$(eval $(call $(1)-objs_$(subst .,,$(suffix $(file)))_template,$(basename $(file)))))
269$(eval $(foreach class,$(classes),$(call foreach-src,$(class))))
270
271DEPENDENCIES = $(allobjs:.o=.d)
272-include $(DEPENDENCIES)
273
274printall:
275 @$(foreach class,$(classes),echo $(class)-objs:=$($(class)-objs); )
276 @echo alldirs:=$(alldirs)
277 @echo allsrcs=$(allsrcs)
278 @echo DEPENDENCIES=$(DEPENDENCIES)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200279 @$(foreach class,$(special-classes),echo $(class):='$($(class))'; )
Uwe Hermannea872452008-10-22 15:49:20 +0000280
281endif
Jordan Crousef6145c32008-03-19 23:56:58 +0000282
Stefan Reinauered564992015-06-11 14:57:36 -0700283$(shell mkdir -p $(KCONFIG_SPLITCONFIG) $(obj) $(objk)/lxdialog $(additional-dirs) $(alldirs))
Jordan Crousef6145c32008-03-19 23:56:58 +0000284
Patrick Georgib3db79e2011-04-21 18:48:50 +0200285cscope:
286 cscope -bR
287
288doxy: doxygen
289doxygen:
290 $(DOXYGEN) Doxyfile
291
292doxyclean: doxygen-clean
293doxygen-clean:
294 rm -rf $(DOXYGEN_OUTPUT_DIR)
295
296clean-for-update: doxygen-clean clean-for-update-target
297 rm -f $(allobjs) .xcompile
298 rm -f $(DEPENDENCIES)
299 rmdir -p $(alldirs) 2>/dev/null >/dev/null || true
300
301clean: clean-for-update clean-target
Martin Roth6959f5c2016-03-23 16:07:54 -0600302 rm -f .ccwrap junit_config junit_config.old
Patrick Georgib144a342017-01-25 14:21:12 +0100303 rm -rf $(obj)
Patrick Georgib3db79e2011-04-21 18:48:50 +0200304
305clean-cscope:
306 rm -f cscope.out
307
Patrick Georgib144a342017-01-25 14:21:12 +0100308distclean: clean-cscope clean
Martin Roth6959f5c2016-03-23 16:07:54 -0600309 rm -f .config .config.old ..config.tmp .kconfig.d .tmpconfig* .ccwrap .xcompile junit.xml
Patrick Georgib3db79e2011-04-21 18:48:50 +0200310
Paul Kocialkowski5d5fcdd2015-08-03 16:44:01 +0200311.PHONY: $(PHONY) clean clean-cscope cscope distclean doxygen doxy