1 # Makefile.rules - OCERA make framework common project rules -*- makefile-gmake -*-
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/
7 # Version: @git-describe@
9 # The OMK build system is distributed under the GNU General Public
10 # License. See file COPYING for details.
14 # V .. if set to 1, full command text is shown else short form is used
15 # W .. whole tree - if set to 1, make is always called from the top-level directory
16 # SUBDIRS .. list of subdirectories intended for make from actual directory
17 # default_CONFIG .. list of default config assignments CONFIG_XXX=y/n ...
19 # We need to ensure definition of sources directory first
21 # Only shell built-in pwd understands -L
22 SOURCES_DIR := $(shell ( pwd -L ) )
25 # If we are not called by OMK leaf Makefile...
27 MAKERULES_DIR := $(abspath $(dir $(filter %Makefile.rules,$(MAKEFILE_LIST))))
30 # OUTPUT_DIR is the place where _compiled, _build and possible other
31 # files/directories are created. By default is the same as
34 OUTPUT_DIR := $(MAKERULES_DIR)
38 ifeq ("$(origin W)", "command line")
46 ifneq ($(OMK_WHOLE_TREE),1)
47 ifndef omk_prereq_checked
48 omk_checks=check-make-ver check-dir
49 export omk_prereq_checked=1
51 all: build-pseudo-pass
53 prepare: $(omk_checks) prepare-pass
54 build-pseudo-pass: prepare
55 +@$(QUIET_CMD_ECHO) "make[omk]: build"
56 $(Q3)$(MAKE) -C $(MAKERULES_DIR) -f Makefile.rules build
57 @$(QUIET_CMD_ECHO) "Compilation finished"
59 prepare-pass:: $(omk_checks)
61 # Run make in the top-level directory
63 $(Q3)$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
66 .PHONY: all default check-make-ver omkize prepare build-pseudo-pass
69 # Usage: $(call canttest,<error message>)
71 ( echo "$(1)" > $(OUTPUT_DIR)/_canttest; echo "$(1)"; exit 1 )
79 #=========================
80 # Include the config file
82 # FIXME: I think CONFIG_FILE_OK variable is useless. We have three
83 # config files and it is not clearly defined to which file is this
85 ifneq ($(CONFIG_FILE_OK),y)
87 CONFIG_FILE := $(OUTPUT_DIR)/config.omk
89 ifneq ($(wildcard $(CONFIG_FILE)-default),)
90 -include $(CONFIG_FILE)-default
92 ifneq ($(MAKECMDGOALS),default-config)
93 $(warning Please, run "make default-config" first)
97 -include $(OUTPUT_DIR)/config.target
99 ifneq ($(wildcard $(CONFIG_FILE)),)
100 -include $(CONFIG_FILE)
103 endif #$(CONFIG_FILE_OK)
106 CONFIG_FILES ?= $(wildcard $(CONFIG_FILE)-default) $(wildcard $(OUTPUT_DIR)/config.target) $(wildcard $(CONFIG_FILE))
109 export SOURCES_DIR MAKERULES_DIR RELATIVE_DIR
110 export CONFIG_FILE CONFIG_FILES OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT
111 # OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes
112 # must to be serialized only in the toplevel make.
115 RELATIVE_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
117 #$(warning === RELATIVE_DIR = "$(RELATIVE_DIR)" ===)
118 override RELATIVE_DIR := $(RELATIVE_DIR:/%=%)
119 override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%)
120 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
121 override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | sed -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g' -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__')
122 #$(warning BACK2TOP_DIR = "$(BACK2TOP_DIR)")
124 #$(warning SOURCES_DIR = "$(SOURCES_DIR)")
125 #$(warning MAKERULES_DIR = "$(OUTPUT_DIR)")
126 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
128 # We have to use RELATIVE_PREFIX because of mingw
129 override RELATIVE_PREFIX := $(RELATIVE_DIR)/
130 override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%)
132 #vpath %.c $(SOURCES_DIR)
133 #vpath %.cc $(SOURCES_DIR)
134 #vpath %.cxx $(SOURCES_DIR)
136 # Define srcdir for Automake compatibility
137 srcdir = $(SOURCES_DIR)
139 # Defines for quiet compilation
141 ifeq ("$(origin V)", "command line")
149 Q = $(if $(filter 0 ,$(OMK_VERBOSE)),@)
150 Q2 = $(if $(filter 0 1 ,$(OMK_VERBOSE)),@)
151 Q3 = $(if $(filter 0 1 2,$(OMK_VERBOSE)),@)
153 ifneq ($(findstring s,$(MAKEFLAGS)),)
154 QUIET_CMD_ECHO = true
157 QUIET_CMD_ECHO = echo
160 # Convenient variables
165 space := $(empty) $(empty)
168 # Escape single quote for use in echo statements
169 escsq = $(subst $(squote),'\$(squote)',$1)
172 # Strip/replace OUTPUT_DIR from/in the argument
173 strip_out = $(patsubst $(OUTPUT_DIR)/%,%,$(1))
174 repl_out = $(patsubst $(OUTPUT_DIR)/%,$$(OUTPUT_DIR)/%,$(1))
179 MAKEFILE_OMK=Makefile.omk
180 # All subdirectories (even linked ones) containing Makefile.omk
181 # Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS)
182 ALL_OMK_SUBDIRS = $(patsubst %/$(MAKEFILE_OMK),%,$(patsubst $(SOURCES_DIR)/%,%,$(wildcard $(SOURCES_DIR)/*/$(MAKEFILE_OMK))))
184 # ===================================================================
185 # We have set up all important variables, so we can check and include
186 # real OCERA style Makefile.omk now
188 include $(SOURCES_DIR)/$(MAKEFILE_OMK)
189 ifeq ($(AUTOMATIC_SUBDIRS),y)
190 SUBDIRS?=$(ALL_OMK_SUBDIRS)
197 @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
198 if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
199 echo "Your make program version is too old and does not support OMK system." ; \
200 echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \
203 distclean dist-clean:
204 @$(QUIET_CMD_ECHO) " RM $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
205 $(Q3)rm -fr $(OUTPUT_DIR)/$(COMPILED_DIR_NAME) $(OUTPUT_DIR)/$(BUILD_DIR_NAME)
207 # Common OMK templates
208 # ====================
210 # Syntax: $(call mkdir_def,<dir name>)
212 [ -d $(1) ] || mkdir -p $(1) || exit 1
215 ifneq ($(OMK_VERBOSE),2)
216 NO_PRINT_DIRECTORY := --no-print-directory
219 ifeq ($(USE_LEAF_MAKEFILES),n)
220 export USE_LEAF_MAKEFILES
221 SUBDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
222 SOURCESDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
224 SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(3)/Makefile
225 SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile
228 BUILD_DIR_NAME = _build
229 COMPILED_DIR_NAME = _compiled
231 BUILD_OMK_DIR := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/omk
232 OMK_WORK_DIR := $(BUILD_OMK_DIR)$(RELATIVE_DIR:%=/%)
235 $(Q3)$(call mkdir_def,$(BUILD_OMK_DIR))
239 # Call a pass in a subdirectory
240 # Usage: $(call omk_pass_subdir_template,<pass name>,<subdir>)
241 define omk_pass_subdir_template
242 .PHONY: $(pass)-$(2)-subdir
243 $(pass)-subdirs: $(pass)-$(2)-subdir
245 +$(Q3)$(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(2) $(NO_PRINT_DIRECTORY) \
246 RELATIVE_DIR=$(RELATIVE_PREFIX)$(2) -C $(2) \
247 -f $(SUBDIR_MAKEFILE) $(pass)
251 check-target = $(1:%=%-check)
254 # Call a pass in a subdirectory
255 # Usage: $(call extra_rules_subdir_template,<subdir>)
256 define extra_rules_subdir_template
257 extra-rules-subdirs: extra-rules-$(1)
259 +$(Q3)$(MAKE) OMK_SERIALIZE_INCLUDED=n MAKERULES_DIR=$(SOURCES_DIR)/$(1) OUTPUT_DIR=$(OUTPUT_DIR) \
260 SOURCES_DIR=$(SOURCES_DIR)/$(1) RELATIVE_DIR=$(RELATIVE_PREFIX)$(1) -C $(SOURCES_DIR)/$(1)
263 .PHONY: extra-rules-subdirs mkdir-omk-work
266 $(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir))))
268 # Usage: $(call omk_pass_template,<pass name>)
269 define omk_pass_template
270 .PHONY: $(pass) $(pass)-msg $(pass)-subdirs
271 $(pass):: $(pass)-subdirs $(pass)-msg mkdir-omk-work
272 $(foreach subdir,$(SUBDIRS),$$(eval $$(call omk_pass_subdir_template,$(pass),$(subdir))))
274 $(pass)-msg: $(pass)-subdirs
275 +@$(QUIET_CMD_ECHO) "make[omk]: $(pass) in $(RELATIVE_DIR)"
279 $(Q3)$(call mkdir_def,$(OMK_WORK_DIR))
283 # =======================
284 # DEFAULT CONFIG PASS
287 $(Q3)echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
288 $(Q3)echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
289 $(Q3)echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
290 $(Q3)echo >> "$(CONFIG_FILE)-default"
291 $(Q3)$(MAKE) $(NO_PRINT_DIRECTORY) -C $(MAKERULES_DIR) \
292 RELATIVE_DIR="" SOURCES_DIR=$(OUTPUT_DIR) \
293 -f $(OUTPUT_DIR)/Makefile default-config-pass
295 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
297 default-config-pass::
298 # $(Q3)echo Default config for $(RELATIVE_DIR)
299 $(Q3)echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
300 $(Q3)$(foreach x, $(default_CONFIG), echo '$(x)' | \
301 sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
305 $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
306 echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
308 $(Q)for i in `find -L . -name Makefile.omk` ; do \
310 if ! test -f "$${d}/Makefile.rules" && ( ! test -f "$${d}/Makefile" || ! cmp --silent Makefile "$${d}/Makefile" ); then \
311 rm -f "$${d}/Makefile"; \
312 cp -v Makefile "$${d}/Makefile"; \
316 include prepare.omk #omkbuild
317 include build.omk #omkbuild