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/
8 # The OMK build system is distributed under the GNU General Public
9 # License. See file COPYING for details.
13 # V .. if set to 1, full command text is shown else short form is used
14 # W .. whole tree - if set to 1, make is always called from the top-level directory
15 # SUBDIRS .. list of subdirectories intended for make from actual directory
16 # default_CONFIG .. list of default config assignments CONFIG_XXX=y/n ...
18 # We need to ensure definition of sources directory first
20 # Only shell built-in pwd understands -L
21 SOURCES_DIR := $(shell ( pwd -L ) )
24 # If we are not called by OMK leaf Makefile...
26 MAKERULES_DIR := $(abspath $(dir $(filter %Makefile.rules,$(MAKEFILE_LIST))))
29 # OUTPUT_DIR is the place where _compiled, _build and possible other
30 # files/directories are created. By default is the same as
33 OUTPUT_DIR := $(MAKERULES_DIR)
37 ifeq ("$(origin W)", "command line")
45 ifneq ($(OMK_WHOLE_TREE),1)
46 ifndef omk_prereq_checked
47 omk_checks=check-make-ver check-dir
48 export omk_prereq_checked=1
50 all: build-pseudo-pass
52 prepare: $(omk_checks) prepare-pass
53 build-pseudo-pass: prepare
54 +@$(QUIET_CMD_ECHO) "make[omk]: build"
55 $(Q3)$(MAKE) -C $(MAKERULES_DIR) -f Makefile.rules build
56 @$(QUIET_CMD_ECHO) "Compilation finished"
58 prepare-pass:: $(omk_checks)
60 # Run make in the top-level directory
62 $(Q3)$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
65 .PHONY: all default check-make-ver omkize prepare build-pseudo-pass
68 # Usage: $(call canttest,<error message>)
70 ( echo "$(1)" > $(OUTPUT_DIR)/_canttest; echo "$(1)"; exit 1 )
78 #=========================
79 # Include the config file
81 # FIXME: I think CONFIG_FILE_OK variable is useless. We have three
82 # config files and it is not clearly defined to which file is this
84 ifneq ($(CONFIG_FILE_OK),y)
86 CONFIG_FILE := $(OUTPUT_DIR)/config.omk
88 ifneq ($(wildcard $(CONFIG_FILE)-default),)
89 -include $(CONFIG_FILE)-default
91 ifneq ($(MAKECMDGOALS),default-config)
92 $(warning Please, run "make default-config" first)
96 -include $(OUTPUT_DIR)/config.target
98 ifneq ($(wildcard $(CONFIG_FILE)),)
99 -include $(CONFIG_FILE)
102 endif #$(CONFIG_FILE_OK)
105 CONFIG_FILES ?= $(wildcard $(CONFIG_FILE)-default) $(wildcard $(OUTPUT_DIR)/config.target) $(wildcard $(CONFIG_FILE))
108 export SOURCES_DIR MAKERULES_DIR RELATIVE_DIR
109 export CONFIG_FILE CONFIG_FILES OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT
110 # OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes
111 # must to be serialized only in the toplevel make.
114 RELATIVE_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
116 #$(warning === RELATIVE_DIR = "$(RELATIVE_DIR)" ===)
117 override RELATIVE_DIR := $(RELATIVE_DIR:/%=%)
118 override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%)
119 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
120 override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | sed -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g' -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__')
121 #$(warning BACK2TOP_DIR = "$(BACK2TOP_DIR)")
123 #$(warning SOURCES_DIR = "$(SOURCES_DIR)")
124 #$(warning MAKERULES_DIR = "$(OUTPUT_DIR)")
125 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
127 # We have to use RELATIVE_PREFIX because of mingw
128 override RELATIVE_PREFIX := $(RELATIVE_DIR)/
129 override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%)
131 #vpath %.c $(SOURCES_DIR)
132 #vpath %.cc $(SOURCES_DIR)
133 #vpath %.cxx $(SOURCES_DIR)
135 # Define srcdir for Automake compatibility
136 srcdir = $(SOURCES_DIR)
138 # Defines for quiet compilation
140 ifeq ("$(origin V)", "command line")
148 Q = $(if $(filter 0 ,$(OMK_VERBOSE)),@)
149 Q2 = $(if $(filter 0 1 ,$(OMK_VERBOSE)),@)
150 Q3 = $(if $(filter 0 1 2,$(OMK_VERBOSE)),@)
152 ifneq ($(findstring s,$(MAKEFLAGS)),)
153 QUIET_CMD_ECHO = true
156 QUIET_CMD_ECHO = echo
159 # Convenient variables
164 space := $(empty) $(empty)
167 # Escape single quote for use in echo statements
168 escsq = $(subst $(squote),'\$(squote)',$1)
171 # Strip/replace OUTPUT_DIR from/in the argument
172 strip_out = $(patsubst $(OUTPUT_DIR)/%,%,$(1))
173 repl_out = $(patsubst $(OUTPUT_DIR)/%,$$(OUTPUT_DIR)/%,$(1))
178 MAKEFILE_OMK=Makefile.omk
179 # All subdirectories (even linked ones) containing Makefile.omk
180 # Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS)
181 ALL_OMK_SUBDIRS = $(patsubst %/$(MAKEFILE_OMK),%,$(patsubst $(SOURCES_DIR)/%,%,$(wildcard $(SOURCES_DIR)/*/$(MAKEFILE_OMK))))
183 # ===================================================================
184 # We have set up all important variables, so we can check and include
185 # real OCERA style Makefile.omk now
187 include $(SOURCES_DIR)/$(MAKEFILE_OMK)
188 ifeq ($(AUTOMATIC_SUBDIRS),y)
189 SUBDIRS?=$(ALL_OMK_SUBDIRS)
196 @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
197 if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
198 echo "Your make program version is too old and does not support OMK system." ; \
199 echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \
202 distclean dist-clean:
203 @$(QUIET_CMD_ECHO) " RM $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
204 $(Q3)rm -fr $(OUTPUT_DIR)/$(COMPILED_DIR_NAME) $(OUTPUT_DIR)/$(BUILD_DIR_NAME)
206 # Common OMK templates
207 # ====================
209 # Syntax: $(call mkdir_def,<dir name>)
211 [ -d $(1) ] || mkdir -p $(1) || exit 1
214 ifneq ($(OMK_VERBOSE),2)
215 NO_PRINT_DIRECTORY := --no-print-directory
218 ifeq ($(USE_LEAF_MAKEFILES),n)
219 export USE_LEAF_MAKEFILES
220 SUBDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
221 SOURCESDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
223 SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(3)/Makefile
224 SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile
227 BUILD_DIR_NAME = _build
228 COMPILED_DIR_NAME = _compiled
230 BUILD_OMK_DIR := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/omk
231 OMK_WORK_DIR := $(BUILD_OMK_DIR)$(RELATIVE_DIR:%=/%)
234 $(Q3)$(call mkdir_def,$(BUILD_OMK_DIR))
238 # Call a pass in a subdirectory
239 # Usage: $(call omk_pass_subdir_template,<pass name>,<subdir>)
240 define omk_pass_subdir_template
241 .PHONY: $(pass)-$(2)-subdir
242 $(pass)-subdirs: $(pass)-$(2)-subdir
244 +$(Q3)$(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(2) $(NO_PRINT_DIRECTORY) \
245 RELATIVE_DIR=$(RELATIVE_PREFIX)$(2) -C $(2) \
246 -f $(SUBDIR_MAKEFILE) $(pass)
250 check-target = $(1:%=%-check)
253 # Call a pass in a subdirectory
254 # Usage: $(call extra_rules_subdir_template,<subdir>)
255 define extra_rules_subdir_template
256 extra-rules-subdirs: extra-rules-$(1)
258 +$(Q3)$(MAKE) OMK_SERIALIZE_INCLUDED=n MAKERULES_DIR=$(SOURCES_DIR)/$(1) OUTPUT_DIR=$(OUTPUT_DIR) \
259 SOURCES_DIR=$(SOURCES_DIR)/$(1) RELATIVE_DIR=$(RELATIVE_PREFIX)$(1) -C $(SOURCES_DIR)/$(1)
262 .PHONY: extra-rules-subdirs mkdir-omk-work
265 $(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir))))
267 # Usage: $(call omk_pass_template,<pass name>)
268 define omk_pass_template
269 .PHONY: $(pass) $(pass)-msg $(pass)-subdirs
270 $(pass):: $(pass)-subdirs $(pass)-msg mkdir-omk-work
271 $(foreach subdir,$(SUBDIRS),$$(eval $$(call omk_pass_subdir_template,$(pass),$(subdir))))
273 $(pass)-msg: $(pass)-subdirs
274 +@$(QUIET_CMD_ECHO) "make[omk]: $(pass) in $(RELATIVE_DIR)"
278 $(Q3)$(call mkdir_def,$(OMK_WORK_DIR))
282 # =======================
283 # DEFAULT CONFIG PASS
286 $(Q3)echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
287 $(Q3)echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
288 $(Q3)echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
289 $(Q3)echo >> "$(CONFIG_FILE)-default"
290 $(Q3)$(MAKE) $(NO_PRINT_DIRECTORY) -C $(MAKERULES_DIR) \
291 RELATIVE_DIR="" SOURCES_DIR=$(OUTPUT_DIR) \
292 -f $(OUTPUT_DIR)/Makefile default-config-pass
294 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
296 default-config-pass::
297 # $(Q3)echo Default config for $(RELATIVE_DIR)
298 $(Q3)echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
299 $(Q3)$(foreach x, $(default_CONFIG), echo '$(x)' | \
300 sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
304 $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
305 echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
307 $(Q)for i in `find -L . -name Makefile.omk` ; do \
309 if ! test -f "$${d}/Makefile.rules" && ( ! test -f "$${d}/Makefile" || ! cmp --silent Makefile "$${d}/Makefile" ); then \
310 rm -f "$${d}/Makefile"; \
311 cp -v Makefile "$${d}/Makefile"; \
315 include prepare.omk #omkbuild
316 include build.omk #omkbuild
319 # mode:makefile-gmake