summaryrefslogtreecommitdiff
path: root/build-0.3/generator
blob: 6ab29f2e2f53e2cf9f67b56785647c3875ed6e92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# file      : build/generator
# copyright : Copyright (c) 2004-2012 Code Synthesis Tools CC
# license   : GNU GPL v2; see accompanying LICENSE file

# This makefile is an optional, transparent generator of "thunk" makefiles
# for the build setups with separate src and out directories. Without the
# generator, the make invocation for this setup has the following general
# form:
#
# make -C out_root/subdir -f src_root/subdir/makefile
#
# With the generator enabled, the same can be achieved with the following
# shorter command:
#
# make -C out_root/subdir
#
# Or, if the subdir directory may not yet exist, with this variant:
#
# make -C out_root dir=subdir
#
# To enable the generator, you will need to instruct make to pre-load it
# for every invocation by adding it to the MAKEFILES environment variable:
#
# export MAKEFILES=build-X.Y/generator
#
# The thunk makefiles for individual sub-directories are generated as
# needed and are automatically removed by the disfigure target.
#

ifdef dir

.PHONY: _all
_all:
	@mkdir -p $(dir)
	@$(MAKE) -C $(dir) dir= $(MAKECMDGOALS)

ifneq ($(MAKECMDGOALS),)
.PHONY: $(MAKECMDGOALS)
$(MAKECMDGOALS): _all
else
.DEFAULT_GOAL := _all
endif

else
ifeq ($(wildcard makefile Makefile GNUmakefile),)

define literal_newline


endef

makefile: empty   :=
makefile: space   := $(empty) #
makefile: tab     := $(empty)	$(empty)
makefile: newline := $(literal_newline)


# Find the src_root directory.
#
# $1 - current directory
#
makefile: find = \
$(if $1,$(if $(wildcard $1/build/bootstrap-dynamic.make),$1,$(call \
find,$(patsubst %/,%,$(dir $1)))))

# Convert /foo/bar to ../../.
#
# $1 - relative path from $(CURDIR)
#
makefile: path = $(subst $(space),,$(foreach d,$(subst /, ,$1/),../))

# $1 - relative path from $(CURDIR)
#
makefile: command_body = \
@echo '\# Automatically generated by build.' >$@$(newline)\
$(tab)@echo 'ifndef dir' >>$@$(newline)\
$(tab)@echo 'override dir :=' >>$@$(newline)\
$(tab)@echo 'include $(if $1,$(call path,$1))build/bootstrap-dynamic.make' >>$@$(newline)\
$(tab)@echo 'include $$(src_root)$1/makefile' >>$@$(newline)\
$(tab)@echo 'endif' >>$@

# $1 - output root directory or empty if none were found
#
makefile: command = $(if $1,$(call command_body,$(subst $1,,$(CURDIR))))

makefile:
	$(call command,$(call find,$(CURDIR)))

endif # makefile
endif # dir