]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/base
37f6d8a0a7af07ea970946e5693b4ad3e8eff856
[omk.git] / snippets / base
1 #  Makefile.rules - OCERA make framework common project rules -*- makefile -*-
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 .PHONY: all default check-make-ver omkize prepare build-pseudo-pass
37
38 ifdef W
39   ifeq ("$(origin W)", "command line")
40     OMK_WHOLE_TREE:=$(W)
41   endif
42 endif
43 ifndef OMK_WHOLE_TREE
44   OMK_WHOLE_TREE:=0
45 endif
46
47 ifneq ($(OMK_WHOLE_TREE),1)
48   ifndef omk_prereq_checked
49   omk_checks=check-make-ver check-dir
50   export omk_prereq_checked=1
51   endif
52 all: build-pseudo-pass
53
54 prepare: $(omk_checks) prepare-pass
55 build-pseudo-pass: prepare
56         +@echo "make[omk]: build"
57         @$(MAKE) -C $(MAKERULES_DIR) -f Makefile.rules build
58         @echo "Compilation finished"
59
60 prepare-pass: $(omk_checks)
61 else
62 # Run make in the top-level directory
63 all:
64         @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
65 endif
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 ifneq ($(OMK_VERBOSE),0)
148   Q =
149 else
150   Q = @
151 endif
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         @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         @$(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         +@$(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         +@$(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)-local $(pass)-msg $(pass)-subdirs
270 $(pass): $(pass)-local
271 $(pass)-local: $(pass)-subdirs $(pass)-msg mkdir-omk-work
272 $(foreach subdir,$(SUBDIRS),$(call omk_pass_subdir_template,$(pass),$(subdir)))
273
274 $(pass)-msg: $(pass)-subdirs
275         +@echo "make[omk]: $(pass) in $(RELATIVE_DIR)"
276 endef
277
278 mkdir-omk-work:
279         @$(call mkdir_def,$(OMK_WORK_DIR))
280
281
282
283 # =======================
284 # DEFAULT CONFIG PASS
285
286 default-config:
287         @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
288         @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
289         @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
290         @echo >> "$(CONFIG_FILE)-default"
291         @$(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-local:
298 #       @echo Default config for $(RELATIVE_DIR)
299         @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
300         @$(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 #omkbuild
317 include build #omkbuild
318
319 # Local Variables:
320 # mode:makefile-gmake
321 # End: