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