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