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 ARCH ?= $(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. 112 OUT ?= $(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 119 override 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. 123 export SRC ?= $(CURDIR) 124 125 # If BASE_VER is not set, read the libchrome revision number from 126 # common-mk/BASE_VER file. 127 ifeq ($(strip $(BASE_VER)),) 128 BASE_VER := $(shell cat $(SRC)/../common-mk/BASE_VER) 129 endif 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. 135 override RELOCATE_BUILD := 0 136 ifneq (${PWD}/,${OUT}) 137 override RELOCATE_BUILD := 1 138 endif 139 # Make sure we're running with no builtin targets. They cause 140 # leakage and mayhem! 141 ifneq (${PWD},${CURDIR}) 142 override RELOCATE_BUILD := 1 143 # If we're run from the build dir, don't let it get cleaned up later. 144 ifeq (${PWD}/,${OUT}) 145 $(shell touch "$(PWD)/.dont_delete_on_clean") 146 endif 147 endif # ifneq (${PWD},${CURDIR} 148 149 # "Relocate" if we need to restart without implicit rules. 150 ifeq ($(subst r,,$(MAKEFLAGS)),$(MAKEFLAGS)) 151 override RELOCATE_BUILD := 1 152 endif 153 154 ifeq (${RELOCATE_BUILD},1) 155 # By default, silence build output. Reused below as well. 156 QUIET = @ 157 ifeq ($(VERBOSE),1) 158 QUIET= 159 endif 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. 164 RUN_ONCE := 0 165 MAKECMDGOALS ?= 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) 176 pass-to-subcall := 1 177 endif 178 179 ifeq ($(pass-to-subcall),) 180 181 # Only call MODULE if we're in a submodule 182 MODULES_LIST := $(filter-out Makefile %.d,$(MAKEFILE_LIST)) 183 ifeq ($(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. 193 define auto_mkdir 194 $(if $(wildcard $(dir $1)),$2,$(QUIET)mkdir -p "$(dir $1)") 195 endef 196 197 # Creates the actual archive with an index. 198 # The target $@ must end with .pic.a or .pie.a. 199 define 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)) 208 endef 209 210 # Default compile from objects using pre-requisites but filters out 211 # subdirs and .d files. 212 define cc_binary 213 $(call COMPILE_BINARY_implementation,CC,$(CFLAGS) $(1),$(EXTRA_FLAGS)) 214 endef 215 216 define cxx_binary 217 $(call COMPILE_BINARY_implementation,CXX,$(CXXFLAGS) $(1),$(EXTRA_FLAGS)) 218 endef 219 220 # Default compile from objects using pre-requisites but filters out 221 # subdirs and .d files. 222 define cc_library 223 $(call COMPILE_LIBRARY_implementation,CC,$(CFLAGS) $(1),$(EXTRA_FLAGS)) 224 endef 225 define cxx_library 226 $(call COMPILE_LIBRARY_implementation,CXX,$(CXXFLAGS) $(1),$(EXTRA_FLAGS)) 227 endef 228 229 # Deletes files silently if they exist. Meant for use in any local 230 # clean targets. 231 define 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,) 236 endef 237 define 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),) 244 endef 245 246 # 247 # Default variable values 248 # 249 250 # Only override toolchain vars if they are from make. 251 CROSS_COMPILE ?= 252 define override_var 253 ifneq ($(filter undefined default,$(origin $1)),) 254 $1 = $(CROSS_COMPILE)$2 255 endif 256 endef 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 265 RMDIR ?= rmdir 266 ECHO = /bin/echo -e 267 268 ifeq ($(lastword $(subst /, ,$(CC))),clang) 269 CDRIVER = clang 270 else 271 CDRIVER = gcc 272 endif 273 274 ifeq ($(lastword $(subst /, ,$(CXX))),clang++) 275 CXXDRIVER = clang 276 else 277 CXXDRIVER = gcc 278 endif 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] 284 check_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] 292 check_compile_cc = $(call check_compile,$(1),CC,c,CFLAGS,$(2),,$(2),$(3)) 293 check_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] 299 check_libs_cc = $(call check_compile,$(1),CC,c,CFLAGS,,$(2),$(2),$(3)) 300 check_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] 305 check_cc = $(call check_compile_cc,'int main() { return 0; }',$(1),$(2)) 306 check_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. 309 SSP_CFLAGS := $(call check_cc,-fstack-protector-strong) 310 ifeq ($(SSP_CFLAGS),) 311 SSP_CFLAGS := $(call check_cc,-fstack-protector-all) 312 endif 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. 319 COMMON_CFLAGS-gcc := -fvisibility=internal -ggdb3 -Wa,--noexecstack 320 COMMON_CFLAGS-clang := -fvisibility=hidden -ggdb 321 COMMON_CFLAGS := -Wall -Werror -fno-strict-aliasing $(SSP_CFLAGS) -O1 -Wformat=2 322 CXXFLAGS += $(COMMON_CFLAGS) $(COMMON_CFLAGS-$(CXXDRIVER)) 323 CFLAGS += $(COMMON_CFLAGS) $(COMMON_CFLAGS-$(CDRIVER)) 324 CPPFLAGS += -D_FORTIFY_SOURCE=2 325 326 # Enable large file support. 327 CPPFLAGS += -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE 328 329 # Disable exceptions based on the CXXEXCEPTIONS setting. 330 ifeq ($(CXXEXCEPTIONS),0) 331 CXXFLAGS := $(CXXFLAGS) -fno-exceptions -fno-unwind-tables \ 332 -fno-asynchronous-unwind-tables 333 endif 334 335 ifeq ($(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 345 endif 346 347 ifeq ($(MODE),profiling) 348 CFLAGS := $(CFLAGS) -O0 -g --coverage 349 CXXFLAGS := $(CXXFLAGS) -O0 -g --coverage 350 LDFLAGS := $(LDFLAGS) --coverage 351 endif 352 353 LDFLAGS := $(LDFLAGS) -Wl,-z,relro -Wl,-z,noexecstack -Wl,-z,now 354 355 # Fancy helpers for color if a prompt is defined 356 ifeq ($(COLOR),1) 357 COLOR_RESET = \x1b[0m 358 COLOR_GREEN = \x1b[32;01m 359 COLOR_RED = \x1b[31;01m 360 COLOR_YELLOW = \x1b[33;01m 361 endif 362 363 # By default, silence build output. 364 QUIET = @ 365 ifeq ($(VERBOSE),1) 366 QUIET= 367 endif 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. 375 OBJ_PIE_FLAG = -fPIE 376 COMPILE_PIE_FLAG = -pie 377 ifeq ($(PIE),0) 378 OBJ_PIE_FLAG = 379 COMPILE_PIE_FLAG = 380 endif 381 382 # Favor member targets first for CXX_BINARY(%) magic. 383 # And strip out nested members if possible. 384 LP := ( 385 RP := ) 386 TARGET_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. 390 define 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)" 403 endef 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) 413 COMMA := , 414 define 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)" 430 endef 431 432 define conditional_strip 433 $(if $(filter 0,$(NOSTRIP)),$(call strip_artifact)) 434 endef 435 436 define 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)",) 446 endef 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. 459 define old_or_no_timestamp 460 $(if $(realpath $%),,$(1)) 461 $(if $(shell find $^ -cnewer "$%" 2>/dev/null),$(1)) 462 endef 463 464 define check_deps 465 $(if $(filter 0,$(words $^)),\ 466 $(error Missing dependencies or declaration of $@($%)),) 467 endef 468 469 # Build a cxx target magically 470 CXX_BINARY(%): 471 $(call check_deps) 472 $(call old_or_no_timestamp,$(call cxx_binary)) 473 clean: CLEAN(CXX_BINARY*) 474 475 CC_BINARY(%): 476 $(call check_deps) 477 $(call old_or_no_timestamp,$(call cc_binary)) 478 clean: CLEAN(CC_BINARY*) 479 480 CXX_STATIC_BINARY(%): 481 $(call check_deps) 482 $(call old_or_no_timestamp,$(call cxx_binary,-static)) 483 clean: CLEAN(CXX_STATIC_BINARY*) 484 485 CC_STATIC_BINARY(%): 486 $(call check_deps) 487 $(call old_or_no_timestamp,$(call cc_binary,-static)) 488 clean: CLEAN(CC_STATIC_BINARY*) 489 490 CXX_LIBRARY(%): 491 $(call check_deps) 492 $(call old_or_no_timestamp,$(call cxx_library)) 493 clean: CLEAN(CXX_LIBRARY*) 494 495 CXX_LIBARY(%): 496 $(error Typo alert! LIBARY != LIBRARY) 497 498 CC_LIBRARY(%): 499 $(call check_deps) 500 $(call old_or_no_timestamp,$(call cc_library)) 501 clean: CLEAN(CC_LIBRARY*) 502 503 CC_LIBARY(%): 504 $(error Typo alert! LIBARY != LIBRARY) 505 506 CXX_STATIC_LIBRARY(%): 507 $(call check_deps) 508 $(call old_or_no_timestamp,$(call update_archive)) 509 clean: CLEAN(CXX_STATIC_LIBRARY*) 510 511 CXX_STATIC_LIBARY(%): 512 $(error Typo alert! LIBARY != LIBRARY) 513 514 CC_STATIC_LIBRARY(%): 515 $(call check_deps) 516 $(call old_or_no_timestamp,$(call update_archive)) 517 clean: CLEAN(CC_STATIC_LIBRARY*) 518 519 CC_STATIC_LIBARY(%): 520 $(error Typo alert! LIBARY != LIBRARY) 521 522 523 TEST(%): % 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. 530 CLEANFILE(%): 531 $(call silent_rm,$(TARGET_OR_MEMBER)) 532 .PHONY: CLEANFILE 533 534 CLEAN(%): 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 546 C_OBJECTS = $(patsubst $(SRC)/%.c,%.o,$(wildcard $(SRC)/*.c)) 547 548 549 # All objects for .cxx files at the top level 550 CXX_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. 569 define 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 "$$@" 588 endef 589 590 define 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 599 endef 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 622 HOST_ARCH ?= $(shell uname -m) 623 override ARCH := $(strip $(ARCH)) 624 override 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. 628 ifeq ($(subst x86,i386,$(ARCH)),i386) 629 QEMU_ARCH := $(subst x86,i386,$(ARCH)) # x86 -> i386 630 else ifeq ($(subst amd64,x86_64,$(ARCH)),x86_64) 631 QEMU_ARCH := $(subst amd64,x86_64,$(ARCH)) # amd64 -> x86_64 632 else 633 QEMU_ARCH = $(ARCH) 634 endif 635 override QEMU_ARCH := $(strip $(QEMU_ARCH)) 636 637 # If we're cross-compiling, try to use qemu for running the tests. 638 ifneq ($(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 652 else 653 USE_QEMU ?= 0 654 endif 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. 658 NEEDS_ROOT ?= $(USE_QEMU) 659 NEEDS_MOUNTS ?= $(USE_QEMU) 660 661 SYSROOT_OUT = $(OUT) 662 ifneq ($(SYSROOT),) 663 SYSROOT_OUT = $(subst $(SYSROOT),,$(OUT)) 664 else 665 # Default to / when all the empty-sysroot logic is done. 666 SYSROOT = / 667 endif 668 669 QEMU_NAME = qemu-$(QEMU_ARCH) 670 QEMU_PATH = /build/bin/$(QEMU_NAME) 671 QEMU_SYSROOT_PATH = $(SYSROOT)$(QEMU_PATH) 672 QEMU_SRC_PATH = /usr/bin/$(QEMU_NAME) 673 QEMU_BINFMT_PATH = /proc/sys/fs/binfmt_misc/$(QEMU_NAME) 674 QEMU_REGISTER_PATH = /proc/sys/fs/binfmt_misc/register 675 676 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:" 677 678 679 # 680 # Output full configuration at top level 681 # 682 683 # Don't show on clean 684 ifneq ($(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 ) 701 endif 702 703 # 704 # Standard targets with detection for when they are improperly configured. 705 # 706 707 # all does not include tests by default 708 all: 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 716 tests: 717 ifeq ($(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)]" 734 endif 735 .PHONY: tests 736 737 qemu_chroot_install: 738 ifeq ($(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) 770 endif 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. 775 SUDO_CMD = sudo 776 UNSHARE_CMD = unshare 777 QEMU_CMD = 778 ROOT_CMD = $(if $(filter 1,$(NEEDS_ROOT)),$(SUDO_CMD) , ) 779 MOUNT_CMD = $(if $(filter 1,$(NEEDS_MOUNTS)),$(ROOT_CMD) mount, \#) 780 UMOUNT_CMD = $(if $(filter 1,$(NEEDS_MOUNTS)),$(ROOT_CMD) umount, \#) 781 QEMU_LDPATH = $(SYSROOT_LDPATH):/lib64:/lib:/usr/lib64:/usr/lib 782 ROOT_CMD_LDPATH = $(SYSROOT_LDPATH):$(SYSROOT)/lib64: 783 ROOT_CMD_LDPATH := $(ROOT_CMD_LDPATH):$(SYSROOT)/lib:$(SYSROOT)/usr/lib64: 784 ROOT_CMD_LDPATH := $(ROOT_CMD_LDPATH):$(SYSROOT)/usr/lib 785 ifeq ($(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 795 else 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 801 endif 802 803 VALGRIND_CMD = 804 ifeq ($(VALGRIND),1) 805 VALGRIND_CMD = /usr/bin/valgrind --tool=memcheck $(VALGRIND_ARGS) -- 806 endif 807 808 define 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) 813 endef 814 815 define 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")) 838 endef 839 840 define 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") 844 endef 845 846 # Use GTEST_ARGS.[arch] if defined. 847 override GTEST_ARGS.real = \ 848 $(call if_qemu,$(GTEST_ARGS.qemu.$(QEMU_ARCH)),$(GTEST_ARGS.host.$(HOST_ARCH))) 849 850 define 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" 861 endef 862 863 # Recursive list reversal so that we get RMDIR_ON_CLEAN in reverse order. 864 define reverse 865 $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1)) 866 endef 867 868 clean: qemu_clean 869 clean: CLEAN($(OUT)*.d) CLEAN($(OUT)*.o) CLEAN($(OUT)*.debug) 870 clean: CLEAN($(OUT)*.test) CLEAN($(OUT)*.depends) 871 clean: CLEAN($(OUT)*.gcno) CLEAN($(OUT)*.gcda) CLEAN($(OUT)*.gcov) 872 clean: CLEAN($(OUT)lcov-coverage.info) CLEAN($(OUT)lcov-html) 873 874 clean: 875 $(QUIET)# Always delete the containing directory last. 876 $(call silent_rmdir,$(OUT)) 877 878 FORCE: ; 879 # Empty rule for use when no special targets are needed, like large_tests 880 NONE: 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 888 OUT_DIRS = $(OUT) 889 LD_DIRS = $(OUT) 890 SRC_DIRS = $(SRC) 891 892 include $(wildcard $(OUT)*.d) 893 SUBMODULE_DIRS = $(wildcard $(SRC)/*/module.mk) 894 include $(SUBMODULE_DIRS) 895 896 897 else ## In duplicate inclusions of common.mk 898 899 # Get the current inclusion directory without a trailing slash 900 MODULE := $(patsubst %/,%, \ 901 $(dir $(lastword $(filter-out %common.mk,$(MAKEFILE_LIST))))) 902 MODULE := $(subst $(SRC)/,,$(MODULE)) 903 MODULE_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 913 clean: CLEAN($(OUT)$(MODULE)/*.d) CLEAN($(OUT)$(MODULE)/*.o) 914 clean: CLEAN($(OUT)$(MODULE)/*.debug) CLEAN($(OUT)$(MODULE)/*.test) 915 clean: CLEAN($(OUT)$(MODULE)/*.depends) 916 clean: CLEAN($(OUT)$(MODULE)/*.gcno) CLEAN($(OUT)$(MODULE)/*.gcda) 917 clean: CLEAN($(OUT)$(MODULE)/*.gcov) CLEAN($(OUT)lcov-coverage.info) 918 clean: CLEAN($(OUT)lcov-html) 919 920 $(info + submodule: $(MODULE_NAME)) 921 # We must eval otherwise they may be dropped. 922 MODULE_C_OBJECTS = $(patsubst $(SRC)/$(MODULE)/%.c,$(MODULE)/%.o,\ 923 $(wildcard $(SRC)/$(MODULE)/*.c)) 924 $(eval $(MODULE_NAME)_C_OBJECTS ?= $(MODULE_C_OBJECTS)) 925 MODULE_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 936 SUBMODULE_DIRS = $(wildcard $(SRC)/$(MODULE)/*/module.mk) 937 include $(wildcard $(OUT)$(MODULE)/*.d) 938 include $(SUBMODULE_DIRS) 939 940 endif 941 endif ## pass-to-subcall wrapper for relocating the call directory 942