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