]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/base
c5e0612c87c98828dbadae480f1c1304261948c6
[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 # We need to ensure definition of sources directory first
16 ifndef SOURCES_DIR
17 SOURCES_DIR := $(shell ( pwd -L ) )
18 endif
19
20 .PHONY: all default check-make-ver omkize
21
22 ifdef W
23   ifeq ("$(origin W)", "command line")
24     OMK_WHOLE_TREE:=$(W)
25   endif
26 endif
27 ifndef OMK_WHOLE_TREE
28   OMK_WHOLE_TREE:=0
29 endif
30
31 ifneq ($(OMK_WHOLE_TREE),1)
32 all: check-make-ver default
33         @echo "Compilation finished"
34 else
35 # Run make in the top-level directory
36 all:
37         @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
38 endif
39
40 #=========================
41 # Include the config file
42
43 ifneq ($(CONFIG_FILE_OK),y)
44 ifndef CONFIG_FILE
45 CONFIG_FILE      := $(MAKERULES_DIR)/config.omk
46 endif
47 ifneq ($(wildcard $(CONFIG_FILE)-default),)
48 -include $(CONFIG_FILE)-default
49 else
50 ifneq ($(MAKECMDGOALS),default-config)
51 $(warning Please, run "make default-config" first)
52 endif
53 endif
54
55 ifneq ($(wildcard $(CONFIG_FILE)),)
56 include $(CONFIG_FILE)
57 CONFIG_FILE_OK = y
58 endif
59 endif #$(CONFIG_FILE_OK)
60
61 export SOURCES_DIR MAKERULES_DIR RELATIVE_DIR
62 export CONFIG_FILE OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT
63 # OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes
64 # must to be serialized only in the toplevel make.
65
66 ifndef RELATIVE_DIR
67 RELATIVE_DIR := $(SOURCES_DIR:$(MAKERULES_DIR)%=%)
68 endif
69 #$(warning  === RELATIVE_DIR = "$(RELATIVE_DIR)" ===)
70 override RELATIVE_DIR := $(RELATIVE_DIR:/%=%)
71 override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%)
72 #$(warning  RELATIVE_DIR = "$(RELATIVE_DIR)")
73 override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | sed -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g'  -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__')
74 #$(warning  BACK2TOP_DIR = "$(BACK2TOP_DIR)")
75
76 #$(warning SOURCES_DIR = "$(SOURCES_DIR)")
77 #$(warning MAKERULES_DIR = "$(MAKERULES_DIR)")
78 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
79
80 # We have to use RELATIVE_PREFIX because of mingw
81 override RELATIVE_PREFIX := $(RELATIVE_DIR)/
82 override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%)
83
84 #vpath %.c $(SOURCES_DIR)
85 #vpath %.cc $(SOURCES_DIR)
86 #vpath %.cxx $(SOURCES_DIR)
87
88 VPATH = $(SOURCES_DIR)
89 srcdir = $(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 ifeq ($(OMK_VERBOSE),1)
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 # Syntax: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local condition>])
149 define omk_pass_template
150 .PHNOY: $(1) $(1)-local
151 $(1):
152         +@$(foreach dir,$(SUBDIRS),$(call mkdir_def,$(2)/$(dir)); \
153                 $(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(dir) $(NO_PRINT_DIRECTORY) \
154                 RELATIVE_DIR=$(RELATIVE_PREFIX)$(dir) -C $(2)/$(dir) \
155                 -f $(SOURCES_DIR)/$(dir)/Makefile $$@ || exit 1 ;) true
156 ifneq ($(4),)
157         @echo "make[omk]: $$@ in $(RELATIVE_DIR)"; \
158         $(call mkdir_def,$(2)); \
159         $(MAKE) $(NO_PRINT_DIRECTORY) SOURCES_DIR=$(SOURCES_DIR) RELATIVE_DIR=$(RELATIVE_DIR) -C $(2) \
160                 -f $(SOURCES_DIR)/Makefile $(3) $$(@:%=%-local)
161 endif
162 endef
163
164 # =======================
165 # DEFAULT CONFIG PASS
166
167 default-config:
168         @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
169         @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
170         @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
171         @echo >> "$(CONFIG_FILE)-default"
172         @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(MAKERULES_DIR) \
173                 RELATIVE_DIR="" SOURCES_DIR=$(MAKERULES_DIR) \
174                 -f $(MAKERULES_DIR)/Makefile default-config-pass
175
176 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
177
178 default-config-pass-local:
179 #       @echo Default config for $(RELATIVE_DIR)
180         @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
181         @$(foreach x, $(default_CONFIG), echo '$(x)' | \
182                 sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
183
184
185 omkize:
186         $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
187            echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
188         fi
189         $(Q)for i in `find -L . -name Makefile.omk` ; do \
190            i=`dirname $${i}`; \
191            if [ x"$$i" != x"." ]; then \
192               rm -f $${i}/Makefile; \
193               cp Makefile $${i}/Makefile; \
194            fi \
195         done