Dennis Kempin | bf37f5d | 2012-06-14 18:44:07 -0700 | [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 | # http://git.chromium.org/gitweb/?p=chromiumos/platform/common-mk.git |
| 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/gen_b.o,CC,c)) |
| 55 | # - For C++ source files |
| 56 | # $(eval $(call add_object_rules,sub/dir/gen_a.o sub/dir/gen_b.o,CXX,cc)) |
| 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=dbg to turn down optimizations (default: opt) |
| 68 | # - ARCH=[x86|arm|supported qemu name] (default: from portage or uname -m) |
| 69 | # - SPLITDEBUG=[0|1] splits debug info in target.debug (default: 0) |
| 70 | # If NOSTRIP=1, SPLITDEBUG will never strip the final emitted objects. |
| 71 | # - NOSTRIP=[0|1] determines if binaries are stripped. (default: 1) |
| 72 | # NOSTRIP=0 and MODE=opt will also drop -g from the CFLAGS. |
| 73 | # - VALGRIND=[0|1] runs tests under valgrind (default: 0) |
| 74 | # - OUT=/path/to/builddir puts all output in given path (default: $PWD) |
| 75 | # - VALGRIND_ARGS="" supplies extra memcheck arguments |
| 76 | # |
| 77 | # Per-target(-ish) variable: |
| 78 | # - NEEDS_ROOT=[0|1] allows a TEST() target to run with root. |
| 79 | # Default is 0 unless it is running under QEmu. |
| 80 | # - NEEDS_MOUNTS=[0|1] allows a TEST() target running on QEmu to get |
| 81 | # setup mounts in the $(SYSROOT) |
| 82 | # |
| 83 | # Caveats: |
| 84 | # - Directories or files with spaces in them DO NOT get along with GNU Make. |
| 85 | # If you need them, all uses of dir/notdir/etc will need to have magic |
| 86 | # wrappers. Proceed at risk to your own sanity. |
| 87 | # - External CXXFLAGS and CFLAGS should be passed via the environment since |
| 88 | # this file does not use 'override' to control them. |
| 89 | # - Our version of GNU Make doesn't seem to support the 'private' variable |
| 90 | # annotation, so you can't tag a variable private on a wrapping target. |
| 91 | |
| 92 | # Behavior configuration variables |
| 93 | SPLITDEBUG ?= 0 |
| 94 | NOSTRIP ?= 1 |
| 95 | VALGRIND ?= 0 |
| 96 | COLOR ?= 1 |
| 97 | VERBOSE ?= 0 |
| 98 | MODE ?= opt |
| 99 | ARCH ?= $(shell uname -m) |
| 100 | # TODO: profiling support not completed. |
| 101 | PROFILING ?= 0 |
| 102 | NEEDS_ROOT = 0 |
| 103 | NEEDS_MOUNTS = 0 |
| 104 | |
| 105 | # Put objects in a separate tree based on makefile locations |
| 106 | # This means you can build a tree without touching it: |
| 107 | # make -C $SRCDIR # will create ./build-$(MODE) |
| 108 | # Or |
| 109 | # make -C $SRCDIR OUT=$PWD |
| 110 | # This variable is extended on subdir calls and doesn't need to be re-called. |
| 111 | OUT ?= $(PWD)/ |
| 112 | |
| 113 | # Make OUT now so we can use realpath. |
| 114 | $(shell mkdir -p "$(OUT)") |
| 115 | |
| 116 | # TODO(wad) Relative paths are resolved against SRC and not the calling dir. |
| 117 | # Ensure a command-line supplied OUT has a slash |
| 118 | override OUT := $(realpath $(OUT))/ |
| 119 | |
| 120 | # SRC is not meant to be set by the end user, but during make call relocation. |
| 121 | # $(PWD) != $(CURDIR) all the time. |
| 122 | export SRC ?= $(CURDIR) |
| 123 | |
| 124 | # Re-start in the $(OUT) directory if we're not there. |
| 125 | # We may be invoked using -C or bare and we need to ensure behavior |
| 126 | # is consistent so we check both PWD vs OUT and PWD vs CURDIR. |
| 127 | override RELOCATE_BUILD := 0 |
| 128 | ifneq (${PWD}/,${OUT}) |
| 129 | override RELOCATE_BUILD := 1 |
| 130 | endif |
| 131 | # Make sure we're running with no builtin targets. They cause |
| 132 | # leakage and mayhem! |
| 133 | ifneq (${PWD},${CURDIR}) |
| 134 | override RELOCATE_BUILD := 1 |
| 135 | # If we're run from the build dir, don't let it get cleaned up later. |
| 136 | ifeq (${PWD}/,${OUT}) |
| 137 | $(shell touch "$(PWD)/.dont_delete_on_clean") |
| 138 | endif |
| 139 | endif # ifneq (${PWD},${CURDIR} |
| 140 | |
| 141 | # "Relocate" if we need to restart without implicit rules. |
| 142 | ifeq ($(subst r,,$(MAKEFLAGS)),$(MAKEFLAGS)) |
| 143 | override RELOCATE_BUILD := 1 |
| 144 | endif |
| 145 | |
| 146 | ifeq (${RELOCATE_BUILD},1) |
| 147 | # By default, silence build output. Reused below as well. |
| 148 | QUIET = @ |
| 149 | ifeq ($(VERBOSE),1) |
| 150 | QUIET= |
| 151 | endif |
| 152 | |
| 153 | # This target will override all targets, including prerequisites. To avoid |
| 154 | # calling $(MAKE) once per prereq on the given CMDGOAL, we guard it with a local |
| 155 | # variable. |
| 156 | RUN_ONCE := 0 |
| 157 | MAKECMDGOALS ?= all |
| 158 | # Keep the rules split as newer make does not allow them to be declared |
| 159 | # on the same line. But the way :: rules work, the _all here will also |
| 160 | # invoke the %:: rule while retaining "_all" as the default. |
| 161 | _all:: |
| 162 | %:: |
| 163 | $(if $(filter 0,$(RUN_ONCE)), \ |
| 164 | $(QUIET)mkdir -p "$(OUT)" && \ |
| 165 | cd $(OUT) && \ |
| 166 | $(MAKE) -r -I "$(SRC)" -f "$(CURDIR)/Makefile" \ |
| 167 | SRC="$(CURDIR)" OUT="$(OUT)" $(foreach g,$(MAKECMDGOALS),"$(g)"),) |
| 168 | $(eval RUN_ONCE := 1) |
| 169 | pass-to-subcall := 1 |
| 170 | endif |
| 171 | |
| 172 | ifeq ($(pass-to-subcall),) |
| 173 | |
| 174 | # Only call MODULE if we're in a submodule |
| 175 | MODULES_LIST := $(filter-out Makefile %.d,$(MAKEFILE_LIST)) |
| 176 | ifeq ($(words $(filter-out Makefile common.mk %.d $(SRC)/Makefile \ |
| 177 | $(SRC)/common.mk,$(MAKEFILE_LIST))),0) |
| 178 | |
| 179 | # All the top-level defines outside of module.mk. |
| 180 | |
| 181 | # |
| 182 | # Helper macros |
| 183 | # |
| 184 | |
| 185 | # Creates the actual archive with an index. |
| 186 | # The target $@ must end with .pic.a or .pie.a. |
| 187 | define update_archive |
| 188 | $(QUIET)mkdir -p "$(dir $(TARGET_OR_MEMBER))" |
| 189 | $(QUIET)# Create the archive in one step to avoid parallel use accessing it |
| 190 | $(QUIET)# before all the symbols are present. |
| 191 | @$(ECHO) "AR $(subst \ |
| 192 | $(SRC)/,,$(^:.o=$(suffix $(basename $(TARGET_OR_MEMBER))).o)) \ |
| 193 | -> $(subst $(SRC)/,,$(TARGET_OR_MEMBER))" |
| 194 | $(QUIET)$(AR) rcs $(TARGET_OR_MEMBER) \ |
| 195 | $(subst $(SRC)/,,$(^:.o=$(suffix $(basename $(TARGET_OR_MEMBER))).o)) |
| 196 | endef |
| 197 | |
| 198 | # Default compile from objects using pre-requisites but filters out |
| 199 | # subdirs and .d files. |
| 200 | define cc_binary |
| 201 | $(call COMPILE_BINARY_implementation,CC,$(CFLAGS) $(1),$(EXTRA_FLAGS)) |
| 202 | endef |
| 203 | |
| 204 | define cxx_binary |
| 205 | $(call COMPILE_BINARY_implementation,CXX,$(CXXFLAGS) $(1),$(EXTRA_FLAGS)) |
| 206 | endef |
| 207 | |
| 208 | # Default compile from objects using pre-requisites but filters out |
| 209 | # subdirs and .d files. |
| 210 | define cc_library |
| 211 | $(call COMPILE_LIBRARY_implementation,CC,$(CFLAGS) $(1),$(EXTRA_FLAGS)) |
| 212 | endef |
| 213 | define cxx_library |
| 214 | $(call COMPILE_LIBRARY_implementation,CXX,$(CXXFLAGS) $(1),$(EXTRA_FLAGS)) |
| 215 | endef |
| 216 | |
| 217 | # Deletes files silently if they exist. Meant for use in any local |
| 218 | # clean targets. |
| 219 | define silent_rm |
| 220 | $(if $(wildcard $(1)), |
| 221 | $(QUIET)($(ECHO) -n '$(COLOR_RED)CLEANFILE$(COLOR_RESET) ' && \ |
| 222 | $(ECHO) '$(subst $(OUT)/,,$(wildcard $(1)))' && \ |
| 223 | $(RM) $(1) 2>/dev/null) || true,) |
| 224 | endef |
| 225 | define silent_rmdir |
| 226 | $(if $(wildcard $(1)), |
| 227 | $(if $(wildcard $(1)/*), |
| 228 | $(QUIET)# $(1) not empty [$(wildcard $(1)/*)]. Not deleting., |
| 229 | $(QUIET)($(ECHO) -n '$(COLOR_RED)CLEANDIR$(COLOR_RESET) ' && \ |
| 230 | $(ECHO) '$(subst $(OUT)/,,$(wildcard $(1)))' && \ |
| 231 | $(RMDIR) $(1) 2>/dev/null) || true),) |
| 232 | endef |
| 233 | |
| 234 | # |
| 235 | # Default variable values |
| 236 | # |
| 237 | |
| 238 | OBJCOPY ?= objcopy |
| 239 | STRIP ?= strip |
| 240 | RMDIR ?= rmdir |
| 241 | # Only override CC and CXX if they are from make. |
| 242 | ifeq ($(origin CC), default) |
| 243 | CC = gcc |
| 244 | endif |
| 245 | ifeq ($(origin CXX), default) |
| 246 | CXX = g++ |
| 247 | endif |
| 248 | ifeq ($(origin RANLIB), default) |
| 249 | RANLIB = ranlib |
| 250 | endif |
| 251 | RANLIB ?= ranlib |
| 252 | ECHO = /bin/echo -e |
| 253 | |
| 254 | ifeq ($(PROFILING),1) |
| 255 | $(warning PROFILING=1 disables relocatable executables.) |
| 256 | endif |
| 257 | |
| 258 | # To update these from an including Makefile: |
| 259 | # CXXFLAGS += -mahflag # Append to the list |
| 260 | # CXXFLAGS := -mahflag $(CXXFLAGS) # Prepend to the list |
| 261 | # CXXFLAGS := $(filter-out badflag,$(CXXFLAGS)) # Filter out a value |
| 262 | # The same goes for CFLAGS. |
| 263 | COMMON_CFLAGS := -Wall -Werror -fstack-protector-strong -fno-strict-aliasing \ |
| 264 | -ggdb3 -Wa,--noexecstack -O1 -fvisibility=internal -Wformat=2 |
| 265 | CXXFLAGS += $(COMMON_CFLAGS) |
| 266 | CFLAGS += $(COMMON_CFLAGS) |
| 267 | CPPFLAGS += -D_FORTIFY_SOURCE=2 |
| 268 | |
| 269 | ifeq ($(PROFILING),1) |
| 270 | CFLAGS := -pg |
| 271 | CXXFLAGS := -pg |
| 272 | endif |
| 273 | |
| 274 | ifeq ($(MODE),opt) |
| 275 | # Up the optimizations. |
| 276 | CFLAGS := $(filter-out -O1,$(CFLAGS)) -O2 |
| 277 | CXXFLAGS := $(filter-out -O1,$(CXXFLAGS)) -O2 |
| 278 | # Only drop -g* if symbols aren't desired. |
| 279 | ifeq ($(NOSTRIP),0) |
| 280 | # TODO: do we want -fomit-frame-pointer on x86? |
| 281 | CFLAGS := $(filter-out -ggdb3,$(CFLAGS)) |
| 282 | CXXFLAGS := $(filter-out -ggdb3,$(CXXFLAGS)) |
| 283 | endif |
| 284 | endif |
| 285 | |
| 286 | LDFLAGS := $(LDFLAGS) -Wl,-z,relro -Wl,-z,noexecstack -Wl,-z,now |
| 287 | |
| 288 | # Fancy helpers for color if a prompt is defined |
| 289 | ifeq ($(COLOR),1) |
| 290 | COLOR_RESET = \x1b[0m |
| 291 | COLOR_GREEN = \x1b[32;01m |
| 292 | COLOR_RED = \x1b[31;01m |
| 293 | COLOR_YELLOW = \x1b[33;01m |
| 294 | endif |
| 295 | |
| 296 | # By default, silence build output. |
| 297 | QUIET = @ |
| 298 | ifeq ($(VERBOSE),1) |
| 299 | QUIET= |
| 300 | endif |
| 301 | |
| 302 | # |
| 303 | # Implementation macros for compile helpers above |
| 304 | # |
| 305 | |
| 306 | # Useful for dealing with pie-broken toolchains. |
| 307 | # Call make with PIE=0 to disable default PIE use. |
| 308 | OBJ_PIE_FLAG = -fPIE |
| 309 | COMPILE_PIE_FLAG = -pie |
| 310 | ifeq ($(PIE),0) |
| 311 | OBJ_PIE_FLAG = |
| 312 | COMPILE_PIE_FLAG = |
| 313 | endif |
| 314 | |
| 315 | # Favor member targets first for CXX_BINARY(%) magic. |
| 316 | # And strip out nested members if possible. |
| 317 | LP := ( |
| 318 | RP := ) |
| 319 | TARGET_OR_MEMBER = $(lastword $(subst $(LP), ,$(subst $(RP),,$(or $%,$@)))) |
| 320 | |
| 321 | # Default compile from objects using pre-requisites but filters out |
| 322 | # all non-.o files. |
| 323 | define COMPILE_BINARY_implementation |
| 324 | @$(ECHO) "LD$(1) $(subst $(PWD)/,,$(TARGET_OR_MEMBER))" |
| 325 | $(QUIET)mkdir -p "$(dir $(TARGET_OR_MEMBER))" |
| 326 | $(QUIET)$($(1)) $(COMPILE_PIE_FLAGS) -o $(TARGET_OR_MEMBER) \ |
| 327 | $(2) $(LDFLAGS) \ |
| 328 | $(filter %.o %.a,$(^:.o=.pie.o)) \ |
| 329 | $(foreach so,$(filter %.so,$^),-L$(dir $(so)) \ |
| 330 | -l$(patsubst lib%,%,$(basename $(notdir $(so))))) \ |
| 331 | $(LDLIBS) |
| 332 | $(call conditional_strip) |
| 333 | @$(ECHO) -n "BIN " |
| 334 | @$(ECHO) "$(COLOR_GREEN)$(subst $(PWD)/,,$(TARGET_OR_MEMBER))$(COLOR_RESET)" |
| 335 | @$(ECHO) " $(COLOR_YELLOW)-----$(COLOR_RESET)" |
| 336 | endef |
| 337 | |
| 338 | # TODO: add version support extracted from PV environment variable |
| 339 | #ifeq ($(PV),9999) |
| 340 | #$(warning PV=$(PV). If shared object versions matter, please force PV=.) |
| 341 | #endif |
| 342 | # Then add -Wl,-soname,$@.$(PV) ? |
| 343 | |
| 344 | # Default compile from objects using pre-requisites but filters out |
| 345 | # all non-.o values. (Remember to add -L$(OUT) -llib) |
| 346 | COMMA := , |
| 347 | define COMPILE_LIBRARY_implementation |
| 348 | @$(ECHO) "SHARED$(1) $(subst $(PWD)/,,$(TARGET_OR_MEMBER))" |
| 349 | $(QUIET)mkdir -p "$(dir $(TARGET_OR_MEMBER))" |
| 350 | $(QUIET)$($(1)) -shared -Wl,-E -o $(TARGET_OR_MEMBER) \ |
| 351 | $(2) $(LDFLAGS) \ |
| 352 | $(if $(filter %.a,$^),-Wl$(COMMA)--whole-archive,) \ |
| 353 | $(filter %.o ,$(^:.o=.pic.o)) \ |
| 354 | $(foreach a,$(filter %.a,$^),-L$(dir $(a)) \ |
| 355 | -l$(patsubst lib%,%,$(basename $(notdir $(a))))) \ |
| 356 | $(foreach so,$(filter %.so,$^),-L$(dir $(so)) \ |
| 357 | -l$(patsubst lib%,%,$(basename $(notdir $(so))))) \ |
| 358 | $(LDLIBS) |
| 359 | $(call conditional_strip) |
| 360 | @$(ECHO) -n "LIB $(COLOR_GREEN)" |
| 361 | @$(ECHO) "$(subst $(PWD)/,,$(TARGET_OR_MEMBER))$(COLOR_RESET)" |
| 362 | @$(ECHO) " $(COLOR_YELLOW)-----$(COLOR_RESET)" |
| 363 | endef |
| 364 | |
| 365 | define conditional_strip |
| 366 | $(if $(filter 0,$(NOSTRIP)),$(call strip_artifact)) |
| 367 | endef |
| 368 | |
| 369 | define strip_artifact |
| 370 | @$(ECHO) "STRIP $(subst $(OUT)/,,$(TARGET_OR_MEMBER))" |
| 371 | $(if $(filter 1,$(SPLITDEBUG)), @$(ECHO) -n "DEBUG "; \ |
| 372 | $(ECHO) "$(COLOR_YELLOW)\ |
| 373 | $(subst $(OUT)/,,$(TARGET_OR_MEMBER)).debug$(COLOR_RESET)") |
| 374 | $(if $(filter 1,$(SPLITDEBUG)), \ |
| 375 | $(QUIET)$(OBJCOPY) --only-keep-debug "$(TARGET_OR_MEMBER)" \ |
| 376 | "$(TARGET_OR_MEMBER).debug") |
| 377 | $(if $(filter-out dbg,$(MODE)),$(QUIET)$(STRIP) --strip-unneeded \ |
| 378 | "$(TARGET_OR_MEMBER)",) |
| 379 | endef |
| 380 | |
| 381 | # |
| 382 | # Global pattern rules |
| 383 | # |
| 384 | |
| 385 | # Below, the archive member syntax is abused to create fancier |
| 386 | # syntactic sugar for recipe authors that avoids needed to know |
| 387 | # subcall options. The downside is that make attempts to look |
| 388 | # into the phony archives for timestamps. This will cause the final |
| 389 | # target to be rebuilt/linked on _every_ call to make even when nothing |
| 390 | # has changed. Until a better way presents itself, we have helpers that |
| 391 | # do the stat check on make's behalf. Dodgy but simple. |
| 392 | define old_or_no_timestamp |
| 393 | $(if $(realpath $%),,$(1)) |
| 394 | $(if $(shell find $^ -cnewer "$%" 2>/dev/null),$(1)) |
| 395 | endef |
| 396 | |
| 397 | define check_deps |
| 398 | $(if $(filter 0,$(words $^)),\ |
| 399 | $(error Missing dependencies or declaration of $@($%)),) |
| 400 | endef |
| 401 | |
| 402 | # Build a cxx target magically |
| 403 | CXX_BINARY(%): |
| 404 | $(call check_deps) |
| 405 | $(call old_or_no_timestamp,$(call cxx_binary)) |
| 406 | clean: CLEAN(CXX_BINARY*) |
| 407 | |
| 408 | CC_BINARY(%): |
| 409 | $(call check_deps) |
| 410 | $(call old_or_no_timestamp,$(call cc_binary)) |
| 411 | clean: CLEAN(CC_BINARY*) |
| 412 | |
| 413 | CXX_STATIC_BINARY(%): |
| 414 | $(call check_deps) |
| 415 | $(call old_or_no_timestamp,$(call cxx_binary,-static)) |
| 416 | clean: CLEAN(CXX_STATIC_BINARY*) |
| 417 | |
| 418 | CC_STATIC_BINARY(%): |
| 419 | $(call check_deps) |
| 420 | $(call old_or_no_timestamp,$(call cc_binary,-static)) |
| 421 | clean: CLEAN(CC_STATIC_BINARY*) |
| 422 | |
| 423 | CXX_LIBRARY(%): |
| 424 | $(call check_deps) |
| 425 | $(call old_or_no_timestamp,$(call cxx_library)) |
| 426 | clean: CLEAN(CXX_LIBRARY*) |
| 427 | |
| 428 | CXX_LIBARY(%): |
| 429 | $(error Typo alert! LIBARY != LIBRARY) |
| 430 | |
| 431 | CC_LIBRARY(%): |
| 432 | $(call check_deps) |
| 433 | $(call old_or_no_timestamp,$(call cc_library)) |
| 434 | clean: CLEAN(CC_LIBRARY*) |
| 435 | |
| 436 | CC_LIBARY(%): |
| 437 | $(error Typo alert! LIBARY != LIBRARY) |
| 438 | |
| 439 | CXX_STATIC_LIBRARY(%): |
| 440 | $(call check_deps) |
| 441 | $(call old_or_no_timestamp,$(call update_archive)) |
| 442 | clean: CLEAN(CXX_STATIC_LIBRARY*) |
| 443 | |
| 444 | CXX_STATIC_LIBARY(%): |
| 445 | $(error Typo alert! LIBARY != LIBRARY) |
| 446 | |
| 447 | CC_STATIC_LIBRARY(%): |
| 448 | $(call check_deps) |
| 449 | $(call old_or_no_timestamp,$(call update_archive)) |
| 450 | clean: CLEAN(CC_STATIC_LIBRARY*) |
| 451 | |
| 452 | CC_STATIC_LIBARY(%): |
| 453 | $(error Typo alert! LIBARY != LIBRARY) |
| 454 | |
| 455 | |
| 456 | TEST(%): % qemu_chroot_install |
| 457 | $(call TEST_implementation) |
| 458 | .PHONY: TEST |
| 459 | |
| 460 | # multiple targets with a wildcard need to share an directory. |
| 461 | # Don't use this directly it just makes sure the directory is removed _after_ |
| 462 | # the files are. |
| 463 | CLEANFILE(%): |
| 464 | $(call silent_rm,$(TARGET_OR_MEMBER)) |
| 465 | .PHONY: CLEANFILE |
| 466 | |
| 467 | CLEAN(%): CLEANFILE(%) |
| 468 | $(QUIET)# CLEAN($%) meta-target called |
| 469 | $(if $(filter-out $(PWD)/,$(dir $(abspath $(TARGET_OR_MEMBER)))), \ |
| 470 | $(call silent_rmdir,$(dir $(abspath $(TARGET_OR_MEMBER)))),\ |
| 471 | $(QUIET)# Not deleting $(dir $(abspath $(TARGET_OR_MEMBER))) yet.) |
| 472 | .PHONY: CLEAN |
| 473 | |
| 474 | # |
| 475 | # Top-level objects and pattern rules |
| 476 | # |
| 477 | |
| 478 | # All objects for .c files at the top level |
| 479 | C_OBJECTS = $(patsubst $(SRC)/%.c,%.o,$(wildcard $(SRC)/*.c)) |
| 480 | |
| 481 | |
| 482 | # All objects for .cxx files at the top level |
| 483 | CXX_OBJECTS = $(patsubst $(SRC)/%.cc,%.o,$(wildcard $(SRC)/*.cc)) |
| 484 | |
| 485 | # Note, the catch-all pattern rules don't work in subdirectories because |
| 486 | # we're building from the $(OUT) directory. At the top-level (here) they will |
| 487 | # work, but we go ahead and match using the module form. Then we can place a |
| 488 | # generic pattern rule to capture leakage from the main Makefile. (Later in the |
| 489 | # file.) |
| 490 | # |
| 491 | # The reason target specific pattern rules work well for modules, |
| 492 | # MODULE_C_OBJECTS, is because it scopes the behavior to the given target which |
| 493 | # ensures we get a relative directory offset from $(OUT) which otherwise would |
| 494 | # not match without further magic on a per-subdirectory basis. |
| 495 | |
| 496 | # Creates object file rules. Call with eval. |
| 497 | # $(1) list of .o files |
| 498 | # $(2) source type (CC or CXX) |
| 499 | # $(3) source suffix (cc or c) |
| 500 | # $(4) compiler flag name (CFLAGS or CXXFLAGS) |
| 501 | # $(5) source dir: _only_ if $(SRC). Leave blank for obj tree. |
| 502 | define add_object_rules |
| 503 | $(patsubst %.o,%.pie.o,$(1)): %.pie.o: $(5)%.$(3) %.o.depends |
| 504 | $$(QUIET)mkdir -p "$$(dir $$@)" |
| 505 | $$(call OBJECT_PATTERN_implementation,$(2),\ |
| 506 | $$(basename $$@),$$($(4)) $$(CPPFLAGS) $$(OBJ_PIE_FLAG)) |
| 507 | |
| 508 | $(patsubst %.o,%.pic.o,$(1)): %.pic.o: $(5)%.$(3) %.o.depends |
| 509 | $$(QUIET)mkdir -p "$$(dir $$@)" |
| 510 | $$(call OBJECT_PATTERN_implementation,$(2),\ |
| 511 | $$(basename $$@),$$($(4)) $$(CPPFLAGS) -fPIC) |
| 512 | |
| 513 | # Placeholder for depends |
| 514 | $(patsubst %.o,%.o.depends,$(1)): |
| 515 | $$(QUIET)mkdir -p "$$(dir $$@)" |
| 516 | $$(QUIET)touch "$$@" |
| 517 | |
| 518 | $(1): %.o: %.pic.o %.pie.o |
| 519 | $$(QUIET)mkdir -p "$$(dir $$@)" |
| 520 | $$(QUIET)touch "$$@" |
| 521 | endef |
| 522 | |
| 523 | define OBJECT_PATTERN_implementation |
| 524 | @$(ECHO) "$(1) $(subst $(SRC)/,,$<) -> $(2).o" |
| 525 | $(QUIET)mkdir -p "$(dir $(2))" |
| 526 | $(QUIET)$($(1)) -c -MD -MF $(2).d $(3) -o $(2).o $< |
| 527 | $(QUIET)# Wrap all the deps in $(wildcard) so a missing header |
| 528 | $(QUIET)# won't cause weirdness. First we remove newlines and \, |
| 529 | $(QUIET)# then wrap it. |
| 530 | $(QUIET)sed -i -e :j -e '$$!N;s|\\\s*\n| |;tj' \ |
| 531 | -e 's|^\(.*\s*:\s*\)\(.*\)$$|\1 $$\(wildcard \2\)|' $(2).d |
| 532 | endef |
| 533 | |
| 534 | # Now actually register handlers for C(XX)_OBJECTS. |
| 535 | $(eval $(call add_object_rules,$(C_OBJECTS),CC,c,CFLAGS,$(SRC)/)) |
| 536 | $(eval $(call add_object_rules,$(CXX_OBJECTS),CXX,cc,CXXFLAGS,$(SRC)/)) |
| 537 | |
| 538 | # Disable default pattern rules to help avoid leakage. |
| 539 | # These may already be handled by '-r', but let's keep it to be safe. |
| 540 | %: %.o ; |
| 541 | %.a: %.o ; |
| 542 | %.o: %.c ; |
| 543 | %.o: %.cc ; |
| 544 | |
| 545 | # NOTE: A specific rule for archive objects is avoided because parallel |
| 546 | # update of the archive causes build flakiness. |
| 547 | # Instead, just make the objects the prerequisites and use update_archive |
| 548 | # To use the foo.a(obj.o) functionality, targets would need to specify the |
| 549 | # explicit object they expect on the prerequisite line. |
| 550 | |
| 551 | # |
| 552 | # Architecture detection and QEMU wrapping |
| 553 | # |
| 554 | |
| 555 | HOST_ARCH ?= $(shell uname -m) |
| 556 | override ARCH := $(strip $(ARCH)) |
| 557 | override HOST_ARCH := $(strip $(HOST_ARCH)) |
| 558 | # emake will supply "x86" or "arm" for ARCH, but |
| 559 | # if uname -m runs and you get x86_64, then this subst |
| 560 | # will break. |
| 561 | ifeq ($(subst x86,i386,$(ARCH)),i386) |
| 562 | QEMU_ARCH := $(subst x86,i386,$(ARCH)) # x86 -> i386 |
| 563 | else ifeq ($(subst amd64,x86_64,$(ARCH)),x86_64) |
| 564 | QEMU_ARCH := $(subst amd64,x86_64,$(ARCH)) # amd64 -> x86_64 |
| 565 | else |
| 566 | QEMU_ARCH = $(ARCH) |
| 567 | endif |
| 568 | override QEMU_ARCH := $(strip $(QEMU_ARCH)) |
| 569 | |
| 570 | # If we're cross-compiling, try to use qemu for running the tests. |
| 571 | ifneq ($(QEMU_ARCH),$(HOST_ARCH)) |
| 572 | ifeq ($(SYSROOT),) |
| 573 | $(info SYSROOT not defined. qemu-based testing disabled) |
| 574 | else |
| 575 | # A SYSROOT is assumed for QEmu use. |
| 576 | USE_QEMU ?= 1 |
| 577 | |
| 578 | # Allow 64-bit hosts to run 32-bit without qemu. |
| 579 | ifeq ($(HOST_ARCH),x86_64) |
| 580 | ifeq ($(QEMU_ARCH),i386) |
| 581 | USE_QEMU = 0 |
| 582 | endif |
| 583 | endif |
| 584 | endif |
| 585 | else |
| 586 | USE_QEMU ?= 0 |
| 587 | endif |
| 588 | |
| 589 | SYSROOT_OUT = $(OUT) |
| 590 | ifneq ($(SYSROOT),) |
| 591 | SYSROOT_OUT = $(subst $(SYSROOT),,$(OUT)) |
| 592 | else |
| 593 | # Default to / when all the empty-sysroot logic is done. |
| 594 | SYSROOT = / |
| 595 | endif |
| 596 | |
| 597 | |
| 598 | # |
| 599 | # Output full configuration at top level |
| 600 | # |
| 601 | |
| 602 | # Don't show on clean |
| 603 | ifneq ($(MAKECMDGOALS),clean) |
| 604 | $(info build configuration:) |
| 605 | $(info - OUT=$(OUT)) |
| 606 | $(info - SRC=$(SRC)) |
| 607 | $(info - MODE=$(MODE)) |
| 608 | $(info - SPLITDEBUG=$(SPLITDEBUG)) |
| 609 | $(info - NOSTRIP=$(NOSTRIP)) |
| 610 | $(info - VALGRIND=$(VALGRIND)) |
| 611 | $(info - COLOR=$(COLOR)) |
| 612 | $(info - ARCH=$(ARCH)) |
| 613 | $(info - QEMU_ARCH=$(QEMU_ARCH)) |
| 614 | $(info - SYSROOT=$(SYSROOT)) |
| 615 | $(info ) |
| 616 | endif |
| 617 | |
| 618 | # |
| 619 | # Standard targets with detection for when they are improperly configured. |
| 620 | # |
| 621 | |
| 622 | # all does not include tests by default |
| 623 | all: |
| 624 | $(QUIET)(test -z "$^" && \ |
| 625 | $(ECHO) "You must add your targets as 'all' prerequisites") || true |
| 626 | $(QUIET)test -n "$^" |
| 627 | |
| 628 | # Builds and runs tests for the target arch |
| 629 | # Run them in parallel |
| 630 | tests: |
| 631 | .PHONY: tests |
| 632 | |
| 633 | qemu_clean: |
| 634 | $(call if_qemu,$(call silent_rm,$(OUT)/qemu-$(QEMU_ARCH))) |
| 635 | |
| 636 | qemu_chroot_install: |
| 637 | ifeq ($(USE_QEMU),1) |
| 638 | $(QUIET)$(ECHO) "QEMU Preparing qemu-$(QEMU_ARCH)" |
| 639 | $(QUIET)cp -fu /usr/bin/qemu-$(QEMU_ARCH) $(OUT)/qemu-$(QEMU_ARCH) |
| 640 | $(QUIET)chmod a+rx $(OUT)/qemu-$(QEMU_ARCH) |
| 641 | endif |
| 642 | .PHONY: qemu_clean qemu_chroot_install |
| 643 | |
| 644 | # TODO(wad) Move to -L $(SYSROOT) and fakechroot when qemu-user |
| 645 | # doesn't hang traversing /proc from SYSROOT. |
| 646 | QEMU_CMD = |
| 647 | ROOT_CMD = $(if $(filter 1,$(NEEDS_ROOT)),sudo , ) |
| 648 | MOUNT_CMD = $(if $(filter 1,$(NEEDS_MOUNTS)),$(ROOT_CMD) mount, \#) |
| 649 | UMOUNT_CMD = $(if $(filter 1,$(NEEDS_MOUNTS)),$(ROOT_CMD) umount, \#) |
| 650 | QEMU_LDPATH = $(SYSROOT_LDPATH):/lib64:/lib:/usr/lib64:/usr/lib |
| 651 | ROOT_CMD_LDPATH = $(SYSROOT_LDPATH):$(SYSROOT)/lib64: |
| 652 | ROOT_CMD_LDPATH := $(ROOT_CMD_LDPATH):$(SYSROOT)/lib:$(SYSROOT)/usr/lib64: |
| 653 | ROOT_CMD_LDPATH := $(ROOT_CMD_LDPATH):$(SYSROOT)/usr/lib |
| 654 | ifeq ($(USE_QEMU),1) |
| 655 | export QEMU_CMD = \ |
| 656 | sudo chroot $(SYSROOT) $(SYSROOT_OUT)qemu-$(QEMU_ARCH) \ |
| 657 | -drop-ld-preload \ |
| 658 | -E LD_LIBRARY_PATH="$(QEMU_LDPATH):$(patsubst $(OUT),,$(LD_DIRS))" \ |
| 659 | -E HOME="$(HOME)" -- |
| 660 | # USE_QEMU conditional function |
| 661 | define if_qemu |
| 662 | $(1) |
| 663 | endef |
| 664 | else |
| 665 | ROOT_CMD = $(if $(filter 1,$(NEEDS_ROOT)),sudo, ) \ |
| 666 | LD_LIBRARY_PATH="$(ROOT_CMD_LDPATH):$(LD_DIRS)" |
| 667 | define if_qemu |
| 668 | $(2) |
| 669 | endef |
| 670 | endif |
| 671 | |
| 672 | VALGRIND_CMD = |
| 673 | ifeq ($(VALGRIND),1) |
| 674 | VALGRIND_CMD = /usr/bin/valgrind --tool=memcheck $(VALGRIND_ARGS) -- |
| 675 | endif |
| 676 | |
| 677 | define TEST_implementation |
| 678 | $(QUIET)$(call TEST_setup) |
| 679 | $(QUIET)$(call TEST_run) |
| 680 | $(QUIET)$(call TEST_teardown) |
| 681 | $(QUIET)exit $$(cat $(OUT)$(TARGET_OR_MEMBER).status.test) |
| 682 | endef |
| 683 | |
| 684 | define TEST_setup |
| 685 | @$(ECHO) -n "TEST $(TARGET_OR_MEMBER) " |
| 686 | @$(ECHO) "[$(COLOR_YELLOW)SETUP$(COLOR_RESET)]" |
| 687 | $(QUIET)# Setup a target-specific results file |
| 688 | $(QUIET)(echo 1 > $(OUT)$(TARGET_OR_MEMBER).status.test) |
| 689 | $(QUIET)(echo > $(OUT)$(TARGET_OR_MEMBER).cleanup.test) |
| 690 | $(QUIET)# No setup if we are not using QEMU |
| 691 | $(QUIET)# TODO(wad) this is racy until we use a vfs namespace |
| 692 | $(call if_qemu,\ |
| 693 | $(QUIET)sudo mkdir -p "$(SYSROOT)/proc" "$(SYSROOT)/dev") |
| 694 | $(call if_qemu,\ |
| 695 | $(QUIET)$(MOUNT_CMD) --bind /proc "$(SYSROOT)/proc") |
| 696 | $(call if_qemu,\ |
| 697 | $(QUIET)$(MOUNT_CMD) --bind /dev "$(SYSROOT)/dev") |
| 698 | $(call if_qemu,\ |
| 699 | $(QUIET)(echo "$(UMOUNT_CMD) -l '$(SYSROOT)/proc'" \ |
| 700 | >> "$(OUT)$(TARGET_OR_MEMBER).cleanup.test")) |
| 701 | $(call if_qemu,\ |
| 702 | $(QUIET)(echo "$(UMOUNT_CMD) -l '$(SYSROOT)/dev'" \ |
| 703 | >> "$(OUT)$(TARGET_OR_MEMBER).cleanup.test")) |
| 704 | endef |
| 705 | |
| 706 | define TEST_teardown |
| 707 | @$(ECHO) -n "TEST $(TARGET_OR_MEMBER) " |
| 708 | @$(ECHO) "[$(COLOR_YELLOW)TEARDOWN$(COLOR_RESET)]" |
| 709 | $(call if_qemu, $(QUIET)$(SHELL) "$(OUT)$(TARGET_OR_MEMBER).cleanup.test") |
| 710 | endef |
| 711 | |
| 712 | # Use GTEST_ARGS.[arch] if defined. |
| 713 | override GTEST_ARGS.real = \ |
| 714 | $(call if_qemu,$(GTEST_ARGS.qemu.$(QEMU_ARCH)),$(GTEST_ARGS.host.$(HOST_ARCH))) |
| 715 | |
| 716 | define TEST_run |
| 717 | @$(ECHO) -n "TEST $(TARGET_OR_MEMBER) " |
| 718 | @$(ECHO) "[$(COLOR_GREEN)RUN$(COLOR_RESET)]" |
| 719 | $(QUIET)(echo 1 > "$(OUT)$(TARGET_OR_MEMBER).status.test") |
| 720 | -($(ROOT_CMD) $(QEMU_CMD) $(VALGRIND_CMD) \ |
| 721 | "$(strip $(call if_qemu, $(SYSROOT_OUT),$(OUT))$(TARGET_OR_MEMBER))" \ |
| 722 | $(if $(filter-out 0,$(words $(GTEST_ARGS.real))),$(GTEST_ARGS.real),\ |
| 723 | $(GTEST_ARGS)) && \ |
| 724 | echo 0 > "$(OUT)$(TARGET_OR_MEMBER).status.test") |
| 725 | endef |
| 726 | |
| 727 | # Recursive list reversal so that we get RMDIR_ON_CLEAN in reverse order. |
| 728 | define reverse |
| 729 | $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1)) |
| 730 | endef |
| 731 | |
| 732 | clean: qemu_clean |
| 733 | clean: CLEAN($(OUT)*.d) CLEAN($(OUT)*.o) CLEAN($(OUT)*.debug) |
| 734 | clean: CLEAN($(OUT)*.test) CLEAN($(OUT)*.depends) |
| 735 | |
| 736 | clean: |
| 737 | $(QUIET)# Always delete the containing directory last. |
| 738 | $(call silent_rmdir,$(OUT)) |
| 739 | |
| 740 | FORCE: ; |
| 741 | # Empty rule for use when no special targets are needed, like large_tests |
| 742 | NONE: |
| 743 | |
| 744 | .PHONY: clean NONE valgrind NONE |
| 745 | .DEFAULT_GOAL := all |
| 746 | # Don't let make blow away "intermediates" |
| 747 | .PRECIOUS: %.pic.o %.pie.o %.a %.pic.a %.pie.a %.test |
| 748 | |
| 749 | # Start accruing build info |
| 750 | OUT_DIRS = $(OUT) |
| 751 | LD_DIRS = $(OUT) |
| 752 | SRC_DIRS = $(SRC) |
| 753 | |
| 754 | include $(wildcard $(OUT)*.d) |
| 755 | SUBMODULE_DIRS = $(wildcard $(SRC)/*/module.mk) |
| 756 | include $(SUBMODULE_DIRS) |
| 757 | |
| 758 | |
| 759 | else ## In duplicate inclusions of common.mk |
| 760 | |
| 761 | # Get the current inclusion directory without a trailing slash |
| 762 | MODULE := $(patsubst %/,%, \ |
| 763 | $(dir $(lastword $(filter-out %common.mk,$(MAKEFILE_LIST))))) |
| 764 | MODULE := $(subst $(SRC)/,,$(MODULE)) |
| 765 | MODULE_NAME := $(subst /,_,$(MODULE)) |
| 766 | #VPATH := $(MODULE):$(VPATH) |
| 767 | |
| 768 | |
| 769 | # Depth first |
| 770 | $(eval OUT_DIRS += $(OUT)$(MODULE)) |
| 771 | $(eval SRC_DIRS += $(OUT)$(MODULE)) |
| 772 | $(eval LD_DIRS := $(LD_DIRS):$(OUT)$(MODULE)) |
| 773 | |
| 774 | # Add the defaults from this dir to rm_clean |
| 775 | clean: CLEAN($(OUT)$(MODULE)/*.d) CLEAN($(OUT)$(MODULE)/*.o) |
| 776 | clean: CLEAN($(OUT)$(MODULE)/*.debug) CLEAN($(OUT)$(MODULE)/*.test) |
| 777 | clean: CLEAN($(OUT)$(MODULE)/*.depends) |
| 778 | |
| 779 | $(info + submodule: $(MODULE_NAME)) |
| 780 | # We must eval otherwise they may be dropped. |
| 781 | MODULE_C_OBJECTS = $(patsubst $(SRC)/$(MODULE)/%.c,$(MODULE)/%.o,\ |
| 782 | $(wildcard $(SRC)/$(MODULE)/*.c)) |
| 783 | $(eval $(MODULE_NAME)_C_OBJECTS ?= $(MODULE_C_OBJECTS)) |
| 784 | MODULE_CXX_OBJECTS = $(patsubst $(SRC)/$(MODULE)/%.cc,$(MODULE)/%.o,\ |
| 785 | $(wildcard $(SRC)/$(MODULE)/*.cc)) |
| 786 | $(eval $(MODULE_NAME)_CXX_OBJECTS ?= $(MODULE_CXX_OBJECTS)) |
| 787 | |
| 788 | # Note, $(MODULE) is implicit in the path to the %.c. |
| 789 | # See $(C_OBJECTS) for more details. |
| 790 | # Register rules for the module objects. |
| 791 | $(eval $(call add_object_rules,$(MODULE_C_OBJECTS),CC,c,CFLAGS,$(SRC)/)) |
| 792 | $(eval $(call add_object_rules,$(MODULE_CXX_OBJECTS),CXX,cc,CXXFLAGS,$(SRC)/)) |
| 793 | |
| 794 | # Continue recursive inclusion of module.mk files |
| 795 | SUBMODULE_DIRS = $(wildcard $(SRC)/$(MODULE)/*/module.mk) |
| 796 | include $(wildcard $(OUT)$(MODULE)/*.d) |
| 797 | include $(SUBMODULE_DIRS) |
| 798 | |
| 799 | endif |
| 800 | endif ## pass-to-subcall wrapper for relocating the call directory |