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