]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/base
Fixed error on inclusion of config.omk if it is broken symlink
[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 OMK_INCLUDED := 1
151 endif
152
153 check-make-ver:
154         @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
155         if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
156                 echo "Your make program version is too old and does not support OMK system." ; \
157                 echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \
158         fi
159
160 distclean dist-clean:
161         @$(QUIET_CMD_ECHO) "  RM      $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
162         @rm -fr $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)  $(MAKERULES_DIR)/$(BUILD_DIR_NAME)
163
164 # Common OMK templates
165 # ====================
166
167 # Syntax: $(call mkdir,<dir name>)
168 define mkdir_def
169         [ -d $(1) ] || mkdir -p $(1) || exit 1
170 endef
171
172 ifneq ($(V),2)
173 NO_PRINT_DIRECTORY := --no-print-directory
174 endif
175
176 ifeq ($(USE_LEAF_MAKEFILES),n)
177 export USE_LEAF_MAKEFILES
178 SUBDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
179 SOURCESDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
180 else
181 SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(3)/Makefile
182 SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile
183 endif
184
185 pass = $(strip $(1))
186
187 # Call a pass in a subdirectory
188 # Usage: $(call omk_pass_subdir_template,<pass name>,<build dir>,<subdir>)
189 define omk_pass_subdir_template
190 .PHONY: $(pass)-$(3)-subdir
191 $(pass)-submakes: $(pass)-$(3)-subdir
192 $(pass)-$(3)-subdir:
193         @$(call mkdir_def,$(2)/$(3))
194         +@$(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(3) $(NO_PRINT_DIRECTORY) \
195                 RELATIVE_DIR=$(RELATIVE_PREFIX)$(3) -C $(2)/$(3) \
196                 -f $(SUBDIR_MAKEFILE) $(pass)-submakes
197 # In subdirectories we can call submakes directly since passes are
198 # already searialized on the toplevel make.
199 endef
200
201 ifdef OMK_TESTSROOT
202 check-target = $(1:%=%-check)
203 endif
204
205 # Usage: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local enable condition>])
206 define omk_pass_template
207 .PHONY: $(pass) $(pass)-local $(pass)-check $(pass)-submakes
208 $(foreach subdir,$(SUBDIRS),$(eval $(call omk_pass_subdir_template,$(pass),$(2),$(subdir))))
209 $(pass):
210 # Submakes have to be called this way and not as dependecies for pass
211 # serialization to work
212         +@$(MAKE) SOURCES_DIR=$(SOURCES_DIR) --no-print-directory \
213                 RELATIVE_DIR=$(RELATIVE_DIR) \
214                 -f $(SOURCESDIR_MAKEFILE) $(pass)-submakes
215 $(pass)-submakes:
216         @true                   # Do not emit "nothing to be done" messages
217
218 ifneq ($(4)$($(pass)_HOOKS),)
219 $(pass)-submakes: $(pass)-this-dir
220 $(pass)-this-dir: $(foreach subdir,$(SUBDIRS),$(pass)-$(subdir)-subdir)
221         +@echo "make[omk]: $(pass) in $(RELATIVE_DIR)"
222         @$(call mkdir_def,$(2))
223         +@$(MAKE) $(NO_PRINT_DIRECTORY) SOURCES_DIR=$(SOURCES_DIR) RELATIVE_DIR=$(RELATIVE_DIR) -C $(2) \
224                 -f $(SOURCESDIR_MAKEFILE) $(3) $(check-target) $(1:%=%-local)
225 $(pass)-local: $($(pass)_HOOKS)
226 endif
227 endef
228
229 # =======================
230 # DEFAULT CONFIG PASS
231
232 default-config:
233         @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
234         @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
235         @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
236         @echo >> "$(CONFIG_FILE)-default"
237         @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(MAKERULES_DIR) \
238                 RELATIVE_DIR="" SOURCES_DIR=$(MAKERULES_DIR) \
239                 -f $(MAKERULES_DIR)/Makefile default-config-pass
240
241 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
242
243 default-config-pass-local:
244 #       @echo Default config for $(RELATIVE_DIR)
245         @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
246         @$(foreach x, $(default_CONFIG), echo '$(x)' | \
247                 sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
248
249
250 omkize:
251         $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
252            echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
253         fi
254         $(Q)for i in `find -L . -name Makefile.omk` ; do \
255            d=`dirname $${i}`; \
256            if ! test -f "$${d}/Makefile.rules" && ( ! test -f "$${d}/Makefile" || ! cmp --silent Makefile "$${d}/Makefile" ); then \
257               rm -f "$${d}/Makefile"; \
258               cp -v Makefile "$${d}/Makefile"; \
259            fi \
260         done