xref: /freebsd/contrib/bmake/boot-strap (revision b9f654b163bce26de79705e77b872427c9f2afa1)
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-meta
86#		disable use of meta mode.
87#
88#	--without-filemon
89#		disable use of filemon(9) which is currently only
90#		available for NetBSD and FreeBSD.
91#
92#	--with-filemon="path/to/filemon.h"
93#		enables use of filemon(9) by meta mode.
94#
95#	--with-machine="machine"
96#		set "machine" to override that determined by
97#		machine.sh
98#
99#	--with-force-machine="machine"
100#		force "machine" even if uname(3) provides a value.
101#
102#	--with-machine_arch="machine_arch"
103#		set "machine_arch" to override that determined by
104#		machine.sh
105#
106#	--with-default-sys-path="syspath"
107#		set an explicit default "syspath" which is where bmake
108#		will look for sys.mk and friends.
109#
110# AUTHOR:
111#	Simon J. Gerraty <sjg@crufty.net>
112
113# RCSid:
114#	$Id: boot-strap,v 1.49 2016/05/29 00:09:14 sjg Exp $
115#
116#	@(#) Copyright (c) 2001 Simon J. Gerraty
117#
118#	This file is provided in the hope that it will
119#	be of use.  There is absolutely NO WARRANTY.
120#	Permission to copy, redistribute or otherwise
121#	use this file is hereby granted provided that
122#	the above copyright notice and this notice are
123#	left intact.
124#
125#	Please send copies of changes and bug-fixes to:
126#	sjg@crufty.net
127#
128
129Mydir=`dirname $0`
130. "$Mydir/os.sh"
131case "$Mydir" in
132/*) ;;
133*) Mydir=`cd "$Mydir" && 'pwd'`;;
134esac
135
136Usage() {
137	[ "$1" ] && echo "ERROR: $@" >&2
138	echo "Usage:" >&2
139	echo "$0 [--<configure_arg> ...][<prefix>][--install]" >&2
140	exit 1
141}
142
143Error() {
144	echo "ERROR: $@" >&2
145	exit 1
146}
147
148source_rc() {
149	rc="$1"; shift
150	for d in ${*:-""}
151	do
152		r="${d:+$d/}$rc"
153		[ -f "$r" -a -s "$r" ] || continue
154		echo "NOTE: reading $r"
155		. "$r"
156		break
157	done
158}
159
160cmd_args="$@"
161
162# clear some things from the environment that we care about
163unset MAKEOBJDIR MAKEOBJDIRPREFIX
164
165# --install[-host-target] will set this
166INSTALL_PREFIX=
167# other things we pass to install step
168INSTALL_ARGS=
169CONFIGURE_ARGS=
170MAKESYSPATH=
171# pick a useful default prefix (for me at least ;-)
172for prefix in /opt/$HOST_TARGET "$HOME/$HOST_TARGET" /usr/pkg /usr/local ""
173do
174	[ -d "${prefix:-.}" ] || continue
175	case "$prefix" in
176	*/$HOST_TARGET)
177		p=`dirname $prefix`
178		if [ -d $p/share ]; then
179			INSTALL_BIN=$HOST_TARGET/bin
180			prefix=$p
181		fi
182		;;
183	esac
184        echo "NOTE: default prefix=$prefix ${INSTALL_BIN:+INSTALL_BIN=$INSTALL_BIN}"
185	break
186done
187srcdir=$Mydir
188mksrc=$Mydir/mk
189objdir=
190quiet=:
191
192${SKIP_RC:+:} source_rc .bmake-boot-strap.rc . "$Mydir/.." "$HOME"
193
194get_optarg() {
195	expr "x$1" : "x[^=]*=\\(.*\\)"
196}
197
198here=`'pwd'`
199if [ $here = $Mydir ]; then
200   # avoid pollution
201   OBJROOT=../
202fi
203
204op=all
205BMAKE=
206
207while :
208do
209	case "$1" in
210	--) shift; break;;
211        --help) sed -n -e "1d;/RCSid/,\$d" -e '/^#\.[a-z]/d' -e '/^#/s,^# *,,p' $0; exit 0;;
212	--prefix) prefix="$2"; shift;;
213	--prefix=*) prefix=`get_optarg "$1"`;;
214	--src=*) srcdir=`get_optarg "$1"`;;
215	--with-mksrc=*|--mksrc=*) mksrc=`get_optarg "$1"`;;
216	--share=*) share_dir=`get_optarg "$1"`;;
217	--share) share_dir="$2"; shift;;
218	--with-default-sys-path=*)
219	    CONFIGURE_ARGS="$1";;
220	--with-default-sys-path)
221	    CONFIGURE_ARGS="$1 $2";;
222	--install) INSTALL_PREFIX=${INSTALL_PREFIX:-$prefix};;
223	--install-host-target)
224                INSTALL_PREFIX=${INSTALL_PREFIX:-$prefix}
225                INSTALL_BIN=$HOST_TARGET/bin;;
226	--install-destdir=*) INSTALL_DESTDIR=`get_optarg "$1"`;;
227	--install-prefix=*) INSTALL_PREFIX=`get_optarg "$1"`;;
228	-DWITH*) INSTALL_ARGS="$INSTALL_ARGS $1";;
229	-s|--src) srcdir="$2"; shift;;
230	-m|--mksrc) mksrc="$2"; shift;;
231	-o|--objdir) objdir="$2"; shift;;
232	-q) quiet=;;
233	-c) source_rc "$2"; shift;;
234	--*) CONFIGURE_ARGS="$CONFIGURE_ARGS $1";;
235	*=*) eval "$1"; export `expr "x$1" : "x\\(.[^=]*\\)=.*"`;;
236	*) break;;
237	esac
238        shift
239done
240
241AddConfigure() {
242	case " $CONFIGURE_ARGS " in
243	*" $1"*) ;;
244	*) CONFIGURE_ARGS="$CONFIGURE_ARGS $1$2";;
245	esac
246}
247
248GetDir() {
249	match="$1"
250	shift
251	fmatch="$1"
252	shift
253	for dir in $*
254	do
255		[ -d "$dir" ] || continue
256		case "/$dir/" in
257		*$match*) ;;
258		*) continue;;
259		esac
260		case "$fmatch" in
261		.) ;;
262		*) [ -s $dir/$fmatch ] || continue;;
263		esac
264		case "$dir/" in
265		*./*) cd "$dir" && 'pwd';;
266		/*) echo $dir;;
267		*) cd "$dir" && 'pwd';;
268		esac
269		break
270	done
271}
272
273FindHereOrAbove() {
274    (
275	_t=-s
276	while :
277	do
278		case "$1" in
279		-C) cd "$2"; shift; shift;;
280		-?) _t=$1; shift;;
281		*) break;;
282		esac
283	done
284	case "$1" in
285	/*)	# we shouldn't be here
286		[ $_t "$1" ] && echo "$1"
287		return
288		;;
289	.../*) want=`echo "$1" | sed 's,^.../*,,'`;;
290	*) want="$1";;
291	esac
292	here=`'pwd'`
293	while :
294	do
295		if [ $_t "./$want" ]; then
296			echo "$here/$want"
297			return
298		fi
299		cd ..
300		here=`'pwd'`
301		case "$here" in
302		/) return;;
303		esac
304	done
305    )
306}
307
308# is $1 missing from $2 (or PATH) ?
309no_path() {
310	eval "__p=\$${2:-PATH}"
311	case ":$__p:" in *:"$1":*) return 1;; *) return 0;; esac
312}
313
314# if $1 exists and is not in path, append it
315add_path () {
316	case "$1" in
317	-?) t=$1; shift;;
318	*) t=-d;;
319	esac
320	case "$2,$1" in
321	MAKESYSPATH,.../*) ;;
322	*) [ $t ${1:-.} ] || return;;
323	esac
324	no_path $* && eval ${2:-PATH}="$__p${__p:+:}$1"
325}
326
327
328srcdir=`GetDir /bmake make-bootstrap.sh.in "$srcdir" "$2" "$Mydir" ./bmake* "$Mydir"/../bmake*`
329[ -d "${srcdir:-/dev/null}" ] || Usage
330case "$mksrc" in
331none|-) # we ignore this now
332	mksrc=$Mydir/mk
333	;;
334.../*)	# find here or above
335	mksrc=`FindHereOrAbove -C "$Mydir" -s "$mksrc/sys.mk"`
336	# that found a file
337	mksrc=`dirname $mksrc`
338	;;
339*)	# guess we want mksrc...
340	mksrc=`GetDir /mk sys.mk "$mksrc" "$3" ./mk* "$srcdir"/mk* "$srcdir"/../mk*`
341	[ -d "${mksrc:-/dev/null}" ] || Usage "Use '-m none' to build without mksrc"
342	;;
343esac
344
345# Ok, get to work...
346objdir="${objdir:-$OBJROOT$HOST_TARGET}"
347[ -d "$objdir" ] || mkdir -p "$objdir"
348[ -d "$objdir" ] || mkdir "$objdir"
349cd "$objdir" || exit 1
350# make it absolute
351objdir=`'pwd'`
352
353ShareDir() {
354	case "/$1" in
355	/) [ -d /share ] || return;;
356	*/$HOST_TARGET)
357		if [ -d "$1/../share" ]; then
358			echo `dirname "$1"`/share
359			return
360		fi
361		;;
362	esac
363	echo $1/share
364}
365
366# make it easy to force prefix to use $HOST_TARGET
367: looking at "$prefix"
368case "$prefix" in
369*/host?target) prefix=`echo "$prefix" | sed "s,host.target,${HOST_TARGET},"`;;
370esac
371
372share_dir="${share_dir:-`ShareDir $prefix`}"
373
374AddConfigure --prefix= "$prefix"
375case "$CONFIGURE_ARGS" in
376*--with-*-sys-path*) ;; # skip
377*) [ "$share_dir" ] && AddConfigure --with-default-sys-path= "$share_dir/mk";;
378esac
379if [ "$mksrc" ]; then
380        AddConfigure --with-mksrc= "$mksrc"
381        # not all cc's support this
382        CFLAGS_MF= CFLAGS_MD=
383        export CFLAGS_MF CFLAGS_MD
384fi
385
386# this makes it easy to run the bmake we just built
387# the :tA dance is needed because 'pwd' and even /bin/pwd
388# may not give the same result as realpath().
389Bmake() {
390    (
391	    cd $Mydir &&
392	    MAKESYSPATH=$mksrc SRCTOP=$Mydir OBJTOP=$objdir \
393	    MAKEOBJDIR='${.CURDIR:S,${SRCTOP:tA},${OBJTOP:tA},}' \
394	    ${BMAKE:-$objdir/bmake} -f $Mydir/Makefile "$@"
395    )
396}
397
398# there is actually a shell where type is not a builtin
399# if type is missing, which(1) had better exists!
400if (type cat) > /dev/null 2>&1; then
401which() {
402        type "$@" | sed 's,[()],,g;s,^[^/][^/]*,,;q'
403}
404fi
405# make sure test below uses the same diff that configure did
406TOOL_DIFF=`which diff`
407export TOOL_DIFF
408
409op_configure() {
410	$srcdir/configure $CONFIGURE_ARGS || exit 1
411}
412
413op_build() {
414	[ -s make-bootstrap.sh ] || op_configure
415	chmod 755 make-bootstrap.sh || exit 1
416	./make-bootstrap.sh || exit 1
417	case "$op" in
418	build) op_test;;
419	esac
420}
421
422op_test() {
423	[ -x bmake ] || op_build
424	Bmake test || exit 1
425}
426
427op_clean() {
428	if [ -x bmake ]; then
429		ln bmake bmake$$
430		BMAKE=$objdir/bmake$$ Bmake clean
431		rm -f bmake$$
432	elif [ $objdir != $srcdir ]; then
433		rm -rf *
434	fi
435}
436
437op_install() {
438	op_test
439	case "$INSTALL_PREFIX,$INSTALL_BIN,$prefix" in
440	,$HOST_TARGET/bin,*/$HOST_TARGET)
441		INSTALL_PREFIX=`dirname $prefix`
442		;;
443	esac
444	INSTALL_PREFIX=${INSTALL_PREFIX:-$prefix}
445	Bmake install prefix=$INSTALL_PREFIX BINDIR=$INSTALL_PREFIX/${INSTALL_BIN:-bin} ${INSTALL_DESTDIR:+DESTDIR=$INSTALL_DESTDIR} $INSTALL_ARGS || exit 1
446}
447
448op_all() {
449	rm -f make-bootstrap.sh bmake *.o
450	if [ -n "$INSTALL_PREFIX" ]; then
451		op_install
452	else
453		op_test
454		MAKE_VERSION=`sed -n '/^_MAKE_VERSION/ { s,.*=  *,,;p; }' $srcdir/Makefile`
455		echo You can install by running:
456		echo
457		echo $0 $cmd_args op=install
458		echo
459		echo "Use --install-prefix=/something to install somewhere other than $prefix"
460		echo "Use --install-destdir=/somewhere to set DESTDIR during install"
461		echo "Use --install-host-target to use INSTALL_BIN=$HOST_TARGET/bin"
462		echo "Use -DWITH_PROG_VERSION to install as bmake-$MAKE_VERSION"
463		echo "Use -DWITHOUT_PROG_LINK to suppress bmake -> bmake-$MAKE_VERSION symlink"
464		echo "Use -DWITHOUT_INSTALL_MK to skip installing files to $prefix/share/mk"
465	fi
466}
467
468op_$op
469exit 0
470