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