]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/base.omk
Adaptation for AQuoSA
[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 #  Adapted by Tommaso Cucinotta, Scuola Superiore Sant'Anna, Pisa, Italy, for the
10 #  Project Adaptive Quality of Service Architecture (AQuoSA)
11 #  More information at: http://aquosa.sourceforge.net
12 #
13 # The OMK build system is distributed under the GNU General Public
14 # License.  See file COPYING for details.
15 #
16 #
17 # input variables
18 # V                .. if set to 1, full command text is shown else short form is used
19 # W                .. whole tree - if set to 1, make is always called from the top-level directory
20 # SUBDIRS          .. list of subdirectories intended for make from actual directory
21 # kernel_SUBDIRS   .. subdirectories within which to propagate kernel targets
22 # library_SUBDIRS  .. list of subdirectories within which to propagate library targets
23 # include_SUBDIRS  .. list of subdirectories within which to propagate include pass (defaults to kernel_SUBDIRS + library_SUBDIRS)
24 # binary_SUBDIRS   .. list of subdirectories within which to propagate binary targets
25 # docs_SUBDIRS     .. list of subdirectories within which to propagate docs target
26 # clean_SUBDIRS    .. list of subdirectories within which to propagate clean targets
27 # default_CONFIG   .. list of default config assignments CONFIG_XXX=y/n ...
28
29 # We need to ensure definition of sources directory first
30 ifndef SOURCES_DIR
31 # Only shell built-in pwd understands -L
32 SOURCES_DIR := $(shell ( pwd -L ) )
33 endif
34
35 include $(MAKERULES_DIR)/variables.mk
36
37 # If we are not called by OMK leaf Makefile...
38 ifndef MAKERULES_DIR
39 MAKERULES_DIR := $(abspath $(dir $(filter %Makefile.rules,$(MAKEFILE_LIST))))
40 endif
41
42 # OUTPUT_DIR is the place where _compiled, _build and possible other
43 # files/directories are created. By default is the same as
44 # $(MAKERULES_DIR).
45 ifndef OUTPUT_DIR
46 OUTPUT_DIR := $(MAKERULES_DIR)
47 endif
48
49 .PHONY: all default check-make-ver omkize
50
51 ifdef W
52   ifeq ("$(origin W)", "command line")
53     OMK_WHOLE_TREE:=$(W)
54   endif
55 endif
56 ifndef OMK_WHOLE_TREE
57   OMK_WHOLE_TREE:=0
58 endif
59
60 ifneq ($(OMK_WHOLE_TREE),1)
61 all: check-make-ver default
62         @echo "Compilation finished"
63 else
64 # Run make in the top-level directory
65 all:
66         @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
67 endif
68
69 ifdef OMK_TESTSROOT
70 # Usage: $(call canttest,<error message>)
71 define canttest
72         ( echo "$(1)" > $(OUTPUT_DIR)/_canttest; echo "$(1)"; exit 1 )
73 endef
74 else
75 define canttest
76         echo "$(1)"
77 endef
78 endif
79
80 #=========================
81 # Include the config file
82
83 # FIXME: I think CONFIG_FILE_OK variable is useless. We have three
84 # config files and it is not clearly defined to which file is this
85 # variable related.
86 ifneq ($(CONFIG_FILE_OK),y)
87 ifndef CONFIG_FILE
88 CONFIG_FILE      := $(OUTPUT_DIR)/config.omk
89 endif
90 ifneq ($(wildcard $(CONFIG_FILE)-default),)
91 -include $(CONFIG_FILE)-default
92 else
93 #ifneq ($(MAKECMDGOALS),default-config)
94 #$(warning Please, run "make default-config" first)
95 #endif
96 endif
97
98 -include $(OUTPUT_DIR)/config.target
99 -include $(MAKERULES_DIR)/variables.mk
100
101 ifneq ($(wildcard $(CONFIG_FILE)),)
102 -include $(CONFIG_FILE)
103 CONFIG_FILE_OK = y
104 endif
105 endif #$(CONFIG_FILE_OK)
106
107
108 CONFIG_FILES ?= $(wildcard $(CONFIG_FILE)-default) $(wildcard $(OUTPUT_DIR)/config.target) $(wildcard $(CONFIG_FILE)) $(wildcard $(MAKERULES_DIR)/variables.mk)
109
110
111 export SOURCES_DIR MAKERULES_DIR RELATIVE_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)/ | sed -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 all_SUBDIRS=$(kernel_SUBDIRS) $(library_SUBDIRS) $(binary_SUBDIRS) $(docs_SUBDIRS) $(clean_SUBDIRS) $(include_SUBDIRS) $(QT_SUBDIRS)
179 include_SUBDIRS+=$(kernel_SUBDIRS) $(library_SUBDIRS)
180
181 check-make-ver:
182         @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -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 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) autom4te.cache config.log config.log config.status
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 # Call a pass in a subdirectory
216 # Usage: $(call omk_pass_subdir_template,<pass name>,<build dir>,<subdir>)
217 define omk_pass_subdir_template
218 .PHONY: $(pass)-$(3)-subdir
219 $(pass)-submakes: $(pass)-$(3)-subdir
220 $(pass)-$(3)-subdir:
221         @$(call mkdir_def,$(2)/$(3))
222         +@$(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(3) $(NO_PRINT_DIRECTORY) \
223                 RELATIVE_DIR=$(RELATIVE_PREFIX)$(3) -C $(2)/$(3) \
224                 -f $(SUBDIR_MAKEFILE) $(pass)-submakes
225 # In subdirectories we can call submakes directly since passes are
226 # already serialized on the toplevel make.
227 endef
228
229 ifdef OMK_TESTSROOT
230 check-target = $(1:%=%-check)
231 endif
232
233 # Call a pass in a subdirectory
234 # Usage: $(call extra_rules_subdir_template,<subdir>)
235 define extra_rules_subdir_template
236 extra-rules-subdirs: extra-rules-$(1)
237 extra-rules-$(1):
238         +@$(MAKE) OMK_SERIALIZE_INCLUDED=n MAKERULES_DIR=$(SOURCES_DIR)/$(1) OUTPUT_DIR=$(OUTPUT_DIR) \
239                 SOURCES_DIR=$(SOURCES_DIR)/$(1) RELATIVE_DIR=$(RELATIVE_PREFIX)$(1) -C $(SOURCES_DIR)/$(1)
240 endef
241
242 .PHONY: extra-rules-subdirs
243 extra-rules-subdirs:
244
245 $(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir))))
246
247 # Usage: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local enable condition>],[<pass directories>])
248 define omk_pass_template
249 .PHONY: $(pass) $(pass)-local $(pass)-check $(pass)-submakes
250 # Produce in SUBDIRS_NODUPS a set of directories without duplicates
251 SUBDIRS_NODUPS:=
252 $(foreach x,$(SUBDIRS) $(5),$(eval SUBDIRS_NODUPS:=$(SUBDIRS_NODUPS:$(x)=) $(x)))
253 $(foreach subdir,$(SUBDIRS_NODUPS),$(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) 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_NODUPS),$(pass)-$(subdir)-subdir)
266         +@echo "make[omk]: $(pass) in $(RELATIVE_DIR)"
267         @$(call mkdir_def,$(2))
268         +@$(MAKE) $(NO_PRINT_DIRECTORY) SOURCES_DIR=$(SOURCES_DIR) RELATIVE_DIR=$(RELATIVE_DIR) -C $(2) \
269                 -f $(SOURCESDIR_MAKEFILE) $(3) $(check-target) $(1:%=%-local)
270 $(pass)-local: $($(pass)_HOOKS)
271 endif
272 endef
273
274 # =======================
275 # DEFAULT CONFIG PASS
276
277 default-config:
278         @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
279         @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
280         @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
281         @echo >> "$(CONFIG_FILE)-default"
282         @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(OUTPUT_DIR) \
283                 RELATIVE_DIR="" SOURCES_DIR=$(OUTPUT_DIR) \
284                 -f $(OUTPUT_DIR)/Makefile default-config-pass
285
286 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always,))
287
288 default-config-pass-local:
289 #       @echo Default config for $(RELATIVE_DIR)
290         @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
291         @$(foreach x, $(default_CONFIG), echo '$(x)' | \
292                 sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
293
294
295 omkize:
296         $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
297            echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
298         fi
299         $(Q)for i in `find -L . -name Makefile.omk` ; do \
300            d=`dirname $${i}`; \
301            if ! test -f "$${d}/Makefile.rules" && ( test -f "$${d}/Makefile" && ! cmp --silent Makefile "$${d}/Makefile" ); then \
302               rm -f "$${d}/Makefile"; \
303               cp -v Makefile "$${d}/Makefile"; \
304            fi \
305         done