summaryrefslogtreecommitdiffstats
path: root/contrib/bmake/boot-strap
blob: 7eff5f65ae1695714665fd85fbd647f4848d415e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
:
# NAME:
#	boot-strap
#
# SYNOPSIS:
#	boot-strap ["options"]
#	boot-strap --prefix=/opt --install
#	boot-strap --prefix=$HOME --install-host-target -DWITH_PROG_VERSION
#	boot-strap ["options"] op=build
#	boot-strap ["options"] op=install
#
# DESCRIPTION:
#	This script is used to configure/build bmake it builds for
#	each host-target in a different subdir to keep the src clean.
#	There is no requirement for an existing make(1).
#
#	On successful completion if no '--install' flag is given,
#	it echos a command to do installation.
#
#	The variable "op" defaults to 'all', and is affected by
#	'--install' flag as above.
#	Other values include:
#
#	configure
#		Just run 'configure'
#
#	build
#		If 'configure' has not been done, do it, then
#		run the build script, and finally 'test'.
#
#	install
#		If 'build' has not been done, do it, 'test' then
#		install. 
#
#	clean
#		attempt to clean up
#
#	test
#		run the unit-tests.  Done automatically after 'build'
#		and before 'install'.
#
#	The above are leveraged by a trivial makefile for the benefit
#	of those that have './configure; make; make install' baked
#	into them.
#	
#	Options:
#
#	-c "rc"
#		Pick up settings from "rc".  
#		We look for '.bmake-boot-strap.rc' before processing
#		options (unless SKIP_RC is set in environment).
#
#	--share "share_dir"
#		Where to put man pages and mk files.
#		If $prefix ends in $HOST_TARGET, and $prefix/../share
#		exits, the default will be that rather than $prefix/share.
#
#	--mksrc "mksrc"
#		Indicate where the mk files can be found.
#		Default is $Mydir/mk
#
#	--install
#		If build and test work, run bmake install.
#		BINDIR=$prefix/bin
#		SHAREDIR=$prefix/share
#
#	--install-host-target
#		As for '--install' but BINDIR=$prefix/$HOST_TARGET/bin
#		This is useful when $prefix/ is shared by multiple
#		machines.
#
#	Flags relevant when installing:
#
#	-DWITHOUT_INSTALL_MK
#		Skip installing mk files.
#		By default they will be installed to $prefix/share/mk
#
#	-DWITH_PROG_VERSION
#		Install 'bmake' as 'bmake-$MAKE_VERSION'
#		A symlink will be made as 'bmake' unless
#		-DWITHOUT_PROG_LINK is set.
#
#	Possibly useful configure_args:
#
#	--without-meta
#		disable use of meta mode.
#
#	--without-filemon
#		disable use of filemon(9) which is currently only
#		available for NetBSD and FreeBSD.
#
#	--with-filemon="path/to/filemon.h"
#		enables use of filemon(9) by meta mode.
#		
#	--with-machine="machine"
#		set "machine" to override that determined by
#		machine.sh
#	
#	--with-force-machine="machine"
#		force "machine" even if uname(3) provides a value.
#
#	--with-machine_arch="machine_arch"
#		set "machine_arch" to override that determined by
#		machine.sh
#
#	--with-default-sys-path="syspath"
#		set an explicit default "syspath" which is where bmake
#		will look for sys.mk and friends.
#
# AUTHOR:
#	Simon J. Gerraty <sjg@crufty.net>

# RCSid:
#	$Id: boot-strap,v 1.42 2013/01/25 20:20:33 sjg Exp $
#
#	@(#) Copyright (c) 2001 Simon J. Gerraty
#
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to copy, redistribute or otherwise
#	use this file is hereby granted provided that 
#	the above copyright notice and this notice are
#	left intact. 
#      
#	Please send copies of changes and bug-fixes to:
#	sjg@crufty.net
#

Mydir=`dirname $0`
. "$Mydir/os.sh"
case "$Mydir" in
/*) ;;
*) Mydir=`cd "$Mydir" && 'pwd'`;;
esac

Usage() {
	[ "$1" ] && echo "ERROR: $@" >&2
	echo "Usage:" >&2
	echo "$0 [--<configure_arg> ...][<prefix>][--install]" >&2
	exit 1
}

Error() {
	echo "ERROR: $@" >&2
	exit 1
}

source_rc() {
	rc="$1"; shift
	for d in ${*:-""}
	do
		r="${d:+$d/}$rc"
		[ -f "$r" -a -s "$r" ] || continue
		echo "NOTE: reading $r"
		. "$r"
		break
	done
}

cmd_args="$@"

# --install[-host-target] will set this
INSTALL_PREFIX=
# other things we pass to install step
INSTALL_ARGS=
CONFIGURE_ARGS=
MAKESYSPATH=
# pick a useful default prefix (for me at least ;-)
for prefix in /opt/$HOST_TARGET "$HOME/$HOST_TARGET" /usr/pkg /usr/local ""
do
	[ -d "${prefix:-.}" ] || continue
	case "$prefix" in
	*/$HOST_TARGET)
		p=`dirname $prefix`
		if [ -d $p/share ]; then
			INSTALL_BIN=$HOST_TARGET/bin
			prefix=$p
		fi
		;;
	esac
        echo "NOTE: default prefix=$prefix ${INSTALL_BIN:+INSTALL_BIN=$INSTALL_BIN}"
	break
done
srcdir=$Mydir
mksrc=$Mydir/mk
objdir=
quiet=:

${SKIP_RC:+:} source_rc .bmake-boot-strap.rc . "$Mydir/.." "$HOME"

get_optarg() {
	expr "x$1" : "x[^=]*=\\(.*\\)"
}

here=`'pwd'`
if [ $here = $Mydir ]; then
   # avoid polution
   OBJROOT=../
fi

op=all
BMAKE=

while :
do
	case "$1" in
	--) shift; break;;
        --help) sed -n -e "1d;/RCSid/,\$d" -e '/^#\.[a-z]/d' -e '/^#/s,^# *,,p' $0; exit 0;;
	--prefix) prefix="$2"; shift;;
	--prefix=*) prefix=`get_optarg "$1"`;;
	--src=*) srcdir=`get_optarg "$1"`;;
	--with-mksrc=*|--mksrc=*) mksrc=`get_optarg "$1"`;;
	--share=*) share_dir=`get_optarg "$1"`;;
	--share) share_dir="$2"; shift;;
	--with-default-sys-path=*)
	    CONFIGURE_ARGS="$1"
	    MAKESYSPATH=`get_optarg "$1"`;;
	--with-default-sys-path)
	    CONFIGURE_ARGS="$1 $2"
	    MAKESYSPATH="$2"; shift;;
	--install) INSTALL_PREFIX=${INSTALL_PREFIX:-$prefix};;
	--install-host-target)
                INSTALL_PREFIX=${INSTALL_PREFIX:-$prefix}
                INSTALL_BIN=$HOST_TARGET/bin;;
	--install-destdir=*) INSTALL_DESTDIR=`get_optarg "$1"`;;
	--install-prefix=*) INSTALL_PREFIX=`get_optarg "$1"`;;
	-DWITH*) INSTALL_ARGS="$INSTALL_ARGS $1";;
	-s|--src) srcdir="$2"; shift;;
	-m|--mksrc) mksrc="$2"; shift;;
	-o|--objdir) objdir="$2"; shift;;
	-q) quiet=;;
	-c) source_rc "$2"; shift;;
	--*) CONFIGURE_ARGS="$CONFIGURE_ARGS $1";;
	*=*) eval "$1"; export `expr "x$1" : "x\\(.[^=]*\\)=.*"`;;
	*) break;;
	esac
        shift
done

AddConfigure() {
	case " $CONFIGURE_ARGS " in
	*" $1"*) ;;
	*) CONFIGURE_ARGS="$CONFIGURE_ARGS $1$2";;
	esac
}

GetDir() {
	match="$1"
	shift
	fmatch="$1"
	shift
	for dir in $*
	do
		[ -d "$dir" ] || continue
		case "/$dir/" in
		*$match*) ;;
		*) continue;;
		esac
		case "$fmatch" in
		.) ;;
		*) [ -s $dir/$fmatch ] || continue;;
		esac
		case "$dir/" in
		*./*) cd "$dir" && 'pwd';;
		/*) echo $dir;;
		*) cd "$dir" && 'pwd';;
		esac
		break
	done
}

FindHereOrAbove() {
    (
	_t=-s
	while :
	do
		case "$1" in
		-C) cd "$2"; shift; shift;;
		-?) _t=$1; shift;;
		*) break;;
		esac
	done
	case "$1" in	
	/*)	# we shouldn't be here
		[ $_t "$1" ] && echo "$1"
		return
		;;
	.../*) want=`echo "$1" | sed 's,^.../*,,'`;;
	*) want="$1";;
	esac
	here=`'pwd'`
	while :
	do
		if [ $_t "./$want" ]; then
			echo "$here/$want"
			return
		fi
		cd ..
		here=`'pwd'`
		case "$here" in
		/) return;;
		esac
	done
    )
}

# is $1 missing from $2 (or PATH) ?
no_path() {
	eval "__p=\$${2:-PATH}"
	case ":$__p:" in *:"$1":*) return 1;; *) return 0;; esac
}

# if $1 exists and is not in path, append it
add_path () {
	case "$1" in
	-?) t=$1; shift;;
	*) t=-d;;
	esac
	case "$2,$1" in
	MAKESYSPATH,.../*) ;;
	*) [ $t ${1:-.} ] || return;;
	esac
	no_path $* && eval ${2:-PATH}="$__p${__p:+:}$1"
}


srcdir=`GetDir /bmake make-bootstrap.sh.in "$srcdir" "$2" "$Mydir" ./bmake* "$Mydir"/../bmake*`
[ -d "${srcdir:-/dev/null}" ] || Usage
case "$mksrc" in
none|-) # we don't want it
	mksrc=
	;;
.../*)	# find here or above
	mksrc=`FindHereOrAbove -C "$Mydir" -s "$mksrc/sys.mk"`
	# that found a file
	mksrc=`dirname $mksrc`
	;;
*)	# guess we want mksrc...
	mksrc=`GetDir /mk sys.mk "$mksrc" "$3" ./mk* "$srcdir"/mk* "$srcdir"/../mk*`
	[ -d "${mksrc:-/dev/null}" ] || Usage "Use '-m none' to build without mksrc"
	;;
esac

# Ok, get to work...
objdir="${objdir:-$OBJROOT$HOST_TARGET}"
[ -d "$objdir" ] || mkdir -p "$objdir"
[ -d "$objdir" ] || mkdir "$objdir"
cd "$objdir" || exit 1
# make it absolute
objdir=`'pwd'`

ShareDir() {
	case "/$1" in
	/) [ -d /share ] || return;;
	*/$HOST_TARGET)
		if [ -d "$1/../share" ]; then
			echo `dirname "$1"`/share
			return
		fi
		;;
	esac
	echo $1/share
}

# make it easy to force prefix to use $HOST_TARGET
: looking at "$prefix"
case "$prefix" in
*/host?target) prefix=`echo "$prefix" | sed "s,host.target,${HOST_TARGET},"`;;
esac

share_dir="${share_dir:-`ShareDir $prefix`}"

AddConfigure --prefix= "$prefix"
case "$CONFIGURE_ARGS" in
*--with-*-sys-path*) ;; # skip
*) [ "$share_dir" ] && AddConfigure --with-default-sys-path= "$share_dir/mk";;
esac
if [ "$mksrc" ]; then
        AddConfigure --with-mksrc= "$mksrc"
        # not all cc's support this
        CFLAGS_MF= CFLAGS_MD=
        export CFLAGS_MF CFLAGS_MD
fi

# this makes it easy to run the bmake we just built
# the :tA dance is needed because 'pwd' and even /bin/pwd
# may not give the same result as realpath().
Bmake() {
    (
	    cd $Mydir &&
	    MAKESYSPATH=$mksrc SRCTOP=$Mydir OBJTOP=$objdir \
	    MAKEOBJDIR='${.CURDIR:S,${SRCTOP:tA},${OBJTOP:tA},}' \
	    ${BMAKE:-$objdir/bmake} -f $Mydir/Makefile "$@"
    )
}

# make sure test below uses the same diff that configure did
TOOL_DIFF=`type diff | sed 's,[()],,g;s,^[^/][^/]*,,;q'`
export TOOL_DIFF

op_configure() {
	$srcdir/configure $CONFIGURE_ARGS || exit 1
}

op_build() {
	[ -s make-bootstrap.sh ] || op_configure
	chmod 755 make-bootstrap.sh || exit 1
	./make-bootstrap.sh || exit 1
	case "$op" in
	build) op_test;;
	esac
}

op_test() {
	[ -x bmake ] || op_build
	Bmake test || exit 1
}

op_clean() {
	if [ -x bmake ]; then
		ln bmake bmake$$
		BMAKE=$objdir/bmake$$ Bmake clean
		rm -f bmake$$
	elif [ $objdir != $srcdir ]; then
		rm -rf *
	fi
}

op_install() {
	op_test
	case "$INSTALL_PREFIX,$INSTALL_BIN,$prefix" in
	,$HOST_TARGET/bin,*/$HOST_TARGET)
		INSTALL_PREFIX=`dirname $prefix`
		;;
	esac
	INSTALL_PREFIX=${INSTALL_PREFIX:-$prefix}
	Bmake install prefix=$INSTALL_PREFIX BINDIR=$INSTALL_PREFIX/${INSTALL_BIN:-bin} ${INSTALL_DESTDIR:+DESTDIR=$INSTALL_DESTDIR} $INSTALL_ARGS || exit 1
}

op_all() {
	rm -f make-bootstrap.sh bmake *.o
	if [ -n "$INSTALL_PREFIX" ]; then
		op_install
	else
		op_test
		MAKE_VERSION=`sed -n '/^MAKE_VERSION/ { s,.*=  *,,;p; }' $srcdir/Makefile`
		echo You can install by running:
		echo
		echo $0 $cmd_args op=install
		echo
		echo "Use --install-prefix=/something to install somewhere other than $prefix"
		echo "Use --install-destdir=/somewhere to set DESTDIR during install"
		echo "Use --install-host-target to use INSTALL_BIN=$HOST_TARGET/bin"
		echo "Use -DWITH_PROG_VERSION to install as bmake-$MAKE_VERSION"
		echo "Use -DWITHOUT_PROG_LINK to supress bmake -> bmake-$MAKE_VERSION symlink"
		echo "Use -DWITHOUT_INSTALL_MK to skip installing files to $prefix/share/mk"
	fi
}

op_$op
exit 0
OpenPOWER on IntegriCloud