xref: /freebsd/contrib/bmake/boot-strap (revision c59c3bf3)
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.61 2024/03/10 17:51:10 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 newer than $2 ...
333is_newer() {
334	case `'ls' -1td "$@" 2> /dev/null | head -1` in
335	$1) return 0;;
336	esac
337	return 1
338}
339
340# is $1 missing from $2 (or PATH) ?
341no_path() {
342	eval "__p=\$${2:-PATH}"
343	case ":$__p:" in *:"$1":*) return 1;; *) return 0;; esac
344}
345
346# if $1 exists and is not in path, append it
347add_path () {
348	case "$1" in
349	-?) t=$1; shift;;
350	*) t=-d;;
351	esac
352	case "$2,$1" in
353	MAKESYSPATH,.../*) ;;
354	*) [ $t ${1:-.} ] || return;;
355	esac
356	no_path $* && eval ${2:-PATH}="$__p${__p:+:}$1"
357}
358
359
360srcdir=`GetDir /bmake make-bootstrap.sh.in "$srcdir" "$2" "$Mydir" ./bmake* "$Mydir"/../bmake*`
361[ -d "${srcdir:-/dev/null}" ] || Usage
362case "$mksrc" in
363none|-) # we ignore this now
364	mksrc=$Mydir/mk
365	;;
366.../*)	# find here or above
367	mksrc=`FindHereOrAbove -C "$Mydir" -s "$mksrc/sys.mk"`
368	# that found a file
369	mksrc=`dirname $mksrc`
370	;;
371*)	# guess we want mksrc...
372	mksrc=`GetDir /mk sys.mk "$mksrc" "$3" ./mk* "$srcdir"/mk* "$srcdir"/../mk*`
373	[ -d "${mksrc:-/dev/null}" ] || Usage "Use '-m none' to build without mksrc"
374	;;
375esac
376
377# Ok, get to work...
378objdir="${objdir:-$OBJROOT$HOST_TARGET}"
379[ -d "$objdir" ] || mkdir -p "$objdir"
380[ -d "$objdir" ] || mkdir "$objdir"
381cd "$objdir" || exit 1
382# make it absolute
383objdir=`'pwd'`
384
385ShareDir() {
386	case "/$1" in
387	/) [ -d /share ] || return;;
388	*/$HOST_TARGET)
389		if [ -d "$1/../share" ]; then
390			echo `dirname "$1"`/share
391			return
392		fi
393		;;
394	esac
395	echo $1/share
396}
397
398# make it easy to force prefix to use $HOST_TARGET
399: looking at "$prefix"
400case "$prefix" in
401*/host?target) prefix=`echo "$prefix" | sed "s,host.target,${HOST_TARGET},"`;;
402esac
403
404share_dir="${share_dir:-`ShareDir $prefix`}"
405
406AddConfigure --prefix= "$prefix"
407case "$CONFIGURE_ARGS" in
408*--with-*-sys-path*) ;; # skip
409*) [ "$share_dir" ] && AddConfigure --with-default-sys-path= "$share_dir/mk";;
410esac
411if [ "$mksrc" ]; then
412        AddConfigure --with-mksrc= "$mksrc"
413        # not all cc's support this
414        CFLAGS_MF= CFLAGS_MD=
415        export CFLAGS_MF CFLAGS_MD
416fi
417
418# this makes it easy to run the bmake we just built
419# the :tA dance is needed because 'pwd' and even /bin/pwd
420# may not give the same result as realpath().
421Bmake() {
422    (
423	    cd $Mydir &&
424	    MAKESYSPATH=$mksrc SRCTOP=$Mydir OBJTOP=$objdir \
425	    MAKEOBJDIR='${.CURDIR:S,${SRCTOP:tA},${OBJTOP:tA},}' \
426	    ${BMAKE:-$objdir/bmake} -f $Mydir/Makefile "$@"
427    )
428}
429
430op_configure() {
431	$srcdir/configure $CONFIGURE_ARGS || exit 1
432}
433
434op_build() {
435	[ -s make-bootstrap.sh ] || op_configure
436	chmod 755 make-bootstrap.sh || exit 1
437	./make-bootstrap.sh || exit 1
438	case "$op" in
439	build) rm -f tested; op_test;;
440	esac
441}
442
443op_test() {
444	[ -x bmake ] || op_build
445	case "$op" in
446	test) ;;
447	*) is_newer bmake tested || return;;
448	esac
449	Bmake test TEST_MAKE=$objdir/bmake "$@" || exit 1
450	touch tested
451}
452
453op_clean() {
454	if [ -x bmake ]; then
455		ln bmake bmake$$
456		BMAKE=$objdir/bmake$$ Bmake clean
457		rm -f bmake$$
458	elif [ $objdir != $srcdir ]; then
459		rm -rf *
460	fi
461}
462
463op_install() {
464	op_test
465	case "$INSTALL_PREFIX,$INSTALL_BIN,$prefix" in
466	,$HOST_TARGET/bin,*/$HOST_TARGET)
467		INSTALL_PREFIX=`dirname $prefix`
468		;;
469	esac
470	INSTALL_PREFIX=${INSTALL_PREFIX:-$prefix}
471	Bmake install prefix=$INSTALL_PREFIX BINDIR=$INSTALL_PREFIX/${INSTALL_BIN:-bin} ${INSTALL_DESTDIR:+DESTDIR=$INSTALL_DESTDIR} $INSTALL_ARGS || exit 1
472}
473
474op_all() {
475	rm -f make-bootstrap.sh bmake *.o
476	if [ -n "$INSTALL_PREFIX" ]; then
477		op_install
478	else
479		op_test
480		MAKE_VERSION=`sed -n '/^_MAKE_VERSION/ { s,.*=  *,,;p; }' $srcdir/Makefile`
481		cat << EOM
482You can install by running:
483
484$0 $cmd_args op=install
485
486Use --install-prefix=/something to install somewhere other than $prefix
487Use --install-destdir=/somewhere to set DESTDIR during install
488Use --install-host-target to use INSTALL_BIN=$HOST_TARGET/bin
489Use -DWITH_PROG_VERSION to install as bmake-$MAKE_VERSION
490Use -DWITHOUT_PROG_LINK to suppress bmake -> bmake-$MAKE_VERSION symlink
491Use -DWITHOUT_INSTALL_MK to skip installing files to $prefix/share/mk
492EOM
493	fi
494	cat << EOM
495
496Note: bmake.cat1 contains ANSI escape sequences.
497You may need the -r or -R option to more/less to view it correctly.
498
499EOM
500}
501
502op_$op "$@"
503exit 0
504