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