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