]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/base
937ee17056acb34cd491e67c5655901c41650cd1
[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
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 all: check-make-ver default
49         @echo "Compilation finished"
50 else
51 # Run make in the top-level directory
52 all:
53         @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
54 endif
55
56 ifdef OMK_TESTSROOT
57 # Usage: $(call canttest,<error message>)
58 define canttest
59         ( echo "$(1)" > $(OUTPUT_DIR)/_canttest; echo "$(1)"; exit 1 )
60 endef
61 else
62 define canttest
63         echo "$(1)"
64 endef
65 endif
66
67 #=========================
68 # Include the config file
69
70 # FIXME: I think CONFIG_FILE_OK variable is useless. We have three
71 # config files and it is not clearly defined to which file is this
72 # variable related.
73 ifneq ($(CONFIG_FILE_OK),y)
74 ifndef CONFIG_FILE
75 CONFIG_FILE      := $(OUTPUT_DIR)/config.omk
76 endif
77 ifneq ($(wildcard $(CONFIG_FILE)-default),)
78 -include $(CONFIG_FILE)-default
79 else
80 ifneq ($(MAKECMDGOALS),default-config)
81 $(warning Please, run "make default-config" first)
82 endif
83 endif
84
85 -include $(OUTPUT_DIR)/config.target
86
87 ifneq ($(wildcard $(CONFIG_FILE)),)
88 -include $(CONFIG_FILE)
89 CONFIG_FILE_OK = y
90 endif
91 endif #$(CONFIG_FILE_OK)
92
93
94 CONFIG_FILES ?= $(wildcard $(CONFIG_FILE)-default) $(wildcard $(OUTPUT_DIR)/config.target) $(wildcard $(CONFIG_FILE))
95
96
97 export SOURCES_DIR MAKERULES_DIR RELATIVE_DIR
98 export CONFIG_FILE CONFIG_FILES OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT
99 # OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes
100 # must to be serialized only in the toplevel make.
101
102 ifndef RELATIVE_DIR
103 RELATIVE_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
104 endif
105 #$(warning  === RELATIVE_DIR = "$(RELATIVE_DIR)" ===)
106 override RELATIVE_DIR := $(RELATIVE_DIR:/%=%)
107 override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%)
108 #$(warning  RELATIVE_DIR = "$(RELATIVE_DIR)")
109 override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | sed -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g'  -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__')
110 #$(warning  BACK2TOP_DIR = "$(BACK2TOP_DIR)")
111
112 #$(warning SOURCES_DIR = "$(SOURCES_DIR)")
113 #$(warning MAKERULES_DIR = "$(OUTPUT_DIR)")
114 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
115
116 # We have to use RELATIVE_PREFIX because of mingw
117 override RELATIVE_PREFIX := $(RELATIVE_DIR)/
118 override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%)
119
120 #vpath %.c $(SOURCES_DIR)
121 #vpath %.cc $(SOURCES_DIR)
122 #vpath %.cxx $(SOURCES_DIR)
123
124 # Define srcdir for Automake compatibility
125 srcdir = $(SOURCES_DIR)
126
127 # Defines for quiet compilation
128 ifdef V
129   ifeq ("$(origin V)", "command line")
130     OMK_VERBOSE = $(V)
131   endif
132 endif
133 ifndef OMK_VERBOSE
134   OMK_VERBOSE = 0
135 endif
136 ifneq ($(OMK_VERBOSE),0)
137   Q =
138 else
139   Q = @
140 endif
141 ifneq ($(findstring s,$(MAKEFLAGS)),)
142   QUIET_CMD_ECHO = true
143   OMK_SILENT = 1
144 else
145   QUIET_CMD_ECHO = echo
146 endif
147
148 # Convenient variables
149 comma   := ,
150 squote  := '
151 #'
152 empty   :=
153 space   := $(empty) $(empty)
154
155 ###
156 # Escape single quote for use in echo statements
157 escsq = $(subst $(squote),'\$(squote)',$1)
158
159 ###
160 # Strip/replace OUTPUT_DIR from/in the argument
161 strip_out = $(patsubst $(OUTPUT_DIR)/%,%,$(1))
162 repl_out  = $(patsubst $(OUTPUT_DIR)/%,$$(OUTPUT_DIR)/%,$(1))
163
164 .PHONY: FORCE
165
166
167 MAKEFILE_OMK=Makefile.omk
168 # All subdirectories (even linked ones) containing Makefile.omk
169 # Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS)
170 ALL_OMK_SUBDIRS = $(patsubst %/$(MAKEFILE_OMK),%,$(patsubst $(SOURCES_DIR)/%,%,$(wildcard $(SOURCES_DIR)/*/$(MAKEFILE_OMK))))
171
172 # ===================================================================
173 # We have set up all important variables, so we can check and include
174 # real OCERA style Makefile.omk now
175 ifndef OMK_INCLUDED
176 include $(SOURCES_DIR)/$(MAKEFILE_OMK)
177 ifeq ($(AUTOMATIC_SUBDIRS),y)
178 SUBDIRS?=$(ALL_OMK_SUBDIRS)
179 endif
180 OMK_INCLUDED := 1
181 endif
182
183 check-make-ver:
184         @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
185         if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
186                 echo "Your make program version is too old and does not support OMK system." ; \
187                 echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \
188         fi
189
190 distclean dist-clean:
191         @$(QUIET_CMD_ECHO) "  RM      $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
192         @rm -fr $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)  $(OUTPUT_DIR)/$(BUILD_DIR_NAME)
193
194 # Common OMK templates
195 # ====================
196
197 # Syntax: $(call mkdir,<dir name>)
198 define mkdir_def
199         [ -d $(1) ] || mkdir -p $(1) || exit 1
200 endef
201
202 ifneq ($(OMK_VERBOSE),2)
203 NO_PRINT_DIRECTORY := --no-print-directory
204 endif
205
206 ifeq ($(USE_LEAF_MAKEFILES),n)
207 export USE_LEAF_MAKEFILES
208 SUBDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
209 SOURCESDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
210 else
211 SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(3)/Makefile
212 SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile
213 endif
214
215 BUILD_DIR_NAME = _build
216 COMPILED_DIR_NAME = _compiled
217
218 BUILD_OMK_DIR := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/omk
219 OMK_WORK_DIR := $(BUILD_OMK_DIR)$(RELATIVE_DIR:%=/%)
220
221 check-dir::
222         @$(call mkdir_def,$(BUILD_OMK_DIR))
223
224 pass = $(strip $(1))
225
226 # Call a pass in a subdirectory
227 # Usage: $(call omk_pass_subdir_template,<pass name>,<subdir>)
228 define omk_pass_subdir_template
229 .PHONY: $(pass)-$(2)-subdir
230 $(pass)-subdirs: $(pass)-$(2)-subdir
231 $(pass)-$(2)-subdir:
232         @$(call mkdir_def,$(OMK_WORK_DIR)/$(2))
233         +@$(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(2) $(NO_PRINT_DIRECTORY) \
234                 RELATIVE_DIR=$(RELATIVE_PREFIX)$(2) -C $(2) \
235                 -f $(SUBDIR_MAKEFILE) $(pass)
236 endef
237
238 ifdef OMK_TESTSROOT
239 check-target = $(1:%=%-check)
240 endif
241
242 # Call a pass in a subdirectory
243 # Usage: $(call extra_rules_subdir_template,<subdir>)
244 define extra_rules_subdir_template
245 extra-rules-subdirs: extra-rules-$(1)
246 extra-rules-$(1):
247         +@$(MAKE) OMK_SERIALIZE_INCLUDED=n MAKERULES_DIR=$(SOURCES_DIR)/$(1) OUTPUT_DIR=$(OUTPUT_DIR) \
248                 SOURCES_DIR=$(SOURCES_DIR)/$(1) RELATIVE_DIR=$(RELATIVE_PREFIX)$(1) -C $(SOURCES_DIR)/$(1)
249 endef
250
251 .PHONY: extra-rules-subdirs
252 extra-rules-subdirs:
253
254 $(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir))))
255
256 # Usage: $(call omk_pass_template,<pass name>)
257 define omk_pass_template
258 .PHONY: $(pass) $(pass)-local $(pass)-msg $(pass)-subdirs
259 $(pass): $(pass)-local
260 $(pass)-local: $(pass)-subdirs $(pass)-msg
261 $(foreach subdir,$(SUBDIRS),$(call omk_pass_subdir_template,$(pass),$(subdir)))
262
263 $(pass)-msg: $(pass)-subdirs
264         +@echo "make[omk]: $(pass) in $(RELATIVE_DIR)"
265 endef
266
267
268 # =======================
269 # DEFAULT CONFIG PASS
270
271 default-config:
272         @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
273         @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
274         @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
275         @echo >> "$(CONFIG_FILE)-default"
276         @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(MAKERULES_DIR) \
277                 RELATIVE_DIR="" SOURCES_DIR=$(OUTPUT_DIR) \
278                 -f $(OUTPUT_DIR)/Makefile default-config-pass
279
280 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
281
282 default-config-pass-local:
283 #       @echo Default config for $(RELATIVE_DIR)
284         @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
285         @$(foreach x, $(default_CONFIG), echo '$(x)' | \
286                 sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
287
288
289 omkize:
290         $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
291            echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
292         fi
293         $(Q)for i in `find -L . -name Makefile.omk` ; do \
294            d=`dirname $${i}`; \
295            if ! test -f "$${d}/Makefile.rules" && ( ! test -f "$${d}/Makefile" || ! cmp --silent Makefile "$${d}/Makefile" ); then \
296               rm -f "$${d}/Makefile"; \
297               cp -v Makefile "$${d}/Makefile"; \
298            fi \
299         done
300 # Local Variables:
301 # mode:makefile-gmake
302 # End: