]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/base.omk
base.omk: need to process given pass can be specified by variable <pass_name>_REQUIRED .
[omk.git] / snippets / base.omk
1 #  Makefile.rules - OCERA make framework common project rules -*- makefile-gmake -*-
2 #
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
6 #
7 #  Homepage: http://rtime.felk.cvut.cz/omk/
8 #  Version:  @git-describe@
9 #
10 # The OMK build system is distributed under the GNU General Public
11 # License.  See file COPYING for details.
12 #
13 #
14 # input variables
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 ...
19
20 # If we are not called by OMK leaf Makefile...
21 ifndef MAKERULES_DIR
22 MAKERULES_DIR := $(abspath $(dir $(filter %Makefile.rules,$(MAKEFILE_LIST))))
23 endif
24
25 # The $(SED4OMK) command for BSD based systems requires -E option to allow
26 # extended regular expressions
27
28 SED4OMK ?= sed
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)
32     SED4OMK := gsed
33   endif
34   ifneq ($(shell ( echo A | $(SED4OMK) -n -e 's/A\|B/y/p' )),y)
35     SED4OMK := gsed -E
36   endif
37   ifneq ($(shell ( echo A | $(SED4OMK) -n -e 's/A\|B/y/p' )),y)
38     $(error No SED program suitable for OMK found)
39   endif
40 endif
41
42 # OUTPUT_DIR is the place where _compiled, _build and possible other
43 # files/directories are created. By default is the same as
44 # $(MAKERULES_DIR).
45 ifndef OUTPUT_DIR
46 OUTPUT_DIR := $(MAKERULES_DIR)
47 endif
48
49 # We need to ensure definition of sources directory first
50 ifndef SOURCES_DIR
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:\\%=%)
56 endif
57
58 .PHONY: all default check-make-ver print-hints omkize
59
60 ifdef W
61   ifeq ("$(origin W)", "command line")
62     OMK_WHOLE_TREE:=$(W)
63   endif
64 endif
65 ifndef OMK_WHOLE_TREE
66   OMK_WHOLE_TREE:=0
67 endif
68
69 ifneq ($(OMK_WHOLE_TREE),1)
70 all: check-make-ver print-hints default
71         @echo "Compilation finished"
72 else
73 # Run make in the top-level directory
74 all:
75         @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
76 endif
77
78 # omk-get-var target allows external scripts/programs to determine the
79 # values of OMK variables such as RELATIVE_DIR etc.
80 .PHONY: omk-get-var
81 omk-get-var:
82         @$(foreach var,$(VAR),echo $(var)=$($(var));)
83
84 #=========================
85 # Include the config file
86
87 ifndef CONFIG_FILE
88 CONFIG_FILE      := $(OUTPUT_DIR)/config.omk
89 endif
90
91 $(CONFIG_FILE)-default:
92         $(MAKE) default-config 
93
94 ifeq ($(MAKECMDGOALS),default-config)
95 export DEFAULT_CONFIG_PASS=1
96 endif
97
98 ifneq ($(DEFAULT_CONFIG_PASS),1)
99 include $(CONFIG_FILE)-default
100 endif
101
102 -include $(OUTPUT_DIR)/config.target
103
104 ifneq ($(wildcard $(CONFIG_FILE)),)
105 -include $(CONFIG_FILE)
106 endif
107
108
109 CONFIG_FILES ?= $(wildcard $(CONFIG_FILE)-default) $(wildcard $(OUTPUT_DIR)/config.target) $(wildcard $(CONFIG_FILE))
110
111
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.
116
117 ifndef RELATIVE_DIR
118 RELATIVE_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
119 endif
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)")
126
127 #$(warning SOURCES_DIR = "$(SOURCES_DIR)")
128 #$(warning MAKERULES_DIR = "$(OUTPUT_DIR)")
129 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
130
131 # We have to use RELATIVE_PREFIX because of mingw
132 override RELATIVE_PREFIX := $(RELATIVE_DIR)/
133 override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%)
134
135 #vpath %.c $(SOURCES_DIR)
136 #vpath %.cc $(SOURCES_DIR)
137 #vpath %.cxx $(SOURCES_DIR)
138
139 # Define srcdir for Automake compatibility
140 srcdir = $(SOURCES_DIR)
141
142 # Defines for quiet compilation
143 ifdef V
144   ifeq ("$(origin V)", "command line")
145     OMK_VERBOSE = $(V)
146   endif
147 endif
148 ifndef OMK_VERBOSE
149   OMK_VERBOSE = 0
150 endif
151 ifneq ($(OMK_VERBOSE),0)
152   Q =
153 else
154   Q = @
155 endif
156 ifneq ($(findstring s,$(MAKEFLAGS)),)
157   QUIET_CMD_ECHO = true
158   OMK_SILENT = 1
159 else
160   QUIET_CMD_ECHO = echo
161 endif
162
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))))
167
168 # ===================================================================
169 # We have set up all important variables, so we can check and include
170 # real OCERA style Makefile.omk now
171 ifndef OMK_INCLUDED
172 include $(SOURCES_DIR)/$(MAKEFILE_OMK)
173 ifeq ($(AUTOMATIC_SUBDIRS),y)
174 SUBDIRS?=$(ALL_OMK_SUBDIRS)
175 endif
176 OMK_INCLUDED := 1
177 endif
178
179 print-hints:
180         @echo 'Use "make V=1" to see the verbose compile lines.'
181
182 check-make-ver:
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 ; \
187         fi
188
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)
192
193 # Common OMK templates
194 # ====================
195
196 # Syntax: $(call mkdir,<dir name>)
197 define mkdir_def
198         [ -d $(1) ] || mkdir -p $(1) || exit 1
199 endef
200
201 ifneq ($(OMK_VERBOSE),2)
202 NO_PRINT_DIRECTORY := --no-print-directory
203 endif
204
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
209 else
210 SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(3)/Makefile
211 SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile
212 endif
213
214 pass = $(strip $(1))
215
216 unexport SUBDIRS
217
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))
224 $(pass)-$(3)-subdir:
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.
231 endef
232
233 ifdef OMK_TESTSROOT
234 check-target = $(1:%=%-check)
235 endif
236
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)
241 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)
244 endef
245
246 .PHONY: extra-rules-subdirs
247 extra-rules-subdirs:
248
249 $(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir))))
250
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))))
255 $(pass):
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
261 $(pass)-submakes:
262         @true                   # Do not emit "nothing to be done" messages
263
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)
273 endif
274 endif
275 endef
276
277 # =======================
278 # DEFAULT CONFIG PASS
279
280 default-config:
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
288
289 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
290
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" ; )
296
297
298 omkize:
299         $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
300            echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
301         fi
302         $(Q)for i in `find -L . -name Makefile.omk` ; do \
303            d=`dirname $${i}`; \
304            if ! test -f "$${d}/Makefile.rules" && ! cmp -s Makefile "$${d}/Makefile"; then \
305               rm -f "$${d}/Makefile"; \
306               cp -v Makefile "$${d}/Makefile"; \
307            fi \
308         done