xref: /freebsd/contrib/bmake/boot-strap (revision f5f7c05209ca2c3748fd8b27c5e80ffad49120eb)
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.42 2013/01/25 20:20:33 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# --install[-host-target] will set this
163INSTALL_PREFIX=
164# other things we pass to install step
165INSTALL_ARGS=
166CONFIGURE_ARGS=
167MAKESYSPATH=
168# pick a useful default prefix (for me at least ;-)
169for prefix in /opt/$HOST_TARGET "$HOME/$HOST_TARGET" /usr/pkg /usr/local ""
170do
171	[ -d "${prefix:-.}" ] || continue
172	case "$prefix" in
173	*/$HOST_TARGET)
174		p=`dirname $prefix`
175		if [ -d $p/share ]; then
176			INSTALL_BIN=$HOST_TARGET/bin
177			prefix=$p
178		fi
179		;;
180	esac
181        echo "NOTE: default prefix=$prefix ${INSTALL_BIN:+INSTALL_BIN=$INSTALL_BIN}"
182	break
183done
184srcdir=$Mydir
185mksrc=$Mydir/mk
186objdir=
187quiet=:
188
189${SKIP_RC:+:} source_rc .bmake-boot-strap.rc . "$Mydir/.." "$HOME"
190
191get_optarg() {
192	expr "x$1" : "x[^=]*=\\(.*\\)"
193}
194
195here=`'pwd'`
196if [ $here = $Mydir ]; then
197   # avoid polution
198   OBJROOT=../
199fi
200
201op=all
202BMAKE=
203
204while :
205do
206	case "$1" in
207	--) shift; break;;
208        --help) sed -n -e "1d;/RCSid/,\$d" -e '/^#\.[a-z]/d' -e '/^#/s,^# *,,p' $0; exit 0;;
209	--prefix) prefix="$2"; shift;;
210	--prefix=*) prefix=`get_optarg "$1"`;;
211	--src=*) srcdir=`get_optarg "$1"`;;
212	--with-mksrc=*|--mksrc=*) mksrc=`get_optarg "$1"`;;
213	--share=*) share_dir=`get_optarg "$1"`;;
214	--share) share_dir="$2"; shift;;
215	--with-default-sys-path=*)
216	    CONFIGURE_ARGS="$1"
217	    MAKESYSPATH=`get_optarg "$1"`;;
218	--with-default-sys-path)
219	    CONFIGURE_ARGS="$1 $2"
220	    MAKESYSPATH="$2"; shift;;
221	--install) INSTALL_PREFIX=${INSTALL_PREFIX:-$prefix};;
222	--install-host-target)
223                INSTALL_PREFIX=${INSTALL_PREFIX:-$prefix}
224                INSTALL_BIN=$HOST_TARGET/bin;;
225	--install-destdir=*) INSTALL_DESTDIR=`get_optarg "$1"`;;
226	--install-prefix=*) INSTALL_PREFIX=`get_optarg "$1"`;;
227	-DWITH*) INSTALL_ARGS="$INSTALL_ARGS $1";;
228	-s|--src) srcdir="$2"; shift;;
229	-m|--mksrc) mksrc="$2"; shift;;
230	-o|--objdir) objdir="$2"; shift;;
231	-q) quiet=;;
232	-c) source_rc "$2"; shift;;
233	--*) CONFIGURE_ARGS="$CONFIGURE_ARGS $1";;
234	*=*) eval "$1"; export `expr "x$1" : "x\\(.[^=]*\\)=.*"`;;
235	*) break;;
236	esac
237        shift
238done
239
240AddConfigure() {
241	case " $CONFIGURE_ARGS " in
242	*" $1"*) ;;
243	*) CONFIGURE_ARGS="$CONFIGURE_ARGS $1$2";;
244	esac
245}
246
247GetDir() {
248	match="$1"
249	shift
250	fmatch="$1"
251	shift
252	for dir in $*
253	do
254		[ -d "$dir" ] || continue
255		case "/$dir/" in
256		*$match*) ;;
257		*) continue;;
258		esac
259		case "$fmatch" in
260		.) ;;
261		*) [ -s $dir/$fmatch ] || continue;;
262		esac
263		case "$dir/" in
264		*./*) cd "$dir" && 'pwd';;
265		/*) echo $dir;;
266		*) cd "$dir" && 'pwd';;
267		esac
268		break
269	done
270}
271
272FindHereOrAbove() {
273    (
274	_t=-s
275	while :
276	do
277		case "$1" in
278		-C) cd "$2"; shift; shift;;
279		-?) _t=$1; shift;;
280		*) break;;
281		esac
282	done
283	case "$1" in
284	/*)	# we shouldn't be here
285		[ $_t "$1" ] && echo "$1"
286		return
287		;;
288	.../*) want=`echo "$1" | sed 's,^.../*,,'`;;
289	*) want="$1";;
290	esac
291	here=`'pwd'`
292	while :
293	do
294		if [ $_t "./$want" ]; then
295			echo "$here/$want"
296			return
297		fi
298		cd ..
299		here=`'pwd'`
300		case "$here" in
301		/) return;;
302		esac
303	done
304    )
305}
306
307# is $1 missing from $2 (or PATH) ?
308no_path() {
309	eval "__p=\$${2:-PATH}"
310	case ":$__p:" in *:"$1":*) return 1;; *) return 0;; esac
311}
312
313# if $1 exists and is not in path, append it
314add_path () {
315	case "$1" in
316	-?) t=$1; shift;;
317	*) t=-d;;
318	esac
319	case "$2,$1" in
320	MAKESYSPATH,.../*) ;;
321	*) [ $t ${1:-.} ] || return;;
322	esac
323	no_path $* && eval ${2:-PATH}="$__p${__p:+:}$1"
324}
325
326
327srcdir=`GetDir /bmake make-bootstrap.sh.in "$srcdir" "$2" "$Mydir" ./bmake* "$Mydir"/../bmake*`
328[ -d "${srcdir:-/dev/null}" ] || Usage
329case "$mksrc" in
330none|-) # we don't want it
331	mksrc=
332	;;
333.../*)	# find here or above
334	mksrc=`FindHereOrAbove -C "$Mydir" -s "$mksrc/sys.mk"`
335	# that found a file
336	mksrc=`dirname $mksrc`
337	;;
338*)	# guess we want mksrc...
339	mksrc=`GetDir /mk sys.mk "$mksrc" "$3" ./mk* "$srcdir"/mk* "$srcdir"/../mk*`
340	[ -d "${mksrc:-/dev/null}" ] || Usage "Use '-m none' to build without mksrc"
341	;;
342esac
343
344# Ok, get to work...
345objdir="${objdir:-$OBJROOT$HOST_TARGET}"
346[ -d "$objdir" ] || mkdir -p "$objdir"
347[ -d "$objdir" ] || mkdir "$objdir"
348cd "$objdir" || exit 1
349# make it absolute
350objdir=`'pwd'`
351
352ShareDir() {
353	case "/$1" in
354	/) [ -d /share ] || return;;
355	*/$HOST_TARGET)
356		if [ -d "$1/../share" ]; then
357			echo `dirname "$1"`/share
358			return
359		fi
360		;;
361	esac
362	echo $1/share
363}
364
365# make it easy to force prefix to use $HOST_TARGET
366: looking at "$prefix"
367case "$prefix" in
368*/host?target) prefix=`echo "$prefix" | sed "s,host.target,${HOST_TARGET},"`;;
369esac
370
371share_dir="${share_dir:-`ShareDir $prefix`}"
372
373AddConfigure --prefix= "$prefix"
374case "$CONFIGURE_ARGS" in
375*--with-*-sys-path*) ;; # skip
376*) [ "$share_dir" ] && AddConfigure --with-default-sys-path= "$share_dir/mk";;
377esac
378if [ "$mksrc" ]; then
379        AddConfigure --with-mksrc= "$mksrc"
380        # not all cc's support this
381        CFLAGS_MF= CFLAGS_MD=
382        export CFLAGS_MF CFLAGS_MD
383fi
384
385# this makes it easy to run the bmake we just built
386# the :tA dance is needed because 'pwd' and even /bin/pwd
387# may not give the same result as realpath().
388Bmake() {
389    (
390	    cd $Mydir &&
391	    MAKESYSPATH=$mksrc SRCTOP=$Mydir OBJTOP=$objdir \
392	    MAKEOBJDIR='${.CURDIR:S,${SRCTOP:tA},${OBJTOP:tA},}' \
393	    ${BMAKE:-$objdir/bmake} -f $Mydir/Makefile "$@"
394    )
395}
396
397# make sure test below uses the same diff that configure did
398TOOL_DIFF=`type diff | sed 's,[()],,g;s,^[^/][^/]*,,;q'`
399export TOOL_DIFF
400
401op_configure() {
402	$srcdir/configure $CONFIGURE_ARGS || exit 1
403}
404
405op_build() {
406	[ -s make-bootstrap.sh ] || op_configure
407	chmod 755 make-bootstrap.sh || exit 1
408	./make-bootstrap.sh || exit 1
409	case "$op" in
410	build) op_test;;
411	esac
412}
413
414op_test() {
415	[ -x bmake ] || op_build
416	Bmake test || exit 1
417}
418
419op_clean() {
420	if [ -x bmake ]; then
421		ln bmake bmake$$
422		BMAKE=$objdir/bmake$$ Bmake clean
423		rm -f bmake$$
424	elif [ $objdir != $srcdir ]; then
425		rm -rf *
426	fi
427}
428
429op_install() {
430	op_test
431	case "$INSTALL_PREFIX,$INSTALL_BIN,$prefix" in
432	,$HOST_TARGET/bin,*/$HOST_TARGET)
433		INSTALL_PREFIX=`dirname $prefix`
434		;;
435	esac
436	INSTALL_PREFIX=${INSTALL_PREFIX:-$prefix}
437	Bmake install prefix=$INSTALL_PREFIX BINDIR=$INSTALL_PREFIX/${INSTALL_BIN:-bin} ${INSTALL_DESTDIR:+DESTDIR=$INSTALL_DESTDIR} $INSTALL_ARGS || exit 1
438}
439
440op_all() {
441	rm -f make-bootstrap.sh bmake *.o
442	if [ -n "$INSTALL_PREFIX" ]; then
443		op_install
444	else
445		op_test
446		MAKE_VERSION=`sed -n '/^MAKE_VERSION/ { s,.*=  *,,;p; }' $srcdir/Makefile`
447		echo You can install by running:
448		echo
449		echo $0 $cmd_args op=install
450		echo
451		echo "Use --install-prefix=/something to install somewhere other than $prefix"
452		echo "Use --install-destdir=/somewhere to set DESTDIR during install"
453		echo "Use --install-host-target to use INSTALL_BIN=$HOST_TARGET/bin"
454		echo "Use -DWITH_PROG_VERSION to install as bmake-$MAKE_VERSION"
455		echo "Use -DWITHOUT_PROG_LINK to supress bmake -> bmake-$MAKE_VERSION symlink"
456		echo "Use -DWITHOUT_INSTALL_MK to skip installing files to $prefix/share/mk"
457	fi
458}
459
460op_$op
461exit 0
462