1 # Makefile.rules - OCERA make framework common project rules -*- makefile -*- #OMK@base
3 # (C) Copyright 2003 by Pavel Pisa - OCERA team member
4 # (C) Copyright 2006 by Michal Sojka - Czech Technical University, FEE, DCE
6 # Homepage: http://rtime.felk.cvut.cz/omk/
8 # The OMK build system is distributed under the GNU General Public
9 # License. See file COPYING for details.
12 # V .. if set to 1, full command text is shown else short form is used
13 # W .. whole tree - if set to 1, make is always called from the top-level directory
14 # SUBDIRS .. list of subdirectories intended for make from actual directory
15 # default_CONFIG .. list of default config assignments CONFIG_XXX=y/n ...
17 # kbuild: Generic definitions
18 OMK_RULES_TYPE=test #OMK@__type
20 # We need to ensure definition of sources directory first
22 # Only shell built-in pwd understands -L
23 SOURCES_DIR := $(shell ( pwd -L ) )
26 # If we are not called by OMK leaf Makefile...
28 MAKERULES_DIR := $(abspath $(dir $(filter %Makefile.rules,$(MAKEFILE_LIST))))
31 # OUTPUT_DIR is the place where _compiled, _build and possible other
32 # files/directories are created. By default is the same as
35 OUTPUT_DIR := $(MAKERULES_DIR)
38 .PHONY: all default check-make-ver omkize
41 ifeq ("$(origin W)", "command line")
49 ifneq ($(OMK_WHOLE_TREE),1)
50 all: check-make-ver default
51 @echo "Compilation finished"
53 # Run make in the top-level directory
55 @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
59 # Usage: $(call canttest,<error message>)
61 ( echo "$(1)" > $(OUTPUT_DIR)/_canttest; echo "$(1)"; exit 1 )
69 #=========================
70 # Include the config file
72 # FIXME: I think CONFIG_FILE_OK variable is useless. We have three
73 # config files and it is not clearly defined to which file is this
75 ifneq ($(CONFIG_FILE_OK),y)
77 CONFIG_FILE := $(OUTPUT_DIR)/config.omk
79 ifneq ($(wildcard $(CONFIG_FILE)-default),)
80 -include $(CONFIG_FILE)-default
82 ifneq ($(MAKECMDGOALS),default-config)
83 $(warning Please, run "make default-config" first)
87 -include $(OUTPUT_DIR)/config.target
89 ifneq ($(wildcard $(CONFIG_FILE)),)
90 -include $(CONFIG_FILE)
93 endif #$(CONFIG_FILE_OK)
96 CONFIG_FILES ?= $(wildcard $(CONFIG_FILE)-default) $(wildcard $(OUTPUT_DIR)/config.target) $(wildcard $(CONFIG_FILE))
99 export SOURCES_DIR MAKERULES_DIR RELATIVE_DIR
100 export CONFIG_FILE CONFIG_FILES OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT
101 # OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes
102 # must to be serialized only in the toplevel make.
105 RELATIVE_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
107 #$(warning === RELATIVE_DIR = "$(RELATIVE_DIR)" ===)
108 override RELATIVE_DIR := $(RELATIVE_DIR:/%=%)
109 override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%)
110 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
111 override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | sed -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g' -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__')
112 #$(warning BACK2TOP_DIR = "$(BACK2TOP_DIR)")
114 #$(warning SOURCES_DIR = "$(SOURCES_DIR)")
115 #$(warning MAKERULES_DIR = "$(OUTPUT_DIR)")
116 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
118 # We have to use RELATIVE_PREFIX because of mingw
119 override RELATIVE_PREFIX := $(RELATIVE_DIR)/
120 override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%)
122 #vpath %.c $(SOURCES_DIR)
123 #vpath %.cc $(SOURCES_DIR)
124 #vpath %.cxx $(SOURCES_DIR)
126 # Define srcdir for Automake compatibility
127 srcdir = $(SOURCES_DIR)
129 # Defines for quiet compilation
131 ifeq ("$(origin V)", "command line")
138 ifneq ($(OMK_VERBOSE),0)
143 ifneq ($(findstring s,$(MAKEFLAGS)),)
144 QUIET_CMD_ECHO = true
147 QUIET_CMD_ECHO = echo
150 MAKEFILE_OMK=Makefile.omk
151 # All subdirectories (even linked ones) containing Makefile.omk
152 # Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS)
153 ALL_OMK_SUBDIRS = $(patsubst %/$(MAKEFILE_OMK),%,$(patsubst $(SOURCES_DIR)/%,%,$(wildcard $(SOURCES_DIR)/*/$(MAKEFILE_OMK))))
155 # ===================================================================
156 # We have set up all important variables, so we can check and include
157 # real OCERA style Makefile.omk now
159 include $(SOURCES_DIR)/$(MAKEFILE_OMK)
160 ifeq ($(AUTOMATIC_SUBDIRS),y)
161 SUBDIRS?=$(ALL_OMK_SUBDIRS)
167 @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
168 if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
169 echo "Your make program version is too old and does not support OMK system." ; \
170 echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \
173 distclean dist-clean:
174 @$(QUIET_CMD_ECHO) " RM $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
175 @rm -fr $(OUTPUT_DIR)/$(COMPILED_DIR_NAME) $(OUTPUT_DIR)/$(BUILD_DIR_NAME)
177 # Common OMK templates
178 # ====================
180 # Syntax: $(call mkdir,<dir name>)
182 [ -d $(1) ] || mkdir -p $(1) || exit 1
185 ifneq ($(OMK_VERBOSE),2)
186 NO_PRINT_DIRECTORY := --no-print-directory
189 ifeq ($(USE_LEAF_MAKEFILES),n)
190 export USE_LEAF_MAKEFILES
191 SUBDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
192 SOURCESDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
194 SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(3)/Makefile
195 SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile
200 # Call a pass in a subdirectory
201 # Usage: $(call omk_pass_subdir_template,<pass name>,<build dir>,<subdir>)
202 define omk_pass_subdir_template
203 .PHONY: $(pass)-$(3)-subdir
204 $(pass)-subdirs: $(pass)-$(3)-subdir
206 @$(call mkdir_def,$(2)/$(3))
207 +@$(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(3) $(NO_PRINT_DIRECTORY) \
208 RELATIVE_DIR=$(RELATIVE_PREFIX)$(3) -C $(2)/$(3) \
209 -f $(SUBDIR_MAKEFILE) $(pass)
212 # Call a pass in a subdirectory
213 # Usage: $(call extra_rules_subdir_template,<subdir>)
214 define extra_rules_subdir_template
215 extra-rules-subdirs: extra-rules-$(1)
217 +@$(MAKE) OMK_SERIALIZE_INCLUDED=n MAKERULES_DIR=$(SOURCES_DIR)/$(1) OUTPUT_DIR=$(OUTPUT_DIR) \
218 SOURCES_DIR=$(SOURCES_DIR)/$(1) RELATIVE_DIR=$(RELATIVE_PREFIX)$(1) -C $(SOURCES_DIR)/$(1)
221 .PHONY: extra-rules-subdirs
224 $(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir))))
226 # Usage: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local enable condition>])
227 define omk_pass_template
228 .PHONY: $(pass) $(pass)-local $(pass)-msg $(pass)-subdirs
229 $(pass): $(pass)-local
230 $(pass)-local: $(pass)-subdirs $(pass)-msg
231 $(foreach subdir,$(SUBDIRS),$(call omk_pass_subdir_template,$(pass),$(2),$(subdir)))
233 $(pass)-msg: $(pass)-subdirs
234 +@echo "make[omk]: $(pass) in $(RELATIVE_DIR)"
237 # =======================
238 # DEFAULT CONFIG PASS
241 @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
242 @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
243 @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
244 @echo >> "$(CONFIG_FILE)-default"
245 @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(MAKERULES_DIR) \
246 RELATIVE_DIR="" SOURCES_DIR=$(OUTPUT_DIR) \
247 -f $(OUTPUT_DIR)/Makefile default-config-pass
249 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
251 default-config-pass-local:
252 # @echo Default config for $(RELATIVE_DIR)
253 @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
254 @$(foreach x, $(default_CONFIG), echo '$(x)' | \
255 sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
259 $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
260 echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
262 $(Q)for i in `find -L . -name Makefile.omk` ; do \
264 if ! test -f "$${d}/Makefile.rules" && ( ! test -f "$${d}/Makefile" || ! cmp --silent Makefile "$${d}/Makefile" ); then \
265 rm -f "$${d}/Makefile"; \
266 cp -v Makefile "$${d}/Makefile"; \
270 # Convenient variables
275 space := $(empty) $(empty)
278 # Escape single quote for use in echo statements
279 escsq = $(subst $(squote),'\$(squote)',$1)
281 BUILD_DIR_NAME = _build
282 COMPILED_DIR_NAME = _compiled
284 USER_INCLUDE_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/include
285 USER_LIB_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/lib
286 USER_UTILS_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin-utils
287 USER_TESTS_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin-tests
288 USER_BIN_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin
289 USER_BUILD_DIR := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/user
290 LINK_BUILD_DIR := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/link
292 # Avoid double slash at the end in the top-level directory
293 USER_OBJS_DIR = $(USER_BUILD_DIR)$(RELATIVE_DIR:%=/%)
294 KERN_OBJS_DIR = $(KERN_BUILD_DIR)$(RELATIVE_DIR:%=/%)
295 OMK_WORK_DIR = $(USER_OBJS_DIR)
297 strip_out = $(patsubst $(OUTPUT_DIR)/%,%,$(1))
298 repl_out = $(patsubst $(OUTPUT_DIR)/%,$$(OUTPUT_DIR)/%,$(1))
306 $(eval $(call omk_pass_template,prepare-pass,$(USER_OBJS_DIR),,always))
309 # Name of the prepared rule. This name is used as prefix of variable
310 # names in .omk.inc and is based on the first target
311 omk_inc_rule = $(firstword $(1:$(OUTPUT_DIR)/%=%))
313 # The name of .omk.inc file is derived from $(omk_inc_rule) and
314 # depends whether the target is under _build or elsewhere.
315 omk_inc_file = $(if $(filter $(BUILD_DIR_NAME)%,$(omk_inc_rule)),\
316 $(OMK_WORK_DIR)/$(notdir $(omk_inc_rule)).omk.inc,\
317 $(OMK_WORK_DIR)/$(subst /,_,$(omk_inc_rule)).omk.inc)
319 target_omk_file = $(OMK_WORK_DIR)/$(notdir $(1)).target.omk
321 # >'< substitution is for echo to work,
322 # >$< substitution to preserve $ when reloading .omk.inc file
323 subst-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(1))))
326 # prepare_rule is used to generate .omk.inc files during prepare-pass.
327 # All paths in this file should be relative to $(OUTPUT_DIR)
328 # Usage: $(eval $(call prepare_rule,<targets>,<deps>,<msg>,<cmd>))
330 prepare-pass-local: $(omk_inc_file)
332 $(omk_inc_file): FORCE
333 #TODO remove the next line
334 @echo " PREP "$$(call strip_out,$$@); \
335 echo 'prepared_rules += $(omk_inc_rule)' > $$@.tmp; \
336 echo '$(omk_inc_rule)_targets = $$(call strip_out,$(1))' >> $$@.tmp; \
337 echo '$(omk_inc_rule)_deps += $$(call strip_out,$(2))' >> $$@.tmp; \
338 echo '$(omk_inc_rule)_msg = $(3)' >> $$@.tmp; \
339 echo '$(omk_inc_rule)_cmd = $(call subst-cmd,$(4))' >> $$@.tmp; \
340 if cmp -s $$@.tmp $$@; then rm $$@.tmp; else mv $$@.tmp $$@; fi
344 # prepare_rule_goal - same as prepare rules but the target is put
345 # as dependency to the default goal, which causes it to be built
347 define prepare_rule_goal
348 $(call prepare_rule,$(1),$(2),$(3),$(4))
349 prepared_goals += $(call strip_out,$(1))
352 prepare-pass-local: $(OMK_WORK_DIR)/__goals.omk.inc
353 $(OMK_WORK_DIR)/__goals.omk.inc: FORCE
354 $(Q)echo 'omk_goals += $(prepared_goals)' > $@.tmp; \
355 if cmp -s $@.tmp $@; then rm $@.tmp; else mv $@.tmp $@; fi
362 # Check GCC version for user build
363 ifndef CC_MAJOR_VERSION
364 CC_MAJOR_VERSION := $(shell $(CC) -dumpversion | sed -e 's/\([^.]\)\..*/\1/')
366 # Prepare suitable define for dependency building
367 ifeq ($(CC_MAJOR_VERSION),2)
368 CC_DEPFLAGS = -Wp,-MD,"$$@.d.tmp"
370 CC_DEPFLAGS = -MT $$@ -MD -MP -MF "$$@.d.tmp"
373 c_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
374 $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -DOMK_FOR_USER
377 if $(c_o_COMPILE) $(CC_DEPFLAGS) -o $$@ -c $$< ; \
378 then mv -f "$$@.d.tmp" "$$@.d"; \
379 else rm -f "$$@.d.tmp"; exit 1; \
383 # Usage: $(call program_template,<executable-name>,<bin|utils|test>)
384 define program_template
385 $(1)_OBJS += $$(patsubst %.c,%.o,$$(filter %.c,$$($(1)_SOURCES)))
386 $(1)_OBJS := $$(addprefix $(USER_OBJS_DIR)/,$$(sort $$($(1)_OBJS:%/=%)))
388 USER_OBJS += $$($(1)_OBJS)
389 USER_SOURCES += $$($(1)_SOURCES)
391 $(call prepare_rule_goal,\
392 $(USER_BIN_DIR)/$(1)$(EXE_SUFFIX),\
393 $$($(1)_OBJS) $$($(1)_LIBS),\
398 program_cmd = $(if $(filter %.cc,$$($(1)_SOURCES)),$(CXX),$(CC)) \
399 $($(1)_OBJS) $($(1)_LIBS:%=-l%) $(LOADLIBES) $(LDFLAGS) -Wl,-rpath-link,$(USER_LIB_DIR) \
400 -Wl,-Map,$(USER_OBJS_DIR)/$(1).exe.map -o $$@
402 $(foreach prog,$(bin_PROGRAMS),$(eval $(call program_template,$(prog),bin)))
403 #$(foreach prog,$(bin_PROGRAMS),$(info >>>$(call program_template,$(prog),bin)<<<))
404 $(foreach src,$(filter %.c,$(USER_SOURCES)),\
405 $(eval $(call prepare_rule,\
406 $(USER_OBJS_DIR)/$(src:%.c=%.o),\
407 $(SOURCES_DIR)/$(src),\
411 $(eval $(call prepare_rule_goal,\
412 $(USER_OBJS_DIR)/test.X.o,\
413 $(SOURCES_DIR)/test.c,\
417 # OUTPUT_DIR is not defined in Makefile.build so we has to pass it on
418 # command-line. After this file will be merged with Makefile.rules, it
419 # will not be necessary to pass it.
420 default: check-dir prepare-pass
421 -$(MAKE) -qp -f $(MAKERULES_DIR)/Makefile.rules build > make.db.build-pseudo-pass;
422 +@echo "make[omk]: build"
423 @$(MAKE) $(NO_PRINT_DIRECTORY) -f $(MAKERULES_DIR)/Makefile.rules build
426 @$(call mkdir_def,$(USER_BUILD_DIR))
427 @$(call mkdir_def,$(USER_INCLUDE_DIR))
428 @$(call mkdir_def,$(USER_LIB_DIR))
429 @$(call mkdir_def,$(USER_BIN_DIR))
430 @$(call mkdir_def,$(USER_UTILS_DIR))
431 @$(call mkdir_def,$(USER_TESTS_DIR))
435 #####################
436 # Build pseudo-pass #
437 #####################
438 ifneq (,$(filter build,$(MAKECMDGOALS)))
439 all: build-pseudo-pass
441 rule_targets = $(value $(1)_targets)
442 rule_deps = $(value $(1)_deps)
443 rule_msg = $(value $(1)_msg)
444 rule_cmd = $(value $(1)_cmd)
446 # Usage: $(eval $(call build_rule,<target>))
448 $(rule_targets): $(rule_deps) #$(omk_inc_file)
449 @$(QUIET_CMD_ECHO) " "$(rule_msg)"$$@" #"$$(call strip_out,$$@)"
453 # TODO: Include __goals.omk.inc only in the subtree
454 -include $(shell true; find $(USER_BUILD_DIR) -name '*.omk.inc') # `true' is a hack for MinGW
455 # FIXME: Disable automatic remake of included files
457 $(foreach rule,$(prepared_rules),$(eval $(call build_rule,$(rule))))
460 build: $(foreach target,$(omk_goals),$(target))
461 endif # (,$(filter build,$(MAKECMDGOALS)))
465 # mode: makefile-gmake
466 # compile-command:"make -qp V=2 > make.db; make V=2"