]> rtime.felk.cvut.cz Git - omk.git/blob - devel-test/Makefile.rules
Some tests
[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 ###
297 # Escape single quote for use in echo statements
298 escsq = $(subst $(squote),'\$(squote)',$1)
299
300 BUILD_DIR_NAME = _build
301 COMPILED_DIR_NAME = _compiled
302
303 USER_INCLUDE_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/include
304 USER_LIB_DIR     := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/lib
305 USER_UTILS_DIR   := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin-utils
306 USER_TESTS_DIR   := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin-tests
307 USER_BIN_DIR     := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin
308 USER_BUILD_DIR   := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/user
309 LINK_BUILD_DIR   := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/link
310
311 # Avoid double slash at the end in the top-level directory
312 USER_OBJS_DIR = $(USER_BUILD_DIR)$(RELATIVE_DIR:%=/%)
313 KERN_OBJS_DIR = $(KERN_BUILD_DIR)$(RELATIVE_DIR:%=/%)
314 OMK_WORK_DIR  = $(USER_OBJS_DIR)
315
316 strip_out = $(patsubst $(OUTPUT_DIR)/%,%,$(1))
317 repl_out  = $(patsubst $(OUTPUT_DIR)/%,$$(OUTPUT_DIR)/%,$(1))
318
319 .PHONY: FORCE
320
321 ################
322 # Prepare pass #
323 ################
324
325 $(eval $(call omk_pass_template,prepare-pass,$(USER_OBJS_DIR),,always))
326
327 ###
328 # Name of the prepared rule.  This name is used as prefix of variable
329 # names in .omk.inc and is based on the first target
330 omk_inc_rule = $(firstword $(1:$(OUTPUT_DIR)/%=%))
331
332 # The name of .omk.inc file is derived from $(omk_inc_rule) and
333 # depends whether the target is under _build or elsewhere.
334 omk_inc_file = $(if $(filter $(BUILD_DIR_NAME)%,$(omk_inc_rule)),\
335                     $(OMK_WORK_DIR)/$(notdir $(omk_inc_rule)).omk.inc,\
336                     $(OMK_WORK_DIR)/$(subst /,_,$(omk_inc_rule)).omk.inc)
337
338 target_omk_file = $(OMK_WORK_DIR)/$(notdir $(1)).target.omk
339
340 # >'< substitution is for echo to work,
341 # >$< substitution to preserve $ when reloading .omk.inc file
342 subst-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(1))))
343
344 ###
345 # prepare_rule is used to generate .omk.inc files during prepare-pass.
346 # All paths in this file should be relative to $(OUTPUT_DIR)
347 # Usage: $(call prepare_rule,<targets>,<deps>,<msg>,<cmd>)
348 define prepare_rule
349 prepare-pass-local: $(omk_inc_file)
350
351 $(omk_inc_file): FORCE
352         $(Q)echo 'prepared_rules += $(omk_inc_rule)' > $$@.tmp; \
353             echo '$(omk_inc_rule)_targets = $$(call strip_out,$(1))' >> $$@.tmp; \
354             echo '$(omk_inc_rule)_deps += $$(call strip_out,$(2))' >> $$@.tmp; \
355             echo '$(omk_inc_rule)_msg = $(3)' >> $$@.tmp; \
356             echo '$(omk_inc_rule)_cmd = $$(call subst-cmd,$$(call repl_out,$(4)))' >> $$@.tmp; \
357         if cmp -s $$@.tmp $$@; then rm $$@.tmp; else mv $$@.tmp $$@; fi
358 endef
359
360 ###
361 # prepare_rule_goal - same as prepare rules but the target is put
362 # as dependency to the default goal, which causes it to be built
363 # upon calling make
364 define prepare_rule_goal
365 $(call prepare_rule,$(1),$(2),$(3),$(4))
366 prepared_goals += $(call strip_out,$(1))
367 endef
368
369 prepare-pass-local: $(OMK_WORK_DIR)/omk.inc
370 $(OMK_WORK_DIR)/omk.inc: FORCE
371         $(Q)echo 'omk_goals += $(prepared_goals)' > $@.tmp; \
372             if cmp -s $@.tmp $@; then rm $@.tmp; else mv $@.tmp $@; fi
373
374
375 ############
376 # Programs #
377 ############
378
379 # Check GCC version for user build
380 ifndef CC_MAJOR_VERSION
381 CC_MAJOR_VERSION := $(shell $(CC) -dumpversion | sed -e 's/\([^.]\)\..*/\1/')
382 endif
383 # Prepare suitable define for dependency building
384 ifeq ($(CC_MAJOR_VERSION),2)
385 CC_DEPFLAGS = -Wp,-MD,"$@.d.tmp"
386 else
387 CC_DEPFLAGS = -MT $@ -MD -MP -MF "$@.d.tmp"
388 endif
389
390 c_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
391         $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -DOMK_FOR_USER
392
393 c_o_cmd = set -e; \
394         if $$(c_o_COMPILE) $$(CC_DEPFLAGS) -o $$$$@ -c $$$$< ; \
395         then mv -f "$$@.d.tmp" "$$@.d" ; \
396         else rm -f "$$@.d.tmp" ; \
397         fi
398
399
400 # Usage: $(call program_template,<executable-name>,<bin|utils|test>)
401 define program_template
402 $(1)_OBJS += $$(patsubst %.c,%.o,$$(filter %.c,$$($(1)_SOURCES)))
403 $(1)_OBJS := $$(addprefix $(USER_OBJS_DIR)/,$$(sort $$($(1)_OBJS:%/=%)))
404
405 USER_OBJS  += $$($(1)_OBJS)
406 USER_SOURCES += $$($(1)_SOURCES)
407
408
409
410 program_cmd = $$(if $$(filter %.cc,$$($(1)_SOURCES)),$$(CXX),$$(CC)) \
411         $$($(1)_OBJS) $$($(1)_LIBS:%=-l%) $$(LOADLIBES) $$(LDFLAGS) -Wl,-rpath-link,$(USER_LIB_DIR) \
412         -Wl,-Map,$(USER_OBJS_DIR)/$(1).exe.map -o $$$$@
413
414 $(call prepare_rule_goal,\
415         $(USER_BIN_DIR)/$(1)$(EXE_SUFFIX),\
416         $$($(1)_OBJS) $$($(1)_LIBS),\
417         "LINK    ",\
418         $$(program_cmd))
419 endef
420
421 $(foreach prog,$(bin_PROGRAMS),$(eval $(call program_template,$(prog),bin)))
422 $(foreach src,$(filter %.c,$(USER_SOURCES)),$(eval $(call prepare_rule,\
423         $(USER_OBJS_DIR)/$(src:%.c=%.o),\
424         $(SOURCES_DIR)/$(src),\
425         "CC      ",\
426         $(c_o_cmd))))
427
428 # OUTPUT_DIR is not defined in Makefile.build so we has to pass it on
429 # command-line. After this file will be merged with Makefile.rules, it
430 # will not be necessary to pass it.
431 default: check-dir prepare-pass
432         -$(MAKE) -qp -f Makefile.build OUTPUT_DIR=$(OUTPUT_DIR) > make.db.build-pseudo-pass;
433         $(MAKE) -f Makefile.build OUTPUT_DIR=$(OUTPUT_DIR)
434
435 check-dir:
436         @$(call mkdir_def,$(USER_BUILD_DIR))
437         @$(call mkdir_def,$(USER_INCLUDE_DIR))
438         @$(call mkdir_def,$(USER_LIB_DIR))
439         @$(call mkdir_def,$(USER_BIN_DIR))
440         @$(call mkdir_def,$(USER_UTILS_DIR))
441         @$(call mkdir_def,$(USER_TESTS_DIR))
442
443 # Local Variables:
444 # mode: makefile-gmake
445 # compile-command:"make -qp V=2 > make.db; make V=2"
446 # End: