1 # Makefile.rules - OCERA make framework common project rules -*- makefile-gmake -*-
3 # (C) Copyright 2003, 2006, 2007, 2008, 2009 by Pavel Pisa - OCERA team member
4 # (C) Copyright 2009, 2010, 2011, 2013, 2015, 2016, 2017, 2019 by Pavel Pisa - PiKRON.com
5 # (C) Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2013, 2015 by Michal Sojka - Czech Technical University, FEE, DCE
7 # Homepage: http://rtime.felk.cvut.cz/omk/
8 # Version: @git-describe@
10 # The OMK build system is distributed under the GNU General Public
11 # License. See file COPYING for details.
15 # V .. if set to 1, full command text is shown else short form is used
16 # W .. whole tree - if set to 1, make is always called from the top-level directory
17 # SUBDIRS .. list of subdirectories intended for make from actual directory
18 # default_CONFIG .. list of default config assignments CONFIG_XXX=y/n ...
20 # If we are not called by OMK leaf Makefile...
22 MAKERULES_DIR := $(abspath $(dir $(filter %Makefile.rules,$(MAKEFILE_LIST))))
25 # The $(SED4OMK) command for BSD based systems requires -E option to allow
26 # extended regular expressions
29 ifneq ($(shell ( echo A | $(SED4OMK) -n -e 's/A\|B/y/p' )),y)
30 SED4OMK := $(SED4OMK) -E
31 ifneq ($(shell ( echo A | $(SED4OMK) -n -e 's/A\|B/y/p' )),y)
34 ifneq ($(shell ( echo A | $(SED4OMK) -n -e 's/A\|B/y/p' )),y)
37 ifneq ($(shell ( echo A | $(SED4OMK) -n -e 's/A\|B/y/p' )),y)
38 $(error No SED program suitable for OMK found)
42 # OUTPUT_DIR is the place where _compiled, _build and possible other
43 # files/directories are created. By default is the same as
46 OUTPUT_DIR := $(MAKERULES_DIR)
49 # We need to ensure definition of sources directory first
51 # Only shell built-in pwd understands -L
52 SOURCES_DIR := $(shell ( pwd -L ) )
53 INVOCATION_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
54 INVOCATION_DIR := $(INVOCATION_DIR:/%=%)
55 INVOCATION_DIR := $(INVOCATION_DIR:\\%=%)
58 .PHONY: all default check-make-ver print-hints omkize
61 ifeq ("$(origin W)", "command line")
69 ifneq ($(OMK_WHOLE_TREE),1)
70 all: check-make-ver print-hints default
71 @echo "Compilation finished"
73 # Run make in the top-level directory
75 @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
78 # omk-get-var target allows external scripts/programs to determine the
79 # values of OMK variables such as RELATIVE_DIR etc.
82 @$(foreach var,$(VAR),echo $(var)=$($(var));)
84 #=========================
85 # Include the config file
88 CONFIG_FILE := $(OUTPUT_DIR)/config.omk
91 $(CONFIG_FILE)-default:
92 $(MAKE) default-config
94 ifeq ($(MAKECMDGOALS),default-config)
95 export DEFAULT_CONFIG_PASS=1
98 ifneq ($(DEFAULT_CONFIG_PASS),1)
99 include $(CONFIG_FILE)-default
102 -include $(OUTPUT_DIR)/config.target
104 ifneq ($(wildcard $(CONFIG_FILE)),)
105 -include $(CONFIG_FILE)
109 CONFIG_FILES ?= $(wildcard $(CONFIG_FILE)-default) $(wildcard $(OUTPUT_DIR)/config.target) $(wildcard $(CONFIG_FILE))
112 export SED4OMK SOURCES_DIR MAKERULES_DIR RELATIVE_DIR INVOCATION_DIR
113 export CONFIG_FILE CONFIG_FILES OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT
114 # OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes
115 # must to be serialized only in the toplevel make.
118 RELATIVE_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
120 #$(warning === RELATIVE_DIR = "$(RELATIVE_DIR)" ===)
121 override RELATIVE_DIR := $(RELATIVE_DIR:/%=%)
122 override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%)
123 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
124 #override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | $(SED4OMK) -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g' -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__')
125 #$(warning BACK2TOP_DIR = "$(BACK2TOP_DIR)")
127 #$(warning SOURCES_DIR = "$(SOURCES_DIR)")
128 #$(warning MAKERULES_DIR = "$(OUTPUT_DIR)")
129 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
131 # We have to use RELATIVE_PREFIX because of mingw
132 override RELATIVE_PREFIX := $(RELATIVE_DIR)/
133 override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%)
135 #vpath %.c $(SOURCES_DIR)
136 #vpath %.cc $(SOURCES_DIR)
137 #vpath %.cxx $(SOURCES_DIR)
139 # Define srcdir for Automake compatibility
140 srcdir = $(SOURCES_DIR)
142 # Defines for quiet compilation
144 ifeq ("$(origin V)", "command line")
151 ifneq ($(OMK_VERBOSE),0)
156 ifneq ($(findstring s,$(MAKEFLAGS)),)
157 QUIET_CMD_ECHO = true
160 QUIET_CMD_ECHO = echo
163 MAKEFILE_OMK=Makefile.omk
164 # All subdirectories (even linked ones) containing Makefile.omk
165 # Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS)
166 ALL_OMK_SUBDIRS = $(patsubst %/$(MAKEFILE_OMK),%,$(patsubst $(SOURCES_DIR)/%,%,$(wildcard $(SOURCES_DIR)/*/$(MAKEFILE_OMK))))
168 # ===================================================================
169 # We have set up all important variables, so we can check and include
170 # real OCERA style Makefile.omk now
172 include $(SOURCES_DIR)/$(MAKEFILE_OMK)
173 ifeq ($(AUTOMATIC_SUBDIRS),y)
174 SUBDIRS?=$(ALL_OMK_SUBDIRS)
180 @echo 'Use "make V=1" to see the verbose compile lines.'
183 @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | $(SED4OMK) -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
184 if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
185 echo "Your make program version ($(MAKE_VERSION)) is too old and does not support OMK system." ; \
186 echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \
189 distclean dist-clean:
190 @$(QUIET_CMD_ECHO) " RM $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
191 @rm -fr $(OUTPUT_DIR)/$(COMPILED_DIR_NAME) $(OUTPUT_DIR)/$(BUILD_DIR_NAME)
193 # Common OMK templates
194 # ====================
196 # Syntax: $(call mkdir,<dir name>)
198 [ -d $(1) ] || mkdir -p $(1) || exit 1
201 ifneq ($(OMK_VERBOSE),2)
202 NO_PRINT_DIRECTORY := --no-print-directory
205 ifeq ($(USE_LEAF_MAKEFILES),n)
206 export USE_LEAF_MAKEFILES
207 SUBDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
208 SOURCESDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
210 SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(3)/Makefile
211 SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile
218 # Call a pass in a subdirectory
219 # Usage: $(call omk_pass_subdir_template,<pass name>,<build dir>,<subdir>)
220 define omk_pass_subdir_template
221 .PHONY: $(pass)-$(3)-subdir
222 $(pass)-submakes: $(pass)-$(3)-subdir
223 $(pass)-$(3)-subdir: MAKEOVERRIDES:=$(filter-out SUBDIRS=%,$(MAKEOVERRIDES))
225 @$(call mkdir_def,$(2)/$(3))
226 +@$(MAKE) --no-builtin-rules SOURCES_DIR=$(SOURCES_DIR)/$(3) $(NO_PRINT_DIRECTORY) \
227 RELATIVE_DIR=$(RELATIVE_PREFIX)$(3) -C $(2)/$(3) \
228 -f $(SUBDIR_MAKEFILE) $(pass)-submakes
229 # In subdirectories we can call submakes directly since passes are
230 # already serialized on the toplevel make.
234 check-target = $(1:%=%-check)
237 # Call a pass in a subdirectory
238 # Usage: $(call extra_rules_subdir_template,<subdir>)
239 define extra_rules_subdir_template
240 extra-rules-subdirs: extra-rules-$(1)
242 +@$(MAKE) OMK_SERIALIZE_INCLUDED=n MAKERULES_DIR=$(SOURCES_DIR)/$(1) OUTPUT_DIR=$(OUTPUT_DIR) \
243 SOURCES_DIR=$(SOURCES_DIR)/$(1) RELATIVE_DIR=$(RELATIVE_PREFIX)$(1) -C $(SOURCES_DIR)/$(1)
246 .PHONY: extra-rules-subdirs
249 $(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir))))
251 # Usage: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local enable condition>])
252 define omk_pass_template
253 .PHONY: $(pass) $(pass)-local $(pass)-check $(pass)-submakes
254 $(foreach subdir,$(SUBDIRS),$(eval $(call omk_pass_subdir_template,$(pass),$(2),$(subdir))))
256 # Submakes have to be called this way and not as dependecies for pass
257 # serialization to work
258 +@$(MAKE) --no-builtin-rules SOURCES_DIR=$(SOURCES_DIR) $(NO_PRINT_DIRECTORY) \
259 RELATIVE_DIR=$(RELATIVE_DIR) \
260 -f $(SOURCESDIR_MAKEFILE) $(pass)-submakes
262 @true # Do not emit "nothing to be done" messages
264 ifneq ($(4)$($(pass)_HOOKS)$($(pass)_REQUIRED),)
265 $(pass)-submakes: $(pass)-this-dir
266 $(pass)-this-dir: $(foreach subdir,$(SUBDIRS),$(pass)-$(subdir)-subdir)
267 +@echo "make[omk]: $(pass) in $(RELATIVE_DIR)"
268 @$(call mkdir_def,$(2))
269 +@$(MAKE) --no-builtin-rules $(NO_PRINT_DIRECTORY) SOURCES_DIR=$(SOURCES_DIR) RELATIVE_DIR=$(RELATIVE_DIR) -C $(2) \
270 -f $(SOURCESDIR_MAKEFILE) $(3) $(check-target) $(1:%=%-local)
271 ifneq ($(pass),clean)
272 $(pass)-local: $($(pass)_HOOKS)
277 # =======================
278 # DEFAULT CONFIG PASS
281 @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
282 @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
283 @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
284 @echo >> "$(CONFIG_FILE)-default"
285 @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(OUTPUT_DIR) \
286 RELATIVE_DIR="" SOURCES_DIR=$(OUTPUT_DIR) \
287 -f $(OUTPUT_DIR)/Makefile default-config-pass
289 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
291 default-config-pass-local:
292 # @echo Default config for $(RELATIVE_DIR)
293 @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
294 @$(foreach x, $(default_CONFIG), echo '$(x)' | \
295 $(SED4OMK) -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
299 $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
300 echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
302 $(Q)for i in `find -L . -name Makefile.omk` ; do \
304 if ! test -f "$${d}/Makefile.rules" && ! cmp -s Makefile "$${d}/Makefile"; then \
305 rm -f "$${d}/Makefile"; \
306 cp -v Makefile "$${d}/Makefile"; \