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