xref: /freebsd/contrib/bmake/bsd.after-import.mk (revision d9a65c5de1c9f30ae71ce0be8fb88be9d20d216d)
1# $Id: bsd.after-import.mk,v 1.22 2025/03/09 02:56:42 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
12.MAIN: all
13all: _makefile _utmakefile
14all: after-import
15
16# we rely on bmake
17.if !defined(.PARSEDIR)
18.error this makefile requires bmake
19.endif
20
21_this := ${MAKEFILE:tA}
22BMAKE_SRC := ${.PARSEDIR}
23
24# it helps to know where the top of the tree is.
25.if !defined(SRCTOP)
26srctop := ${.MAKE.MAKEFILES:M*src/share/mk/sys.mk:H:H:H}
27.if empty(srctop)
28# likely locations?
29.for d in contrib/bmake external/bsd/bmake/dist
30.if ${BMAKE_SRC:M*/$d} != ""
31srctop := ${BMAKE_SRC:tA:S,/$d,,}
32.endif
33.endfor
34.endif
35.if !empty(srctop)
36SRCTOP := ${srctop}
37.endif
38.endif
39
40# This lets us match what boot-strap does
41.if defined(.MAKE.OS)
42HOST_OS:= ${.MAKE.OS}
43.elif !defined(HOST_OS)
44HOST_OS!= uname
45.endif
46
47BOOTSTRAP_ARGS = \
48	--prefix /usr \
49	--share /usr/share
50
51.if !empty(DEFAULT_SYS_PATH)
52BOOTSTRAP_ARGS += --with-default-sys-path='${DEFAULT_SYS_PATH}'
53.endif
54
55# run boot-strap with minimal influence
56bootstrap:	${BMAKE_SRC}/boot-strap ${MAKEFILE}
57	HOME=/ ${BMAKE_SRC}/boot-strap -o ${HOST_OS} ${BOOTSTRAP_ARGS} ${BOOTSTRAP_XTRAS}
58	touch ${.TARGET}
59
60# Makefiles need a little more tweaking than say config.h
61MAKEFILE_SED = 	sed -e '/^MACHINE/d' \
62	-e '/include.*VERSION/d' \
63	-e '/^CC=/s,=,?=,' \
64	-e '/^PROG/ { s,=,?=,;s,bmake,$${.CURDIR:T},; }' \
65	-e '/^\..*include  *</ { s,<\([a-z]\),<bsd.\1,;/autoconf/d; }' \
66	-e 's,${SRCTOP},$${SRCTOP},g'
67
68.ifdef WITH_SPACE_ASSIGN
69.space := ${x x:L:S/x//Wg}
70.else
71.space =
72# this line noise achieves the equivalent of negating
73# /^(.if|.elif|#)|==/
74#
75MAKEFILE_SED += \
76	-e '/^\.[e ]*l*if/!{/^\#/!{/==/!s,^\([^[:space:]][^=]*\)[[:space:]][[:space:]]*\([!?:+]*=\),\1\2,;};}' \
77
78.endif
79
80# These are the simple files we want to capture
81configured_files= config.h Makefile.config unit-tests/Makefile.config
82
83# FreeBSD has dropped their tag with svn
84.if ${HOST_OS:NFreeBSD} == ""
85ECHO_TAG= :
86.else
87ECHO_TAG?= echo
88.endif
89
90after-import: bootstrap ${MAKEFILE}
91.for f in ${configured_files:M*.[ch]}
92	@echo Capturing $f
93	@mkdir -p ${${.CURDIR}/$f:L:H}
94	@(${ECHO_TAG} '/* $$${HOST_OS}$$ */'; cat ${HOST_OS}/$f) > ${.CURDIR}/$f
95.endfor
96.for f in ${configured_files:M*Makefile*}
97	@echo Capturing $f
98	@mkdir -p ${${.CURDIR}/$f:L:H}
99	@(echo '# This is a generated file, do NOT edit!'; \
100	echo '# See ${_this:S,${SRCTOP}/,,}'; \
101	echo '#'; ${ECHO_TAG} '# $$${HOST_OS}$$'; echo; \
102	echo 'SRCTOP${.space}?= $${.CURDIR:${${.CURDIR}/$f:L:H:S,${SRCTOP}/,,:C,[^/]+,H,g:S,/,:,g}}'; echo; \
103	${MAKEFILE_SED} ${HOST_OS}/$f ) > ${.CURDIR}/$f
104.endfor
105
106.if make(all)
107_makefile _utmakefile: bootstrap ${MAKEFILE}
108.endif
109
110# this needs the most work
111_makefile:
112	@echo Generating ${.CURDIR}/Makefile
113	@(echo '# This is a generated file, do NOT edit!'; \
114	echo '# See ${_this:S,${SRCTOP}/,,}'; \
115	echo '#'; ${ECHO_TAG} '# $$${HOST_OS}$$'; \
116	echo; echo 'SRCTOP${.space}?= $${.CURDIR:${.CURDIR:S,${SRCTOP}/,,:C,[^/]+,H,g:S,/,:,g}}'; \
117	echo; echo '# look here first for config.h'; \
118	echo 'CFLAGS${.space}+= -I$${.CURDIR}'; echo; \
119	echo '# for after-import'; \
120	echo 'CLEANDIRS${.space}+= ${HOST_OS}'; \
121	echo 'CLEANFILES${.space}+= bootstrap'; echo; \
122	${MAKEFILE_SED} \
123	${1 2:L:@n@-e '/start-delete$n/,/end-delete$n/d'@} \
124	${BMAKE_SRC}/Makefile; \
125	echo; echo '# override some simple things'; \
126	echo 'BINDIR${.space}= /usr/bin'; \
127	echo 'MANDIR${.space}= ${MANDIR:U/usr/share/man}'; \
128	echo; echo '# make sure we get this'; \
129	echo 'CFLAGS${.space}+= $${COPTS.$${.IMPSRC:T}}'; \
130	echo; echo 'after-import: ${_this:S,${SRCTOP},\${SRCTOP},}'; \
131	echo '	cd $${.CURDIR} && $${.MAKE} -f ${_this:S,${SRCTOP},\${SRCTOP},}'; \
132	echo ) > ${.TARGET}
133	@cmp -s ${.TARGET} ${.CURDIR}/Makefile || \
134	    mv ${.TARGET} ${.CURDIR}/Makefile
135
136_utmakefile:
137	@echo Generating ${.CURDIR}/unit-tests/Makefile
138	@mkdir -p ${.CURDIR}/unit-tests
139	@(echo '# This is a generated file, do NOT edit!'; \
140	echo '# See ${_this:S,${SRCTOP}/,,}'; \
141	echo '#'; ${ECHO_TAG} '# $$${HOST_OS}$$'; \
142	${MAKEFILE_SED} \
143	-e '/^UNIT_TESTS/s,=.*,= $${srcdir},' \
144	${BMAKE_SRC}/unit-tests/Makefile ) > ${.TARGET}
145	@cmp -s ${.TARGET} ${.CURDIR}/unit-tests/Makefile || \
146	    mv ${.TARGET} ${.CURDIR}/unit-tests/Makefile
147
148
149.include <bsd.obj.mk>
150
151