xref: /freebsd/contrib/bmake/bsd.after-import.mk (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1# $Id: bsd.after-import.mk,v 1.17 2021/10/22 06:31:32 sjg Exp $
2
3# This makefile is for use when integrating bmake into a BSD build
4# system.  Use this makefile after importing bmake.
5# It will bootstrap the new version,
6# capture the generated files we need, and add an after-import
7# target to allow the process to be easily repeated.
8
9# The goal is to allow the benefits of autoconf without
10# the overhead of running configure.
11
12all: _makefile _utmakefile
13all: after-import
14
15# we rely on bmake
16.if !defined(.PARSEDIR)
17.error this makefile requires bmake
18.endif
19
20_this := ${MAKEFILE:tA}
21BMAKE_SRC := ${.PARSEDIR}
22
23# it helps to know where the top of the tree is.
24.if !defined(SRCTOP)
25srctop := ${.MAKE.MAKEFILES:M*src/share/mk/sys.mk:H:H:H}
26.if empty(srctop)
27# likely locations?
28.for d in contrib/bmake external/bsd/bmake/dist
29.if ${BMAKE_SRC:M*/$d} != ""
30srctop := ${BMAKE_SRC:tA:S,/$d,,}
31.endif
32.endfor
33.endif
34.if !empty(srctop)
35SRCTOP := ${srctop}
36.endif
37.endif
38
39# This lets us match what boot-strap does
40.if defined(.MAKE.OS)
41HOST_OS:= ${.MAKE.OS}
42.elif !defined(HOST_OS)
43HOST_OS!= uname
44.endif
45
46BOOTSTRAP_ARGS = \
47	--prefix /usr \
48	--share /usr/share
49
50.if !empty(DEFAULT_SYS_PATH)
51BOOTSTRAP_ARGS += --with-default-sys-path='${DEFAULT_SYS_PATH}'
52.endif
53
54# run boot-strap with minimal influence
55bootstrap:	${BMAKE_SRC}/boot-strap ${MAKEFILE}
56	HOME=/ ${BMAKE_SRC}/boot-strap -o ${HOST_OS} ${BOOTSTRAP_ARGS} ${BOOTSTRAP_XTRAS}
57	touch ${.TARGET}
58
59# Makefiles need a little more tweaking than say config.h
60MAKEFILE_SED = 	sed -e '/^MACHINE/d' \
61	-e '/include.*VERSION/d' \
62	-e '/^CC=/s,=,?=,' \
63	-e '/^PROG/ { s,=,?=,;s,bmake,$${.CURDIR:T},; }' \
64	-e 's,^.-include,.sinclude,' \
65	-e '/^\..*include  *</ { s,<,<bsd.,;/autoconf/d; }' \
66	-e 's,${SRCTOP},$${SRCTOP},g'
67
68# These are the simple files we want to capture
69configured_files= config.h Makefile.config unit-tests/Makefile.config
70
71after-import: bootstrap ${MAKEFILE}
72.for f in ${configured_files:M*.[ch]}
73	@echo Capturing $f
74	@mkdir -p ${${.CURDIR}/$f:L:H}
75	@(echo '/* $$${HOST_OS}$$ */'; cat ${HOST_OS}/$f) > ${.CURDIR}/$f
76.endfor
77.for f in ${configured_files:M*Makefile*}
78	@echo Capturing $f
79	@mkdir -p ${${.CURDIR}/$f:L:H}
80	@(echo '# This is a generated file, do NOT edit!'; \
81	echo '# See ${_this:S,${SRCTOP}/,,}'; \
82	echo '#'; echo '# $$${HOST_OS}$$'; echo; \
83	echo 'SRCTOP?= $${.CURDIR:${${.CURDIR}/$f:L:H:S,${SRCTOP}/,,:C,[^/]+,H,g:S,/,:,g}}'; echo; \
84	${MAKEFILE_SED} ${HOST_OS}/$f ) > ${.CURDIR}/$f
85.endfor
86
87# this needs the most work
88_makefile:	bootstrap ${MAKEFILE}
89	@echo Generating ${.CURDIR}/Makefile
90	@(echo '# This is a generated file, do NOT edit!'; \
91	echo '# See ${_this:S,${SRCTOP}/,,}'; \
92	echo '#'; echo '# $$${HOST_OS}$$'; \
93	echo; echo 'SRCTOP?= $${.CURDIR:${.CURDIR:S,${SRCTOP}/,,:C,[^/]+,H,g:S,/,:,g}}'; \
94	echo; echo '# look here first for config.h'; \
95	echo 'CFLAGS+= -I$${.CURDIR}'; echo; \
96	echo '# for after-import'; \
97	echo 'CLEANDIRS+= ${HOST_OS}'; \
98	echo 'CLEANFILES+= bootstrap'; echo; \
99	${MAKEFILE_SED} \
100	${1 2:L:@n@-e '/start-delete$n/,/end-delete$n/d'@} \
101	${BMAKE_SRC}/Makefile; \
102	echo; echo '# override some simple things'; \
103	echo 'BINDIR= /usr/bin'; \
104	echo 'MANDIR= ${MANDIR:U/usr/share/man}'; \
105	echo; echo '# make sure we get this'; \
106	echo 'CFLAGS+= $${COPTS.$${.IMPSRC:T}}'; \
107	echo; echo 'after-import: ${_this:S,${SRCTOP},\${SRCTOP},}'; \
108	echo '	cd $${.CURDIR} && $${.MAKE} -f ${_this:S,${SRCTOP},\${SRCTOP},}'; \
109	echo ) > ${.TARGET}
110	@cmp -s ${.TARGET} ${.CURDIR}/Makefile || \
111	    mv ${.TARGET} ${.CURDIR}/Makefile
112
113_utmakefile: bootstrap ${MAKEFILE}
114	@echo Generating ${.CURDIR}/unit-tests/Makefile
115	@mkdir -p ${.CURDIR}/unit-tests
116	@(echo '# This is a generated file, do NOT edit!'; \
117	echo '# See ${_this:S,${SRCTOP}/,,}'; \
118	echo '#'; echo '# $$${HOST_OS}$$'; \
119	${MAKEFILE_SED} \
120	-e '/^UNIT_TESTS/s,=.*,= $${srcdir},' \
121	${BMAKE_SRC}/unit-tests/Makefile ) > ${.TARGET}
122	@cmp -s ${.TARGET} ${.CURDIR}/unit-tests/Makefile || \
123	    mv ${.TARGET} ${.CURDIR}/unit-tests/Makefile
124
125
126.include <bsd.obj.mk>
127
128