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