]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/base
VPATH variable completely removed from rules.
[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 .PHONY: all default check-make-ver omkize
24
25 ifdef W
26   ifeq ("$(origin W)", "command line")
27     OMK_WHOLE_TREE:=$(W)
28   endif
29 endif
30 ifndef OMK_WHOLE_TREE
31   OMK_WHOLE_TREE:=0
32 endif
33
34 ifneq ($(OMK_WHOLE_TREE),1)
35 all: check-make-ver default
36         @echo "Compilation finished"
37 else
38 # Run make in the top-level directory
39 all:
40         @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
41 endif
42
43 #=========================
44 # Include the config file
45
46 ifneq ($(CONFIG_FILE_OK),y)
47 ifndef CONFIG_FILE
48 CONFIG_FILE      := $(MAKERULES_DIR)/config.omk
49 endif
50 ifneq ($(wildcard $(CONFIG_FILE)-default),)
51 -include $(CONFIG_FILE)-default
52 else
53 ifneq ($(MAKECMDGOALS),default-config)
54 $(warning Please, run "make default-config" first)
55 endif
56 endif
57
58 ifneq ($(wildcard $(CONFIG_FILE)),)
59 include $(CONFIG_FILE)
60 CONFIG_FILE_OK = y
61 endif
62 endif #$(CONFIG_FILE_OK)
63
64 export SOURCES_DIR MAKERULES_DIR RELATIVE_DIR
65 export CONFIG_FILE OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT
66 # OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes
67 # must to be serialized only in the toplevel make.
68
69 ifndef RELATIVE_DIR
70 RELATIVE_DIR := $(SOURCES_DIR:$(MAKERULES_DIR)%=%)
71 endif
72 #$(warning  === RELATIVE_DIR = "$(RELATIVE_DIR)" ===)
73 override RELATIVE_DIR := $(RELATIVE_DIR:/%=%)
74 override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%)
75 #$(warning  RELATIVE_DIR = "$(RELATIVE_DIR)")
76 override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | sed -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g'  -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__')
77 #$(warning  BACK2TOP_DIR = "$(BACK2TOP_DIR)")
78
79 #$(warning SOURCES_DIR = "$(SOURCES_DIR)")
80 #$(warning MAKERULES_DIR = "$(MAKERULES_DIR)")
81 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
82
83 # We have to use RELATIVE_PREFIX because of mingw
84 override RELATIVE_PREFIX := $(RELATIVE_DIR)/
85 override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%)
86
87 #vpath %.c $(SOURCES_DIR)
88 #vpath %.cc $(SOURCES_DIR)
89 #vpath %.cxx $(SOURCES_DIR)
90
91 # Defines for quiet compilation
92 ifdef V
93   ifeq ("$(origin V)", "command line")
94     OMK_VERBOSE = $(V)
95   endif
96 endif
97 ifndef OMK_VERBOSE
98   OMK_VERBOSE = 0
99 endif
100 ifneq ($(OMK_VERBOSE),0)
101   Q =
102 else
103   Q = @
104 endif
105 ifneq ($(findstring s,$(MAKEFLAGS)),)
106   QUIET_CMD_ECHO = true
107   OMK_SILENT = 1
108 else
109   QUIET_CMD_ECHO = echo
110 endif
111
112 MAKEFILE_OMK=Makefile.omk
113 # All subdirectories (even linked ones) containing Makefile.omk
114 # Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS)
115 ALL_OMK_SUBDIRS = $(patsubst %/$(MAKEFILE_OMK),%,$(patsubst $(SOURCES_DIR)/%,%,$(wildcard $(SOURCES_DIR)/*/$(MAKEFILE_OMK))))
116
117 # ===================================================================
118 # We have set up all important variables, so we can check and include
119 # real OCERA style Makefile.omk now
120 ifndef OMK_INCLUDED
121 include $(SOURCES_DIR)/$(MAKEFILE_OMK)
122 OMK_INCLUDED := 1
123 endif
124
125 check-make-ver:
126         @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
127         if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
128                 echo "Your make program version is too old and does not support OMK system." ; \
129                 echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \
130         fi
131
132 distclean dist-clean:
133         @$(QUIET_CMD_ECHO) "  RM      $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
134         @rm -fr $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)  $(MAKERULES_DIR)/$(BUILD_DIR_NAME)
135
136 # Common OMK templates
137 # ====================
138
139 # Syntax: $(call mkdir,<dir name>)
140 define mkdir_def
141         [ -d $(1) ] || mkdir -p $(1) || exit 1
142 endef
143
144 ifneq ($(V),2)
145 NO_PRINT_DIRECTORY := --no-print-directory
146 endif
147
148 ifeq ($(USE_LEAF_MAKEFILES),n)
149 export USE_LEAF_MAKEFILES
150 SUBDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
151 SOURCESDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
152 else
153 SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(dir)/Makefile
154 SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile
155 endif
156
157 # Syntax: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local condition>])
158 define omk_pass_template
159 .PHNOY: $(1) $(1)-local
160 $(1):
161         +@$(foreach dir,$(SUBDIRS),$(call mkdir_def,$(2)/$(dir)); \
162                 $(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(dir) $(NO_PRINT_DIRECTORY) \
163                 RELATIVE_DIR=$(RELATIVE_PREFIX)$(dir) -C $(2)/$(dir) \
164                 -f $(SUBDIR_MAKEFILE) $$@ || exit 1 ;) true
165 ifneq ($(4),)
166         @echo "make[omk]: $$@ in $(RELATIVE_DIR)"; \
167         $(call mkdir_def,$(2)); \
168         $(MAKE) $(NO_PRINT_DIRECTORY) SOURCES_DIR=$(SOURCES_DIR) RELATIVE_DIR=$(RELATIVE_DIR) -C $(2) \
169                 -f $(SOURCESDIR_MAKEFILE) $(3) $$(@:%=%-local)
170 endif
171 endef
172
173 # =======================
174 # DEFAULT CONFIG PASS
175
176 default-config:
177         @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
178         @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
179         @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
180         @echo >> "$(CONFIG_FILE)-default"
181         @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(MAKERULES_DIR) \
182                 RELATIVE_DIR="" SOURCES_DIR=$(MAKERULES_DIR) \
183                 -f $(MAKERULES_DIR)/Makefile default-config-pass
184
185 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
186
187 default-config-pass-local:
188 #       @echo Default config for $(RELATIVE_DIR)
189         @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
190         @$(foreach x, $(default_CONFIG), echo '$(x)' | \
191                 sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
192
193
194 omkize:
195         $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
196            echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
197         fi
198         $(Q)for i in `find -L . -name Makefile.omk` ; do \
199            i=`dirname $${i}`; \
200            if [ x"$$i" != x"." ]; then \
201               rm -f $${i}/Makefile; \
202               cp Makefile $${i}/Makefile; \
203            fi \
204         done