]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/base.omk
Generated Makefile.rules contain OMK version number
[omk.git] / snippets / base.omk
1 #  Makefile.rules - OCERA make framework common project rules -*- makefile-gmake -*-
2 #
3 #  (C) Copyright 2003 by Pavel Pisa - OCERA team member
4 #  (C) Copyright 2006 by Michal Sojka - Czech Technical University, FEE, DCE
5 #
6 #  Homepage: http://rtime.felk.cvut.cz/omk/
7 #  Version:  @git-describe@
8 #
9 # The OMK build system is distributed under the GNU General Public
10 # License.  See file COPYING for details.
11 #
12 #
13 # input variables
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 ...
18
19 # We need to ensure definition of sources directory first
20 ifndef SOURCES_DIR
21 # Only shell built-in pwd understands -L
22 SOURCES_DIR := $(shell ( pwd -L ) )
23 endif
24
25 # If we are not called by OMK leaf Makefile...
26 ifndef MAKERULES_DIR
27 MAKERULES_DIR := $(abspath $(dir $(filter %Makefile.rules,$(MAKEFILE_LIST))))
28 endif
29
30 # OUTPUT_DIR is the place where _compiled, _build and possible other
31 # files/directories are created. By default is the same as
32 # $(MAKERULES_DIR).
33 ifndef OUTPUT_DIR
34 OUTPUT_DIR := $(MAKERULES_DIR)
35 endif
36
37 ifdef W
38   ifeq ("$(origin W)", "command line")
39     OMK_WHOLE_TREE:=$(W)
40   endif
41 endif
42 ifndef OMK_WHOLE_TREE
43   OMK_WHOLE_TREE:=0
44 endif
45
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
50   endif
51 all: build-pseudo-pass
52
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"
58
59 prepare-pass:: $(omk_checks)
60 else
61 # Run make in the top-level directory
62 all:
63         $(Q3)$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
64 endif
65
66 .PHONY: all default check-make-ver omkize prepare build-pseudo-pass
67
68 ifdef OMK_TESTSROOT
69 # Usage: $(call canttest,<error message>)
70 define canttest
71         ( echo "$(1)" > $(OUTPUT_DIR)/_canttest; echo "$(1)"; exit 1 )
72 endef
73 else
74 define canttest
75         echo "$(1)"
76 endef
77 endif
78
79 #=========================
80 # Include the config file
81
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
84 # variable related.
85 ifneq ($(CONFIG_FILE_OK),y)
86 ifndef CONFIG_FILE
87 CONFIG_FILE      := $(OUTPUT_DIR)/config.omk
88 endif
89 ifneq ($(wildcard $(CONFIG_FILE)-default),)
90 -include $(CONFIG_FILE)-default
91 else
92 ifneq ($(MAKECMDGOALS),default-config)
93 $(warning Please, run "make default-config" first)
94 endif
95 endif
96
97 -include $(OUTPUT_DIR)/config.target
98
99 ifneq ($(wildcard $(CONFIG_FILE)),)
100 -include $(CONFIG_FILE)
101 CONFIG_FILE_OK = y
102 endif
103 endif #$(CONFIG_FILE_OK)
104
105
106 CONFIG_FILES ?= $(wildcard $(CONFIG_FILE)-default) $(wildcard $(OUTPUT_DIR)/config.target) $(wildcard $(CONFIG_FILE))
107
108
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.
113
114 ifndef RELATIVE_DIR
115 RELATIVE_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
116 endif
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)")
123
124 #$(warning SOURCES_DIR = "$(SOURCES_DIR)")
125 #$(warning MAKERULES_DIR = "$(OUTPUT_DIR)")
126 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
127
128 # We have to use RELATIVE_PREFIX because of mingw
129 override RELATIVE_PREFIX := $(RELATIVE_DIR)/
130 override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%)
131
132 #vpath %.c $(SOURCES_DIR)
133 #vpath %.cc $(SOURCES_DIR)
134 #vpath %.cxx $(SOURCES_DIR)
135
136 # Define srcdir for Automake compatibility
137 srcdir = $(SOURCES_DIR)
138
139 # Defines for quiet compilation
140 ifdef V
141   ifeq ("$(origin V)", "command line")
142     OMK_VERBOSE = $(V)
143   endif
144 endif
145 ifndef OMK_VERBOSE
146   OMK_VERBOSE = 0
147 endif
148
149 Q =  $(if $(filter 0    ,$(OMK_VERBOSE)),@)
150 Q2 = $(if $(filter 0 1  ,$(OMK_VERBOSE)),@)
151 Q3 = $(if $(filter 0 1 2,$(OMK_VERBOSE)),@)
152
153 ifneq ($(findstring s,$(MAKEFLAGS)),)
154   QUIET_CMD_ECHO = true
155   OMK_SILENT = 1
156 else
157   QUIET_CMD_ECHO = echo
158 endif
159
160 # Convenient variables
161 comma   := ,
162 squote  := '
163 #'
164 empty   :=
165 space   := $(empty) $(empty)
166
167 ###
168 # Escape single quote for use in echo statements
169 escsq = $(subst $(squote),'\$(squote)',$1)
170
171 ###
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))
175
176 .PHONY: FORCE
177
178
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))))
183
184 # ===================================================================
185 # We have set up all important variables, so we can check and include
186 # real OCERA style Makefile.omk now
187 ifndef OMK_INCLUDED
188 include $(SOURCES_DIR)/$(MAKEFILE_OMK)
189 ifeq ($(AUTOMATIC_SUBDIRS),y)
190 SUBDIRS?=$(ALL_OMK_SUBDIRS)
191 endif
192 OMK_INCLUDED := 1
193 endif
194
195
196 check-make-ver:
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 ; \
201         fi
202
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)
206
207 # Common OMK templates
208 # ====================
209
210 # Syntax: $(call mkdir_def,<dir name>)
211 define mkdir_def
212         [ -d $(1) ] || mkdir -p $(1) || exit 1
213 endef
214
215 ifneq ($(OMK_VERBOSE),2)
216 NO_PRINT_DIRECTORY := --no-print-directory
217 endif
218
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
223 else
224 SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(3)/Makefile
225 SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile
226 endif
227
228 BUILD_DIR_NAME = _build
229 COMPILED_DIR_NAME = _compiled
230
231 BUILD_OMK_DIR := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/omk
232 OMK_WORK_DIR := $(BUILD_OMK_DIR)$(RELATIVE_DIR:%=/%)
233
234 check-dir::
235         $(Q3)$(call mkdir_def,$(BUILD_OMK_DIR))
236
237 pass = $(strip $(1))
238
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
244 $(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)
248 endef
249
250 ifdef OMK_TESTSROOT
251 check-target = $(1:%=%-check)
252 endif
253
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)
258 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)
261 endef
262
263 .PHONY: extra-rules-subdirs mkdir-omk-work
264 extra-rules-subdirs:
265
266 $(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir))))
267
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))))
273
274 $(pass)-msg: $(pass)-subdirs
275         +@$(QUIET_CMD_ECHO) "make[omk]: $(pass) in $(RELATIVE_DIR)"
276 endef
277
278 mkdir-omk-work:
279         $(Q3)$(call mkdir_def,$(OMK_WORK_DIR))
280
281
282
283 # =======================
284 # DEFAULT CONFIG PASS
285
286 default-config:
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
294
295 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
296
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" ; )
302
303
304 omkize:
305         $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
306            echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
307         fi
308         $(Q)for i in `find -L . -name Makefile.omk` ; do \
309            d=`dirname $${i}`; \
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"; \
313            fi \
314         done
315
316 include prepare.omk #omkbuild
317 include build.omk #omkbuild