xref: /freebsd/contrib/bmake/boot-strap (revision aa77200569e397d6ff1fdb4d255d0fa254d0a128)
1:
2# NAME:
3#	boot-strap
4#
5# SYNOPSIS:
6#	boot-strap [--"configure_arg" ... ][-s "srcdir"][-m "mksrc"]\\
7#		["prefix" ["bmakesrc" ["mksrc"]]]
8#
9# DESCRIPTION:
10#	This script is used to configure/build bmake it builds for
11#	each OS in a subdir to keep the src clean.
12#	On successful completion it echos commands to put the new
13#	bmake binary into the /configs tree (if it exists)
14#	(http://www.crufty.net/FreeWare/configs.html), $prefix/bin
15#	and a suitable ~/*bin directory.
16#
17#	Options:
18#
19#	-c "rc"
20#		Pick up settings from "rc".
21#		We look for '.bmake-boot-strap.rc' before processing
22#		options.
23#
24#	--share "share_dir"
25#		Where to put man pages and mk files.
26#		If $prefix ends in $HOST_TARGET, and $prefix/../share
27#		exits, the default will be that rather than $prefix/share.
28#
29#	--mksrc "mksrc"
30#		Indicate where the mk files can be found.
31#		Default is ./mk or ../mk, set to 'none' to force
32#		building without "mksrc" but in that case a sys.mk
33#		needs to exist in the default syspath ($share_dir/mk)
34#
35#	Possibly useful configure_args:
36#
37#	--without-meta
38#		disable use of meta mode.
39#
40#	--without-filemon
41#		disable use of filemon(9) which is currently only
42#		available for NetBSD and FreeBSD.
43#
44#	--with-filemon="path/to/filemon.h"
45#		enables use of filemon(9) by meta mode.
46#
47#	--with-machine="machine"
48#		set "machine" to override that determined by
49#		machine.sh
50#
51#	--with-force-machine="machine"
52#		force "machine" even if uname(3) provides a value.
53#
54#	--with-machine_arch="machine_arch"
55#		set "machine_arch" to override that determined by
56#		machine.sh
57#
58#	--with-default-sys-path="syspath"
59#		set an explicit default "syspath" which is where bmake
60#		will look for sys.mk and friends.
61#
62# AUTHOR:
63#	Simon J. Gerraty <sjg@crufty.net>
64
65# RCSid:
66#	$Id: boot-strap,v 1.39 2012/03/26 17:08:22 sjg Exp $
67#
68#	@(#) Copyright (c) 2001 Simon J. Gerraty
69#
70#	This file is provided in the hope that it will
71#	be of use.  There is absolutely NO WARRANTY.
72#	Permission to copy, redistribute or otherwise
73#	use this file is hereby granted provided that
74#	the above copyright notice and this notice are
75#	left intact.
76#
77#	Please send copies of changes and bug-fixes to:
78#	sjg@crufty.net
79#
80
81Mydir=`dirname $0`
82. "$Mydir/os.sh"
83case "$Mydir" in
84/*) ;;
85*) Mydir=`cd "$Mydir" && 'pwd'`;;
86esac
87
88
89Usage() {
90	[ "$1" ] && echo "ERROR: $@" >&2
91	echo "Usage:" >&2
92	echo "$0 [--<configure_arg> ...][-s <srcdir>][-m <mksrc>][<prefix> [[<srcdir>] [<mksrc>]]]" >&2
93	exit 1
94}
95
96Error() {
97	echo "ERROR: $@" >&2
98	exit 1
99}
100
101source_rc() {
102	rc="$1"; shift
103	for d in ${*:-""}
104	do
105		r="${d:+$d/}$rc"
106		[ -f "$r" -a -s "$r" ] || continue
107		echo "NOTE: reading $r"
108		. "$r"
109		break
110	done
111}
112
113CONFIGURE_ARGS=
114MAKESYSPATH=
115# pick a useful default prefix (for me at least ;-)
116for prefix in /opt/$HOST_TARGET "$HOME/$HOST_TARGET" /usr/pkg /usr/local ""
117do
118	[ -d "${prefix:-.}" ] && break
119done
120srcdir=
121mksrc=
122objdir=
123quiet=:
124
125source_rc .bmake-boot-strap.rc . "$Mydir/.." "$HOME"
126
127get_optarg() {
128	expr "x$1" : "x[^=]*=\\(.*\\)"
129}
130
131while :
132do
133	case "$1" in
134	--) shift; break;;
135	--prefix) prefix="$2"; shift;;
136	--prefix=*) prefix=`get_optarg "$1"`;;
137	--src=*) srcdir=`get_optarg "$1"`;;
138	--with-mksrc=*|--mksrc=*) mksrc=`get_optarg "$1"`;;
139	--share=*) share_dir=`get_optarg "$1"`;;
140	--share) share_dir="$2"; shift;;
141	--with-default-sys-path=*)
142	    CONFIGURE_ARGS="$1"
143	    MAKESYSPATH=`get_optarg "$1"`;;
144	--with-default-sys-path)
145	    CONFIGURE_ARGS="$1 $2"
146	    MAKESYSPATH="$2"; shift;;
147	-s|--src) srcdir="$2"; shift;;
148	-m|--mksrc) mksrc="$2"; shift;;
149	-o|--objdir) objdir="$2"; shift;;
150	-q) quiet=;;
151	-c) source_rc "$2"; shift;;
152	--*) CONFIGURE_ARGS="$CONFIGURE_ARGS $1";;
153	*=*) eval "$1"; export `expr "x$1" : "x\\(.[^=]*\\)=.*"`;;
154	*) break;;
155	esac
156        shift
157done
158
159AddConfigure() {
160	case " $CONFIGURE_ARGS " in
161	*" $1"*) ;;
162	*) CONFIGURE_ARGS="$CONFIGURE_ARGS $1$2";;
163	esac
164}
165
166GetDir() {
167	match="$1"
168	shift
169	fmatch="$1"
170	shift
171	for dir in $*
172	do
173		[ -d "$dir" ] || continue
174		case "/$dir/" in
175		*$match*) ;;
176		*) continue;;
177		esac
178		case "$fmatch" in
179		.) ;;
180		*) [ -s $dir/$fmatch ] || continue;;
181		esac
182		case "$dir/" in
183		*./*) cd "$dir" && 'pwd';;
184		/*) echo $dir;;
185		*) cd "$dir" && 'pwd';;
186		esac
187		break
188	done
189}
190
191FindHereOrAbove() {
192    (
193	_t=-s
194	while :
195	do
196		case "$1" in
197		-C) cd "$2"; shift; shift;;
198		-?) _t=$1; shift;;
199		*) break;;
200		esac
201	done
202	case "$1" in
203	/*)	# we shouldn't be here
204		[ $_t "$1" ] && echo "$1"
205		return
206		;;
207	.../*) want=`echo "$1" | sed 's,^.../*,,'`;;
208	*) want="$1";;
209	esac
210	here=`'pwd'`
211	while :
212	do
213		if [ $_t "./$want" ]; then
214			echo "$here/$want"
215			return
216		fi
217		cd ..
218		here=`'pwd'`
219		case "$here" in
220		/) return;;
221		esac
222	done
223    )
224}
225
226# is $1 missing from $2 (or PATH) ?
227no_path() {
228	eval "__p=\$${2:-PATH}"
229	case ":$__p:" in *:"$1":*) return 1;; *) return 0;; esac
230}
231
232# if $1 exists and is not in path, append it
233add_path () {
234	case "$1" in
235	-?) t=$1; shift;;
236	*) t=-d;;
237	esac
238	case "$2,$1" in
239	MAKESYSPATH,.../*) ;;
240	*) [ $t ${1:-.} ] || return;;
241	esac
242	no_path $* && eval ${2:-PATH}="$__p${__p:+:}$1"
243}
244
245
246srcdir=`GetDir /bmake make-bootstrap.sh.in "$srcdir" "$2" "$Mydir" ./bmake* "$Mydir"/../bmake*`
247[ -d "${srcdir:-/dev/null}" ] || Usage
248case "$mksrc" in
249none|-) # we don't want it
250	mksrc=
251	;;
252.../*)	# find here or above
253	mksrc=`FindHereOrAbove -C "$Mydir" -s "$mksrc/sys.mk"`
254	# that found a file
255	mksrc=`dirname $mksrc`
256	;;
257*)	# guess we want mksrc...
258	mksrc=`GetDir /mk sys.mk "$mksrc" "$3" ./mk* "$srcdir"/mk* "$srcdir"/../mk*`
259	[ -d "${mksrc:-/dev/null}" ] || Usage "Use '-m none' to build without mksrc"
260	;;
261esac
262
263# Ok, get to work...
264objdir="${objdir:-$OS}"
265[ -d "$objdir" ] || mkdir -p "$objdir"
266[ -d "$objdir" ] || mkdir "$objdir"
267cd "$objdir" || exit 1
268# make it absolute
269objdir=`'pwd'`
270
271ShareDir() {
272	case "/$1" in
273	/) [ -d /share ] || return;;
274	*/$HOST_TARGET)
275		if [ -d "$1/../share" ]; then
276			echo `dirname "$1"`/share
277			return
278		fi
279		;;
280	esac
281	echo $1/share
282}
283
284# make it easy to force prefix to use $HOST_TARGET
285: looking at "$prefix"
286case "$prefix" in
287*/host?target) prefix=`echo "$prefix" | sed "s,host.target,${HOST_TARGET},"`;;
288esac
289
290share_dir="${share_dir:-`ShareDir $prefix`}"
291
292AddConfigure --prefix= "$prefix"
293case "$CONFIGURE_ARGS" in
294*--with-*-sys-path*) ;; # skip
295*) [ "$share_dir" ] && AddConfigure --with-default-sys-path= "$share_dir/mk";;
296esac
297if [ "$mksrc" ]; then
298        AddConfigure --with-mksrc= "$mksrc"
299        # not all cc's support this
300        CFLAGS_MF= CFLAGS_MD=
301        export CFLAGS_MF CFLAGS_MD
302fi
303
304$srcdir/configure $CONFIGURE_ARGS || exit 1
305chmod 755 make-bootstrap.sh || exit 1
306./make-bootstrap.sh || exit 1
307if [ -z "$MAKESYSPATH" ]; then
308       add_path "${share_dir:-...}/mk" MAKESYSPATH
309       case "$HOST_TARGET" in
310       netbsd*) add_path /usr/share/mk MAKESYSPATH;;
311       esac
312fi
313if [ -s "${mksrc:-/dev/null}/install-mk" ]; then
314        sh "${mksrc}/install-mk" "$objdir/mk"
315        case "$MAKESYSPATH" in
316        .../mk*) ;;
317        *) MAKESYSPATH=".../mk:${MAKESYSPATH}";;
318        esac
319fi
320# make sure test below uses the same diff that configure did
321TOOL_DIFF=`type diff | sed 's,[()],,g;s,^[^/][^/]*,,;q'`
322export MAKESYSPATH TOOL_DIFF
323if [ "$mksrc" ]; then
324        $objdir/bmake test || exit 1
325else
326        # assume nothing
327        $objdir/bmake -r -m / test || exit 1
328fi
329# If -q given, we don't want all the install instructions
330$quiet exit 0
331
332make_version=`./bmake -r -m / -f ./Makefile -V MAKE_VERSION | ( read one two; echo $one )`
333bmake_version=bmake-$make_version
334
335if [ -s /usr/share/tmac/andoc.tmac ]; then
336	# this should be ok
337	man_subdir=man1
338	man_src=$srcdir/bmake.1
339else
340	# guess not
341	man_subdir=cat1
342	man_src=$srcdir/bmake.cat1
343fi
344
345install_prefix() {
346    (
347	bin_dir=
348	share_dir=
349	man_dir=
350	mk_dir=
351	while :
352	do
353		case "$1" in
354		*=*) eval "$1"; shift;;
355		*) break;;
356		esac
357	done
358	bin_dir=${bin_dir:-$1/bin}
359	share_dir=${share_dir:-`ShareDir "$1"`}
360	man_dir=${man_dir:-$share_dir/man}
361	mk_dir=${mk_dir:-$share_dir/mk}
362	echo
363	echo Commands to install into $1/
364	echo
365	echo mkdir -p $bin_dir
366	echo cp $objdir/bmake $bin_dir/$bmake_version
367	echo rm -f $bin_dir/bmake
368	echo ln -s $bmake_version $bin_dir/bmake
369	echo mkdir -p $man_dir/$man_subdir
370	echo cp $man_src $man_dir/$man_subdir/bmake.1
371	if [ "$mksrc" ]; then
372		ev=`env | grep '_MK='`
373		echo $ev sh $mksrc/install-mk $mk_dir
374	fi
375    )
376}
377
378case "$prefix/" in
379"$HOME"/*) ;;
380*)	CONFIGS=${CONFIGS:-/configs}
381	[ -d $CONFIGS ] &&
382	install_prefix mksrc= "$CONFIGS/$OS/$OSMAJOR.X/$MACHINE_ARCH$prefix"
383	# I like to keep a copy here...
384	install_prefix share_dir="$HOME/share" "$HOME/$HOST_TARGET"
385	;;
386esac
387
388install_prefix "$prefix"
389