]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/base
Added ALL_OMK_SUBDIRS variable.
[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 # The OMK build system is distributed under the GNU General Public
7 # License.  See file COPYING for details.
8 #
9 # input variables
10 # V                .. if set to 1, full command text is shown else short form is used
11 # W                .. whole tree - if set to 1, make is always called from the top-level directory
12 # SUBDIRS          .. list of subdirectories intended for make from actual directory
13 # default_CONFIG   .. list of default config assignments CONFIG_XXX=y/n ...
14 #
15 # output variables
16 # ALL_OMK_SUBDIRS  .. Expands to all subdirectories (even linked ones) containing Makefile.omk
17
18 # We need to ensure definition of sources directory first
19 ifndef SOURCES_DIR
20 SOURCES_DIR := $(shell ( pwd -L ) )
21 endif
22
23 .PHONY: all default check-make-ver
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 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 #vpath %.c $(SOURCES_DIR)
83 #vpath %.cc $(SOURCES_DIR)
84 #vpath %.cxx $(SOURCES_DIR)
85
86 VPATH = $(SOURCES_DIR)
87 srcdir = $(SOURCES_DIR)
88
89 # Defines for quiet compilation
90 ifdef V
91   ifeq ("$(origin V)", "command line")
92     OMK_VERBOSE = $(V)
93   endif
94 endif
95 ifndef OMK_VERBOSE
96   OMK_VERBOSE = 0
97 endif
98 ifeq ($(OMK_VERBOSE),1)
99   Q =
100 else
101   Q = @
102 endif
103 ifneq ($(findstring s,$(MAKEFLAGS)),)
104   QUIET_CMD_ECHO = true
105   OMK_SILENT = 1
106 else
107   QUIET_CMD_ECHO = echo
108 endif
109
110 # ===================================================================
111 # We have set up all important variables, so we can check and include
112 # real OCERA style Makefile.omk now
113 ifndef OMK_INCLUDED
114 include $(SOURCES_DIR)/Makefile.omk
115 OMK_INCLUDED := 1
116 endif
117
118 check-make-ver:
119         @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
120         if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
121                 echo "Your make program version is too old and does not support OMK system." ; \
122                 echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \
123         fi
124
125 distclean dist-clean:
126         @$(QUIET_CMD_ECHO) "  RM      $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
127         @rm -fr $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)  $(MAKERULES_DIR)/$(BUILD_DIR_NAME)
128
129 # Common OMK templates
130 # ====================
131
132 # Syntax: $(call mkdir,<dir name>)
133 define mkdir_def
134         [ -d $(1) ] || mkdir -p $(1) || exit 1
135 endef
136
137 ifneq ($(V),2)
138 NO_PRINT_DIRECTORY := --no-print-directory
139 endif
140
141 # Syntax: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local condition>])
142 define omk_pass_template
143 .PHNOY: $(1) $(1)-local
144 $(1):
145         +@$(foreach dir,$(SUBDIRS),$(call mkdir_def,$(2)/$(dir)); \
146                 $(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(dir) $(NO_PRINT_DIRECTORY) \
147                 RELATIVE_DIR=$(RELATIVE_DIR)/$(dir) -C $(2)/$(dir) \
148                 -f $(SOURCES_DIR)/$(dir)/Makefile $$@ || exit 1 ;) true
149 ifneq ($(4),)
150         @echo "make[omk]: $$@ in $(RELATIVE_DIR)"; \
151         $(call mkdir_def,$(2)); \
152         $(MAKE) $(NO_PRINT_DIRECTORY) -C $(2) \
153                 -f $(SOURCES_DIR)/Makefile $(3) $$(@:%=%-local)
154 endif
155 endef
156
157 # =======================
158 # DEFAULT CONFIG PASS
159
160 default-config:
161         @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
162         @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
163         @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
164         @echo >> "$(CONFIG_FILE)-default"
165         @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(MAKERULES_DIR) \
166                 RELATIVE_DIR="" SOURCES_DIR=$(MAKERULES_DIR) \
167                 -f $(MAKERULES_DIR)/Makefile default-config-pass
168
169 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
170
171 default-config-pass-local:
172 #       @echo Default config for $(RELATIVE_DIR)
173         @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
174         @$(foreach x, $(default_CONFIG), echo $(x) | \
175                 sed -e 's/^.*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
176
177 # =======================
178 # Useful macros
179
180 # All subdirectories (even linked ones) containing Makefile.omk
181 # Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS)
182 ALL_OMK_SUBDIRS = $(shell find -L $(SOURCES_DIR) -maxdepth 2 -mindepth 2 -name Makefile.omk|sed 's!$(SOURCES_DIR)/\(.*\)/.*!\1!')