]> rtime.felk.cvut.cz Git - omk.git/blob - devel-test/Makefile.rules
f2e9ecfa862c4ae1714832b93a8319c5c0bb4773
[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)-subdirs: $(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)
210 endef
211
212 # Call a pass in a subdirectory
213 # Usage: $(call extra_rules_subdir_template,<subdir>)
214 define extra_rules_subdir_template
215 extra-rules-subdirs: extra-rules-$(1)
216 extra-rules-$(1):
217         +@$(MAKE) OMK_SERIALIZE_INCLUDED=n MAKERULES_DIR=$(SOURCES_DIR)/$(1) OUTPUT_DIR=$(OUTPUT_DIR) \
218                 SOURCES_DIR=$(SOURCES_DIR)/$(1) RELATIVE_DIR=$(RELATIVE_PREFIX)$(1) -C $(SOURCES_DIR)/$(1)
219 endef
220
221 .PHONY: extra-rules-subdirs
222 extra-rules-subdirs:
223
224 $(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir))))
225
226 # Usage: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local enable condition>])
227 define omk_pass_template
228 .PHONY: $(pass) $(pass)-local $(pass)-msg $(pass)-subdirs
229 $(pass): $(pass)-local
230 $(pass)-local: $(pass)-subdirs $(pass)-msg
231 $(foreach subdir,$(SUBDIRS),$(call omk_pass_subdir_template,$(pass),$(2),$(subdir)))
232
233 $(pass)-msg: $(pass)-subdirs
234         +@echo "make[omk]: $(pass) in $(RELATIVE_DIR)"
235 endef
236
237 # =======================
238 # DEFAULT CONFIG PASS
239
240 default-config:
241         @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
242         @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
243         @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
244         @echo >> "$(CONFIG_FILE)-default"
245         @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(MAKERULES_DIR) \
246                 RELATIVE_DIR="" SOURCES_DIR=$(OUTPUT_DIR) \
247                 -f $(OUTPUT_DIR)/Makefile default-config-pass
248
249 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
250
251 default-config-pass-local:
252 #       @echo Default config for $(RELATIVE_DIR)
253         @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
254         @$(foreach x, $(default_CONFIG), echo '$(x)' | \
255                 sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
256
257
258 omkize:
259         $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
260            echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
261         fi
262         $(Q)for i in `find -L . -name Makefile.omk` ; do \
263            d=`dirname $${i}`; \
264            if ! test -f "$${d}/Makefile.rules" && ( ! test -f "$${d}/Makefile" || ! cmp --silent Makefile "$${d}/Makefile" ); then \
265               rm -f "$${d}/Makefile"; \
266               cp -v Makefile "$${d}/Makefile"; \
267            fi \
268         done
269                                                              #OMK@build
270 # Convenient variables
271 comma   := ,
272 squote  := '
273 #'
274 empty   :=
275 space   := $(empty) $(empty)
276
277 ###
278 # Escape single quote for use in echo statements
279 escsq = $(subst $(squote),'\$(squote)',$1)
280
281 BUILD_DIR_NAME = _build
282 COMPILED_DIR_NAME = _compiled
283
284 USER_INCLUDE_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/include
285 USER_LIB_DIR     := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/lib
286 USER_UTILS_DIR   := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin-utils
287 USER_TESTS_DIR   := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin-tests
288 USER_BIN_DIR     := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin
289 USER_BUILD_DIR   := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/user
290 LINK_BUILD_DIR   := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/link
291
292 # Avoid double slash at the end in the top-level directory
293 USER_OBJS_DIR = $(USER_BUILD_DIR)$(RELATIVE_DIR:%=/%)
294 KERN_OBJS_DIR = $(KERN_BUILD_DIR)$(RELATIVE_DIR:%=/%)
295 OMK_WORK_DIR  = $(USER_OBJS_DIR)
296
297 strip_out = $(patsubst $(OUTPUT_DIR)/%,%,$(1))
298 repl_out  = $(patsubst $(OUTPUT_DIR)/%,$$(OUTPUT_DIR)/%,$(1))
299
300 .PHONY: FORCE
301
302 ################
303 # Prepare pass #
304 ################
305
306 $(eval $(call omk_pass_template,prepare-pass,$(USER_OBJS_DIR),,always))
307
308 ###
309 # Name of the prepared rule.  This name is used as prefix of variable
310 # names in .omk.inc and is based on the first target
311 omk_inc_rule = $(firstword $(1:$(OUTPUT_DIR)/%=%))
312
313 # The name of .omk.inc file is derived from $(omk_inc_rule) and
314 # depends whether the target is under _build or elsewhere.
315 omk_inc_file = $(if $(filter $(BUILD_DIR_NAME)%,$(omk_inc_rule)),\
316                     $(OMK_WORK_DIR)/$(notdir $(omk_inc_rule)).omk.inc,\
317                     $(OMK_WORK_DIR)/$(subst /,_,$(omk_inc_rule)).omk.inc)
318
319 target_omk_file = $(OMK_WORK_DIR)/$(notdir $(1)).target.omk
320
321 # >'< substitution is for echo to work,
322 # >$< substitution to preserve $ when reloading .omk.inc file
323 subst-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(1))))
324
325 ###
326 # prepare_rule is used to generate .omk.inc files during prepare-pass.
327 # All paths in this file should be relative to $(OUTPUT_DIR)
328 # Usage: $(eval $(call prepare_rule,<targets>,<deps>,<msg>,<cmd>))
329 define prepare_rule
330 prepare-pass-local: $(omk_inc_file)
331
332 $(omk_inc_file): FORCE
333 #TODO remove the next line
334         @echo "  PREP    "$$(call strip_out,$$@); \
335             echo 'prepared_rules += $(omk_inc_rule)' > $$@.tmp; \
336             echo '$(omk_inc_rule)_targets = $$(call strip_out,$(1))' >> $$@.tmp; \
337             echo '$(omk_inc_rule)_deps += $$(call strip_out,$(2))' >> $$@.tmp; \
338             echo '$(omk_inc_rule)_msg = $(3)' >> $$@.tmp; \
339             echo '$(omk_inc_rule)_cmd = $(call subst-cmd,$(4))' >> $$@.tmp; \
340         if cmp -s $$@.tmp $$@; then rm $$@.tmp; else mv $$@.tmp $$@; fi
341 endef
342
343 ###
344 # prepare_rule_goal - same as prepare rules but the target is put
345 # as dependency to the default goal, which causes it to be built
346 # upon calling make
347 define prepare_rule_goal
348 $(call prepare_rule,$(1),$(2),$(3),$(4))
349 prepared_goals += $(call strip_out,$(1))
350 endef
351
352 prepare-pass-local: $(OMK_WORK_DIR)/__goals.omk.inc
353 $(OMK_WORK_DIR)/__goals.omk.inc: FORCE
354         $(Q)echo 'omk_goals += $(prepared_goals)' > $@.tmp; \
355             if cmp -s $@.tmp $@; then rm $@.tmp; else mv $@.tmp $@; fi
356
357
358 ############
359 # Programs #
360 ############
361
362 # Check GCC version for user build
363 ifndef CC_MAJOR_VERSION
364 CC_MAJOR_VERSION := $(shell $(CC) -dumpversion | sed -e 's/\([^.]\)\..*/\1/')
365 endif
366 # Prepare suitable define for dependency building
367 ifeq ($(CC_MAJOR_VERSION),2)
368 CC_DEPFLAGS = -Wp,-MD,"$$@.d.tmp"
369 else
370 CC_DEPFLAGS = -MT $$@ -MD -MP -MF "$$@.d.tmp"
371 endif
372
373 c_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
374         $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -DOMK_FOR_USER
375
376 c_o_cmd = \
377         if $(c_o_COMPILE) $(CC_DEPFLAGS) -o $$@ -c $$< ; \
378         then mv -f "$$@.d.tmp" "$$@.d"; \
379         else rm -f "$$@.d.tmp"; exit 1; \
380         fi
381
382
383 # Usage: $(call program_template,<executable-name>,<bin|utils|test>)
384 define program_template
385 $(1)_OBJS += $$(patsubst %.c,%.o,$$(filter %.c,$$($(1)_SOURCES)))
386 $(1)_OBJS := $$(addprefix $(USER_OBJS_DIR)/,$$(sort $$($(1)_OBJS:%/=%)))
387
388 USER_OBJS  += $$($(1)_OBJS)
389 USER_SOURCES += $$($(1)_SOURCES)
390
391 $(call prepare_rule_goal,\
392         $(USER_BIN_DIR)/$(1)$(EXE_SUFFIX),\
393         $$($(1)_OBJS) $$($(1)_LIBS),\
394         "LINK    ",\
395         $(program_cmd))
396 endef
397
398 program_cmd = $(if $(filter %.cc,$$($(1)_SOURCES)),$(CXX),$(CC)) \
399         $($(1)_OBJS) $($(1)_LIBS:%=-l%) $(LOADLIBES) $(LDFLAGS) -Wl,-rpath-link,$(USER_LIB_DIR) \
400         -Wl,-Map,$(USER_OBJS_DIR)/$(1).exe.map -o $$@
401
402 $(foreach prog,$(bin_PROGRAMS),$(eval $(call program_template,$(prog),bin)))
403 #$(foreach prog,$(bin_PROGRAMS),$(info >>>$(call program_template,$(prog),bin)<<<))
404 $(foreach src,$(filter %.c,$(USER_SOURCES)),\
405         $(eval $(call prepare_rule,\
406                 $(USER_OBJS_DIR)/$(src:%.c=%.o),\
407                 $(SOURCES_DIR)/$(src),\
408                 "CC      ",\
409                 $(c_o_cmd))))
410
411 $(eval $(call prepare_rule_goal,\
412                 $(USER_OBJS_DIR)/test.X.o,\
413                 $(SOURCES_DIR)/test.c,\
414                 "CC      ",\
415                 $(c_o_cmd)))
416
417 # OUTPUT_DIR is not defined in Makefile.build so we has to pass it on
418 # command-line. After this file will be merged with Makefile.rules, it
419 # will not be necessary to pass it.
420 default: check-dir prepare-pass
421         -$(MAKE) -qp -f $(MAKERULES_DIR)/Makefile.rules build > make.db.build-pseudo-pass;
422         +@echo "make[omk]: build"
423         @$(MAKE) $(NO_PRINT_DIRECTORY) -f $(MAKERULES_DIR)/Makefile.rules build
424
425 check-dir:
426         @$(call mkdir_def,$(USER_BUILD_DIR))
427         @$(call mkdir_def,$(USER_INCLUDE_DIR))
428         @$(call mkdir_def,$(USER_LIB_DIR))
429         @$(call mkdir_def,$(USER_BIN_DIR))
430         @$(call mkdir_def,$(USER_UTILS_DIR))
431         @$(call mkdir_def,$(USER_TESTS_DIR))
432
433
434
435 #####################
436 # Build pseudo-pass #
437 #####################
438 ifneq (,$(filter build,$(MAKECMDGOALS)))
439 all: build-pseudo-pass
440
441 rule_targets = $(value $(1)_targets)
442 rule_deps    = $(value $(1)_deps)
443 rule_msg     = $(value $(1)_msg)
444 rule_cmd     = $(value $(1)_cmd)
445
446 # Usage: $(eval $(call build_rule,<target>))
447 define build_rule
448 $(rule_targets): $(rule_deps) #$(omk_inc_file)
449         @$(QUIET_CMD_ECHO) "  "$(rule_msg)"$$@" #"$$(call strip_out,$$@)"
450         $(Q)$(rule_cmd)
451 endef
452
453 # TODO: Include __goals.omk.inc only in the subtree
454 -include $(shell true; find $(USER_BUILD_DIR) -name '*.omk.inc') # `true' is a hack for MinGW
455 # FIXME: Disable automatic remake of included files
456
457 $(foreach rule,$(prepared_rules),$(eval $(call build_rule,$(rule))))
458
459 .PHONY: build
460 build: $(foreach target,$(omk_goals),$(target))
461 endif # (,$(filter build,$(MAKECMDGOALS)))
462
463
464 # Local Variables:
465 # mode: makefile-gmake
466 # compile-command:"make -qp V=2 > make.db; make V=2"
467 # End: