xref: /freebsd/contrib/bmake/boot-strap (revision 6137b5f7b8c183ee8806d79b3f1d8e5e3ddb3df3)
1:
2# NAME:
3#	boot-strap
4#
5# SYNOPSIS:
6#	boot-strap ["options"]
7#	boot-strap --prefix=/opt --install
8#	boot-strap --prefix=$HOME --install-host-target -DWITH_PROG_VERSION
9#	boot-strap ["options"] op=build
10#	boot-strap ["options"] op=install
11#
12# DESCRIPTION:
13#	This script is used to configure/build bmake it builds for
14#	each host-target in a different subdir to keep the src clean.
15#	There is no requirement for an existing make(1).
16#
17#	On successful completion if no '--install' flag is given,
18#	it echos a command to do installation.
19#
20#	The variable "op" defaults to 'all', and is affected by
21#	'--install' flag as above.
22#	Other values include:
23#
24#	configure
25#		Just run 'configure'
26#
27#	build
28#		If 'configure' has not been done, do it, then
29#		run the build script, and finally 'test'.
30#
31#	install
32#		If 'build' has not been done, do it, 'test' then
33#		install.
34#
35#	clean
36#		attempt to clean up
37#
38#	test
39#		run the unit-tests.  Done automatically after 'build'
40#		and before 'install'.
41#
42#	The above are leveraged by a trivial makefile for the benefit
43#	of those that have './configure; make; make install' baked
44#	into them.
45#
46#	Options:
47#
48#	-c "rc"
49#		Pick up settings from "rc".
50#		We look for '.bmake-boot-strap.rc' before processing
51#		options (unless SKIP_RC is set in environment).
52#
53#	--share "share_dir"
54#		Where to put man pages and mk files.
55#		If $prefix ends in $HOST_TARGET, and $prefix/../share
56#		exits, the default will be that rather than $prefix/share.
57#
58#	--mksrc "mksrc"
59#		Indicate where the mk files can be found.
60#		Default is $Mydir/mk
61#
62#	--install
63#		If build and test work, run bmake install.
64#		BINDIR=$prefix/bin
65#		SHAREDIR=$prefix/share
66#
67#	--install-host-target
68#		As for '--install' but BINDIR=$prefix/$HOST_TARGET/bin
69#		This is useful when $prefix/ is shared by multiple
70#		machines.
71#
72#	Flags relevant when installing:
73#
74#	-DWITHOUT_INSTALL_MK
75#		Skip installing mk files.
76#		By default they will be installed to $prefix/share/mk
77#
78#	-DWITH_PROG_VERSION
79#		Install 'bmake' as 'bmake-$MAKE_VERSION'
80#		A symlink will be made as 'bmake' unless
81#		-DWITHOUT_PROG_LINK is set.
82#
83#	Possibly useful configure_args:
84#
85#	--without-makefile
86#		do not generate 'makefile'.
87#
88#		'makefile' is used to enable the classic
89#		'./configure; make; make install' dance, but on
90#		systems with case insensitive filesystems it can lead
91#		to infinite recursion.
92#
93#		It is disabled by default on Darwin, and Cygwin.
94#
95#	--without-meta
96#		disable use of meta mode.
97#
98#		Even without filemon(9) meta mode is very useful
99#		both for debugging build and improving reliability of
100#		update builds.
101#
102#	--without-filemon
103#		disable use of filemon(9) which is currently only
104#		available for NetBSD and FreeBSD.
105#
106#	--with-filemon=ktrace
107#		on NetBSD or others with fktrace(2), use ktrace
108#		version of filemon.
109#
110#	--with-filemon="path/to/filemon.h"
111#		enables use of filemon(9) by meta mode.
112#
113#	--with-machine="machine"
114#		set "machine" to override that determined by
115#		machine.sh
116#
117#	--with-force-machine="machine"
118#		force "machine" even if uname(3) provides a value.
119#
120#	--with-machine_arch="machine_arch"
121#		set "machine_arch" to override that determined by
122#		machine.sh
123#
124#	--with-force_machine_arch="machine_arch"
125#		force "machine_arch" to override that determined by
126#		machine.sh
127#
128#	--with-default-sys-path="syspath"
129#		set an explicit default "syspath" which is where bmake
130#		will look for sys.mk and friends.
131#
132# AUTHOR:
133#	Simon J. Gerraty <sjg@crufty.net>
134
135# RCSid:
136#	$Id: boot-strap,v 1.59 2023/11/26 18:19:43 sjg Exp $
137#
138#	@(#) Copyright (c) 2001 Simon J. Gerraty
139#
140#	This file is provided in the hope that it will
141#	be of use.  There is absolutely NO WARRANTY.
142#	Permission to copy, redistribute or otherwise
143#	use this file is hereby granted provided that
144#	the above copyright notice and this notice are
145#	left intact.
146#
147#	Please send copies of changes and bug-fixes to:
148#	sjg@crufty.net
149#
150
151Mydir=`dirname $0`
152. "$Mydir/os.sh"
153case "$Mydir" in
154/*) ;;
155*) Mydir=`cd "$Mydir" && 'pwd'`;;
156esac
157
158Usage() {
159	[ "$1" ] && echo "ERROR: $@" >&2
160	echo "Usage:" >&2
161	echo "$0 [--<configure_arg> ...][<prefix>][--install]" >&2
162	exit 1
163}
164
165Error() {
166	echo "ERROR: $@" >&2
167	exit 1
168}
169
170source_rc() {
171	rc="$1"; shift
172	for d in ${*:-""}
173	do
174		r="${d:+$d/}$rc"
175		[ -f "$r" -a -s "$r" ] || continue
176		echo "NOTE: reading $r"
177		. "$r"
178		break
179	done
180}
181
182cmd_args="$@"
183
184# clear some things from the environment that we care about
185unset MAKEOBJDIR MAKEOBJDIRPREFIX
186# or that might be incompatible
187unset MAKE MAKEFLAGS
188
189# --install[-host-target] will set this
190INSTALL_PREFIX=
191# other things we pass to install step
192INSTALL_ARGS=
193CONFIGURE_ARGS=
194MAKESYSPATH=
195# pick a useful default prefix (for me at least ;-)
196for prefix in /opt/$HOST_TARGET "$HOME/$HOST_TARGET" /usr/pkg /usr/local ""
197do
198	[ -d "${prefix:-.}" ] || continue
199	case "$prefix" in
200	*/$HOST_TARGET)
201		p=`dirname $prefix`
202		if [ -d $p/share ]; then
203			INSTALL_BIN=$HOST_TARGET/bin
204			prefix=$p
205		fi
206		;;
207	esac
208        echo "NOTE: default prefix=$prefix ${INSTALL_BIN:+INSTALL_BIN=$INSTALL_BIN}"
209	break
210done
211srcdir=$Mydir
212mksrc=$Mydir/mk
213objdir=
214quiet=:
215
216${SKIP_RC:+:} source_rc .bmake-boot-strap.rc . "$Mydir/.." "$HOME"
217
218get_optarg() {
219	expr "x$1" : "x[^=]*=\\(.*\\)"
220}
221
222here=`'pwd'`
223if [ $here = $Mydir ]; then
224   # avoid pollution
225   OBJROOT=../
226fi
227
228op=all
229BMAKE=
230
231while :
232do
233	case "$1" in
234	--) shift; break;;
235        --help) sed -n -e "1d;/RCSid/,\$d" -e '/^#\.[a-z]/d' -e '/^#/s,^# *,,p' $0; exit 0;;
236	--prefix) prefix="$2"; shift;;
237	--prefix=*) prefix=`get_optarg "$1"`;;
238	--src=*) srcdir=`get_optarg "$1"`;;
239	--with-mksrc=*|--mksrc=*) mksrc=`get_optarg "$1"`;;
240	--share=*) share_dir=`get_optarg "$1"`;;
241	--share) share_dir="$2"; shift;;
242	--with-default-sys-path=*)
243	    CONFIGURE_ARGS="$1";;
244	--with-default-sys-path)
245	    CONFIGURE_ARGS="$1 $2";;
246	--install) INSTALL_PREFIX=${INSTALL_PREFIX:-$prefix};;
247	--install-host-target)
248                INSTALL_PREFIX=${INSTALL_PREFIX:-$prefix}
249                INSTALL_BIN=$HOST_TARGET/bin;;
250	--install-destdir=*) INSTALL_DESTDIR=`get_optarg "$1"`;;
251	--install-prefix=*) INSTALL_PREFIX=`get_optarg "$1"`;;
252	-DWITH*) INSTALL_ARGS="$INSTALL_ARGS $1";;
253	-s|--src) srcdir="$2"; shift;;
254	-m|--mksrc) mksrc="$2"; shift;;
255	-o|--objdir) objdir="$2"; shift;;
256	-q) quiet=;;
257	-c) source_rc "$2"; shift;;
258	--*) CONFIGURE_ARGS="$CONFIGURE_ARGS $1";;
259	*=*) eval "$1"; export `expr "x$1" : "x\\(.[^=]*\\)=.*"`;;
260	*) break;;
261	esac
262        shift
263done
264
265AddConfigure() {
266	case " $CONFIGURE_ARGS " in
267	*" $1"*) ;;
268	*) CONFIGURE_ARGS="$CONFIGURE_ARGS $1$2";;
269	esac
270}
271
272GetDir() {
273	match="$1"
274	shift
275	fmatch="$1"
276	shift
277	for dir in $*
278	do
279		[ -d "$dir" ] || continue
280		case "/$dir/" in
281		*$match*) ;;
282		*) continue;;
283		esac
284		case "$fmatch" in
285		.) ;;
286		*) [ -s $dir/$fmatch ] || continue;;
287		esac
288		case "$dir/" in
289		*./*) cd "$dir" && 'pwd';;
290		/*) echo $dir;;
291		*) cd "$dir" && 'pwd';;
292		esac
293		break
294	done
295}
296
297FindHereOrAbove() {
298    (
299	_t=-s
300	while :
301	do
302		case "$1" in
303		-C) cd "$2"; shift; shift;;
304		-?) _t=$1; shift;;
305		*) break;;
306		esac
307	done
308	case "$1" in
309	/*)	# we shouldn't be here
310		[ $_t "$1" ] && echo "$1"
311		return
312		;;
313	.../*) want=`echo "$1" | sed 's,^.../*,,'`;;
314	*) want="$1";;
315	esac
316	here=`'pwd'`
317	while :
318	do
319		if [ $_t "./$want" ]; then
320			echo "$here/$want"
321			return
322		fi
323		cd ..
324		here=`'pwd'`
325		case "$here" in
326		/) return;;
327		esac
328	done
329    )
330}
331
332# is $1 missing from $2 (or PATH) ?
333no_path() {
334	eval "__p=\$${2:-PATH}"
335	case ":$__p:" in *:"$1":*) return 1;; *) return 0;; esac
336}
337
338# if $1 exists and is not in path, append it
339add_path () {
340	case "$1" in
341	-?) t=$1; shift;;
342	*) t=-d;;
343	esac
344	case "$2,$1" in
345	MAKESYSPATH,.../*) ;;
346	*) [ $t ${1:-.} ] || return;;
347	esac
348	no_path $* && eval ${2:-PATH}="$__p${__p:+:}$1"
349}
350
351
352srcdir=`GetDir /bmake make-bootstrap.sh.in "$srcdir" "$2" "$Mydir" ./bmake* "$Mydir"/../bmake*`
353[ -d "${srcdir:-/dev/null}" ] || Usage
354case "$mksrc" in
355none|-) # we ignore this now
356	mksrc=$Mydir/mk
357	;;
358.../*)	# find here or above
359	mksrc=`FindHereOrAbove -C "$Mydir" -s "$mksrc/sys.mk"`
360	# that found a file
361	mksrc=`dirname $mksrc`
362	;;
363*)	# guess we want mksrc...
364	mksrc=`GetDir /mk sys.mk "$mksrc" "$3" ./mk* "$srcdir"/mk* "$srcdir"/../mk*`
365	[ -d "${mksrc:-/dev/null}" ] || Usage "Use '-m none' to build without mksrc"
366	;;
367esac
368
369# Ok, get to work...
370objdir="${objdir:-$OBJROOT$HOST_TARGET}"
371[ -d "$objdir" ] || mkdir -p "$objdir"
372[ -d "$objdir" ] || mkdir "$objdir"
373cd "$objdir" || exit 1
374# make it absolute
375objdir=`'pwd'`
376
377ShareDir() {
378	case "/$1" in
379	/) [ -d /share ] || return;;
380	*/$HOST_TARGET)
381		if [ -d "$1/../share" ]; then
382			echo `dirname "$1"`/share
383			return
384		fi
385		;;
386	esac
387	echo $1/share
388}
389
390# make it easy to force prefix to use $HOST_TARGET
391: looking at "$prefix"
392case "$prefix" in
393*/host?target) prefix=`echo "$prefix" | sed "s,host.target,${HOST_TARGET},"`;;
394esac
395
396share_dir="${share_dir:-`ShareDir $prefix`}"
397
398AddConfigure --prefix= "$prefix"
399case "$CONFIGURE_ARGS" in
400*--with-*-sys-path*) ;; # skip
401*) [ "$share_dir" ] && AddConfigure --with-default-sys-path= "$share_dir/mk";;
402esac
403if [ "$mksrc" ]; then
404        AddConfigure --with-mksrc= "$mksrc"
405        # not all cc's support this
406        CFLAGS_MF= CFLAGS_MD=
407        export CFLAGS_MF CFLAGS_MD
408fi
409
410# this makes it easy to run the bmake we just built
411# the :tA dance is needed because 'pwd' and even /bin/pwd
412# may not give the same result as realpath().
413Bmake() {
414    (
415	    cd $Mydir &&
416	    MAKESYSPATH=$mksrc SRCTOP=$Mydir OBJTOP=$objdir \
417	    MAKEOBJDIR='${.CURDIR:S,${SRCTOP:tA},${OBJTOP:tA},}' \
418	    ${BMAKE:-$objdir/bmake} -f $Mydir/Makefile "$@"
419    )
420}
421
422op_configure() {
423	$srcdir/configure $CONFIGURE_ARGS || exit 1
424}
425
426op_build() {
427	[ -s make-bootstrap.sh ] || op_configure
428	chmod 755 make-bootstrap.sh || exit 1
429	./make-bootstrap.sh || exit 1
430	case "$op" in
431	build) op_test;;
432	esac
433}
434
435op_test() {
436	[ -x bmake ] || op_build
437	Bmake test TEST_MAKE=$objdir/bmake "$@" || exit 1
438}
439
440op_clean() {
441	if [ -x bmake ]; then
442		ln bmake bmake$$
443		BMAKE=$objdir/bmake$$ Bmake clean
444		rm -f bmake$$
445	elif [ $objdir != $srcdir ]; then
446		rm -rf *
447	fi
448}
449
450op_install() {
451	op_test
452	case "$INSTALL_PREFIX,$INSTALL_BIN,$prefix" in
453	,$HOST_TARGET/bin,*/$HOST_TARGET)
454		INSTALL_PREFIX=`dirname $prefix`
455		;;
456	esac
457	INSTALL_PREFIX=${INSTALL_PREFIX:-$prefix}
458	Bmake install prefix=$INSTALL_PREFIX BINDIR=$INSTALL_PREFIX/${INSTALL_BIN:-bin} ${INSTALL_DESTDIR:+DESTDIR=$INSTALL_DESTDIR} $INSTALL_ARGS || exit 1
459}
460
461op_all() {
462	rm -f make-bootstrap.sh bmake *.o
463	if [ -n "$INSTALL_PREFIX" ]; then
464		op_install
465	else
466		op_test
467		MAKE_VERSION=`sed -n '/^_MAKE_VERSION/ { s,.*=  *,,;p; }' $srcdir/Makefile`
468		cat << EOM
469You can install by running:
470
471$0 $cmd_args op=install
472
473Use --install-prefix=/something to install somewhere other than $prefix
474Use --install-destdir=/somewhere to set DESTDIR during install
475Use --install-host-target to use INSTALL_BIN=$HOST_TARGET/bin
476Use -DWITH_PROG_VERSION to install as bmake-$MAKE_VERSION
477Use -DWITHOUT_PROG_LINK to suppress bmake -> bmake-$MAKE_VERSION symlink
478Use -DWITHOUT_INSTALL_MK to skip installing files to $prefix/share/mk
479EOM
480	fi
481	cat << EOM
482
483Note: bmake.cat1 contains ANSI escape sequences.
484You may need the -r or -R option to more/less to view it correctly.
485
486EOM
487}
488
489op_$op "$@"
490exit 0
491