]> rtime.felk.cvut.cz Git - omk.git/blob - devel-test/Makefile.rules
7fd3affcd98846caac3a5926bb61af88032d889f
[omk.git] / devel-test / Makefile.rules
1 #  Makefile.rules - OCERA make framework common project rules -*- makefile -*- #OMK@base
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 ####                                                         #OMK@build
17 # kbuild: Generic definitions
18 OMK_RULES_TYPE=test                                          #OMK@__type
19                                                              #OMK@base
20 # We need to ensure definition of sources directory first
21 ifndef SOURCES_DIR
22 # Only shell built-in pwd understands -L
23 SOURCES_DIR := $(shell ( pwd -L ) )
24 endif
25
26 # If we are not called by OMK leaf Makefile...
27 ifndef MAKERULES_DIR
28 MAKERULES_DIR := $(abspath $(dir $(filter %Makefile.rules,$(MAKEFILE_LIST))))
29 endif
30
31 # OUTPUT_DIR is the place where _compiled, _build and possible other
32 # files/directories are created. By default is the same as
33 # $(MAKERULES_DIR).
34 ifndef OUTPUT_DIR
35 OUTPUT_DIR := $(MAKERULES_DIR)
36 endif
37
38 .PHONY: all default check-make-ver omkize
39
40 ifdef W
41   ifeq ("$(origin W)", "command line")
42     OMK_WHOLE_TREE:=$(W)
43   endif
44 endif
45 ifndef OMK_WHOLE_TREE
46   OMK_WHOLE_TREE:=0
47 endif
48
49 ifneq ($(OMK_WHOLE_TREE),1)
50 all: check-make-ver default
51         @echo "Compilation finished"
52 else
53 # Run make in the top-level directory
54 all:
55         @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
56 endif
57
58 ifdef OMK_TESTSROOT
59 # Usage: $(call canttest,<error message>)
60 define canttest
61         ( echo "$(1)" > $(OUTPUT_DIR)/_canttest; echo "$(1)"; exit 1 )
62 endef
63 else
64 define canttest
65         echo "$(1)"
66 endef
67 endif
68
69 #=========================
70 # Include the config file
71
72 # FIXME: I think CONFIG_FILE_OK variable is useless. We have three
73 # config files and it is not clearly defined to which file is this
74 # variable related.
75 ifneq ($(CONFIG_FILE_OK),y)
76 ifndef CONFIG_FILE
77 CONFIG_FILE      := $(OUTPUT_DIR)/config.omk
78 endif
79 ifneq ($(wildcard $(CONFIG_FILE)-default),)
80 -include $(CONFIG_FILE)-default
81 else
82 ifneq ($(MAKECMDGOALS),default-config)
83 $(warning Please, run "make default-config" first)
84 endif
85 endif
86
87 -include $(OUTPUT_DIR)/config.target
88
89 ifneq ($(wildcard $(CONFIG_FILE)),)
90 -include $(CONFIG_FILE)
91 CONFIG_FILE_OK = y
92 endif
93 endif #$(CONFIG_FILE_OK)
94
95
96 CONFIG_FILES ?= $(wildcard $(CONFIG_FILE)-default) $(wildcard $(OUTPUT_DIR)/config.target) $(wildcard $(CONFIG_FILE))
97
98
99 export SOURCES_DIR MAKERULES_DIR RELATIVE_DIR
100 export CONFIG_FILE CONFIG_FILES OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT
101 # OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes
102 # must to be serialized only in the toplevel make.
103
104 ifndef RELATIVE_DIR
105 RELATIVE_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
106 endif
107 #$(warning  === RELATIVE_DIR = "$(RELATIVE_DIR)" ===)
108 override RELATIVE_DIR := $(RELATIVE_DIR:/%=%)
109 override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%)
110 #$(warning  RELATIVE_DIR = "$(RELATIVE_DIR)")
111 override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | sed -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g'  -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__')
112 #$(warning  BACK2TOP_DIR = "$(BACK2TOP_DIR)")
113
114 #$(warning SOURCES_DIR = "$(SOURCES_DIR)")
115 #$(warning MAKERULES_DIR = "$(OUTPUT_DIR)")
116 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
117
118 # We have to use RELATIVE_PREFIX because of mingw
119 override RELATIVE_PREFIX := $(RELATIVE_DIR)/
120 override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%)
121
122 #vpath %.c $(SOURCES_DIR)
123 #vpath %.cc $(SOURCES_DIR)
124 #vpath %.cxx $(SOURCES_DIR)
125
126 # Define srcdir for Automake compatibility
127 srcdir = $(SOURCES_DIR)
128
129 # Defines for quiet compilation
130 ifdef V
131   ifeq ("$(origin V)", "command line")
132     OMK_VERBOSE = $(V)
133   endif
134 endif
135 ifndef OMK_VERBOSE
136   OMK_VERBOSE = 0
137 endif
138 ifneq ($(OMK_VERBOSE),0)
139   Q =
140 else
141   Q = @
142 endif
143 ifneq ($(findstring s,$(MAKEFLAGS)),)
144   QUIET_CMD_ECHO = true
145   OMK_SILENT = 1
146 else
147   QUIET_CMD_ECHO = echo
148 endif
149
150 MAKEFILE_OMK=Makefile.omk
151 # All subdirectories (even linked ones) containing Makefile.omk
152 # Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS)
153 ALL_OMK_SUBDIRS = $(patsubst %/$(MAKEFILE_OMK),%,$(patsubst $(SOURCES_DIR)/%,%,$(wildcard $(SOURCES_DIR)/*/$(MAKEFILE_OMK))))
154
155 # ===================================================================
156 # We have set up all important variables, so we can check and include
157 # real OCERA style Makefile.omk now
158 ifndef OMK_INCLUDED
159 include $(SOURCES_DIR)/$(MAKEFILE_OMK)
160 ifeq ($(AUTOMATIC_SUBDIRS),y)
161 SUBDIRS?=$(ALL_OMK_SUBDIRS)
162 endif
163 OMK_INCLUDED := 1
164 endif
165
166 check-make-ver:
167         @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
168         if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
169                 echo "Your make program version is too old and does not support OMK system." ; \
170                 echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \
171         fi
172
173 distclean dist-clean:
174         @$(QUIET_CMD_ECHO) "  RM      $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
175         @rm -fr $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)  $(OUTPUT_DIR)/$(BUILD_DIR_NAME)
176
177 # Common OMK templates
178 # ====================
179
180 # Syntax: $(call mkdir,<dir name>)
181 define mkdir_def
182         [ -d $(1) ] || mkdir -p $(1) || exit 1
183 endef
184
185 ifneq ($(OMK_VERBOSE),2)
186 NO_PRINT_DIRECTORY := --no-print-directory
187 endif
188
189 ifeq ($(USE_LEAF_MAKEFILES),n)
190 export USE_LEAF_MAKEFILES
191 SUBDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
192 SOURCESDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
193 else
194 SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(3)/Makefile
195 SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile
196 endif
197
198 pass = $(strip $(1))
199
200 # Call a pass in a subdirectory
201 # Usage: $(call omk_pass_subdir_template,<pass name>,<build dir>,<subdir>)
202 define omk_pass_subdir_template
203 .PHONY: $(pass)-$(3)-subdir
204 $(pass)-submakes: $(pass)-$(3)-subdir
205 $(pass)-$(3)-subdir:
206         @$(call mkdir_def,$(2)/$(3))
207         +@$(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(3) $(NO_PRINT_DIRECTORY) \
208                 RELATIVE_DIR=$(RELATIVE_PREFIX)$(3) -C $(2)/$(3) \
209                 -f $(SUBDIR_MAKEFILE) $(pass)-submakes
210 # In subdirectories we can call submakes directly since passes are
211 # already searialized on the toplevel make.
212 endef
213
214 ifdef OMK_TESTSROOT
215 check-target = $(1:%=%-check)
216 endif
217
218 # Call a pass in a subdirectory
219 # Usage: $(call extra_rules_subdir_template,<subdir>)
220 define extra_rules_subdir_template
221 extra-rules-subdirs: extra-rules-$(1)
222 extra-rules-$(1):
223         +@$(MAKE) OMK_SERIALIZE_INCLUDED=n MAKERULES_DIR=$(SOURCES_DIR)/$(1) OUTPUT_DIR=$(OUTPUT_DIR) \
224                 SOURCES_DIR=$(SOURCES_DIR)/$(1) RELATIVE_DIR=$(RELATIVE_PREFIX)$(1) -C $(SOURCES_DIR)/$(1)
225 endef
226
227 .PHONY: extra-rules-subdirs
228 extra-rules-subdirs:
229
230 $(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir))))
231
232 # Usage: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local enable condition>])
233 define omk_pass_template
234 .PHONY: $(pass) $(pass)-local $(pass)-check $(pass)-submakes
235 $(foreach subdir,$(SUBDIRS),$(eval $(call omk_pass_subdir_template,$(pass),$(2),$(subdir))))
236 $(pass):
237 # Submakes have to be called this way and not as dependecies for pass
238 # serialization to work
239         +@$(MAKE) SOURCES_DIR=$(SOURCES_DIR) $(NO_PRINT_DIRECTORY) \
240                 RELATIVE_DIR=$(RELATIVE_DIR) \
241                 -f $(SOURCESDIR_MAKEFILE) $(pass)-submakes
242 $(pass)-submakes:
243         @true                   # Do not emit "nothing to be done" messages
244
245 ifneq ($(4)$($(pass)_HOOKS),)
246 $(pass)-submakes: $(pass)-this-dir
247 $(pass)-this-dir: $(foreach subdir,$(SUBDIRS),$(pass)-$(subdir)-subdir)
248         +@echo "make[omk]: $(pass) in $(RELATIVE_DIR)"
249         @$(call mkdir_def,$(2))
250         +@$(MAKE) $(NO_PRINT_DIRECTORY) SOURCES_DIR=$(SOURCES_DIR) RELATIVE_DIR=$(RELATIVE_DIR) -C $(2) \
251                 -f $(SOURCESDIR_MAKEFILE) $(3) $(check-target) $(1:%=%-local)
252 $(pass)-local: $($(pass)_HOOKS)
253 endif
254 endef
255
256 # =======================
257 # DEFAULT CONFIG PASS
258
259 default-config:
260         @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
261         @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
262         @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
263         @echo >> "$(CONFIG_FILE)-default"
264         @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(OUTPUT_DIR) \
265                 RELATIVE_DIR="" SOURCES_DIR=$(OUTPUT_DIR) \
266                 -f $(OUTPUT_DIR)/Makefile default-config-pass
267
268 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
269
270 default-config-pass-local:
271 #       @echo Default config for $(RELATIVE_DIR)
272         @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
273         @$(foreach x, $(default_CONFIG), echo '$(x)' | \
274                 sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
275
276
277 omkize:
278         $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
279            echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
280         fi
281         $(Q)for i in `find -L . -name Makefile.omk` ; do \
282            d=`dirname $${i}`; \
283            if ! test -f "$${d}/Makefile.rules" && ( ! test -f "$${d}/Makefile" || ! cmp --silent Makefile "$${d}/Makefile" ); then \
284               rm -f "$${d}/Makefile"; \
285               cp -v Makefile "$${d}/Makefile"; \
286            fi \
287         done
288                                                              #OMK@build
289 # Convenient variables
290 comma   := ,
291 squote  := '
292 #'
293 empty   :=
294 space   := $(empty) $(empty)
295
296 BUILD_DIR_NAME = _build
297 COMPILED_DIR_NAME = _compiled
298
299 USER_INCLUDE_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/include
300 USER_LIB_DIR     := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/lib
301 USER_UTILS_DIR   := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin-utils
302 USER_TESTS_DIR   := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin-tests
303 USER_BIN_DIR     := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin
304 USER_BUILD_DIR   := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/user
305 LINK_BUILD_DIR   := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/link
306
307 USER_OBJS_DIR = $(USER_BUILD_DIR)/$(RELATIVE_DIR)
308 KERN_OBJS_DIR = $(KERN_BUILD_DIR)/$(RELATIVE_DIR)
309 OMK_WORK_DIR  = $(USER_OBJS_DIR)
310
311 strip_out = $(patsubst $(OUTPUT_DIR)/%,%,$(1))
312 repl_out  = $(patsubst $(OUTPUT_DIR)/%,$$(OUTPUT_DIR)/%,$(1))
313
314 .PHONY: FORCE
315
316 ################
317 # Prepare pass #
318 ################
319
320 $(eval $(call omk_pass_template,prepare-pass,$(USER_OBJS_DIR),,always))
321
322 ###
323 # Name of the prepared rule.  This name is used as prefix of variable
324 # names in .omk.inc and is based on the first target
325 omk_inc_rule = $(firstword $(1:$(OUTPUT_DIR)/%=%))
326
327 # The name of .omk.inc file is derived from $(omk_inc_rule) and
328 # depends whether the target is under _build or elsewhere.
329 omk_inc_file = $(if $(filter $(BUILD_DIR_NAME)%,$(omk_inc_rule)),\
330                     $(OMK_WORK_DIR)/$(notdir $(omk_inc_rule)).omk.inc,\
331                     $(OMK_WORK_DIR)/$(subst /,_,$(omk_inc_rule)).omk.inc)
332
333 target_omk_file = $(OMK_WORK_DIR)/$(notdir $(1)).target.omk
334
335 ###
336 # prepare_rule is used to generate .omk.inc files during prepare-pass.
337 # All paths in this file should be relative to $(OUTPUT_DIR)
338 # Usage: $(call prepare_rule,<targets>,<deps>,<msg>,<cmd>)
339 define prepare_rule
340 prepare-pass-local: $(omk_inc_file)
341
342 $(omk_inc_file): FORCE
343         $(Q)echo 'prepared_rules += $(omk_inc_rule)' > $$@.tmp; \
344             echo '$(omk_inc_rule)_targets = $$(call strip_out,$(1))' >> $$@.tmp; \
345             echo '$(omk_inc_rule)_deps += $$(call strip_out,$(2))' >> $$@.tmp; \
346             echo '$(omk_inc_rule)_msg = $(3)' >> $$@.tmp; \
347             echo '$(omk_inc_rule)_cmd = $$(call repl_out,$(4))' >> $$@.tmp; \
348         if cmp -s $$@.tmp $$@; then rm $$@.tmp; else mv $$@.tmp $$@; fi
349 endef
350
351 ###
352 # prepare_rule_default - same as prepare rules but the target is put
353 # as dependency to the default build rule, which causes it to be built
354 # upon calling make
355 define prepare_rule_default
356 $(call prepare_rule,$(1),$(2),$(3),$(4))
357 prepared_default_build_targets += $(call strip_out,$(1))
358 endef
359
360 prepare-pass-local: $(OMK_WORK_DIR)/omk.inc
361 $(OMK_WORK_DIR)/omk.inc: FORCE
362         $(Q)echo 'default_build_targets += $(prepared_default_build_targets)' > $@.tmp; \
363             if cmp -s $@.tmp $@; then rm $@.tmp; else mv $@.tmp $@; fi
364
365
366 ############
367 # Programs #
368 ############
369
370 # Usage: $(call program_template,<executable-name>,<bin|utils|test>)
371 define program_template
372 $(1)_OBJS += $$(patsubst %.c,%.o,$$(filter %.c,$$($(1)_SOURCES)))
373 $(1)_OBJS := $$(addprefix $(USER_OBJS_DIR),$$(sort $$($(1)_OBJS:%/=%)))
374
375 USER_OBJS  += $$($(1)_OBJS)
376 USER_SOURCES += $$($(1)_SOURCES)
377
378
379
380 program_cmd = $$(if $$(filter %.cc,$$($(1)_SOURCES)),$$(CXX),$$(CC)) \
381         $$($(1)_OBJS) $$($(1)_LIBS:%=-l%) $$(LOADLIBES) $$(LDFLAGS) -Wl,-rpath-link,$(USER_LIB_DIR) \
382         -Wl,-Map,$(USER_OBJS_DIR)/$(1).exe.map -o $$$$@
383
384 $(call prepare_rule_default,\
385         $(USER_BIN_DIR)/$(1)$(EXE_SUFFIX),\
386         $$($(1)_OBJS) $$($(1)_LIBS),\
387         "LINK    ",\
388         $$(program_cmd))
389 endef
390
391 c_o_cmd := set -e; cd $$(dir $$@); \
392         if $$(c_o_COMPILE) $$(CC_DEPFLAGS) -o $$@ -c $$< ; \
393         then mv -f "$$@.d.tmp" "$$@.d" ; \
394         else rm -f "$$@.d.tmp" ; \
395         fi
396
397
398 $(foreach prog,$(bin_PROGRAMS),$(eval $(call program_template,$(prog),bin)))
399 $(foreach src,$(filter %.c,$(USER_SOURCES)),$(eval $(call prepare_rule,\
400         $(USER_OBJS_DIR)/$(src:%.c=%.o),\
401         $(SOURCES_DIR)/$(src),\
402         "CC      ",\
403         $(c_o_cmd))))
404
405 default: check-dir prepare-pass
406         $(MAKE) -qp -f Makefile.build > make.db.build-pseudo-pass; \
407         $(MAKE) -f Makefile.build
408
409 check-dir:
410         @$(call mkdir_def,$(USER_BUILD_DIR))
411         @$(call mkdir_def,$(USER_INCLUDE_DIR))
412         @$(call mkdir_def,$(USER_LIB_DIR))
413         @$(call mkdir_def,$(USER_BIN_DIR))
414         @$(call mkdir_def,$(USER_UTILS_DIR))
415         @$(call mkdir_def,$(USER_TESTS_DIR))