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