xref: /freebsd/contrib/bmake/bsd.after-import.mk (revision 17b7a0c595a51eaa7e83f16e99e1555bd13a445b)
1# $Id: bsd.after-import.mk,v 1.19 2024/09/21 22:44:55 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 '/^\..*include  *</ { s,<\([a-z]\),<bsd.\1,;/autoconf/d; }' \
65	-e 's,${SRCTOP},$${SRCTOP},g'
66
67# These are the simple files we want to capture
68configured_files= config.h Makefile.config unit-tests/Makefile.config
69
70# FreeBSD has dropped their tag with svn
71.if ${HOST_OS:NFreeBSD} == ""
72ECHO_TAG= :
73.else
74ECHO_TAG?= echo
75.endif
76
77after-import: bootstrap ${MAKEFILE}
78.for f in ${configured_files:M*.[ch]}
79	@echo Capturing $f
80	@mkdir -p ${${.CURDIR}/$f:L:H}
81	@(${ECHO_TAG} '/* $$${HOST_OS}$$ */'; cat ${HOST_OS}/$f) > ${.CURDIR}/$f
82.endfor
83.for f in ${configured_files:M*Makefile*}
84	@echo Capturing $f
85	@mkdir -p ${${.CURDIR}/$f:L:H}
86	@(echo '# This is a generated file, do NOT edit!'; \
87	echo '# See ${_this:S,${SRCTOP}/,,}'; \
88	echo '#'; ${ECHO_TAG} '# $$${HOST_OS}$$'; echo; \
89	echo 'SRCTOP?= $${.CURDIR:${${.CURDIR}/$f:L:H:S,${SRCTOP}/,,:C,[^/]+,H,g:S,/,:,g}}'; echo; \
90	${MAKEFILE_SED} ${HOST_OS}/$f ) > ${.CURDIR}/$f
91.endfor
92
93# this needs the most work
94_makefile:	bootstrap ${MAKEFILE}
95	@echo Generating ${.CURDIR}/Makefile
96	@(echo '# This is a generated file, do NOT edit!'; \
97	echo '# See ${_this:S,${SRCTOP}/,,}'; \
98	echo '#'; ${ECHO_TAG} '# $$${HOST_OS}$$'; \
99	echo; echo 'SRCTOP?= $${.CURDIR:${.CURDIR:S,${SRCTOP}/,,:C,[^/]+,H,g:S,/,:,g}}'; \
100	echo; echo '# look here first for config.h'; \
101	echo 'CFLAGS+= -I$${.CURDIR}'; echo; \
102	echo '# for after-import'; \
103	echo 'CLEANDIRS+= ${HOST_OS}'; \
104	echo 'CLEANFILES+= bootstrap'; echo; \
105	${MAKEFILE_SED} \
106	${1 2:L:@n@-e '/start-delete$n/,/end-delete$n/d'@} \
107	${BMAKE_SRC}/Makefile; \
108	echo; echo '# override some simple things'; \
109	echo 'BINDIR= /usr/bin'; \
110	echo 'MANDIR= ${MANDIR:U/usr/share/man}'; \
111	echo; echo '# make sure we get this'; \
112	echo 'CFLAGS+= $${COPTS.$${.IMPSRC:T}}'; \
113	echo; echo 'after-import: ${_this:S,${SRCTOP},\${SRCTOP},}'; \
114	echo '	cd $${.CURDIR} && $${.MAKE} -f ${_this:S,${SRCTOP},\${SRCTOP},}'; \
115	echo ) > ${.TARGET}
116	@cmp -s ${.TARGET} ${.CURDIR}/Makefile || \
117	    mv ${.TARGET} ${.CURDIR}/Makefile
118
119_utmakefile: bootstrap ${MAKEFILE}
120	@echo Generating ${.CURDIR}/unit-tests/Makefile
121	@mkdir -p ${.CURDIR}/unit-tests
122	@(echo '# This is a generated file, do NOT edit!'; \
123	echo '# See ${_this:S,${SRCTOP}/,,}'; \
124	echo '#'; ${ECHO_TAG} '# $$${HOST_OS}$$'; \
125	${MAKEFILE_SED} \
126	-e '/^UNIT_TESTS/s,=.*,= $${srcdir},' \
127	${BMAKE_SRC}/unit-tests/Makefile ) > ${.TARGET}
128	@cmp -s ${.TARGET} ${.CURDIR}/unit-tests/Makefile || \
129	    mv ${.TARGET} ${.CURDIR}/unit-tests/Makefile
130
131
132.include <bsd.obj.mk>
133
134