1 # Makefile.rules - OCERA make framework common project rules -*- makefile -*-
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 # We need to ensure definition of sources directory first
19 # Only shell built-in pwd understands -L
20 SOURCES_DIR := $(shell ( pwd -L ) )
23 # If we are not called by OMK leaf Makefile...
25 MAKERULES_DIR := $(abspath $(dir $(filter %Makefile.rules,$(MAKEFILE_LIST))))
28 # OUTPUT_DIR is the place where _compiled, _build and possible other
29 # files/directories are created. By default is the same as
32 OUTPUT_DIR := $(MAKERULES_DIR)
35 .PHONY: all default check-make-ver omkize
38 ifeq ("$(origin W)", "command line")
46 ifneq ($(OMK_WHOLE_TREE),1)
47 all: check-make-ver default
48 @echo "Compilation finished"
50 # Run make in the top-level directory
52 @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
56 # Usage: $(call canttest,<error message>)
58 ( echo "$(1)" > $(OUTPUT_DIR)/_canttest; echo "$(1)"; exit 1 )
66 #=========================
67 # Include the config file
69 # FIXME: I think CONFIG_FILE_OK variable is useless. We have three
70 # config files and it is not clearly defined to which file is this
72 ifneq ($(CONFIG_FILE_OK),y)
74 CONFIG_FILE := $(OUTPUT_DIR)/config.omk
76 ifneq ($(wildcard $(CONFIG_FILE)-default),)
77 -include $(CONFIG_FILE)-default
79 ifneq ($(MAKECMDGOALS),default-config)
80 $(warning Please, run "make default-config" first)
84 -include $(OUTPUT_DIR)/config.target
86 ifneq ($(wildcard $(CONFIG_FILE)),)
87 -include $(CONFIG_FILE)
90 endif #$(CONFIG_FILE_OK)
93 CONFIG_FILES ?= $(wildcard $(CONFIG_FILE)-default) $(wildcard $(OUTPUT_DIR)/config.target) $(wildcard $(CONFIG_FILE))
96 export SOURCES_DIR MAKERULES_DIR RELATIVE_DIR
97 export CONFIG_FILE CONFIG_FILES OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT
98 # OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes
99 # must to be serialized only in the toplevel make.
102 RELATIVE_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
104 #$(warning === RELATIVE_DIR = "$(RELATIVE_DIR)" ===)
105 override RELATIVE_DIR := $(RELATIVE_DIR:/%=%)
106 override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%)
107 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
108 override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | sed -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g' -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__')
109 #$(warning BACK2TOP_DIR = "$(BACK2TOP_DIR)")
111 #$(warning SOURCES_DIR = "$(SOURCES_DIR)")
112 #$(warning MAKERULES_DIR = "$(OUTPUT_DIR)")
113 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
115 # We have to use RELATIVE_PREFIX because of mingw
116 override RELATIVE_PREFIX := $(RELATIVE_DIR)/
117 override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%)
119 #vpath %.c $(SOURCES_DIR)
120 #vpath %.cc $(SOURCES_DIR)
121 #vpath %.cxx $(SOURCES_DIR)
123 # Define srcdir for Automake compatibility
124 srcdir = $(SOURCES_DIR)
126 # Defines for quiet compilation
128 ifeq ("$(origin V)", "command line")
135 ifneq ($(OMK_VERBOSE),0)
140 ifneq ($(findstring s,$(MAKEFLAGS)),)
141 QUIET_CMD_ECHO = true
144 QUIET_CMD_ECHO = echo
147 MAKEFILE_OMK=Makefile.omk
148 # All subdirectories (even linked ones) containing Makefile.omk
149 # Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS)
150 ALL_OMK_SUBDIRS = $(patsubst %/$(MAKEFILE_OMK),%,$(patsubst $(SOURCES_DIR)/%,%,$(wildcard $(SOURCES_DIR)/*/$(MAKEFILE_OMK))))
152 # ===================================================================
153 # We have set up all important variables, so we can check and include
154 # real OCERA style Makefile.omk now
156 include $(SOURCES_DIR)/$(MAKEFILE_OMK)
157 ifeq ($(AUTOMATIC_SUBDIRS),y)
158 SUBDIRS?=$(ALL_OMK_SUBDIRS)
164 @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
165 if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
166 echo "Your make program version is too old and does not support OMK system." ; \
167 echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \
170 distclean dist-clean:
171 @$(QUIET_CMD_ECHO) " RM $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
172 @rm -fr $(OUTPUT_DIR)/$(COMPILED_DIR_NAME) $(OUTPUT_DIR)/$(BUILD_DIR_NAME)
174 # Common OMK templates
175 # ====================
177 # Syntax: $(call mkdir,<dir name>)
179 [ -d $(1) ] || mkdir -p $(1) || exit 1
182 ifneq ($(OMK_VERBOSE),2)
183 NO_PRINT_DIRECTORY := --no-print-directory
186 ifeq ($(USE_LEAF_MAKEFILES),n)
187 export USE_LEAF_MAKEFILES
188 SUBDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
189 SOURCESDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
191 SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(3)/Makefile
192 SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile
197 # Call a pass in a subdirectory
198 # Usage: $(call omk_pass_subdir_template,<pass name>,<build dir>,<subdir>)
199 define omk_pass_subdir_template
200 .PHONY: $(pass)-$(3)-subdir
201 $(pass)-submakes: $(pass)-$(3)-subdir
203 @$(call mkdir_def,$(2)/$(3))
204 +@$(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(3) $(NO_PRINT_DIRECTORY) \
205 RELATIVE_DIR=$(RELATIVE_PREFIX)$(3) -C $(2)/$(3) \
206 -f $(SUBDIR_MAKEFILE) $(pass)-submakes
207 # In subdirectories we can call submakes directly since passes are
208 # already searialized on the toplevel make.
212 check-target = $(1:%=%-check)
215 # Call a pass in a subdirectory
216 # Usage: $(call extra_rules_subdir_template,<subdir>)
217 define extra_rules_subdir_template
218 extra-rules-subdirs: extra-rules-$(1)
220 +@$(MAKE) OMK_SERIALIZE_INCLUDED=n MAKERULES_DIR=$(SOURCES_DIR)/$(1) OUTPUT_DIR=$(OUTPUT_DIR) \
221 SOURCES_DIR=$(SOURCES_DIR)/$(1) RELATIVE_DIR=$(RELATIVE_PREFIX)$(1) -C $(SOURCES_DIR)/$(1)
224 .PHONY: extra-rules-subdirs
227 $(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir))))
229 # Usage: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local enable condition>])
230 define omk_pass_template
231 .PHONY: $(pass) $(pass)-local $(pass)-check $(pass)-submakes
232 $(foreach subdir,$(SUBDIRS),$(eval $(call omk_pass_subdir_template,$(pass),$(2),$(subdir))))
234 # Submakes have to be called this way and not as dependecies for pass
235 # serialization to work
236 +@$(MAKE) SOURCES_DIR=$(SOURCES_DIR) $(NO_PRINT_DIRECTORY) \
237 RELATIVE_DIR=$(RELATIVE_DIR) \
238 -f $(SOURCESDIR_MAKEFILE) $(pass)-submakes
240 @true # Do not emit "nothing to be done" messages
242 ifneq ($(4)$($(pass)_HOOKS),)
243 $(pass)-submakes: $(pass)-this-dir
244 $(pass)-this-dir: $(foreach subdir,$(SUBDIRS),$(pass)-$(subdir)-subdir)
245 +@echo "make[omk]: $(pass) in $(RELATIVE_DIR)"
246 @$(call mkdir_def,$(2))
247 +@$(MAKE) $(NO_PRINT_DIRECTORY) SOURCES_DIR=$(SOURCES_DIR) RELATIVE_DIR=$(RELATIVE_DIR) -C $(2) \
248 -f $(SOURCESDIR_MAKEFILE) $(3) $(check-target) $(1:%=%-local)
249 $(pass)-local: $($(pass)_HOOKS)
253 # =======================
254 # DEFAULT CONFIG PASS
257 @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
258 @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
259 @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
260 @echo >> "$(CONFIG_FILE)-default"
261 @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(OUTPUT_DIR) \
262 RELATIVE_DIR="" SOURCES_DIR=$(OUTPUT_DIR) \
263 -f $(OUTPUT_DIR)/Makefile default-config-pass
265 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
267 default-config-pass-local:
268 # @echo Default config for $(RELATIVE_DIR)
269 @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
270 @$(foreach x, $(default_CONFIG), echo '$(x)' | \
271 sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
275 $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
276 echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
278 $(Q)for i in `find -L . -name Makefile.omk` ; do \
280 if ! test -f "$${d}/Makefile.rules" && ( ! test -f "$${d}/Makefile" || ! cmp --silent Makefile "$${d}/Makefile" ); then \
281 rm -f "$${d}/Makefile"; \
282 cp -v Makefile "$${d}/Makefile"; \