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