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