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