xref: /aosp_15_r20/external/modp_b64/common.mk (revision c3cd14fb6370b1f0a2b85e79eb9b360b8782fd16)
1*c3cd14fbSJerome Gaillard# copyright (c) 2012 the chromium os authors. all rights reserved.
2*c3cd14fbSJerome Gaillard# use of this source code is governed by a bsd-style license that can be
3*c3cd14fbSJerome Gaillard# found in the license file.
4*c3cd14fbSJerome Gaillard#
5*c3cd14fbSJerome Gaillard# If this file is part of another source distribution, it's license may be
6*c3cd14fbSJerome Gaillard# stored in LICENSE.makefile or LICENSE.common.mk.
7*c3cd14fbSJerome Gaillard#
8*c3cd14fbSJerome Gaillard# NOTE NOTE NOTE
9*c3cd14fbSJerome Gaillard#  The authoritative common.mk is located in:
10*c3cd14fbSJerome Gaillard#    https://chromium.googlesource.com/chromiumos/platform2/+/master/common-mk
11*c3cd14fbSJerome Gaillard#  Please make all changes there, then copy into place in other repos.
12*c3cd14fbSJerome Gaillard# NOTE NOTE NOTE
13*c3cd14fbSJerome Gaillard#
14*c3cd14fbSJerome Gaillard# This file provides a common architecture for building C/C++ source trees.
15*c3cd14fbSJerome Gaillard# It uses recursive makefile inclusion to create a single make process which
16*c3cd14fbSJerome Gaillard# can be built in the source tree or with the build artifacts placed elsewhere.
17*c3cd14fbSJerome Gaillard#
18*c3cd14fbSJerome Gaillard# It is fully parallelizable for all targets, including static archives.
19*c3cd14fbSJerome Gaillard#
20*c3cd14fbSJerome Gaillard# To use:
21*c3cd14fbSJerome Gaillard# 1. Place common.mk in your top source level
22*c3cd14fbSJerome Gaillard# 2. In your top-level Makefile, place "include common.mk" at the top
23*c3cd14fbSJerome Gaillard# 3. In all subdirectories, create a 'module.mk' file that starts with:
24*c3cd14fbSJerome Gaillard#      include common.mk
25*c3cd14fbSJerome Gaillard#    And then contains the remainder of your targets.
26*c3cd14fbSJerome Gaillard# 4. All build targets should look like:
27*c3cd14fbSJerome Gaillard#    relative/path/target: relative/path/obj.o
28*c3cd14fbSJerome Gaillard#
29*c3cd14fbSJerome Gaillard# See existing makefiles for rule examples.
30*c3cd14fbSJerome Gaillard#
31*c3cd14fbSJerome Gaillard# Exported macros:
32*c3cd14fbSJerome Gaillard#   - cc_binary, cxx_binary provide standard compilation steps for binaries
33*c3cd14fbSJerome Gaillard#   - cxx_library, cc_library provide standard compilation steps for
34*c3cd14fbSJerome Gaillard#     shared objects.
35*c3cd14fbSJerome Gaillard#   All of the above optionally take an argument for extra flags.
36*c3cd14fbSJerome Gaillard#   - update_archive creates/updates a given .a target
37*c3cd14fbSJerome Gaillard#
38*c3cd14fbSJerome Gaillard# Instead of using the build macros, most users can just use wrapped targets:
39*c3cd14fbSJerome Gaillard#   - CXX_BINARY, CC_BINARY, CC_STATIC_BINARY, CXX_STATIC_BINARY
40*c3cd14fbSJerome Gaillard#   - CXX_LIBRARY, CC_LIBRARY, CC_STATIC_LIBRARY, CXX_STATIC_LIBRARY
41*c3cd14fbSJerome Gaillard#   - E.g., CXX_BINARY(mahbinary): foo.o
42*c3cd14fbSJerome Gaillard#   - object.depends targets may be used when a prerequisite is required for an
43*c3cd14fbSJerome Gaillard#     object file. Because object files result in multiple build artifacts to
44*c3cd14fbSJerome Gaillard#     handle PIC and PIE weirdness. E.g.
45*c3cd14fbSJerome Gaillard#       foo.o.depends: generated/dbus.h
46*c3cd14fbSJerome Gaillard#   - TEST(binary) or TEST(CXX_BINARY(binary)) may be used as a prerequisite
47*c3cd14fbSJerome Gaillard#     for the tests target to trigger an automated test run.
48*c3cd14fbSJerome Gaillard#   - CLEAN(file_or_dir) dependency can be added to 'clean'.
49*c3cd14fbSJerome Gaillard#
50*c3cd14fbSJerome Gaillard# If source code is being generated, rules will need to be registered for
51*c3cd14fbSJerome Gaillard# compiling the objects.  This can be done by adding one of the following
52*c3cd14fbSJerome Gaillard# to the Makefile:
53*c3cd14fbSJerome Gaillard#   - For C source files
54*c3cd14fbSJerome Gaillard#   $(eval $(call add_object_rules,sub/dir/gen_a.o sub/dir/b.o,CC,c,CFLAGS))
55*c3cd14fbSJerome Gaillard#   - For C++ source files
56*c3cd14fbSJerome Gaillard#   $(eval $(call add_object_rules,sub/dir/gen_a.o sub/dir/b.o,CXX,cc,CXXFLAGS))
57*c3cd14fbSJerome Gaillard#
58*c3cd14fbSJerome Gaillard# Exported targets meant to have prerequisites added to:
59*c3cd14fbSJerome Gaillard#  - all - Your desired targets should be given
60*c3cd14fbSJerome Gaillard#  - tests - Any TEST(test_binary) targets should be given
61*c3cd14fbSJerome Gaillard#  - FORCE - force the given target to run regardless of changes
62*c3cd14fbSJerome Gaillard#            In most cases, using .PHONY is preferred.
63*c3cd14fbSJerome Gaillard#
64*c3cd14fbSJerome Gaillard# Possible command line variables:
65*c3cd14fbSJerome Gaillard#   - COLOR=[0|1] to set ANSI color output (default: 1)
66*c3cd14fbSJerome Gaillard#   - VERBOSE=[0|1] to hide/show commands (default: 0)
67*c3cd14fbSJerome Gaillard#   - MODE=[opt|dbg|profiling] (default: opt)
68*c3cd14fbSJerome Gaillard#          opt - Enable optimizations for release builds
69*c3cd14fbSJerome Gaillard#          dbg - Turn down optimization for debugging
70*c3cd14fbSJerome Gaillard#          profiling - Turn off optimization and turn on profiling/coverage
71*c3cd14fbSJerome Gaillard#                      support.
72*c3cd14fbSJerome Gaillard#   - ARCH=[x86|arm|supported qemu name] (default: from portage or uname -m)
73*c3cd14fbSJerome Gaillard#   - SPLITDEBUG=[0|1] splits debug info in target.debug (default: 0)
74*c3cd14fbSJerome Gaillard#        If NOSTRIP=1, SPLITDEBUG will never strip the final emitted objects.
75*c3cd14fbSJerome Gaillard#   - NOSTRIP=[0|1] determines if binaries are stripped. (default: 1)
76*c3cd14fbSJerome Gaillard#        NOSTRIP=0 and MODE=opt will also drop -g from the CFLAGS.
77*c3cd14fbSJerome Gaillard#   - VALGRIND=[0|1] runs tests under valgrind (default: 0)
78*c3cd14fbSJerome Gaillard#   - OUT=/path/to/builddir puts all output in given path (default: $PWD)
79*c3cd14fbSJerome Gaillard#   - VALGRIND_ARGS="" supplies extra memcheck arguments
80*c3cd14fbSJerome Gaillard#
81*c3cd14fbSJerome Gaillard# Per-target(-ish) variable:
82*c3cd14fbSJerome Gaillard#   - NEEDS_ROOT=[0|1] allows a TEST() target to run with root.
83*c3cd14fbSJerome Gaillard#     Default is 0 unless it is running under QEmu.
84*c3cd14fbSJerome Gaillard#   - NEEDS_MOUNTS=[0|1] allows a TEST() target running on QEmu to get
85*c3cd14fbSJerome Gaillard#     setup mounts in the $(SYSROOT)
86*c3cd14fbSJerome Gaillard#
87*c3cd14fbSJerome Gaillard# Caveats:
88*c3cd14fbSJerome Gaillard# - Directories or files with spaces in them DO NOT get along with GNU Make.
89*c3cd14fbSJerome Gaillard#   If you need them, all uses of dir/notdir/etc will need to have magic
90*c3cd14fbSJerome Gaillard#   wrappers.  Proceed at risk to your own sanity.
91*c3cd14fbSJerome Gaillard# - External CXXFLAGS and CFLAGS should be passed via the environment since
92*c3cd14fbSJerome Gaillard#   this file does not use 'override' to control them.
93*c3cd14fbSJerome Gaillard# - Our version of GNU Make doesn't seem to support the 'private' variable
94*c3cd14fbSJerome Gaillard#   annotation, so you can't tag a variable private on a wrapping target.
95*c3cd14fbSJerome Gaillard
96*c3cd14fbSJerome Gaillard# Behavior configuration variables
97*c3cd14fbSJerome GaillardSPLITDEBUG ?= 0
98*c3cd14fbSJerome GaillardNOSTRIP ?= 1
99*c3cd14fbSJerome GaillardVALGRIND ?= 0
100*c3cd14fbSJerome GaillardCOLOR ?= 1
101*c3cd14fbSJerome GaillardVERBOSE ?= 0
102*c3cd14fbSJerome GaillardMODE ?= opt
103*c3cd14fbSJerome GaillardCXXEXCEPTIONS ?= 0
104*c3cd14fbSJerome GaillardARCH ?= $(shell uname -m)
105*c3cd14fbSJerome Gaillard
106*c3cd14fbSJerome Gaillard# Put objects in a separate tree based on makefile locations
107*c3cd14fbSJerome Gaillard# This means you can build a tree without touching it:
108*c3cd14fbSJerome Gaillard#   make -C $SRCDIR  # will create ./build-$(MODE)
109*c3cd14fbSJerome Gaillard# Or
110*c3cd14fbSJerome Gaillard#   make -C $SRCDIR OUT=$PWD
111*c3cd14fbSJerome Gaillard# This variable is extended on subdir calls and doesn't need to be re-called.
112*c3cd14fbSJerome GaillardOUT ?= $(PWD)/
113*c3cd14fbSJerome Gaillard
114*c3cd14fbSJerome Gaillard# Make OUT now so we can use realpath.
115*c3cd14fbSJerome Gaillard$(shell mkdir -p "$(OUT)")
116*c3cd14fbSJerome Gaillard
117*c3cd14fbSJerome Gaillard# TODO(wad) Relative paths are resolved against SRC and not the calling dir.
118*c3cd14fbSJerome Gaillard# Ensure a command-line supplied OUT has a slash
119*c3cd14fbSJerome Gaillardoverride OUT := $(realpath $(OUT))/
120*c3cd14fbSJerome Gaillard
121*c3cd14fbSJerome Gaillard# SRC is not meant to be set by the end user, but during make call relocation.
122*c3cd14fbSJerome Gaillard# $(PWD) != $(CURDIR) all the time.
123*c3cd14fbSJerome Gaillardexport SRC ?= $(CURDIR)
124*c3cd14fbSJerome Gaillard
125*c3cd14fbSJerome Gaillard# If BASE_VER is not set, read the libchrome revision number from
126*c3cd14fbSJerome Gaillard# common-mk/BASE_VER file.
127*c3cd14fbSJerome Gaillardifeq ($(strip $(BASE_VER)),)
128*c3cd14fbSJerome GaillardBASE_VER := $(shell cat $(SRC)/../common-mk/BASE_VER)
129*c3cd14fbSJerome Gaillardendif
130*c3cd14fbSJerome Gaillard$(info Using BASE_VER=$(BASE_VER))
131*c3cd14fbSJerome Gaillard
132*c3cd14fbSJerome Gaillard# Re-start in the $(OUT) directory if we're not there.
133*c3cd14fbSJerome Gaillard# We may be invoked using -C or bare and we need to ensure behavior
134*c3cd14fbSJerome Gaillard# is consistent so we check both PWD vs OUT and PWD vs CURDIR.
135*c3cd14fbSJerome Gaillardoverride RELOCATE_BUILD := 0
136*c3cd14fbSJerome Gaillardifneq (${PWD}/,${OUT})
137*c3cd14fbSJerome Gaillardoverride RELOCATE_BUILD := 1
138*c3cd14fbSJerome Gaillardendif
139*c3cd14fbSJerome Gaillard# Make sure we're running with no builtin targets. They cause
140*c3cd14fbSJerome Gaillard# leakage and mayhem!
141*c3cd14fbSJerome Gaillardifneq (${PWD},${CURDIR})
142*c3cd14fbSJerome Gaillardoverride RELOCATE_BUILD := 1
143*c3cd14fbSJerome Gaillard# If we're run from the build dir, don't let it get cleaned up later.
144*c3cd14fbSJerome Gaillardifeq (${PWD}/,${OUT})
145*c3cd14fbSJerome Gaillard$(shell touch "$(PWD)/.dont_delete_on_clean")
146*c3cd14fbSJerome Gaillardendif
147*c3cd14fbSJerome Gaillardendif  # ifneq (${PWD},${CURDIR}
148*c3cd14fbSJerome Gaillard
149*c3cd14fbSJerome Gaillard# "Relocate" if we need to restart without implicit rules.
150*c3cd14fbSJerome Gaillardifeq ($(subst r,,$(MAKEFLAGS)),$(MAKEFLAGS))
151*c3cd14fbSJerome Gaillardoverride RELOCATE_BUILD := 1
152*c3cd14fbSJerome Gaillardendif
153*c3cd14fbSJerome Gaillard
154*c3cd14fbSJerome Gaillardifeq (${RELOCATE_BUILD},1)
155*c3cd14fbSJerome Gaillard# By default, silence build output. Reused below as well.
156*c3cd14fbSJerome GaillardQUIET = @
157*c3cd14fbSJerome Gaillardifeq ($(VERBOSE),1)
158*c3cd14fbSJerome Gaillard  QUIET=
159*c3cd14fbSJerome Gaillardendif
160*c3cd14fbSJerome Gaillard
161*c3cd14fbSJerome Gaillard# This target will override all targets, including prerequisites. To avoid
162*c3cd14fbSJerome Gaillard# calling $(MAKE) once per prereq on the given CMDGOAL, we guard it with a local
163*c3cd14fbSJerome Gaillard# variable.
164*c3cd14fbSJerome GaillardRUN_ONCE := 0
165*c3cd14fbSJerome GaillardMAKECMDGOALS ?= all
166*c3cd14fbSJerome Gaillard# Keep the rules split as newer make does not allow them to be declared
167*c3cd14fbSJerome Gaillard# on the same line.  But the way :: rules work, the _all here will also
168*c3cd14fbSJerome Gaillard# invoke the %:: rule while retaining "_all" as the default.
169*c3cd14fbSJerome Gaillard_all::
170*c3cd14fbSJerome Gaillard%::
171*c3cd14fbSJerome Gaillard	$(if $(filter 0,$(RUN_ONCE)), \
172*c3cd14fbSJerome Gaillard	  cd "$(OUT)" && \
173*c3cd14fbSJerome Gaillard	  $(MAKE) -r -I "$(SRC)" -f "$(CURDIR)/Makefile" \
174*c3cd14fbSJerome Gaillard	    SRC="$(CURDIR)" OUT="$(OUT)" $(foreach g,$(MAKECMDGOALS),"$(g)"),)
175*c3cd14fbSJerome Gaillard	$(eval RUN_ONCE := 1)
176*c3cd14fbSJerome Gaillardpass-to-subcall := 1
177*c3cd14fbSJerome Gaillardendif
178*c3cd14fbSJerome Gaillard
179*c3cd14fbSJerome Gaillardifeq ($(pass-to-subcall),)
180*c3cd14fbSJerome Gaillard
181*c3cd14fbSJerome Gaillard# Only call MODULE if we're in a submodule
182*c3cd14fbSJerome GaillardMODULES_LIST := $(filter-out Makefile %.d,$(MAKEFILE_LIST))
183*c3cd14fbSJerome Gaillardifeq ($(words $(filter-out Makefile common.mk %.d $(SRC)/Makefile \
184*c3cd14fbSJerome Gaillard                           $(SRC)/common.mk,$(MAKEFILE_LIST))),0)
185*c3cd14fbSJerome Gaillard
186*c3cd14fbSJerome Gaillard# All the top-level defines outside of module.mk.
187*c3cd14fbSJerome Gaillard
188*c3cd14fbSJerome Gaillard#
189*c3cd14fbSJerome Gaillard# Helper macros
190*c3cd14fbSJerome Gaillard#
191*c3cd14fbSJerome Gaillard
192*c3cd14fbSJerome Gaillard# Create the directory if it doesn't yet exist.
193*c3cd14fbSJerome Gaillarddefine auto_mkdir
194*c3cd14fbSJerome Gaillard  $(if $(wildcard $(dir $1)),$2,$(QUIET)mkdir -p "$(dir $1)")
195*c3cd14fbSJerome Gaillardendef
196*c3cd14fbSJerome Gaillard
197*c3cd14fbSJerome Gaillard# Creates the actual archive with an index.
198*c3cd14fbSJerome Gaillard# The target $@ must end with .pic.a or .pie.a.
199*c3cd14fbSJerome Gaillarddefine update_archive
200*c3cd14fbSJerome Gaillard  $(call auto_mkdir,$(TARGET_OR_MEMBER))
201*c3cd14fbSJerome Gaillard  $(QUIET)# Create the archive in one step to avoid parallel use accessing it
202*c3cd14fbSJerome Gaillard  $(QUIET)# before all the symbols are present.
203*c3cd14fbSJerome Gaillard  @$(ECHO) "AR		$(subst \
204*c3cd14fbSJerome Gaillard$(SRC)/,,$(^:.o=$(suffix $(basename $(TARGET_OR_MEMBER))).o)) \
205*c3cd14fbSJerome Gaillard-> $(subst $(SRC)/,,$(TARGET_OR_MEMBER))"
206*c3cd14fbSJerome Gaillard  $(QUIET)$(AR) rcs $(TARGET_OR_MEMBER) \
207*c3cd14fbSJerome Gaillard          $(subst $(SRC)/,,$(^:.o=$(suffix $(basename $(TARGET_OR_MEMBER))).o))
208*c3cd14fbSJerome Gaillardendef
209*c3cd14fbSJerome Gaillard
210*c3cd14fbSJerome Gaillard# Default compile from objects using pre-requisites but filters out
211*c3cd14fbSJerome Gaillard# subdirs and .d files.
212*c3cd14fbSJerome Gaillarddefine cc_binary
213*c3cd14fbSJerome Gaillard  $(call COMPILE_BINARY_implementation,CC,$(CFLAGS) $(1),$(EXTRA_FLAGS))
214*c3cd14fbSJerome Gaillardendef
215*c3cd14fbSJerome Gaillard
216*c3cd14fbSJerome Gaillarddefine cxx_binary
217*c3cd14fbSJerome Gaillard  $(call COMPILE_BINARY_implementation,CXX,$(CXXFLAGS) $(1),$(EXTRA_FLAGS))
218*c3cd14fbSJerome Gaillardendef
219*c3cd14fbSJerome Gaillard
220*c3cd14fbSJerome Gaillard# Default compile from objects using pre-requisites but filters out
221*c3cd14fbSJerome Gaillard# subdirs and .d files.
222*c3cd14fbSJerome Gaillarddefine cc_library
223*c3cd14fbSJerome Gaillard  $(call COMPILE_LIBRARY_implementation,CC,$(CFLAGS) $(1),$(EXTRA_FLAGS))
224*c3cd14fbSJerome Gaillardendef
225*c3cd14fbSJerome Gaillarddefine cxx_library
226*c3cd14fbSJerome Gaillard  $(call COMPILE_LIBRARY_implementation,CXX,$(CXXFLAGS) $(1),$(EXTRA_FLAGS))
227*c3cd14fbSJerome Gaillardendef
228*c3cd14fbSJerome Gaillard
229*c3cd14fbSJerome Gaillard# Deletes files silently if they exist. Meant for use in any local
230*c3cd14fbSJerome Gaillard# clean targets.
231*c3cd14fbSJerome Gaillarddefine silent_rm
232*c3cd14fbSJerome Gaillard  $(if $(wildcard $(1)),
233*c3cd14fbSJerome Gaillard  $(QUIET)($(ECHO) -n '$(COLOR_RED)CLEANFILE$(COLOR_RESET)		' && \
234*c3cd14fbSJerome Gaillard    $(ECHO) '$(subst $(OUT)/,,$(wildcard $(1)))' && \
235*c3cd14fbSJerome Gaillard    $(RM) $(1) 2>/dev/null) || true,)
236*c3cd14fbSJerome Gaillardendef
237*c3cd14fbSJerome Gaillarddefine silent_rmdir
238*c3cd14fbSJerome Gaillard  $(if $(wildcard $(1)),
239*c3cd14fbSJerome Gaillard    $(if $(wildcard $(1)/*),
240*c3cd14fbSJerome Gaillard  $(QUIET)# $(1) not empty [$(wildcard $(1)/*)]. Not deleting.,
241*c3cd14fbSJerome Gaillard  $(QUIET)($(ECHO) -n '$(COLOR_RED)CLEANDIR$(COLOR_RESET)		' && \
242*c3cd14fbSJerome Gaillard    $(ECHO) '$(subst $(OUT)/,,$(wildcard $(1)))' && \
243*c3cd14fbSJerome Gaillard    $(RMDIR) $(1) 2>/dev/null) || true),)
244*c3cd14fbSJerome Gaillardendef
245*c3cd14fbSJerome Gaillard
246*c3cd14fbSJerome Gaillard#
247*c3cd14fbSJerome Gaillard# Default variable values
248*c3cd14fbSJerome Gaillard#
249*c3cd14fbSJerome Gaillard
250*c3cd14fbSJerome Gaillard# Only override toolchain vars if they are from make.
251*c3cd14fbSJerome GaillardCROSS_COMPILE ?=
252*c3cd14fbSJerome Gaillarddefine override_var
253*c3cd14fbSJerome Gaillardifneq ($(filter undefined default,$(origin $1)),)
254*c3cd14fbSJerome Gaillard$1 = $(CROSS_COMPILE)$2
255*c3cd14fbSJerome Gaillardendif
256*c3cd14fbSJerome Gaillardendef
257*c3cd14fbSJerome Gaillard$(eval $(call override_var,AR,ar))
258*c3cd14fbSJerome Gaillard$(eval $(call override_var,CC,gcc))
259*c3cd14fbSJerome Gaillard$(eval $(call override_var,CXX,g++))
260*c3cd14fbSJerome Gaillard$(eval $(call override_var,OBJCOPY,objcopy))
261*c3cd14fbSJerome Gaillard$(eval $(call override_var,PKG_CONFIG,pkg-config))
262*c3cd14fbSJerome Gaillard$(eval $(call override_var,RANLIB,ranlib))
263*c3cd14fbSJerome Gaillard$(eval $(call override_var,STRIP,strip))
264*c3cd14fbSJerome Gaillard
265*c3cd14fbSJerome GaillardRMDIR ?= rmdir
266*c3cd14fbSJerome GaillardECHO = /bin/echo -e
267*c3cd14fbSJerome Gaillard
268*c3cd14fbSJerome Gaillardifeq ($(lastword $(subst /, ,$(CC))),clang)
269*c3cd14fbSJerome GaillardCDRIVER = clang
270*c3cd14fbSJerome Gaillardelse
271*c3cd14fbSJerome GaillardCDRIVER = gcc
272*c3cd14fbSJerome Gaillardendif
273*c3cd14fbSJerome Gaillard
274*c3cd14fbSJerome Gaillardifeq ($(lastword $(subst /, ,$(CXX))),clang++)
275*c3cd14fbSJerome GaillardCXXDRIVER = clang
276*c3cd14fbSJerome Gaillardelse
277*c3cd14fbSJerome GaillardCXXDRIVER = gcc
278*c3cd14fbSJerome Gaillardendif
279*c3cd14fbSJerome Gaillard
280*c3cd14fbSJerome Gaillard# Internal macro to support check_XXX macros below.
281*c3cd14fbSJerome Gaillard# Usage: $(call check_compile, [code], [compiler], [code_type], [c_flags],
282*c3cd14fbSJerome Gaillard#               [extra_c_flags], [library_flags], [success_ret], [fail_ret])
283*c3cd14fbSJerome Gaillard# Return: [success_ret] if compile succeeded, otherwise [fail_ret]
284*c3cd14fbSJerome Gaillardcheck_compile = $(shell printf '%b\n' $(1) | \
285*c3cd14fbSJerome Gaillard  $($(2)) $($(4)) -x $(3) $(LDFLAGS) $(5) - $(6) -o /dev/null > /dev/null 2>&1 \
286*c3cd14fbSJerome Gaillard  && echo "$(7)" || echo "$(8)")
287*c3cd14fbSJerome Gaillard
288*c3cd14fbSJerome Gaillard# Helper macro to check whether a test program will compile with the specified
289*c3cd14fbSJerome Gaillard# compiler flags.
290*c3cd14fbSJerome Gaillard# Usage: $(call check_compile_cc, [code], [flags], [alternate_flags])
291*c3cd14fbSJerome Gaillard# Return: [flags] if compile succeeded, otherwise [alternate_flags]
292*c3cd14fbSJerome Gaillardcheck_compile_cc = $(call check_compile,$(1),CC,c,CFLAGS,$(2),,$(2),$(3))
293*c3cd14fbSJerome Gaillardcheck_compile_cxx = $(call check_compile,$(1),CXX,c++,CXXFLAGS,$(2),,$(2),$(3))
294*c3cd14fbSJerome Gaillard
295*c3cd14fbSJerome Gaillard# Helper macro to check whether a test program will compile with the specified
296*c3cd14fbSJerome Gaillard# libraries.
297*c3cd14fbSJerome Gaillard# Usage: $(call check_compile_cc, [code], [library_flags], [alternate_flags])
298*c3cd14fbSJerome Gaillard# Return: [library_flags] if compile succeeded, otherwise [alternate_flags]
299*c3cd14fbSJerome Gaillardcheck_libs_cc = $(call check_compile,$(1),CC,c,CFLAGS,,$(2),$(2),$(3))
300*c3cd14fbSJerome Gaillardcheck_libs_cxx = $(call check_compile,$(1),CXX,c++,CXXFLAGS,,$(2),$(2),$(3))
301*c3cd14fbSJerome Gaillard
302*c3cd14fbSJerome Gaillard# Helper macro to check whether the compiler accepts the specified flags.
303*c3cd14fbSJerome Gaillard# Usage: $(call check_compile_cc, [flags], [alternate_flags])
304*c3cd14fbSJerome Gaillard# Return: [flags] if compile succeeded, otherwise [alternate_flags]
305*c3cd14fbSJerome Gaillardcheck_cc = $(call check_compile_cc,'int main() { return 0; }',$(1),$(2))
306*c3cd14fbSJerome Gaillardcheck_cxx = $(call check_compile_cxx,'int main() { return 0; }',$(1),$(2))
307*c3cd14fbSJerome Gaillard
308*c3cd14fbSJerome Gaillard# Choose the stack protector flags based on whats supported by the compiler.
309*c3cd14fbSJerome GaillardSSP_CFLAGS := $(call check_cc,-fstack-protector-strong)
310*c3cd14fbSJerome Gaillardifeq ($(SSP_CFLAGS),)
311*c3cd14fbSJerome Gaillard SSP_CFLAGS := $(call check_cc,-fstack-protector-all)
312*c3cd14fbSJerome Gaillardendif
313*c3cd14fbSJerome Gaillard
314*c3cd14fbSJerome Gaillard# To update these from an including Makefile:
315*c3cd14fbSJerome Gaillard#  CXXFLAGS += -mahflag  # Append to the list
316*c3cd14fbSJerome Gaillard#  CXXFLAGS := -mahflag $(CXXFLAGS) # Prepend to the list
317*c3cd14fbSJerome Gaillard#  CXXFLAGS := $(filter-out badflag,$(CXXFLAGS)) # Filter out a value
318*c3cd14fbSJerome Gaillard# The same goes for CFLAGS.
319*c3cd14fbSJerome GaillardCOMMON_CFLAGS-gcc := -fvisibility=internal -ggdb3 -Wa,--noexecstack
320*c3cd14fbSJerome GaillardCOMMON_CFLAGS-clang := -fvisibility=hidden -ggdb
321*c3cd14fbSJerome GaillardCOMMON_CFLAGS := -Wall -Werror -fno-strict-aliasing $(SSP_CFLAGS) -O1 -Wformat=2
322*c3cd14fbSJerome GaillardCXXFLAGS += $(COMMON_CFLAGS) $(COMMON_CFLAGS-$(CXXDRIVER))
323*c3cd14fbSJerome GaillardCFLAGS += $(COMMON_CFLAGS) $(COMMON_CFLAGS-$(CDRIVER))
324*c3cd14fbSJerome GaillardCPPFLAGS += -D_FORTIFY_SOURCE=2
325*c3cd14fbSJerome Gaillard
326*c3cd14fbSJerome Gaillard# Enable large file support.
327*c3cd14fbSJerome GaillardCPPFLAGS += -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
328*c3cd14fbSJerome Gaillard
329*c3cd14fbSJerome Gaillard# Disable exceptions based on the CXXEXCEPTIONS setting.
330*c3cd14fbSJerome Gaillardifeq ($(CXXEXCEPTIONS),0)
331*c3cd14fbSJerome Gaillard  CXXFLAGS := $(CXXFLAGS) -fno-exceptions -fno-unwind-tables \
332*c3cd14fbSJerome Gaillard    -fno-asynchronous-unwind-tables
333*c3cd14fbSJerome Gaillardendif
334*c3cd14fbSJerome Gaillard
335*c3cd14fbSJerome Gaillardifeq ($(MODE),opt)
336*c3cd14fbSJerome Gaillard  # Up the optimizations.
337*c3cd14fbSJerome Gaillard  CFLAGS := $(filter-out -O1,$(CFLAGS)) -O2
338*c3cd14fbSJerome Gaillard  CXXFLAGS := $(filter-out -O1,$(CXXFLAGS)) -O2
339*c3cd14fbSJerome Gaillard  # Only drop -g* if symbols aren't desired.
340*c3cd14fbSJerome Gaillard  ifeq ($(NOSTRIP),0)
341*c3cd14fbSJerome Gaillard    # TODO: do we want -fomit-frame-pointer on x86?
342*c3cd14fbSJerome Gaillard    CFLAGS := $(filter-out -ggdb3,$(CFLAGS))
343*c3cd14fbSJerome Gaillard    CXXFLAGS := $(filter-out -ggdb3,$(CXXFLAGS))
344*c3cd14fbSJerome Gaillard  endif
345*c3cd14fbSJerome Gaillardendif
346*c3cd14fbSJerome Gaillard
347*c3cd14fbSJerome Gaillardifeq ($(MODE),profiling)
348*c3cd14fbSJerome Gaillard  CFLAGS := $(CFLAGS) -O0 -g  --coverage
349*c3cd14fbSJerome Gaillard  CXXFLAGS := $(CXXFLAGS) -O0 -g  --coverage
350*c3cd14fbSJerome Gaillard  LDFLAGS := $(LDFLAGS) --coverage
351*c3cd14fbSJerome Gaillardendif
352*c3cd14fbSJerome Gaillard
353*c3cd14fbSJerome GaillardLDFLAGS := $(LDFLAGS) -Wl,-z,relro -Wl,-z,noexecstack -Wl,-z,now
354*c3cd14fbSJerome Gaillard
355*c3cd14fbSJerome Gaillard# Fancy helpers for color if a prompt is defined
356*c3cd14fbSJerome Gaillardifeq ($(COLOR),1)
357*c3cd14fbSJerome GaillardCOLOR_RESET = \x1b[0m
358*c3cd14fbSJerome GaillardCOLOR_GREEN = \x1b[32;01m
359*c3cd14fbSJerome GaillardCOLOR_RED = \x1b[31;01m
360*c3cd14fbSJerome GaillardCOLOR_YELLOW = \x1b[33;01m
361*c3cd14fbSJerome Gaillardendif
362*c3cd14fbSJerome Gaillard
363*c3cd14fbSJerome Gaillard# By default, silence build output.
364*c3cd14fbSJerome GaillardQUIET = @
365*c3cd14fbSJerome Gaillardifeq ($(VERBOSE),1)
366*c3cd14fbSJerome Gaillard  QUIET=
367*c3cd14fbSJerome Gaillardendif
368*c3cd14fbSJerome Gaillard
369*c3cd14fbSJerome Gaillard#
370*c3cd14fbSJerome Gaillard# Implementation macros for compile helpers above
371*c3cd14fbSJerome Gaillard#
372*c3cd14fbSJerome Gaillard
373*c3cd14fbSJerome Gaillard# Useful for dealing with pie-broken toolchains.
374*c3cd14fbSJerome Gaillard# Call make with PIE=0 to disable default PIE use.
375*c3cd14fbSJerome GaillardOBJ_PIE_FLAG = -fPIE
376*c3cd14fbSJerome GaillardCOMPILE_PIE_FLAG = -pie
377*c3cd14fbSJerome Gaillardifeq ($(PIE),0)
378*c3cd14fbSJerome Gaillard  OBJ_PIE_FLAG =
379*c3cd14fbSJerome Gaillard  COMPILE_PIE_FLAG =
380*c3cd14fbSJerome Gaillardendif
381*c3cd14fbSJerome Gaillard
382*c3cd14fbSJerome Gaillard# Favor member targets first for CXX_BINARY(%) magic.
383*c3cd14fbSJerome Gaillard# And strip out nested members if possible.
384*c3cd14fbSJerome GaillardLP := (
385*c3cd14fbSJerome GaillardRP := )
386*c3cd14fbSJerome GaillardTARGET_OR_MEMBER = $(lastword $(subst $(LP), ,$(subst $(RP),,$(or $%,$@))))
387*c3cd14fbSJerome Gaillard
388*c3cd14fbSJerome Gaillard# Default compile from objects using pre-requisites but filters out
389*c3cd14fbSJerome Gaillard# all non-.o files.
390*c3cd14fbSJerome Gaillarddefine COMPILE_BINARY_implementation
391*c3cd14fbSJerome Gaillard  @$(ECHO) "LD$(1)		$(subst $(PWD)/,,$(TARGET_OR_MEMBER))"
392*c3cd14fbSJerome Gaillard  $(call auto_mkdir,$(TARGET_OR_MEMBER))
393*c3cd14fbSJerome Gaillard  $(QUIET)$($(1)) $(COMPILE_PIE_FLAGS) -o $(TARGET_OR_MEMBER) \
394*c3cd14fbSJerome Gaillard    $(2) $(LDFLAGS) \
395*c3cd14fbSJerome Gaillard    $(filter %.o %.a,$(^:.o=.pie.o)) \
396*c3cd14fbSJerome Gaillard    $(foreach so,$(filter %.so,$^),-L$(dir $(so)) \
397*c3cd14fbSJerome Gaillard                            -l$(patsubst lib%,%,$(basename $(notdir $(so))))) \
398*c3cd14fbSJerome Gaillard    $(LDLIBS)
399*c3cd14fbSJerome Gaillard  $(call conditional_strip)
400*c3cd14fbSJerome Gaillard  @$(ECHO) -n "BIN		"
401*c3cd14fbSJerome Gaillard  @$(ECHO) "$(COLOR_GREEN)$(subst $(PWD)/,,$(TARGET_OR_MEMBER))$(COLOR_RESET)"
402*c3cd14fbSJerome Gaillard  @$(ECHO) "	$(COLOR_YELLOW)-----$(COLOR_RESET)"
403*c3cd14fbSJerome Gaillardendef
404*c3cd14fbSJerome Gaillard
405*c3cd14fbSJerome Gaillard# TODO: add version support extracted from PV environment variable
406*c3cd14fbSJerome Gaillard#ifeq ($(PV),9999)
407*c3cd14fbSJerome Gaillard#$(warning PV=$(PV). If shared object versions matter, please force PV=.)
408*c3cd14fbSJerome Gaillard#endif
409*c3cd14fbSJerome Gaillard# Then add -Wl,-soname,$@.$(PV) ?
410*c3cd14fbSJerome Gaillard
411*c3cd14fbSJerome Gaillard# Default compile from objects using pre-requisites but filters out
412*c3cd14fbSJerome Gaillard# all non-.o values. (Remember to add -L$(OUT) -llib)
413*c3cd14fbSJerome GaillardCOMMA := ,
414*c3cd14fbSJerome Gaillarddefine COMPILE_LIBRARY_implementation
415*c3cd14fbSJerome Gaillard  @$(ECHO) "SHARED$(1)	$(subst $(PWD)/,,$(TARGET_OR_MEMBER))"
416*c3cd14fbSJerome Gaillard  $(call auto_mkdir,$(TARGET_OR_MEMBER))
417*c3cd14fbSJerome Gaillard  $(QUIET)$($(1)) -shared -Wl,-E -o $(TARGET_OR_MEMBER) \
418*c3cd14fbSJerome Gaillard    $(2) $(LDFLAGS) \
419*c3cd14fbSJerome Gaillard    $(if $(filter %.a,$^),-Wl$(COMMA)--whole-archive,) \
420*c3cd14fbSJerome Gaillard    $(filter %.o ,$(^:.o=.pic.o)) \
421*c3cd14fbSJerome Gaillard    $(foreach a,$(filter %.a,$^),-L$(dir $(a)) \
422*c3cd14fbSJerome Gaillard                            -l$(patsubst lib%,%,$(basename $(notdir $(a))))) \
423*c3cd14fbSJerome Gaillard    $(foreach so,$(filter %.so,$^),-L$(dir $(so)) \
424*c3cd14fbSJerome Gaillard                            -l$(patsubst lib%,%,$(basename $(notdir $(so))))) \
425*c3cd14fbSJerome Gaillard    $(LDLIBS)
426*c3cd14fbSJerome Gaillard  $(call conditional_strip)
427*c3cd14fbSJerome Gaillard  @$(ECHO) -n "LIB		$(COLOR_GREEN)"
428*c3cd14fbSJerome Gaillard  @$(ECHO) "$(subst $(PWD)/,,$(TARGET_OR_MEMBER))$(COLOR_RESET)"
429*c3cd14fbSJerome Gaillard  @$(ECHO) "	$(COLOR_YELLOW)-----$(COLOR_RESET)"
430*c3cd14fbSJerome Gaillardendef
431*c3cd14fbSJerome Gaillard
432*c3cd14fbSJerome Gaillarddefine conditional_strip
433*c3cd14fbSJerome Gaillard  $(if $(filter 0,$(NOSTRIP)),$(call strip_artifact))
434*c3cd14fbSJerome Gaillardendef
435*c3cd14fbSJerome Gaillard
436*c3cd14fbSJerome Gaillarddefine strip_artifact
437*c3cd14fbSJerome Gaillard  @$(ECHO) "STRIP		$(subst $(OUT)/,,$(TARGET_OR_MEMBER))"
438*c3cd14fbSJerome Gaillard  $(if $(filter 1,$(SPLITDEBUG)), @$(ECHO) -n "DEBUG	"; \
439*c3cd14fbSJerome Gaillard    $(ECHO) "$(COLOR_YELLOW)\
440*c3cd14fbSJerome Gaillard$(subst $(OUT)/,,$(TARGET_OR_MEMBER)).debug$(COLOR_RESET)")
441*c3cd14fbSJerome Gaillard  $(if $(filter 1,$(SPLITDEBUG)), \
442*c3cd14fbSJerome Gaillard    $(QUIET)$(OBJCOPY) --only-keep-debug "$(TARGET_OR_MEMBER)" \
443*c3cd14fbSJerome Gaillard      "$(TARGET_OR_MEMBER).debug")
444*c3cd14fbSJerome Gaillard  $(if $(filter-out dbg,$(MODE)),$(QUIET)$(STRIP) --strip-unneeded \
445*c3cd14fbSJerome Gaillard    "$(TARGET_OR_MEMBER)",)
446*c3cd14fbSJerome Gaillardendef
447*c3cd14fbSJerome Gaillard
448*c3cd14fbSJerome Gaillard#
449*c3cd14fbSJerome Gaillard# Global pattern rules
450*c3cd14fbSJerome Gaillard#
451*c3cd14fbSJerome Gaillard
452*c3cd14fbSJerome Gaillard# Below, the archive member syntax is abused to create fancier
453*c3cd14fbSJerome Gaillard# syntactic sugar for recipe authors that avoids needed to know
454*c3cd14fbSJerome Gaillard# subcall options.  The downside is that make attempts to look
455*c3cd14fbSJerome Gaillard# into the phony archives for timestamps. This will cause the final
456*c3cd14fbSJerome Gaillard# target to be rebuilt/linked on _every_ call to make even when nothing
457*c3cd14fbSJerome Gaillard# has changed.  Until a better way presents itself, we have helpers that
458*c3cd14fbSJerome Gaillard# do the stat check on make's behalf.  Dodgy but simple.
459*c3cd14fbSJerome Gaillarddefine old_or_no_timestamp
460*c3cd14fbSJerome Gaillard  $(if $(realpath $%),,$(1))
461*c3cd14fbSJerome Gaillard  $(if $(shell find $^ -cnewer "$%" 2>/dev/null),$(1))
462*c3cd14fbSJerome Gaillardendef
463*c3cd14fbSJerome Gaillard
464*c3cd14fbSJerome Gaillarddefine check_deps
465*c3cd14fbSJerome Gaillard  $(if $(filter 0,$(words $^)),\
466*c3cd14fbSJerome Gaillard    $(error Missing dependencies or declaration of $@($%)),)
467*c3cd14fbSJerome Gaillardendef
468*c3cd14fbSJerome Gaillard
469*c3cd14fbSJerome Gaillard# Build a cxx target magically
470*c3cd14fbSJerome GaillardCXX_BINARY(%):
471*c3cd14fbSJerome Gaillard	$(call check_deps)
472*c3cd14fbSJerome Gaillard	$(call old_or_no_timestamp,$(call cxx_binary))
473*c3cd14fbSJerome Gaillardclean: CLEAN(CXX_BINARY*)
474*c3cd14fbSJerome Gaillard
475*c3cd14fbSJerome GaillardCC_BINARY(%):
476*c3cd14fbSJerome Gaillard	$(call check_deps)
477*c3cd14fbSJerome Gaillard	$(call old_or_no_timestamp,$(call cc_binary))
478*c3cd14fbSJerome Gaillardclean: CLEAN(CC_BINARY*)
479*c3cd14fbSJerome Gaillard
480*c3cd14fbSJerome GaillardCXX_STATIC_BINARY(%):
481*c3cd14fbSJerome Gaillard	$(call check_deps)
482*c3cd14fbSJerome Gaillard	$(call old_or_no_timestamp,$(call cxx_binary,-static))
483*c3cd14fbSJerome Gaillardclean: CLEAN(CXX_STATIC_BINARY*)
484*c3cd14fbSJerome Gaillard
485*c3cd14fbSJerome GaillardCC_STATIC_BINARY(%):
486*c3cd14fbSJerome Gaillard	$(call check_deps)
487*c3cd14fbSJerome Gaillard	$(call old_or_no_timestamp,$(call cc_binary,-static))
488*c3cd14fbSJerome Gaillardclean: CLEAN(CC_STATIC_BINARY*)
489*c3cd14fbSJerome Gaillard
490*c3cd14fbSJerome GaillardCXX_LIBRARY(%):
491*c3cd14fbSJerome Gaillard	$(call check_deps)
492*c3cd14fbSJerome Gaillard	$(call old_or_no_timestamp,$(call cxx_library))
493*c3cd14fbSJerome Gaillardclean: CLEAN(CXX_LIBRARY*)
494*c3cd14fbSJerome Gaillard
495*c3cd14fbSJerome GaillardCXX_LIBARY(%):
496*c3cd14fbSJerome Gaillard	$(error Typo alert! LIBARY != LIBRARY)
497*c3cd14fbSJerome Gaillard
498*c3cd14fbSJerome GaillardCC_LIBRARY(%):
499*c3cd14fbSJerome Gaillard	$(call check_deps)
500*c3cd14fbSJerome Gaillard	$(call old_or_no_timestamp,$(call cc_library))
501*c3cd14fbSJerome Gaillardclean: CLEAN(CC_LIBRARY*)
502*c3cd14fbSJerome Gaillard
503*c3cd14fbSJerome GaillardCC_LIBARY(%):
504*c3cd14fbSJerome Gaillard	$(error Typo alert! LIBARY != LIBRARY)
505*c3cd14fbSJerome Gaillard
506*c3cd14fbSJerome GaillardCXX_STATIC_LIBRARY(%):
507*c3cd14fbSJerome Gaillard	$(call check_deps)
508*c3cd14fbSJerome Gaillard	$(call old_or_no_timestamp,$(call update_archive))
509*c3cd14fbSJerome Gaillardclean: CLEAN(CXX_STATIC_LIBRARY*)
510*c3cd14fbSJerome Gaillard
511*c3cd14fbSJerome GaillardCXX_STATIC_LIBARY(%):
512*c3cd14fbSJerome Gaillard	$(error Typo alert! LIBARY != LIBRARY)
513*c3cd14fbSJerome Gaillard
514*c3cd14fbSJerome GaillardCC_STATIC_LIBRARY(%):
515*c3cd14fbSJerome Gaillard	$(call check_deps)
516*c3cd14fbSJerome Gaillard	$(call old_or_no_timestamp,$(call update_archive))
517*c3cd14fbSJerome Gaillardclean: CLEAN(CC_STATIC_LIBRARY*)
518*c3cd14fbSJerome Gaillard
519*c3cd14fbSJerome GaillardCC_STATIC_LIBARY(%):
520*c3cd14fbSJerome Gaillard	$(error Typo alert! LIBARY != LIBRARY)
521*c3cd14fbSJerome Gaillard
522*c3cd14fbSJerome Gaillard
523*c3cd14fbSJerome GaillardTEST(%): % qemu_chroot_install
524*c3cd14fbSJerome Gaillard	$(call TEST_implementation)
525*c3cd14fbSJerome Gaillard.PHONY: TEST
526*c3cd14fbSJerome Gaillard
527*c3cd14fbSJerome Gaillard# multiple targets with a wildcard need to share an directory.
528*c3cd14fbSJerome Gaillard# Don't use this directly it just makes sure the directory is removed _after_
529*c3cd14fbSJerome Gaillard# the files are.
530*c3cd14fbSJerome GaillardCLEANFILE(%):
531*c3cd14fbSJerome Gaillard	$(call silent_rm,$(TARGET_OR_MEMBER))
532*c3cd14fbSJerome Gaillard.PHONY: CLEANFILE
533*c3cd14fbSJerome Gaillard
534*c3cd14fbSJerome GaillardCLEAN(%): CLEANFILE(%)
535*c3cd14fbSJerome Gaillard	$(QUIET)# CLEAN($%) meta-target called
536*c3cd14fbSJerome Gaillard	$(if $(filter-out $(PWD)/,$(dir $(abspath $(TARGET_OR_MEMBER)))), \
537*c3cd14fbSJerome Gaillard	  $(call silent_rmdir,$(dir $(abspath $(TARGET_OR_MEMBER)))),\
538*c3cd14fbSJerome Gaillard	  $(QUIET)# Not deleting $(dir $(abspath $(TARGET_OR_MEMBER))) yet.)
539*c3cd14fbSJerome Gaillard.PHONY: CLEAN
540*c3cd14fbSJerome Gaillard
541*c3cd14fbSJerome Gaillard#
542*c3cd14fbSJerome Gaillard# Top-level objects and pattern rules
543*c3cd14fbSJerome Gaillard#
544*c3cd14fbSJerome Gaillard
545*c3cd14fbSJerome Gaillard# All objects for .c files at the top level
546*c3cd14fbSJerome GaillardC_OBJECTS = $(patsubst $(SRC)/%.c,%.o,$(wildcard $(SRC)/*.c))
547*c3cd14fbSJerome Gaillard
548*c3cd14fbSJerome Gaillard
549*c3cd14fbSJerome Gaillard# All objects for .cxx files at the top level
550*c3cd14fbSJerome GaillardCXX_OBJECTS = $(patsubst $(SRC)/%.cc,%.o,$(wildcard $(SRC)/*.cc))
551*c3cd14fbSJerome Gaillard
552*c3cd14fbSJerome Gaillard# Note, the catch-all pattern rules don't work in subdirectories because
553*c3cd14fbSJerome Gaillard# we're building from the $(OUT) directory. At the top-level (here) they will
554*c3cd14fbSJerome Gaillard# work, but we go ahead and match using the module form.  Then we can place a
555*c3cd14fbSJerome Gaillard# generic pattern rule to capture leakage from the main Makefile. (Later in the
556*c3cd14fbSJerome Gaillard# file.)
557*c3cd14fbSJerome Gaillard#
558*c3cd14fbSJerome Gaillard# The reason target specific pattern rules work well for modules,
559*c3cd14fbSJerome Gaillard# MODULE_C_OBJECTS, is because it scopes the behavior to the given target which
560*c3cd14fbSJerome Gaillard# ensures we get a relative directory offset from $(OUT) which otherwise would
561*c3cd14fbSJerome Gaillard# not match without further magic on a per-subdirectory basis.
562*c3cd14fbSJerome Gaillard
563*c3cd14fbSJerome Gaillard# Creates object file rules. Call with eval.
564*c3cd14fbSJerome Gaillard# $(1) list of .o files
565*c3cd14fbSJerome Gaillard# $(2) source type (CC or CXX)
566*c3cd14fbSJerome Gaillard# $(3) source suffix (cc or c)
567*c3cd14fbSJerome Gaillard# $(4) compiler flag name (CFLAGS or CXXFLAGS)
568*c3cd14fbSJerome Gaillard# $(5) source dir: _only_ if $(SRC). Leave blank for obj tree.
569*c3cd14fbSJerome Gaillarddefine add_object_rules
570*c3cd14fbSJerome Gaillard$(patsubst %.o,%.pie.o,$(1)): %.pie.o: $(5)%.$(3) %.o.depends
571*c3cd14fbSJerome Gaillard	$$(call auto_mkdir,$$@)
572*c3cd14fbSJerome Gaillard	$$(call OBJECT_PATTERN_implementation,$(2),\
573*c3cd14fbSJerome Gaillard          $$(basename $$@),$$($(4)) $$(CPPFLAGS) $$(OBJ_PIE_FLAG))
574*c3cd14fbSJerome Gaillard
575*c3cd14fbSJerome Gaillard$(patsubst %.o,%.pic.o,$(1)): %.pic.o: $(5)%.$(3) %.o.depends
576*c3cd14fbSJerome Gaillard	$$(call auto_mkdir,$$@)
577*c3cd14fbSJerome Gaillard	$$(call OBJECT_PATTERN_implementation,$(2),\
578*c3cd14fbSJerome Gaillard          $$(basename $$@),$$($(4)) $$(CPPFLAGS) -fPIC)
579*c3cd14fbSJerome Gaillard
580*c3cd14fbSJerome Gaillard# Placeholder for depends
581*c3cd14fbSJerome Gaillard$(patsubst %.o,%.o.depends,$(1)):
582*c3cd14fbSJerome Gaillard	$$(call auto_mkdir,$$@)
583*c3cd14fbSJerome Gaillard	$$(QUIET)touch "$$@"
584*c3cd14fbSJerome Gaillard
585*c3cd14fbSJerome Gaillard$(1): %.o: %.pic.o %.pie.o
586*c3cd14fbSJerome Gaillard	$$(call auto_mkdir,$$@)
587*c3cd14fbSJerome Gaillard	$$(QUIET)touch "$$@"
588*c3cd14fbSJerome Gaillardendef
589*c3cd14fbSJerome Gaillard
590*c3cd14fbSJerome Gaillarddefine OBJECT_PATTERN_implementation
591*c3cd14fbSJerome Gaillard  @$(ECHO) "$(1)		$(subst $(SRC)/,,$<) -> $(2).o"
592*c3cd14fbSJerome Gaillard  $(call auto_mkdir,$@)
593*c3cd14fbSJerome Gaillard  $(QUIET)$($(1)) -c -MD -MF $(2).d $(3) -o $(2).o $<
594*c3cd14fbSJerome Gaillard  $(QUIET)# Wrap all the deps in $$(wildcard) so a missing header
595*c3cd14fbSJerome Gaillard  $(QUIET)# won't cause weirdness.  First we remove newlines and \,
596*c3cd14fbSJerome Gaillard  $(QUIET)# then wrap it.
597*c3cd14fbSJerome Gaillard  $(QUIET)sed -i -e :j -e '$$!N;s|\\\s*\n| |;tj' \
598*c3cd14fbSJerome Gaillard    -e 's|^\(.*\s*:\s*\)\(.*\)$$|\1 $$\(wildcard \2\)|' $(2).d
599*c3cd14fbSJerome Gaillardendef
600*c3cd14fbSJerome Gaillard
601*c3cd14fbSJerome Gaillard# Now actually register handlers for C(XX)_OBJECTS.
602*c3cd14fbSJerome Gaillard$(eval $(call add_object_rules,$(C_OBJECTS),CC,c,CFLAGS,$(SRC)/))
603*c3cd14fbSJerome Gaillard$(eval $(call add_object_rules,$(CXX_OBJECTS),CXX,cc,CXXFLAGS,$(SRC)/))
604*c3cd14fbSJerome Gaillard
605*c3cd14fbSJerome Gaillard# Disable default pattern rules to help avoid leakage.
606*c3cd14fbSJerome Gaillard# These may already be handled by '-r', but let's keep it to be safe.
607*c3cd14fbSJerome Gaillard%: %.o ;
608*c3cd14fbSJerome Gaillard%.a: %.o ;
609*c3cd14fbSJerome Gaillard%.o: %.c ;
610*c3cd14fbSJerome Gaillard%.o: %.cc ;
611*c3cd14fbSJerome Gaillard
612*c3cd14fbSJerome Gaillard# NOTE: A specific rule for archive objects is avoided because parallel
613*c3cd14fbSJerome Gaillard#       update of the archive causes build flakiness.
614*c3cd14fbSJerome Gaillard# Instead, just make the objects the prerequisites and use update_archive
615*c3cd14fbSJerome Gaillard# To use the foo.a(obj.o) functionality, targets would need to specify the
616*c3cd14fbSJerome Gaillard# explicit object they expect on the prerequisite line.
617*c3cd14fbSJerome Gaillard
618*c3cd14fbSJerome Gaillard#
619*c3cd14fbSJerome Gaillard# Architecture detection and QEMU wrapping
620*c3cd14fbSJerome Gaillard#
621*c3cd14fbSJerome Gaillard
622*c3cd14fbSJerome GaillardHOST_ARCH ?= $(shell uname -m)
623*c3cd14fbSJerome Gaillardoverride ARCH := $(strip $(ARCH))
624*c3cd14fbSJerome Gaillardoverride HOST_ARCH := $(strip $(HOST_ARCH))
625*c3cd14fbSJerome Gaillard# emake will supply "x86" or "arm" for ARCH, but
626*c3cd14fbSJerome Gaillard# if uname -m runs and you get x86_64, then this subst
627*c3cd14fbSJerome Gaillard# will break.
628*c3cd14fbSJerome Gaillardifeq ($(subst x86,i386,$(ARCH)),i386)
629*c3cd14fbSJerome Gaillard  QEMU_ARCH := $(subst x86,i386,$(ARCH))  # x86 -> i386
630*c3cd14fbSJerome Gaillardelse ifeq ($(subst amd64,x86_64,$(ARCH)),x86_64)
631*c3cd14fbSJerome Gaillard  QEMU_ARCH := $(subst amd64,x86_64,$(ARCH))  # amd64 -> x86_64
632*c3cd14fbSJerome Gaillardelse
633*c3cd14fbSJerome Gaillard  QEMU_ARCH = $(ARCH)
634*c3cd14fbSJerome Gaillardendif
635*c3cd14fbSJerome Gaillardoverride QEMU_ARCH := $(strip $(QEMU_ARCH))
636*c3cd14fbSJerome Gaillard
637*c3cd14fbSJerome Gaillard# If we're cross-compiling, try to use qemu for running the tests.
638*c3cd14fbSJerome Gaillardifneq ($(QEMU_ARCH),$(HOST_ARCH))
639*c3cd14fbSJerome Gaillard  ifeq ($(SYSROOT),)
640*c3cd14fbSJerome Gaillard    $(info SYSROOT not defined. qemu-based testing disabled)
641*c3cd14fbSJerome Gaillard  else
642*c3cd14fbSJerome Gaillard    # A SYSROOT is assumed for QEmu use.
643*c3cd14fbSJerome Gaillard    USE_QEMU ?= 1
644*c3cd14fbSJerome Gaillard
645*c3cd14fbSJerome Gaillard    # Allow 64-bit hosts to run 32-bit without qemu.
646*c3cd14fbSJerome Gaillard    ifeq ($(HOST_ARCH),x86_64)
647*c3cd14fbSJerome Gaillard      ifeq ($(QEMU_ARCH),i386)
648*c3cd14fbSJerome Gaillard        USE_QEMU = 0
649*c3cd14fbSJerome Gaillard      endif
650*c3cd14fbSJerome Gaillard    endif
651*c3cd14fbSJerome Gaillard  endif
652*c3cd14fbSJerome Gaillardelse
653*c3cd14fbSJerome Gaillard  USE_QEMU ?= 0
654*c3cd14fbSJerome Gaillardendif
655*c3cd14fbSJerome Gaillard
656*c3cd14fbSJerome Gaillard# Normally we don't need to run as root or do bind mounts, so only
657*c3cd14fbSJerome Gaillard# enable it by default when we're using QEMU.
658*c3cd14fbSJerome GaillardNEEDS_ROOT ?= $(USE_QEMU)
659*c3cd14fbSJerome GaillardNEEDS_MOUNTS ?= $(USE_QEMU)
660*c3cd14fbSJerome Gaillard
661*c3cd14fbSJerome GaillardSYSROOT_OUT = $(OUT)
662*c3cd14fbSJerome Gaillardifneq ($(SYSROOT),)
663*c3cd14fbSJerome Gaillard  SYSROOT_OUT = $(subst $(SYSROOT),,$(OUT))
664*c3cd14fbSJerome Gaillardelse
665*c3cd14fbSJerome Gaillard  # Default to / when all the empty-sysroot logic is done.
666*c3cd14fbSJerome Gaillard  SYSROOT = /
667*c3cd14fbSJerome Gaillardendif
668*c3cd14fbSJerome Gaillard
669*c3cd14fbSJerome GaillardQEMU_NAME = qemu-$(QEMU_ARCH)
670*c3cd14fbSJerome GaillardQEMU_PATH = /build/bin/$(QEMU_NAME)
671*c3cd14fbSJerome GaillardQEMU_SYSROOT_PATH = $(SYSROOT)$(QEMU_PATH)
672*c3cd14fbSJerome GaillardQEMU_SRC_PATH = /usr/bin/$(QEMU_NAME)
673*c3cd14fbSJerome GaillardQEMU_BINFMT_PATH = /proc/sys/fs/binfmt_misc/$(QEMU_NAME)
674*c3cd14fbSJerome GaillardQEMU_REGISTER_PATH = /proc/sys/fs/binfmt_misc/register
675*c3cd14fbSJerome Gaillard
676*c3cd14fbSJerome GaillardQEMU_MAGIC_arm = ":$(QEMU_NAME):M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/build/bin/qemu-arm:"
677*c3cd14fbSJerome Gaillard
678*c3cd14fbSJerome Gaillard
679*c3cd14fbSJerome Gaillard#
680*c3cd14fbSJerome Gaillard# Output full configuration at top level
681*c3cd14fbSJerome Gaillard#
682*c3cd14fbSJerome Gaillard
683*c3cd14fbSJerome Gaillard# Don't show on clean
684*c3cd14fbSJerome Gaillardifneq ($(MAKECMDGOALS),clean)
685*c3cd14fbSJerome Gaillard  $(info build configuration:)
686*c3cd14fbSJerome Gaillard  $(info - OUT=$(OUT))
687*c3cd14fbSJerome Gaillard  $(info - SRC=$(SRC))
688*c3cd14fbSJerome Gaillard  $(info - MODE=$(MODE))
689*c3cd14fbSJerome Gaillard  $(info - SPLITDEBUG=$(SPLITDEBUG))
690*c3cd14fbSJerome Gaillard  $(info - NOSTRIP=$(NOSTRIP))
691*c3cd14fbSJerome Gaillard  $(info - VALGRIND=$(VALGRIND))
692*c3cd14fbSJerome Gaillard  $(info - COLOR=$(COLOR))
693*c3cd14fbSJerome Gaillard  $(info - CXXEXCEPTIONS=$(CXXEXCEPTIONS))
694*c3cd14fbSJerome Gaillard  $(info - ARCH=$(ARCH))
695*c3cd14fbSJerome Gaillard  $(info - QEMU_ARCH=$(QEMU_ARCH))
696*c3cd14fbSJerome Gaillard  $(info - USE_QEMU=$(USE_QEMU))
697*c3cd14fbSJerome Gaillard  $(info - NEEDS_ROOT=$(NEEDS_ROOT))
698*c3cd14fbSJerome Gaillard  $(info - NEEDS_MOUNTS=$(NEEDS_MOUNTS))
699*c3cd14fbSJerome Gaillard  $(info - SYSROOT=$(SYSROOT))
700*c3cd14fbSJerome Gaillard  $(info )
701*c3cd14fbSJerome Gaillardendif
702*c3cd14fbSJerome Gaillard
703*c3cd14fbSJerome Gaillard#
704*c3cd14fbSJerome Gaillard# Standard targets with detection for when they are improperly configured.
705*c3cd14fbSJerome Gaillard#
706*c3cd14fbSJerome Gaillard
707*c3cd14fbSJerome Gaillard# all does not include tests by default
708*c3cd14fbSJerome Gaillardall:
709*c3cd14fbSJerome Gaillard	$(QUIET)(test -z "$^" && \
710*c3cd14fbSJerome Gaillard	$(ECHO) "You must add your targets as 'all' prerequisites") || true
711*c3cd14fbSJerome Gaillard	$(QUIET)test -n "$^"
712*c3cd14fbSJerome Gaillard
713*c3cd14fbSJerome Gaillard# Builds and runs tests for the target arch
714*c3cd14fbSJerome Gaillard# Run them in parallel
715*c3cd14fbSJerome Gaillard# After the test have completed, if profiling, run coverage analysis
716*c3cd14fbSJerome Gaillardtests:
717*c3cd14fbSJerome Gaillardifeq ($(MODE),profiling)
718*c3cd14fbSJerome Gaillard	@$(ECHO) "COVERAGE [$(COLOR_YELLOW)STARTED$(COLOR_RESET)]"
719*c3cd14fbSJerome Gaillard	$(QUIET)FILES="";						\
720*c3cd14fbSJerome Gaillard		for GCNO in `find . -name "*.gcno"`; do			\
721*c3cd14fbSJerome Gaillard			GCDA="$${GCNO%.gcno}.gcda";			\
722*c3cd14fbSJerome Gaillard			if [ -e $${GCDA} ]; then			\
723*c3cd14fbSJerome Gaillard				FILES="$${FILES} $${GCDA}";		\
724*c3cd14fbSJerome Gaillard			fi						\
725*c3cd14fbSJerome Gaillard		done;							\
726*c3cd14fbSJerome Gaillard		if [ -n "$${FILES}" ]; then				\
727*c3cd14fbSJerome Gaillard			gcov -l $${FILES};				\
728*c3cd14fbSJerome Gaillard			lcov --capture --directory .			\
729*c3cd14fbSJerome Gaillard				--output-file=lcov-coverage.info;	\
730*c3cd14fbSJerome Gaillard			genhtml lcov-coverage.info			\
731*c3cd14fbSJerome Gaillard				--output-directory lcov-html;		\
732*c3cd14fbSJerome Gaillard		fi
733*c3cd14fbSJerome Gaillard	@$(ECHO) "COVERAGE [$(COLOR_YELLOW)FINISHED$(COLOR_RESET)]"
734*c3cd14fbSJerome Gaillardendif
735*c3cd14fbSJerome Gaillard.PHONY: tests
736*c3cd14fbSJerome Gaillard
737*c3cd14fbSJerome Gaillardqemu_chroot_install:
738*c3cd14fbSJerome Gaillardifeq ($(USE_QEMU),1)
739*c3cd14fbSJerome Gaillard	$(QUIET)$(ECHO) "QEMU   Preparing $(QEMU_NAME)"
740*c3cd14fbSJerome Gaillard	@# Copying strategy
741*c3cd14fbSJerome Gaillard	@# Compare /usr/bin/qemu inode to /build/$board/build/bin/qemu, if different
742*c3cd14fbSJerome Gaillard	@# hard link to a temporary file, then rename temp to target. This should
743*c3cd14fbSJerome Gaillard	@# ensure that once $QEMU_SYSROOT_PATH exists it will always exist, regardless
744*c3cd14fbSJerome Gaillard	@# of simultaneous test setups.
745*c3cd14fbSJerome Gaillard	$(QUIET)if [[ ! -e $(QEMU_SYSROOT_PATH) || \
746*c3cd14fbSJerome Gaillard	    `stat -c %i $(QEMU_SRC_PATH)` != `stat -c %i $(QEMU_SYSROOT_PATH)` \
747*c3cd14fbSJerome Gaillard	    ]]; then \
748*c3cd14fbSJerome Gaillard	  $(ROOT_CMD) ln -Tf $(QEMU_SRC_PATH) $(QEMU_SYSROOT_PATH).$$$$; \
749*c3cd14fbSJerome Gaillard	  $(ROOT_CMD) mv -Tf $(QEMU_SYSROOT_PATH).$$$$ $(QEMU_SYSROOT_PATH); \
750*c3cd14fbSJerome Gaillard	fi
751*c3cd14fbSJerome Gaillard
752*c3cd14fbSJerome Gaillard	@# Prep the binfmt handler. First mount if needed, then unregister any bad
753*c3cd14fbSJerome Gaillard	@# mappings and then register our mapping.
754*c3cd14fbSJerome Gaillard	@# There may still be some race conditions here where one script de-registers
755*c3cd14fbSJerome Gaillard	@# and another script starts executing before it gets re-registered, however
756*c3cd14fbSJerome Gaillard	@# it should be rare.
757*c3cd14fbSJerome Gaillard	-$(QUIET)[[ -e $(QEMU_REGISTER_PATH) ]] || \
758*c3cd14fbSJerome Gaillard	  $(ROOT_CMD) mount binfmt_misc -t binfmt_misc \
759*c3cd14fbSJerome Gaillard	    /proc/sys/fs/binfmt_misc
760*c3cd14fbSJerome Gaillard
761*c3cd14fbSJerome Gaillard	-$(QUIET)if [[ -e $(QEMU_BINFMT_PATH) && \
762*c3cd14fbSJerome Gaillard	      `awk '$$1 == "interpreter" {print $$NF}' $(QEMU_BINFMT_PATH)` != \
763*c3cd14fbSJerome Gaillard	      "$(QEMU_PATH)" ]]; then \
764*c3cd14fbSJerome Gaillard	  echo -1 | $(ROOT_CMD) tee $(QEMU_BINFMT_PATH) >/dev/null; \
765*c3cd14fbSJerome Gaillard	fi
766*c3cd14fbSJerome Gaillard
767*c3cd14fbSJerome Gaillard	-$(if $(QEMU_MAGIC_$(ARCH)),$(QUIET)[[ -e $(QEMU_BINFMT_PATH) ]] || \
768*c3cd14fbSJerome Gaillard	  echo $(QEMU_MAGIC_$(ARCH)) | $(ROOT_CMD) tee $(QEMU_REGISTER_PATH) \
769*c3cd14fbSJerome Gaillard	    >/dev/null)
770*c3cd14fbSJerome Gaillardendif
771*c3cd14fbSJerome Gaillard.PHONY: qemu_clean qemu_chroot_install
772*c3cd14fbSJerome Gaillard
773*c3cd14fbSJerome Gaillard# TODO(wad) Move to -L $(SYSROOT) and fakechroot when qemu-user
774*c3cd14fbSJerome Gaillard#           doesn't hang traversing /proc from SYSROOT.
775*c3cd14fbSJerome GaillardSUDO_CMD = sudo
776*c3cd14fbSJerome GaillardUNSHARE_CMD = unshare
777*c3cd14fbSJerome GaillardQEMU_CMD =
778*c3cd14fbSJerome GaillardROOT_CMD = $(if $(filter 1,$(NEEDS_ROOT)),$(SUDO_CMD) , )
779*c3cd14fbSJerome GaillardMOUNT_CMD = $(if $(filter 1,$(NEEDS_MOUNTS)),$(ROOT_CMD) mount, \#)
780*c3cd14fbSJerome GaillardUMOUNT_CMD = $(if $(filter 1,$(NEEDS_MOUNTS)),$(ROOT_CMD) umount, \#)
781*c3cd14fbSJerome GaillardQEMU_LDPATH = $(SYSROOT_LDPATH):/lib64:/lib:/usr/lib64:/usr/lib
782*c3cd14fbSJerome GaillardROOT_CMD_LDPATH = $(SYSROOT_LDPATH):$(SYSROOT)/lib64:
783*c3cd14fbSJerome GaillardROOT_CMD_LDPATH := $(ROOT_CMD_LDPATH):$(SYSROOT)/lib:$(SYSROOT)/usr/lib64:
784*c3cd14fbSJerome GaillardROOT_CMD_LDPATH := $(ROOT_CMD_LDPATH):$(SYSROOT)/usr/lib
785*c3cd14fbSJerome Gaillardifeq ($(USE_QEMU),1)
786*c3cd14fbSJerome Gaillard  export QEMU_CMD = \
787*c3cd14fbSJerome Gaillard   $(SUDO_CMD) chroot $(SYSROOT) $(QEMU_PATH) \
788*c3cd14fbSJerome Gaillard   -drop-ld-preload \
789*c3cd14fbSJerome Gaillard   -E LD_LIBRARY_PATH="$(QEMU_LDPATH):$(patsubst $(OUT),,$(LD_DIRS))" \
790*c3cd14fbSJerome Gaillard   -E HOME="$(HOME)" -E SRC="$(SRC)" --
791*c3cd14fbSJerome Gaillard  # USE_QEMU conditional function
792*c3cd14fbSJerome Gaillard  define if_qemu
793*c3cd14fbSJerome Gaillard    $(1)
794*c3cd14fbSJerome Gaillard  endef
795*c3cd14fbSJerome Gaillardelse
796*c3cd14fbSJerome Gaillard  ROOT_CMD = $(if $(filter 1,$(NEEDS_ROOT)),sudo, ) \
797*c3cd14fbSJerome Gaillard    LD_LIBRARY_PATH="$(ROOT_CMD_LDPATH):$(LD_DIRS)"
798*c3cd14fbSJerome Gaillard  define if_qemu
799*c3cd14fbSJerome Gaillard    $(2)
800*c3cd14fbSJerome Gaillard  endef
801*c3cd14fbSJerome Gaillardendif
802*c3cd14fbSJerome Gaillard
803*c3cd14fbSJerome GaillardVALGRIND_CMD =
804*c3cd14fbSJerome Gaillardifeq ($(VALGRIND),1)
805*c3cd14fbSJerome Gaillard  VALGRIND_CMD = /usr/bin/valgrind --tool=memcheck $(VALGRIND_ARGS) --
806*c3cd14fbSJerome Gaillardendif
807*c3cd14fbSJerome Gaillard
808*c3cd14fbSJerome Gaillarddefine TEST_implementation
809*c3cd14fbSJerome Gaillard  $(QUIET)$(call TEST_setup)
810*c3cd14fbSJerome Gaillard  $(QUIET)$(call TEST_run)
811*c3cd14fbSJerome Gaillard  $(QUIET)$(call TEST_teardown)
812*c3cd14fbSJerome Gaillard  $(QUIET)exit $$(cat $(OUT)$(TARGET_OR_MEMBER).status.test)
813*c3cd14fbSJerome Gaillardendef
814*c3cd14fbSJerome Gaillard
815*c3cd14fbSJerome Gaillarddefine TEST_setup
816*c3cd14fbSJerome Gaillard  @$(ECHO) -n "TEST		$(TARGET_OR_MEMBER) "
817*c3cd14fbSJerome Gaillard  @$(ECHO) "[$(COLOR_YELLOW)SETUP$(COLOR_RESET)]"
818*c3cd14fbSJerome Gaillard  $(QUIET)# Setup a target-specific results file
819*c3cd14fbSJerome Gaillard  $(QUIET)(echo > $(OUT)$(TARGET_OR_MEMBER).setup.test)
820*c3cd14fbSJerome Gaillard  $(QUIET)(echo 1 > $(OUT)$(TARGET_OR_MEMBER).status.test)
821*c3cd14fbSJerome Gaillard  $(QUIET)(echo > $(OUT)$(TARGET_OR_MEMBER).cleanup.test)
822*c3cd14fbSJerome Gaillard  $(QUIET)# No setup if we are not using QEMU
823*c3cd14fbSJerome Gaillard  $(QUIET)# TODO(wad) this is racy until we use a vfs namespace
824*c3cd14fbSJerome Gaillard  $(call if_qemu,\
825*c3cd14fbSJerome Gaillard    $(QUIET)(echo "mkdir -p '$(SYSROOT)/proc' '$(SYSROOT)/dev' \
826*c3cd14fbSJerome Gaillard                            '$(SYSROOT)/mnt/host/source'" \
827*c3cd14fbSJerome Gaillard             >> "$(OUT)$(TARGET_OR_MEMBER).setup.test"))
828*c3cd14fbSJerome Gaillard  $(call if_qemu,\
829*c3cd14fbSJerome Gaillard    $(QUIET)(echo "$(MOUNT_CMD) --bind /mnt/host/source \
830*c3cd14fbSJerome Gaillard             '$(SYSROOT)/mnt/host/source'" \
831*c3cd14fbSJerome Gaillard             >> "$(OUT)$(TARGET_OR_MEMBER).setup.test"))
832*c3cd14fbSJerome Gaillard  $(call if_qemu,\
833*c3cd14fbSJerome Gaillard    $(QUIET)(echo "$(MOUNT_CMD) --bind /proc '$(SYSROOT)/proc'" \
834*c3cd14fbSJerome Gaillard             >> "$(OUT)$(TARGET_OR_MEMBER).setup.test"))
835*c3cd14fbSJerome Gaillard  $(call if_qemu,\
836*c3cd14fbSJerome Gaillard    $(QUIET)(echo "$(MOUNT_CMD) --bind /dev '$(SYSROOT)/dev'" \
837*c3cd14fbSJerome Gaillard             >> "$(OUT)$(TARGET_OR_MEMBER).setup.test"))
838*c3cd14fbSJerome Gaillardendef
839*c3cd14fbSJerome Gaillard
840*c3cd14fbSJerome Gaillarddefine TEST_teardown
841*c3cd14fbSJerome Gaillard  @$(ECHO) -n "TEST		$(TARGET_OR_MEMBER) "
842*c3cd14fbSJerome Gaillard  @$(ECHO) "[$(COLOR_YELLOW)TEARDOWN$(COLOR_RESET)]"
843*c3cd14fbSJerome Gaillard  $(call if_qemu, $(QUIET)$(SHELL) "$(OUT)$(TARGET_OR_MEMBER).cleanup.test")
844*c3cd14fbSJerome Gaillardendef
845*c3cd14fbSJerome Gaillard
846*c3cd14fbSJerome Gaillard# Use GTEST_ARGS.[arch] if defined.
847*c3cd14fbSJerome Gaillardoverride GTEST_ARGS.real = \
848*c3cd14fbSJerome Gaillard $(call if_qemu,$(GTEST_ARGS.qemu.$(QEMU_ARCH)),$(GTEST_ARGS.host.$(HOST_ARCH)))
849*c3cd14fbSJerome Gaillard
850*c3cd14fbSJerome Gaillarddefine TEST_run
851*c3cd14fbSJerome Gaillard  @$(ECHO) -n "TEST		$(TARGET_OR_MEMBER) "
852*c3cd14fbSJerome Gaillard  @$(ECHO) "[$(COLOR_GREEN)RUN$(COLOR_RESET)]"
853*c3cd14fbSJerome Gaillard  $(QUIET)(echo 1 > "$(OUT)$(TARGET_OR_MEMBER).status.test")
854*c3cd14fbSJerome Gaillard  $(QUIET)(echo $(ROOT_CMD) SRC="$(SRC)" $(QEMU_CMD) $(VALGRIND_CMD) \
855*c3cd14fbSJerome Gaillard    "$(strip $(call if_qemu, $(SYSROOT_OUT),$(OUT))$(TARGET_OR_MEMBER))" \
856*c3cd14fbSJerome Gaillard      $(if $(filter-out 0,$(words $(GTEST_ARGS.real))),$(GTEST_ARGS.real),\
857*c3cd14fbSJerome Gaillard           $(GTEST_ARGS)) >> "$(OUT)$(TARGET_OR_MEMBER).setup.test")
858*c3cd14fbSJerome Gaillard  -$(QUIET)$(call if_qemu,$(SUDO_CMD) $(UNSHARE_CMD) -m) $(SHELL) \
859*c3cd14fbSJerome Gaillard      $(OUT)$(TARGET_OR_MEMBER).setup.test \
860*c3cd14fbSJerome Gaillard  && echo 0 > "$(OUT)$(TARGET_OR_MEMBER).status.test"
861*c3cd14fbSJerome Gaillardendef
862*c3cd14fbSJerome Gaillard
863*c3cd14fbSJerome Gaillard# Recursive list reversal so that we get RMDIR_ON_CLEAN in reverse order.
864*c3cd14fbSJerome Gaillarddefine reverse
865*c3cd14fbSJerome Gaillard$(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1))
866*c3cd14fbSJerome Gaillardendef
867*c3cd14fbSJerome Gaillard
868*c3cd14fbSJerome Gaillardclean: qemu_clean
869*c3cd14fbSJerome Gaillardclean: CLEAN($(OUT)*.d) CLEAN($(OUT)*.o) CLEAN($(OUT)*.debug)
870*c3cd14fbSJerome Gaillardclean: CLEAN($(OUT)*.test) CLEAN($(OUT)*.depends)
871*c3cd14fbSJerome Gaillardclean: CLEAN($(OUT)*.gcno) CLEAN($(OUT)*.gcda) CLEAN($(OUT)*.gcov)
872*c3cd14fbSJerome Gaillardclean: CLEAN($(OUT)lcov-coverage.info) CLEAN($(OUT)lcov-html)
873*c3cd14fbSJerome Gaillard
874*c3cd14fbSJerome Gaillardclean:
875*c3cd14fbSJerome Gaillard	$(QUIET)# Always delete the containing directory last.
876*c3cd14fbSJerome Gaillard	$(call silent_rmdir,$(OUT))
877*c3cd14fbSJerome Gaillard
878*c3cd14fbSJerome GaillardFORCE: ;
879*c3cd14fbSJerome Gaillard# Empty rule for use when no special targets are needed, like large_tests
880*c3cd14fbSJerome GaillardNONE:
881*c3cd14fbSJerome Gaillard
882*c3cd14fbSJerome Gaillard.PHONY: clean NONE valgrind NONE
883*c3cd14fbSJerome Gaillard.DEFAULT_GOAL  :=  all
884*c3cd14fbSJerome Gaillard# Don't let make blow away "intermediates"
885*c3cd14fbSJerome Gaillard.PRECIOUS: %.pic.o %.pie.o %.a %.pic.a %.pie.a %.test
886*c3cd14fbSJerome Gaillard
887*c3cd14fbSJerome Gaillard# Start accruing build info
888*c3cd14fbSJerome GaillardOUT_DIRS = $(OUT)
889*c3cd14fbSJerome GaillardLD_DIRS = $(OUT)
890*c3cd14fbSJerome GaillardSRC_DIRS = $(SRC)
891*c3cd14fbSJerome Gaillard
892*c3cd14fbSJerome Gaillardinclude $(wildcard $(OUT)*.d)
893*c3cd14fbSJerome GaillardSUBMODULE_DIRS = $(wildcard $(SRC)/*/module.mk)
894*c3cd14fbSJerome Gaillardinclude $(SUBMODULE_DIRS)
895*c3cd14fbSJerome Gaillard
896*c3cd14fbSJerome Gaillard
897*c3cd14fbSJerome Gaillardelse  ## In duplicate inclusions of common.mk
898*c3cd14fbSJerome Gaillard
899*c3cd14fbSJerome Gaillard# Get the current inclusion directory without a trailing slash
900*c3cd14fbSJerome GaillardMODULE := $(patsubst %/,%, \
901*c3cd14fbSJerome Gaillard           $(dir $(lastword $(filter-out %common.mk,$(MAKEFILE_LIST)))))
902*c3cd14fbSJerome GaillardMODULE := $(subst $(SRC)/,,$(MODULE))
903*c3cd14fbSJerome GaillardMODULE_NAME := $(subst /,_,$(MODULE))
904*c3cd14fbSJerome Gaillard#VPATH := $(MODULE):$(VPATH)
905*c3cd14fbSJerome Gaillard
906*c3cd14fbSJerome Gaillard
907*c3cd14fbSJerome Gaillard# Depth first
908*c3cd14fbSJerome Gaillard$(eval OUT_DIRS += $(OUT)$(MODULE))
909*c3cd14fbSJerome Gaillard$(eval SRC_DIRS += $(OUT)$(MODULE))
910*c3cd14fbSJerome Gaillard$(eval LD_DIRS := $(LD_DIRS):$(OUT)$(MODULE))
911*c3cd14fbSJerome Gaillard
912*c3cd14fbSJerome Gaillard# Add the defaults from this dir to rm_clean
913*c3cd14fbSJerome Gaillardclean: CLEAN($(OUT)$(MODULE)/*.d) CLEAN($(OUT)$(MODULE)/*.o)
914*c3cd14fbSJerome Gaillardclean: CLEAN($(OUT)$(MODULE)/*.debug) CLEAN($(OUT)$(MODULE)/*.test)
915*c3cd14fbSJerome Gaillardclean: CLEAN($(OUT)$(MODULE)/*.depends)
916*c3cd14fbSJerome Gaillardclean: CLEAN($(OUT)$(MODULE)/*.gcno) CLEAN($(OUT)$(MODULE)/*.gcda)
917*c3cd14fbSJerome Gaillardclean: CLEAN($(OUT)$(MODULE)/*.gcov) CLEAN($(OUT)lcov-coverage.info)
918*c3cd14fbSJerome Gaillardclean: CLEAN($(OUT)lcov-html)
919*c3cd14fbSJerome Gaillard
920*c3cd14fbSJerome Gaillard$(info + submodule: $(MODULE_NAME))
921*c3cd14fbSJerome Gaillard# We must eval otherwise they may be dropped.
922*c3cd14fbSJerome GaillardMODULE_C_OBJECTS = $(patsubst $(SRC)/$(MODULE)/%.c,$(MODULE)/%.o,\
923*c3cd14fbSJerome Gaillard  $(wildcard $(SRC)/$(MODULE)/*.c))
924*c3cd14fbSJerome Gaillard$(eval $(MODULE_NAME)_C_OBJECTS ?= $(MODULE_C_OBJECTS))
925*c3cd14fbSJerome GaillardMODULE_CXX_OBJECTS = $(patsubst $(SRC)/$(MODULE)/%.cc,$(MODULE)/%.o,\
926*c3cd14fbSJerome Gaillard  $(wildcard $(SRC)/$(MODULE)/*.cc))
927*c3cd14fbSJerome Gaillard$(eval $(MODULE_NAME)_CXX_OBJECTS ?= $(MODULE_CXX_OBJECTS))
928*c3cd14fbSJerome Gaillard
929*c3cd14fbSJerome Gaillard# Note, $(MODULE) is implicit in the path to the %.c.
930*c3cd14fbSJerome Gaillard# See $(C_OBJECTS) for more details.
931*c3cd14fbSJerome Gaillard# Register rules for the module objects.
932*c3cd14fbSJerome Gaillard$(eval $(call add_object_rules,$(MODULE_C_OBJECTS),CC,c,CFLAGS,$(SRC)/))
933*c3cd14fbSJerome Gaillard$(eval $(call add_object_rules,$(MODULE_CXX_OBJECTS),CXX,cc,CXXFLAGS,$(SRC)/))
934*c3cd14fbSJerome Gaillard
935*c3cd14fbSJerome Gaillard# Continue recursive inclusion of module.mk files
936*c3cd14fbSJerome GaillardSUBMODULE_DIRS = $(wildcard $(SRC)/$(MODULE)/*/module.mk)
937*c3cd14fbSJerome Gaillardinclude $(wildcard $(OUT)$(MODULE)/*.d)
938*c3cd14fbSJerome Gaillardinclude $(SUBMODULE_DIRS)
939*c3cd14fbSJerome Gaillard
940*c3cd14fbSJerome Gaillardendif
941*c3cd14fbSJerome Gaillardendif  ## pass-to-subcall wrapper for relocating the call directory
942