Home | History | Annotate | Line # | Download | only in src
build.sh revision 1.9
      1 #! /bin/sh
      2 #  $NetBSD: build.sh,v 1.9 2001/10/30 21:04:05 jmc Exp $
      3 #
      4 # Top level build wrapper, for a system containing no tools.
      5 #
      6 # This script should run on any POSIX-compliant shell.  For systems
      7 # with a strange /bin/sh, "ksh" may be an ample alternative.
      8 #
      9 
     10 bomb () {
     11 	echo ""
     12 	echo "ERROR: $@"
     13 	echo "*** BUILD ABORTED ***"
     14 	exit 1
     15 }
     16 
     17 getarch () {
     18 	# Translate a MACHINE into a default MACHINE_ARCH.
     19 	case $MACHINE in
     20 		arm26|dnard|evbarm|hpcarm|netwinder)
     21 			MACHINE_ARCH=arm;;
     22 
     23 		acorn32|arm32|cats)
     24 			MACHINE_ARCH=arm32;;
     25 
     26 		sun2)
     27 			MACHINE_ARCH=m68000;;
     28 
     29 		amiga|atari|cesfic|hp300|sun3|*68k)
     30 			MACHINE_ARCH=m68k;;
     31 
     32 		mipsco|newsmips|sgimips)
     33 			MACHINE_ARCH=mipseb;;
     34 
     35 		algor|arc|cobalt|hpcmips|playstation2|pmax)
     36 			MACHINE_ARCH=mipsel;;
     37 
     38 		pc532)
     39 			MACHINE_ARCH=ns32k;;
     40 
     41 		bebox|prep|sandpoint|walnut|*ppc)
     42 			MACHINE_ARCH=powerpc;;
     43 
     44 		mmeye)
     45 			MACHINE_ARCH=sh3eb;;
     46 
     47 		dreamcast|evbsh3|hpcsh)
     48 			MACHINE_ARCH=sh3el;;
     49 
     50 		alpha|i386|sparc|sparc64|vax|x86_64)
     51 			MACHINE_ARCH=$MACHINE;;
     52 
     53 		*)	bomb "unknown target MACHINE: $MACHINE";;
     54 	esac
     55 }
     56 
     57 # Emulate "mkdir -p" for systems that have an Old "mkdir".
     58 mkdirp () {
     59 	IFS=/; set -- $@; unset IFS
     60 	_d=
     61 	if [ "$1" = "" ]; then _d=/; shift; fi
     62 
     63 	for _f in "$@"; do
     64 		if [ "$_f" != "" ]; then
     65 			[ -d "$_d$_f" ] || mkdir "$_d$_f" || return 1
     66 			_d="$_d$_f/"
     67 		fi
     68 	done
     69 }
     70 
     71 usage () {
     72 	echo "Usage:"
     73 	echo "$0 [-r] [-a arch] [-j njob] [-m mach] [-D dest] [-R release] [-T tools]"
     74 	echo "    -b: bootstrap only. Build nbmake, install it and then exit."
     75 	echo "    -m: set target MACHINE to mach (REQUIRED, or set in environment)"
     76 	echo "    -D: set DESTDIR to dest (REQUIRED, or set in environment)"
     77 	echo "    -T: set TOOLDIR to tools (REQUIRED, or set in environment)"
     78 	echo ""
     79 	echo "    -a: set target MACHINE_ARCH to arch (otherwise deduced from MACHINE)"
     80 	echo "    -j: set NBUILDJOBS to njob"
     81 	echo "    -r: remove TOOLDIR and DESTDIR before the build"
     82 	echo "    -R: build a release (set RELEASEDIR) to release"
     83 	exit 1
     84 }
     85 
     86 opts='a:bhj:m:rD:R:T:'
     87 
     88 if type getopts >/dev/null 2>&1; then
     89 	# Use POSIX getopts.
     90 	getoptcmd='getopts $opts opt && opt=-$opt'
     91 	optargcmd=':'
     92 else
     93 	type getopt >/dev/null 2>&1 || bomb "/bin/sh shell is too old; try ksh"
     94 
     95 	# Use old-style getopt(1) (doesn't handle whitespace in args).
     96 	args="`getopt $opts $*`"
     97 	[ $? = 0 ] || usage
     98 	set -- $args
     99 
    100 	getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
    101 	optargcmd='OPTARG="$1"; shift'
    102 fi
    103 	
    104 opt_a=no
    105 while eval $getoptcmd; do case $opt in
    106 	-a)	eval $optargcmd
    107 		MACHINE_ARCH=$OPTARG; opt_a=yes;;
    108 
    109 	-b)	BOOTSTRAP_ONLY=1;;
    110 
    111 	-j)	eval $optargcmd
    112 		buildjobs="NBUILDJOBS=$OPTARG";;
    113 
    114 	# -m overrides MACHINE_ARCH unless "-a" is specified
    115 	-m)	eval $optargcmd
    116 		MACHINE=$OPTARG; [ "$opt_a" != "yes" ] && getarch;;
    117 
    118 	-r)	removedirs=true;;
    119 
    120 	-D)	eval $optargcmd
    121 		DESTDIR="$OPTARG";;
    122 
    123 	-R)	eval $optargcmd
    124 		releasedir="RELEASEDIR=$OPTARG"; buildtarget=release;;
    125 
    126 	-T)	eval $optargcmd
    127 		TOOLDIR="$OPTARG";;
    128 
    129 	--)		break;;
    130 	-'?'|-h)	usage;;
    131 esac; done
    132 
    133 for var in MACHINE DESTDIR TOOLDIR; do
    134 	if ! eval 'test -n "$'$var'"'; then
    135 		echo "$var must be set in the environment before running build.sh."
    136 		echo ""; usage
    137 	fi
    138 done
    139 
    140 # Set up environment.
    141 test -n "$MACHINE_ARCH" || getarch
    142 test -d usr.bin/make || bomb "build.sh must be run from the top source level"
    143 
    144 # Remove the target directories.
    145 if ${removedirs-false}; then
    146 	echo "Removing DESTDIR and TOOLDIR...."
    147 	rm -rf $DESTDIR $TOOLDIR
    148 fi
    149 
    150 mkdirp $TOOLDIR/bin || bomb "mkdir of $TOOLDIR/bin failed"
    151 
    152 # Test make source file timestamps against installed nbmake binary.
    153 if [ -x $TOOLDIR/bin/nbmake ]; then
    154 	for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
    155 		if [ $f -nt $TOOLDIR/bin/nbmake ]; then
    156 			rebuildmake=true; break
    157 		fi
    158 	done
    159 else
    160 	rebuildmake=true
    161 fi
    162 
    163 # Build $TOOLDIR/bin/nbmake.
    164 if ${rebuildmake-false}; then
    165 	echo "Building nbmake...."
    166 
    167 	# Go to a temporary directory in case building .o's happens.
    168 	srcdir=`pwd`
    169 	tmpdir=${TMPDIR-/tmp}/nbbuild$$
    170 
    171 	mkdir $tmpdir || bomb "cannot mkdir: $tmpdir"
    172 	trap "rm -r -f $tmpdir" 0
    173 	trap "exit 1" 1 2 3 15
    174 	cd $tmpdir
    175 
    176 	${HOST_CC-cc} ${HOST_CFLAGS} -DMAKE_BOOTSTRAP \
    177 		-o $TOOLDIR/bin/nbmake -I$srcdir/usr.bin/make \
    178 		$srcdir/usr.bin/make/*.c $srcdir/usr.bin/make/lst.lib/*.c \
    179 		|| bomb "build of nbmake failed"
    180 
    181 	# Clean up.
    182 	cd $srcdir
    183 	rm -r -f $tmpdir
    184 	trap 0 1 2 3 15
    185 
    186 	# Some compilers are just *that* braindead.
    187 	rm -f $srcdir/usr.bin/make/*.o $srcdir/usr.bin/make/lst.lib/*.o
    188 fi
    189 
    190 # Build a nbmake wrapper script, usable by hand as well as by build.sh.
    191 makeprog=$TOOLDIR/bin/nbmake-$MACHINE
    192 
    193 if ${rebuildmake-false} || [ ! -f $makeprog ] || [ $makeprog -ot build.sh ]; then
    194 	rm -f $makeprog
    195 	cat >$makeprog <<EOF
    196 #! /bin/sh
    197 # Set proper variables to allow easy "make" building of a NetBSD subtree.
    198 # Generated from:  \$NetBSD: build.sh,v 1.9 2001/10/30 21:04:05 jmc Exp $
    199 #
    200 exec $TOOLDIR/bin/nbmake MACHINE=$MACHINE MACHINE_ARCH=$MACHINE_ARCH \
    201 USETOOLS=yes USE_NEW_TOOLCHAIN=yes TOOLDIR="$TOOLDIR" \${1+\$@}
    202 EOF
    203 	chmod +x $makeprog
    204 fi
    205 
    206 if [ "$BOOTSTRAP_ONLY" != "1" ]; then
    207 	exec $makeprog -m `pwd`/share/mk ${buildtarget-build} \
    208 		MKTOOLS=yes DESTDIR="$DESTDIR" TOOLDIR="$TOOLDIR" \
    209 		$buildjobs $releasedir
    210 fi
    211