1# file      : build/generator
2# copyright : Copyright (c) 2004-2012 Code Synthesis Tools CC
3# license   : GNU GPL v2; see accompanying LICENSE file
4
5# This makefile is an optional, transparent generator of "thunk" makefiles
6# for the build setups with separate src and out directories. Without the
7# generator, the make invocation for this setup has the following general
8# form:
9#
10# make -C out_root/subdir -f src_root/subdir/makefile
11#
12# With the generator enabled, the same can be achieved with the following
13# shorter command:
14#
15# make -C out_root/subdir
16#
17# Or, if the subdir directory may not yet exist, with this variant:
18#
19# make -C out_root dir=subdir
20#
21# To enable the generator, you will need to instruct make to pre-load it
22# for every invocation by adding it to the MAKEFILES environment variable:
23#
24# export MAKEFILES=build-X.Y/generator
25#
26# The thunk makefiles for individual sub-directories are generated as
27# needed and are automatically removed by the disfigure target.
28#
29
30ifdef dir
31
32.PHONY: _all
33_all:
34	@mkdir -p $(dir)
35	@$(MAKE) -C $(dir) dir= $(MAKECMDGOALS)
36
37ifneq ($(MAKECMDGOALS),)
38.PHONY: $(MAKECMDGOALS)
39$(MAKECMDGOALS): _all
40else
41.DEFAULT_GOAL := _all
42endif
43
44else
45ifeq ($(wildcard makefile Makefile GNUmakefile),)
46
47define literal_newline
48
49
50endef
51
52makefile: empty   :=
53makefile: space   := $(empty) #
54makefile: tab     := $(empty)	$(empty)
55makefile: newline := $(literal_newline)
56
57
58# Find the src_root directory.
59#
60# $1 - current directory
61#
62makefile: find = \
63$(if $1,$(if $(wildcard $1/build/bootstrap-dynamic.make),$1,$(call \
64find,$(patsubst %/,%,$(dir $1)))))
65
66# Convert /foo/bar to ../../.
67#
68# $1 - relative path from $(CURDIR)
69#
70makefile: path = $(subst $(space),,$(foreach d,$(subst /, ,$1/),../))
71
72# $1 - relative path from $(CURDIR)
73#
74makefile: command_body = \
75@echo '\# Automatically generated by build.' >$@$(newline)\
76$(tab)@echo 'ifndef dir' >>$@$(newline)\
77$(tab)@echo 'override dir :=' >>$@$(newline)\
78$(tab)@echo 'include $(if $1,$(call path,$1))build/bootstrap-dynamic.make' >>$@$(newline)\
79$(tab)@echo 'include $$(src_root)$1/makefile' >>$@$(newline)\
80$(tab)@echo 'endif' >>$@
81
82# $1 - output root directory or empty if none were found
83#
84makefile: command = $(if $1,$(call command_body,$(subst $1,,$(CURDIR))))
85
86makefile:
87	$(call command,$(call find,$(CURDIR)))
88
89endif # makefile
90endif # dir
91