]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/base
omkbuild.py: Makefile.rules are constructed by replacing include directives in snippets
[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 MAKEFILE_OMK=Makefile.omk
149 # All subdirectories (even linked ones) containing Makefile.omk
150 # Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS)
151 ALL_OMK_SUBDIRS = $(patsubst %/$(MAKEFILE_OMK),%,$(patsubst $(SOURCES_DIR)/%,%,$(wildcard $(SOURCES_DIR)/*/$(MAKEFILE_OMK))))
152
153 # ===================================================================
154 # We have set up all important variables, so we can check and include
155 # real OCERA style Makefile.omk now
156 ifndef OMK_INCLUDED
157 include $(SOURCES_DIR)/$(MAKEFILE_OMK)
158 ifeq ($(AUTOMATIC_SUBDIRS),y)
159 SUBDIRS?=$(ALL_OMK_SUBDIRS)
160 endif
161 OMK_INCLUDED := 1
162 endif
163
164 check-make-ver:
165         @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
166         if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
167                 echo "Your make program version is too old and does not support OMK system." ; \
168                 echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \
169         fi
170
171 distclean dist-clean:
172         @$(QUIET_CMD_ECHO) "  RM      $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
173         @rm -fr $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)  $(OUTPUT_DIR)/$(BUILD_DIR_NAME)
174
175 # Common OMK templates
176 # ====================
177
178 # Syntax: $(call mkdir,<dir name>)
179 define mkdir_def
180         [ -d $(1) ] || mkdir -p $(1) || exit 1
181 endef
182
183 ifneq ($(OMK_VERBOSE),2)
184 NO_PRINT_DIRECTORY := --no-print-directory
185 endif
186
187 ifeq ($(USE_LEAF_MAKEFILES),n)
188 export USE_LEAF_MAKEFILES
189 SUBDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
190 SOURCESDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
191 else
192 SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(3)/Makefile
193 SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile
194 endif
195
196 pass = $(strip $(1))
197
198 # Call a pass in a subdirectory
199 # Usage: $(call omk_pass_subdir_template,<pass name>,<build dir>,<subdir>)
200 define omk_pass_subdir_template
201 .PHONY: $(pass)-$(3)-subdir
202 $(pass)-submakes: $(pass)-$(3)-subdir
203 $(pass)-$(3)-subdir:
204         @$(call mkdir_def,$(2)/$(3))
205         +@$(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(3) $(NO_PRINT_DIRECTORY) \
206                 RELATIVE_DIR=$(RELATIVE_PREFIX)$(3) -C $(2)/$(3) \
207                 -f $(SUBDIR_MAKEFILE) $(pass)-submakes
208 # In subdirectories we can call submakes directly since passes are
209 # already searialized on the toplevel make.
210 endef
211
212 ifdef OMK_TESTSROOT
213 check-target = $(1:%=%-check)
214 endif
215
216 # Call a pass in a subdirectory
217 # Usage: $(call extra_rules_subdir_template,<subdir>)
218 define extra_rules_subdir_template
219 extra-rules-subdirs: extra-rules-$(1)
220 extra-rules-$(1):
221         +@$(MAKE) OMK_SERIALIZE_INCLUDED=n MAKERULES_DIR=$(SOURCES_DIR)/$(1) OUTPUT_DIR=$(OUTPUT_DIR) \
222                 SOURCES_DIR=$(SOURCES_DIR)/$(1) RELATIVE_DIR=$(RELATIVE_PREFIX)$(1) -C $(SOURCES_DIR)/$(1)
223 endef
224
225 .PHONY: extra-rules-subdirs
226 extra-rules-subdirs:
227
228 $(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir))))
229
230 # Usage: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local enable condition>])
231 define omk_pass_template
232 .PHONY: $(pass) $(pass)-local $(pass)-check $(pass)-submakes
233 $(foreach subdir,$(SUBDIRS),$(eval $(call omk_pass_subdir_template,$(pass),$(2),$(subdir))))
234 $(pass):
235 # Submakes have to be called this way and not as dependecies for pass
236 # serialization to work
237         +@$(MAKE) SOURCES_DIR=$(SOURCES_DIR) $(NO_PRINT_DIRECTORY) \
238                 RELATIVE_DIR=$(RELATIVE_DIR) \
239                 -f $(SOURCESDIR_MAKEFILE) $(pass)-submakes
240 $(pass)-submakes:
241         @true                   # Do not emit "nothing to be done" messages
242
243 ifneq ($(4)$($(pass)_HOOKS),)
244 $(pass)-submakes: $(pass)-this-dir
245 $(pass)-this-dir: $(foreach subdir,$(SUBDIRS),$(pass)-$(subdir)-subdir)
246         +@echo "make[omk]: $(pass) in $(RELATIVE_DIR)"
247         @$(call mkdir_def,$(2))
248         +@$(MAKE) $(NO_PRINT_DIRECTORY) SOURCES_DIR=$(SOURCES_DIR) RELATIVE_DIR=$(RELATIVE_DIR) -C $(2) \
249                 -f $(SOURCESDIR_MAKEFILE) $(3) $(check-target) $(1:%=%-local)
250 $(pass)-local: $($(pass)_HOOKS)
251 endif
252 endef
253
254 # =======================
255 # DEFAULT CONFIG PASS
256
257 default-config:
258         @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
259         @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
260         @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
261         @echo >> "$(CONFIG_FILE)-default"
262         @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(OUTPUT_DIR) \
263                 RELATIVE_DIR="" SOURCES_DIR=$(OUTPUT_DIR) \
264                 -f $(OUTPUT_DIR)/Makefile default-config-pass
265
266 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
267
268 default-config-pass-local:
269 #       @echo Default config for $(RELATIVE_DIR)
270         @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
271         @$(foreach x, $(default_CONFIG), echo '$(x)' | \
272                 sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
273
274
275 omkize:
276         $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
277            echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
278         fi
279         $(Q)for i in `find -L . -name Makefile.omk` ; do \
280            d=`dirname $${i}`; \
281            if ! test -f "$${d}/Makefile.rules" && ( ! test -f "$${d}/Makefile" || ! cmp --silent Makefile "$${d}/Makefile" ); then \
282               rm -f "$${d}/Makefile"; \
283               cp -v Makefile "$${d}/Makefile"; \
284            fi \
285         done