Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: common.mk

Issue 3530001: first cut: establishes base helper classes and main (Closed) Base URL: http://git.chromium.org/git/cros_boot_mode.git
Patch Set: truncation is bad, m'kay. . . Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « bootloader_type_unittest.cc ('k') | developer_switch.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright (c) 2010 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 # This file provides a common architecture for building C/C++ source trees.
6 # It uses recursive makefile inclusion to create a single make process which
7 # can be built in the source tree or with the build products placed elsewhere.
8 #
9 # To use:
10 # 1. Place common.mk in your top source level
11 # 2. In your top-level Makefile, place "include common.mk" at the top
12 # 3. In all subdirectories, create a 'module.mk' file that starts with:
13 # include common.mk
14 # And then contains the remainder of your targets.
15 # 4. All build targets should look like:
16 # $(OUT)relative/path/target: ...
17 #
18 # See existing makefiles for rule examples.
19 #
20 # Exported macros:
21 # - cc_binary, cxx_binary provide standard compilation steps for binaries
22 # - cxx_library, cc_library provide standard compilation steps for sos
23 # All of the above optionally take an argument for extra flags.
24 # - update_archive creates/updates a given .a target
25 #
26 # Exported variables
27 # - RM_ON_CLEAN - append files to remove on calls to clean
28 # - RMDIR_ON_CLEAN - append dirs to remove on calls to clean
29 #
30 # Exported targets meant to have prerequisites added to:
31 # - all - Your $(OUT)target should be given
32 # - small_tests - requires something, even if it is just NONE
33 # - large_tests - requires something, even if it is just NONE
34 # - NONE - nop target for *_tests
35 # - FORCE - force the given target to run regardless of changes
36 #
37 # Possible command line variables:
38 # - COLOR=[0|1] to set ANSI color output (default: 1)
39 # - VERBOSE=[0|1] to hide/show commands (default: 0)
40 # - MODE=dbg to turn down optimizations (default: opt)
41 # - ARCH=[x86|arm|supported qemu name] (default: from portage or uname -m)
42 # - SPLITDEBUG=[0|1] splits debug info in target.debug (default: 1)
43 # [SPLITDEBUG=0 MODE=opt will disable compiling with -g]
44 # - VALGRIND=[0|1] runs tests under valgrind (default: 0)
45 # - OUT=/path/to/builddir puts all output in given path
46 # (default: $PWD/build-$MODE. Use OUT=. for normal behavior)
47 # - VALGRIND_ARGS="" supplies extra memcheck arguments
48 #
49 # External CXXFLAGS and CFLAGS should be passed via the environment since this
50 # file does not use 'override' to control them.
51
52 # Behavior configuration variables
53 SPLITDEBUG ?= 1
54 VALGRIND ?= 0
55 COLOR ?= 1
56 VERBOSE ?= 0
57 MODE ?= opt
58 ARCH ?= $(shell uname -m)
59 # TODO: profiling support not completed.
60 PROFILING ?= 0
61
62 # Put objects in a separate tree based on makefile locations
63 # This means you can build a tree without touching it:
64 # make -C $SRCDIR # will create ./build
65 # Or
66 # make -C $SRCDIR OUT=$PWD
67 # This variable is extended on subdir calls and doesn't need to be re-called.
68 OUT ?= $(PWD)/build-$(MODE)/
69 # Ensure a command-line supplied OUT has a slash
70 override OUT := $(abspath $(OUT))/
71
72 # Only call MODULE if we're in a submodule
73 MODULES_LIST := $(filter-out Makefile %.d,$(MAKEFILE_LIST))
74 ifeq ($(words $(filter-out Makefile common.mk %.d,$(MAKEFILE_LIST))),0)
75 # Setup a top level SRC
76 SRC ?= $(PWD)
77
78 #
79 # Helper macros
80 #
81
82 # Creates the actual archive with an index.
83 # $(1) is the object suffix modified: pie or pic.
84 define update_archive
85 $(QUIET)mkdir -p $(dir $@)
86 $(QUIET)# Create the archive in one step to avoid parallel use accessing it
87 $(QUIET)# before all the symbols are present.
88 @$(ECHO) "AR $(subst $(PWD)/,,$(^:.o=.$(1).o)) -> $(subst $(PWD)/,,$@)"
89 $(QUIET)$(AR) rcs $@ $(^:.o=.$(1).o)
90 endef
91
92 # Default compile from objects using pre-requisites but filters out
93 # subdirs and .d files.
94 define cc_binary
95 $(call COMPILE_BINARY_implementation,CC,$(CFLAGS) $(1))
96 endef
97
98 define cxx_binary
99 $(call COMPILE_BINARY_implementation,CXX,$(CXXFLAGS) $(1))
100 endef
101
102 # Default compile from objects using pre-requisites but filters out
103 # subdirs and .d files.
104 define cc_library
105 $(call COMPILE_LIBRARY_implementation,CC,$(CFLAGS) $(1))
106 endef
107 define cxx_library
108 $(call COMPILE_LIBRARY_implementation,CXX,$(CXXFLAGS) $(1))
109 endef
110
111
112 # Deletes files silently if they exist. Meant for use in any local
113 # clean targets.
114 define silent_rm
115 $(QUIET)(test -n "$(wildcard $(1))" && \
116 $(ECHO) -n '$(COLOR_RED)CLEANFILE$(COLOR_RESET) ' && \
117 $(ECHO) '$(subst $(PWD)/,,$(wildcard $(1)))' && \
118 $(RM) -f $(1) 2>/dev/null) || true
119 endef
120 define silent_rmdir
121 $(QUIET)(test -n "$(wildcard $(1))" && \
122 $(ECHO) -n '$(COLOR_RED)CLEANDIR$(COLOR_RESET) ' && \
123 $(ECHO) '$(subst $(PWD)/,,$(wildcard $(1)))' && \
124 $(RMDIR) $(1) 2>/dev/null) || true
125 endef
126
127
128
129 #
130 # Default variables for use in including makefiles
131 #
132
133 # All objects for .c files at the top level
134 C_OBJECTS := $(patsubst %.c,$(OUT)%.o,$(wildcard *.c))
135
136 # All objects for .cxx files at the top level
137 CXX_OBJECTS := $(patsubst %.cc,$(OUT)%.o,$(wildcard *.cc))
138
139 #
140 # Default variable values
141 #
142
143 OBJCOPY ?= objcopy
144 STRIP ?= strip
145 RMDIR ?= rmdir
146 # Only override CC and CXX if they are from make.
147 ifeq ($(origin CC), default)
148 CC = gcc
149 endif
150 ifeq ($(origin CXX), default)
151 CXX = g++
152 endif
153 ifeq ($(origin RANLIB), default)
154 RANLIB = ranlib
155 endif
156 RANLIB ?= ranlib
157 ECHO = /bin/echo -e
158
159 ifeq ($(PROFILING),1)
160 $(warning PROFILING=1 disables relocatable executables.)
161 endif
162
163 # To update these from an including Makefile:
164 # CXXFLAGS += -mahflag # Append to the list
165 # CXXFLAGS := -mahflag $(CXXFLAGS) # Prepend to the list
166 # CXXFLAGS := $(filter-out badflag,$(CXXFLAGS)) # Filter out a value
167 # The same goes for CFLAGS.
168 CXXFLAGS := $(CXXFLAGS) -Wall -Werror -fstack-protector-all -DFORTIFY_SOURCE \
169 -O2 -ggdb3 -DNDEBUG -Wa,--noexecstack
170 CFLAGS := $(CFLAGS) -Wall -Werror -fstack-protector-all -DFORTIFY_SOURCE \
171 -O2 -ggdb3 -DNDEBUG -Wa,--noexecstack
172
173 ifeq ($(PROFILING),1)
174 CFLAGS := -pg
175 CXXFLAGS := -pg
176 endif
177
178 ifeq ($(MODE),dbg)
179 CFLAGS := $(filter-out -O2 -DNDEBUG,$(CFLAGS)) -O1
180 CXXFLAGS := $(filter-out -O2 -DNDEBUG,$(CXXFLAGS)) -O1
181 # TODO: May need -nopie. need to check gdb
182 else # opt
183 ifeq ($(SPLITDEBUG),0)
184 # TODO: do we want -fomit-frame-pointer on x86?
185 CFLAGS := $(filter-out -ggdb3,$(CFLAGS))
186 CXXFLAGS := $(filter-out -ggdb3,$(CXXFLAGS))
187 endif
188 endif
189
190 LDFLAGS := $(LDFLAGS) -Wl,-z,relro -Wl,-z,noexecstack
191
192 # Fancy helpers for color if a prompt is defined
193 ifeq ($(COLOR),1)
194 COLOR_RESET = \x1b[0m
195 COLOR_GREEN = \x1b[32;01m
196 COLOR_RED = \x1b[31;01m
197 COLOR_YELLOW = \x1b[33;01m
198 endif
199
200 # By default, silence build output.
201 QUIET = @
202 ifeq ($(VERBOSE),1)
203 QUIET=
204 endif
205
206 #
207 # Implementation macros for compile helpers above
208 #
209
210 # Useful for dealing with pie-broken toolchains.
211 # Call make with PIE=0 to disable default PIE use.
212 OBJ_PIE_FLAG = -fPIE
213 COMPILE_PIE_FLAG = -pie
214 ifeq ($(PIE),0)
215 OBJ_PIE_FLAG =
216 COMPILE_PIE_FLAG =
217 endif
218
219 # Default compile from objects using pre-requisites but filters out
220 # all non-.o files.
221 define COMPILE_BINARY_implementation
222 @$(ECHO) "LD$(1) $(subst $(PWD)/,,$@)"
223 $(QUIET)$($(1)) $(COMPILE_PIE_FLAGS) -o $@ \
224 $(filter %.o %.so %.a,$(^:.o=.pie.o)) $(LDFLAGS) $(2)
225 $(call strip_library)
226 @$(ECHO) "BIN $(COLOR_GREEN)$(subst $(PWD)/,,$@)$(COLOR_RESET)"
227 @$(ECHO) " $(COLOR_YELLOW)-----$(COLOR_RESET)"
228 endef
229
230 # TODO: add version support extracted from PV environment variable
231 #ifeq ($(PV),9999)
232 #$(warning PV=$(PV). If shared object versions matter, please force PV=.)
233 #endif
234 # Then add -Wl,-soname,$@.$(PV) ?
235
236 # Default compile from objects using pre-requisites but filters out
237 # all non-.o values. (Remember to add -L$(OUT) -llib)
238 define COMPILE_LIBRARY_implementation
239 @$(ECHO) "SHARED$(1) $(subst $(PWD)/,,$@)"
240 $(QUIET)$($(1)) -shared -Wl,-E -o $@ \
241 $(filter %.o %.so %.a,$(^:.o=.pic.o)) $(2) $(LDFLAGS)
242 $(call strip_library)
243 @$(ECHO) "LIB $(COLOR_GREEN)$(subst $(PWD)/,,$@)$(COLOR_RESET)"
244 @$(ECHO) " $(COLOR_YELLOW)-----$(COLOR_RESET)"
245 endef
246
247 define strip_library
248 @$(ECHO) "STRIP $(subst $(PWD)/,,$@)"
249 $(if $(filter 1,$(SPLITDEBUG)), @$(ECHO) -n "DEBUG "; \
250 $(ECHO) "$(COLOR_YELLOW)$(subst $(PWD)/,,$@).debug$(COLOR_RESET)")
251 $(if $(filter 1,$(SPLITDEBUG)), \
252 $(QUIET)$(OBJCOPY) --only-keep-debug "$@" "$@.debug")
253 $(if $(filter-out dbg,$(MODE)),$(QUIET)$(STRIP) --strip-unneeded "$@",)
254 endef
255
256 define strip_binary
257 @$(ECHO) "STRIP $(subst $(PWD)/,,$@)"
258 $(if $(filter 1,$(SPLITDEBUG)), @$(ECHO) -n "DEBUG "; \
259 $(ECHO) "$(COLOR_YELLOW)$(subst $(PWD)/,,$@).debug$(COLOR_RESET)")
260 $(if $(filter 1,$(SPLITDEBUG)), \
261 $(QUIET)$(OBJCOPY) --only-keep-debug "$@" "$@.debug")
262 $(if $(filter-out dbg,$(MODE)),$(QUIET)$(STRIP) --strip-unneeded "$@",)
263 endef
264
265 #
266 # Pattern rules
267 #
268
269 %.o: %.pie.o %.pic.o
270 $(QUIET)touch $@
271
272 $(OUT)%.pie.o: %.c
273 $(call OBJECT_PATTERN_implementation,CC,$(CFLAGS) $(OBJ_PIE_FLAG))
274
275 $(OUT)%.pic.o: %.c
276 $(call OBJECT_PATTERN_implementation,CC,$(CFLAGS) -fPIC)
277
278 $(OUT)%.pie.o: %.cc
279 $(call OBJECT_PATTERN_implementation,CXX,$(CXXFLAGS) $(OBJ_PIE_FLAG))
280
281 $(OUT)%.pic.o: %.cc
282 $(call OBJECT_PATTERN_implementation,CXX,$(CXXFLAGS) -fPIC)
283
284
285 define OBJECT_PATTERN_implementation
286 @$(ECHO) "$(subst $(PWD)/,,$(1)) $(subst $(PWD)/,,$<)"
287 $(QUIET)mkdir -p $(dir $@)
288 $(QUIET)$($(1)) -c -MD -MF $(basename $@).d -o $@ $< $(2)
289 $(QUIET)# Wrap all the deps in $(wildcard) so a missing header
290 $(QUIET)# won't cause weirdness. First we remove newlines and \,
291 $(QUIET)# then wrap it.
292 $(QUIET)sed -i -e :j -e '$$!N;s|\\\s*\n| |;tj' \
293 -e 's|^\(.*\s*:\s*\)\(.*\)$$|\1 $$\(wildcard \2\)|' $(basename $@).d
294 endef
295
296 # NOTE: A specific rule for archive objects is avoided because parallel
297 # update of the archive causes build flakiness.
298 # Instead, just make the objects the prerequisites and use update_archive
299 # To use the foo.a(obj.o) functionality, targets would need to specify the
300 # explicit object they expect on the prerequisite line.
301
302 #
303 # Architecture detection and QEMU wrapping
304 #
305
306 ARCH ?= $(shell uname -m)
307 HOST_ARCH ?= $(shell uname -m)
308 # emake will supply "x86" or "arm" for ARCH, but
309 # if uname -m runs and you get x86_64, then this subst
310 # will break.
311 ifeq ($(subst x86,i386,$(ARCH)),i386)
312 QEMU_ARCH := $(subst x86,i386,$(ARCH)) # x86 -> i386
313 else
314 QEMU_ARCH = $(ARCH)
315 endif
316
317 # If we're cross-compiling, try to use qemu for running the tests.
318 QEMU_CMD ?=
319 ifneq ($(QEMU_ARCH),$(HOST_ARCH))
320 ifeq ($(SYSROOT),)
321 $(info SYSROOT not defined. qemu-based testing disabled)
322 else
323 # A SYSROOT is assumed for QEmu use.
324 USE_QEMU ?= 1
325 endif
326 endif
327
328 #
329 # Output full configuration at top level
330 #
331
332 # Don't show on clean
333 ifneq ($(MAKECMDGOALS),clean)
334 $(info build configuration:)
335 $(info - OUT=$(OUT))
336 $(info - MODE=$(MODE))
337 $(info - SPLITDEBUG=$(SPLITDEBUG))
338 $(info - VALGRIND=$(VALGRIND))
339 $(info - COLOR=$(COLOR))
340 $(info - ARCH=$(ARCH))
341 $(info - QEMU_ARCH=$(QEMU_ARCH))
342 $(info - SYSROOT=$(SYSROOT))
343 $(info )
344 endif
345
346 #
347 # Standard targets with detection for when they are improperly configured.
348 #
349
350 # all does not include tests by default
351 all:
352 $(QUIET)(test -z "$^" && \
353 $(ECHO) "You must add your targets as 'all' prerequisites") || true
354 $(QUIET)test -n "$^"
355
356 # Builds and runs tests for the target arch
357 tests: small_tests large_tests
358
359 small_tests: qemu FORCE
360 $(call TEST_implementation)
361
362 large_tests: qemu FORCE
363 $(call TEST_implementation)
364
365 qemu_clean: FORCE
366 ifeq ($(USE_QEMU),1)
367 $(call silent_rm,$(PWD)/qemu-$(QEMU_ARCH))
368 endif
369
370 qemu: FORCE
371 ifeq ($(USE_QEMU),1)
372 $(QUIET)$(ECHO) "QEMU Preparing qemu-$(QEMU_ARCH)"
373 $(QUIET)cp -f /usr/bin/qemu-$(QEMU_ARCH) $(PWD)/qemu-$(QEMU_ARCH)
374 $(QUIET)chmod a+rx $(PWD)/qemu-$(QEMU_ARCH)
375 endif
376
377 # TODO(wad) separate chroot from qemu to make it possible to cross-compile
378 # and test outside of the chroot.
379 ifeq ($(USE_QEMU),1)
380 export QEMU_CMD = sudo chroot $(SYSROOT) \
381 $(subst $(SYSROOT),,$(PWD))/qemu-$(QEMU_ARCH) \
382 -drop-ld-preload \
383 -E LD_LIBRARY_PATH="$(SYSROOT_LDPATH)" \
384 -E HOME="$(HOME)" --
385 endif
386
387 VALGRIND_CMD =
388 ifeq ($(VALGRIND),1)
389 VALGRIND_CMD = /usr/bin/valgrind --tool=memcheck $(VALGRIND_ARGS) --
390 endif
391
392 define TEST_implementation
393 $(QUIET)(test -z "$(filter FORCE qemu,$^)" && \
394 $(ECHO) "No '$@' prerequisites defined!") || true
395 $(QUIET)test -n "$^"
396 $(QUIET)# TODO(wad) take root away after the chroot.
397 $(QUIET)test "$(filter NONE,$^)" = "NONE" || \
398 ((test "1" = "$(VALGRIND)" && test -n "$(USE_QEMU)" && \
399 sudo mkdir -p $(SYSROOT)/proc && \
400 sudo mount --bind /proc $(SYSROOT)/proc); \
401 (status=0 && for tgt in $(subst $(SYSROOT),,$(filter-out FORCE qemu,$^)); \
402 do \
403 $(ECHO) "TEST $$tgt"; \
404 $(QEMU_CMD) $(VALGRIND_CMD) $$tgt $(GTEST_ARGS); \
405 status=$$((status + $$?)); \
406 done; (test "1" = "$(VALGRIND)" && test -n "$(USE_QEMU)" && \
407 sudo umount $(SYSROOT)/proc); exit $$status))
408 endef
409
410
411 # Add the defaults from this dir to rm_clean
412 define default_rm_clean
413 $(OUT)$(1)*.d $(OUT)$(1)*.o $(OUT)$(1)*.debug
414 endef
415
416 # Start with the defaults
417 RM_ON_CLEAN = $(call default_rm_clean)
418 RMDIR_ON_CLEAN = $(OUT)
419
420 # Recursive list reversal so that we get RMDIR_ON_CLEAN in reverse order.
421 define reverse
422 $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1))
423 endef
424
425 rm_clean: FORCE
426 $(call silent_rm,$(RM_ON_CLEAN))
427
428 rmdir_clean: FORCE rm_clean
429 $(call silent_rmdir,$(call reverse,$(RMDIR_ON_CLEAN)))
430
431 clean: qemu_clean rmdir_clean
432
433 FORCE: ;
434 # Empty rule for use when no special targets are needed, like large_tests
435 NONE:
436
437 .PHONY: clean NONE qemu_clean valgrind rm_clean rmdir_clean
438 .DEFAULT_GOAL := all
439 # Don't let make blow away "intermediates"
440 .PRECIOUS: $(OUT)%.pic.o $(OUT)%.pie.o
441
442 # Start accruing build info
443 OUT_DIRS = $(OUT)
444 SRC_DIRS = .
445
446 include $(wildcard $(OUT)*.d)
447 SUBMODULE_DIRS = $(wildcard */module.mk)
448 include $(SUBMODULE_DIRS)
449
450 else ## In duplicate inclusions of common.mk
451
452 # Get the current inclusion directory without a trailing slash
453 MODULE := $(patsubst %/,%, \
454 $(dir $(lastword $(filter-out %common.mk,$(MAKEFILE_LIST)))))
455 MODULE_NAME := $(subst /,_,$(MODULE))
456
457 # Depth first
458 $(eval OUT_DIRS += $(OUT)/$(MODULE))
459 $(eval SRC_DIRS += $(MODULE))
460
461 # Add the defaults from this dir to rm_clean
462 $(eval RM_ON_CLEAN += $(call default_rm_clean,$(MODULE)/))
463 $(eval RMDIR_ON_CLEAN += $(wildcard $(OUT)$(MODULE)/))
464
465 $(info + submodule: $(MODULE_NAME))
466 # We must eval otherwise they may be dropped.
467 $(eval $(MODULE_NAME)_C_OBJECTS ?= \
468 $(patsubst %.c,$(OUT)%.o,$(wildcard $(MODULE)/*.c)))
469 $(eval $(MODULE_NAME)_CXX_OBJECTS ?= \
470 $(patsubst %.cc,$(OUT)%.o,$(wildcard $(MODULE)/*.cc)))
471
472 # Continue recursive inclusion of module.mk files
473 SUBMODULE_DIRS = $(wildcard $(MODULE)/*/module.mk)
474
475 include $(wildcard $(OUT)$(MODULE)/*.d)
476 include $(SUBMODULE_DIRS)
477 endif
478
OLDNEW
« no previous file with comments | « bootloader_type_unittest.cc ('k') | developer_switch.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698