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