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