Home | History | Annotate | Line # | Download | only in src
build.sh revision 1.79
      1 #! /usr/bin/env sh
      2 #  $NetBSD: build.sh,v 1.79 2003/01/02 23:11:07 lukem 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" or "bash" may be an ample alternative.
      8 #
      9 # Note, however, that due to the way the interpreter is invoked above,
     10 # if a POSIX-compliant shell is the first in the PATH, you won't have
     11 # to take any further action.
     12 #
     13 
     14 bomb()
     15 {
     16 	echo ""
     17 	echo "ERROR: $@"
     18 	echo "*** BUILD ABORTED ***"
     19 	exit 1
     20 }
     21 
     22 cd "$(dirname $0)"
     23 [ -d usr.bin/make ] ||
     24     bomb "build.sh must be run from the top source level"
     25 [ -f share/mk/bsd.own.mk ] ||
     26     bomb "src/share/mk is missing; please re-fetch the source tree"
     27 
     28 uname_s=$(uname -s 2>/dev/null)
     29 uname_m=$(uname -m 2>/dev/null)
     30 
     31 # If $PWD is a valid name of the current directory, POSIX mandates that pwd
     32 # return it by default which causes problems in the presence of symlinks.
     33 # Unsetting PWD is simpler than changing every occurrence of pwd to use -P.
     34 #
     35 # XXX Except that doesn't work on Solaris.
     36 unset PWD
     37 if [ "${uname_s}" = "SunOS" ]; then
     38 	TOP=$(pwd -P)
     39 else
     40 	TOP=$(pwd)
     41 fi
     42 
     43 getarch()
     44 {
     45 	# Translate a MACHINE into a default MACHINE_ARCH.
     46 	case $MACHINE in
     47 		acorn26|acorn32|cats|netwinder|shark|*arm)
     48 			MACHINE_ARCH=arm;;
     49 
     50 		sun2)
     51 			MACHINE_ARCH=m68000;;
     52 
     53 		amiga|atari|cesfic|hp300|sun3|*68k)
     54 			MACHINE_ARCH=m68k;;
     55 
     56 		mipsco|newsmips|sbmips|sgimips)
     57 			MACHINE_ARCH=mipseb;;
     58 
     59 		algor|arc|cobalt|evbmips|hpcmips|playstation2|pmax)
     60 			MACHINE_ARCH=mipsel;;
     61 
     62 		pc532)
     63 			MACHINE_ARCH=ns32k;;
     64 
     65 		bebox|prep|sandpoint|*ppc)
     66 			MACHINE_ARCH=powerpc;;
     67 
     68 		evbsh3|mmeye)
     69 			MACHINE_ARCH=sh3eb;;
     70 
     71 		dreamcast|hpcsh)
     72 			MACHINE_ARCH=sh3el;;
     73 
     74 		hp700)
     75 			MACHINE_ARCH=hppa;;
     76 
     77 		evbsh5)
     78 			MACHINE_ARCH=sh5el;;
     79 
     80 		alpha|i386|sparc|sparc64|vax|x86_64)
     81 			MACHINE_ARCH=$MACHINE;;
     82 
     83 		*)	bomb "unknown target MACHINE: $MACHINE";;
     84 	esac
     85 }
     86 
     87 validatearch()
     88 {
     89 	# Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
     90 	case $MACHINE_ARCH in
     91 		alpha|arm|armeb|hppa|i386|m68000|m68k|mipse[bl]|ns32k|powerpc|sh[35]e[bl]|sparc|sparc64|vax|x86_64)
     92 			;;
     93 
     94 		*)	bomb "unknown target MACHINE_ARCH: $MACHINE_ARCH";;
     95 	esac
     96 }
     97 
     98 getmakevar()
     99 {
    100 	$make -m ${TOP}/share/mk -s -f- _x_ <<EOF
    101 _x_:
    102 	echo \${$1}
    103 .include <bsd.prog.mk>
    104 .include <bsd.kernobj.mk>
    105 EOF
    106 }
    107 
    108 resolvepath()
    109 {
    110 	case $OPTARG in
    111 	/*)	;;
    112 	*)	OPTARG="$TOP/$OPTARG";;
    113 	esac
    114 }
    115 
    116 usage()
    117 {
    118 	cat <<_usage_
    119 Usage:
    120 $(basename $0) [-bdEnortUu] [-a arch] [-B buildid] [-D dest] [-i idir] [-j njob]
    121     [-k kernel] [-M obj] [-m mach] [-O obj] [-R release] [-T tools] [-w wrapper]
    122 
    123     -a arch	set MACHINE_ARCH to arch (otherwise deduced from MACHINE)
    124     -B buildid	set BUILDID to buildid
    125     -b		build nbmake and nbmake wrapper script, if needed
    126     -D dest	set DESTDIR to dest
    127     -d		build a full distribution into DESTDIR (including etc files)
    128     -E		set "expert" mode; disables some DESTDIR checks
    129     -i idir	installworld from DESTDIR to idir
    130     -j njob	run up to njob jobs in parallel; see make(1)
    131     -k kernel	build a kernel using the named configuration file
    132     -M obj	set obj root directory to obj (sets MAKEOBJDIRPREFIX)
    133     -m mach	set MACHINE to mach (not required if NetBSD native)
    134     -n		show commands that would be executed, but do not execute them
    135     -O obj	set obj root directory to obj (sets a MAKEOBJDIR pattern)
    136     -o		set MKOBJDIRS=no (do not create objdirs at start of build)
    137     -R release	build a release (and set RELEASEDIR to release)
    138     -r		remove contents of TOOLDIR and DESTDIR before building
    139     -T tools	set TOOLDIR to tools
    140     -t		build and install tools only (implies -b)
    141     -U		set UNPRIVED
    142     -u		set UPDATE
    143     -w wrapper	create nbmake script at wrapper
    144 		(default TOOLDIR/bin/nbmake-MACHINE)
    145 
    146 Note: if -T is unset and TOOLDIR is not set in the environment,
    147       nbmake will be [re]built unconditionally.
    148 _usage_
    149 	exit 1
    150 }
    151 
    152 # Set defaults.
    153 MAKEFLAGS=
    154 buildtarget=build
    155 do_buildsystem=true
    156 do_buildkernel=false
    157 do_buildtools=false
    158 do_rebuildmake=false
    159 do_removedirs=false
    160 expert_mode=false
    161 makeenv=
    162 makewrapper=
    163 installworlddir=
    164 opt_a=no
    165 opts='a:B:bD:dEhi:j:k:M:m:nO:oR:rT:tUuw:'
    166 runcmd=
    167 
    168 if type getopts >/dev/null 2>&1; then
    169 	# Use POSIX getopts.
    170 	getoptcmd='getopts $opts opt && opt=-$opt'
    171 	optargcmd=':'
    172 else
    173 	type getopt >/dev/null 2>&1 ||
    174 	    bomb "/bin/sh shell is too old; try ksh or bash"
    175 
    176 	# Use old-style getopt(1) (doesn't handle whitespace in args).
    177 	args="$(getopt $opts $*)"
    178 	[ $? = 0 ] || usage
    179 	set -- $args
    180 
    181 	getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
    182 	optargcmd='OPTARG="$1"; shift'
    183 fi
    184 
    185 # Parse command line options.
    186 while eval $getoptcmd; do
    187     case $opt in
    188 	-a)	eval $optargcmd
    189 		MACHINE_ARCH=$OPTARG; opt_a=yes;;
    190 
    191 	-B)	eval $optargcmd
    192 		BUILDID=$OPTARG;;
    193 
    194 	-b)	do_buildsystem=false;;
    195 
    196 	-D)	eval $optargcmd; resolvepath
    197 		DESTDIR="$OPTARG"; export DESTDIR
    198 		makeenv="$makeenv DESTDIR";;
    199 
    200 	-d)	buildtarget=distribution;;
    201 
    202 	-E)	expert_mode=true;;
    203 
    204 	-i)	eval $optargcmd
    205 		installworlddir=$OPTARG;;
    206 
    207 	-j)	eval $optargcmd
    208 		parallel="-j $OPTARG";;
    209 
    210 	-k)	do_buildkernel=true; do_buildsystem=false
    211 		eval $optargcmd
    212 		kernconfname=$OPTARG;;
    213 
    214 	-M)	eval $optargcmd; resolvepath
    215 		MAKEOBJDIRPREFIX="$OPTARG"; export MAKEOBJDIRPREFIX
    216 		makeobjdir=$OPTARG
    217 		makeenv="$makeenv MAKEOBJDIRPREFIX";;
    218 
    219 	# -m overrides MACHINE_ARCH unless "-a" is specified
    220 	-m)	eval $optargcmd
    221 		MACHINE=$OPTARG; [ "$opt_a" != "yes" ] && getarch;;
    222 
    223 	-n)	runcmd=echo;;
    224 
    225 	-O)	eval $optargcmd; resolvepath
    226 		MAKEOBJDIR="\${.CURDIR:C,^$TOP,$OPTARG,}"; export MAKEOBJDIR
    227 		makeobjdir=$OPTARG
    228 		makeenv="$makeenv MAKEOBJDIR";;
    229 
    230 	-o)	MKOBJDIRS=no;;
    231 
    232 	-R)	eval $optargcmd; resolvepath
    233 		RELEASEDIR=$OPTARG; export RELEASEDIR
    234 		makeenv="$makeenv RELEASEDIR"
    235 		buildtarget=release;;
    236 
    237 	-r)	do_removedirs=true; do_rebuildmake=true;;
    238 
    239 	-T)	eval $optargcmd; resolvepath
    240 		TOOLDIR="$OPTARG"; export TOOLDIR;;
    241 
    242 	-t)	do_buildtools=true; do_buildsystem=false;;
    243 
    244 	-U)	UNPRIVED=yes; export UNPRIVED
    245 		makeenv="$makeenv UNPRIVED";;
    246 
    247 	-u)	UPDATE=yes; export UPDATE
    248 		makeenv="$makeenv UPDATE";;
    249 
    250 	-w)	eval $optargcmd; resolvepath
    251 		makewrapper="$OPTARG";;
    252 
    253 	--)		break;;
    254 	-'?'|-h)	usage;;
    255     esac
    256 done
    257 
    258 # Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
    259 if [ -z "$MACHINE" ]; then
    260 	if [ "${uname_s}" != "NetBSD" ]; then
    261 		echo "MACHINE must be set, or -m must be used, for cross builds."
    262 		echo ""; usage
    263 	fi
    264 	MACHINE=${uname_m}
    265 fi
    266 [ -n "$MACHINE_ARCH" ] || getarch
    267 validatearch
    268 
    269 # Set up default make(1) environment.
    270 makeenv="$makeenv TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
    271 if [ ! -z "$BUILDID" ]; then
    272 	makeenv="$makeenv BUILDID"
    273 fi
    274 MAKEFLAGS="-m $TOP/share/mk $MAKEFLAGS MKOBJDIRS=${MKOBJDIRS-yes}"
    275 export MAKEFLAGS MACHINE MACHINE_ARCH
    276 
    277 # Test make source file timestamps against installed nbmake binary,
    278 # if TOOLDIR is pre-set.
    279 #
    280 # Note that we do NOT try to grovel "mk.conf" here to find out if TOOLDIR
    281 # is set there, because it can contain make variable expansions and other
    282 # stuff only parseable *after* we have a working nbmake.  So this logic
    283 # can only work if the user has pre-set TOOLDIR in the environment or
    284 # used the -T option to build.sh.
    285 #
    286 make="${TOOLDIR-nonexistent}/bin/nbmake"
    287 if [ -x $make ]; then
    288 	for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
    289 		if [ $f -nt $make ]; then
    290 			do_rebuildmake=true; break
    291 		fi
    292 	done
    293 else
    294 	do_rebuildmake=true
    295 fi
    296 
    297 # Build bootstrap nbmake if needed.
    298 if $do_rebuildmake; then
    299 	$runcmd echo "===> Bootstrapping nbmake"
    300 	tmpdir="${TMPDIR-/tmp}/nbbuild$$"
    301 
    302 	$runcmd mkdir "$tmpdir" || bomb "cannot mkdir: $tmpdir"
    303 	trap "cd /; rm -r -f \"$tmpdir\"" 0
    304 	trap "exit 1" 1 2 3 15
    305 	$runcmd cd "$tmpdir"
    306 
    307 	$runcmd env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
    308 		CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
    309 		"$TOP/tools/make/configure" \
    310 		|| bomb "configure of nbmake failed"
    311 	$runcmd sh buildmake.sh || bomb "build of nbmake failed"
    312 
    313 	make="$tmpdir/nbmake"
    314 	$runcmd cd "$TOP"
    315 	$runcmd rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
    316 fi
    317 
    318 EXTERNAL_TOOLCHAIN=$(getmakevar EXTERNAL_TOOLCHAIN)
    319 if [ "$runcmd" = "echo" ]; then
    320 	TOOLCHAIN_MISSING=no
    321 else
    322 	TOOLCHAIN_MISSING=$(getmakevar TOOLCHAIN_MISSING)
    323 fi
    324 if [ "${TOOLCHAIN_MISSING}" = "yes" -a \
    325      "${EXTERNAL_TOOLCHAIN}" = "" ]; then
    326 	echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
    327 	echo
    328 	echo "MACHINE: ${MACHINE}"
    329 	echo "MACHINE_ARCH: ${MACHINE_ARCH}"
    330 	echo
    331 	echo "All builds for this platform should be done via a traditional make"
    332 	echo
    333 	echo "If you wish to use an external cross-toolchain, set"
    334 	echo
    335 	echo "EXTERNAL_TOOLCHAIN=<path to toolchain root>"
    336 	echo
    337 	echo "in either the environment or mk.conf and rerun"
    338 	echo
    339 	echo "$0 $*"
    340 	exit 1
    341 fi
    342 
    343 # If TOOLDIR isn't already set, make objdirs in "tools" in case the
    344 # default setting from <bsd.own.mk> is used.
    345 if [ -z "$TOOLDIR" ] && [ "$MKOBJDIRS" != "no" ]; then
    346 	$runcmd cd tools
    347 	$runcmd $make -m ${TOP}/share/mk obj NOSUBDIR= \
    348 		|| bomb "make obj failed in tools"
    349 	$runcmd cd "$TOP"
    350 fi
    351 
    352 #
    353 # If setting -M or -O to root an obj dir make sure the base directory is made
    354 # before continuing as bsd.own.mk will need this to pick up _SRC_TOP_OBJ_
    355 #
    356 if [ "$MKOBJDIRS" != "no" ] && [ ! -z "$makeobjdir" ]; then
    357 	$runcmd mkdir -p "$makeobjdir"
    358 fi
    359 
    360 # Find DESTDIR and TOOLDIR.
    361 if [ "$runcmd" = "echo" ]; then
    362 	# shown symbolically with -n because these may come from mk.conf
    363 	DESTDIR='$DESTDIR'
    364 	TOOLDIR='$TOOLDIR'
    365 else
    366 	DESTDIR=$(getmakevar DESTDIR)
    367 	[ $? = 0 ] || bomb "getmakevar DESTDIR failed"
    368 	$runcmd echo "===> DESTDIR path: $DESTDIR"
    369 
    370 	TOOLDIR=$(getmakevar TOOLDIR)
    371 	[ $? = 0 ] || bomb "getmakevar TOOLDIR failed"
    372 	$runcmd echo "===> TOOLDIR path: $TOOLDIR"
    373 
    374 	export DESTDIR TOOLDIR
    375 fi
    376 
    377 # Check validity of TOOLDIR and DESTDIR.
    378 if [ -z "$TOOLDIR" ] || [ "$TOOLDIR" = "/" ]; then
    379 	bomb "TOOLDIR '$TOOLDIR' invalid"
    380 fi
    381 removedirs="$TOOLDIR"
    382 
    383 if [ -z "$DESTDIR" ] || [ "$DESTDIR" = "/" ]; then
    384 	if $do_buildsystem; then
    385 		if [ "$buildtarget" != "build" ] || \
    386 		   [ "${uname_s}" != "NetBSD" ] || \
    387 		   [ "${uname_m}" != "$MACHINE" ]; then
    388 			bomb "DESTDIR must be set to a non-root path for cross builds or -d or -R."
    389 		fi
    390 		if ! $expert_mode; then
    391 			bomb "DESTDIR must be set to a non-root path for non -E (expert) builds"
    392 		fi
    393 		$runcmd echo "===> WARNING: Building to /, in expert mode."
    394 		$runcmd echo "===>          This may cause your system to break!  Reasons include:"
    395 		$runcmd echo "===>             - your kernel is not up to date"
    396 		$runcmd echo "===>             - the libraries or toolchain have changed"
    397 		$runcmd echo "===>          YOU HAVE BEEN WARNED!"
    398 	fi
    399 else
    400 	removedirs="$removedirs $DESTDIR"
    401 fi
    402 
    403 if [ "$installworlddir" = "/" ]; then
    404 	if [ "${uname_s}" != "NetBSD" ] || \
    405 	   [ "${uname_m}" != "$MACHINE" ]; then
    406 		bomb "-i installworlddir must be set to a non-root path for cross builds."
    407 	fi
    408 fi
    409 
    410 # Remove the target directories.
    411 if $do_removedirs; then
    412 	for f in $removedirs; do
    413 		$runcmd echo "===> Removing $f"
    414 		$runcmd rm -r -f $f
    415 	done
    416 fi
    417 
    418 # Recreate $TOOLDIR.
    419 $runcmd mkdir -p "$TOOLDIR/bin" || bomb "mkdir of '$TOOLDIR/bin' failed"
    420 
    421 # Install nbmake if it was built.
    422 if $do_rebuildmake; then
    423 	$runcmd rm -f "$TOOLDIR/bin/nbmake"
    424 	$runcmd cp $make "$TOOLDIR/bin/nbmake" \
    425 		|| bomb "failed to install \$TOOLDIR/bin/nbmake"
    426 	make="$TOOLDIR/bin/nbmake"
    427 	$runcmd rm -r -f "$tmpdir"
    428 	trap 0 1 2 3 15
    429 fi
    430 
    431 # Build a nbmake wrapper script, usable by hand as well as by build.sh.
    432 if [ -z "$makewrapper" ]; then
    433 	makewrapper="$TOOLDIR/bin/nbmake-$MACHINE"
    434 	if [ ! -z "$BUILDID" ]; then
    435 		makewrapper="$makewrapper-$BUILDID"
    436 	fi
    437 fi
    438 
    439 $runcmd rm -f "$makewrapper"
    440 if [ "$runcmd" = "echo" ]; then
    441 	echo 'cat <<EOF >'$makewrapper
    442 	makewrapout=
    443 else
    444 	makewrapout=">>\$makewrapper"
    445 fi
    446 
    447 eval cat <<EOF $makewrapout
    448 #! /bin/sh
    449 # Set proper variables to allow easy "make" building of a NetBSD subtree.
    450 # Generated from:  \$NetBSD: build.sh,v 1.79 2003/01/02 23:11:07 lukem Exp $
    451 #
    452 
    453 EOF
    454 for f in $makeenv; do
    455 	eval echo "$f=\'\$$(echo $f)\'\;\ export\ $f" $makewrapout
    456 done
    457 eval echo "USETOOLS=yes\; export USETOOLS" $makewrapout
    458 
    459 eval cat <<'EOF' $makewrapout
    460 
    461 exec "$TOOLDIR/bin/nbmake" ${1+"$@"}
    462 EOF
    463 [ "$runcmd" = "echo" ] && echo EOF
    464 $runcmd chmod +x "$makewrapper"
    465 
    466 if $do_buildsystem; then
    467 	# Build everything.
    468 	${runcmd-exec} "$makewrapper" $parallel $buildtarget \
    469 		|| bomb "failed to make $buildtarget"
    470 else
    471 	# One or more of do_buildtools and do_buildkernel
    472 	# might be set.  Do them in the appropriate order.
    473 	if $do_buildtools; then
    474 		if [ "$MKOBJDIRS" != "no" ]; then
    475 			$runcmd "$makewrapper" $parallel obj-tools \
    476 				|| bomb "failed to make obj-tools"
    477 		fi
    478 		$runcmd cd tools
    479 		if [ "$UPDATE" = "" ]; then
    480 			$runcmd "$makewrapper" cleandir dependall install \
    481 				|| bomb "failed to make tools"
    482 		else
    483 			$runcmd "$makewrapper" dependall install \
    484 				|| bomb "failed to make tools"
    485 		fi
    486 	fi
    487 	if $do_buildkernel; then
    488 		if ! $do_buildtools; then
    489 			# Building tools every time we build a kernel
    490 			# is clearly unnecessary.  We could try to
    491 			# figure out whether rebuilding the tools is
    492 			# necessary this time, but it doesn't seem
    493 			# worth the trouble.  Instead, we say it's the
    494 			# user's responsibility to rebuild the tools if
    495 			# necessary.
    496 			$runcmd echo "===> Building kernel" \
    497 				"without building new tools"
    498 		fi
    499 		$runcmd echo "===> Building kernel ${kernconfname}"
    500 		# The correct value of KERNOBJDIR might depend on a
    501 		# prior "make obj" in TOP/etc.
    502 		if [ "$MKOBJDIRS" != "no" ] && [ ! -z "$makeobjdir" ]; then
    503 			$runcmd cd "$TOP/etc"
    504 			$runcmd "$makewrapper" obj \
    505 				|| bomb "failed to make obj in etc"
    506 			$runcmd cd "$TOP"
    507 		fi
    508 		if [ "$runcmd" = "echo" ]; then
    509 			# shown symbolically with -n
    510 			# because getmakevar might not work yet
    511 			KERNCONFDIR='$KERNCONFDIR'
    512 			KERNOBJDIR='$KERNOBJDIR'
    513 		else
    514 			KERNCONFDIR="$( getmakevar KERNCONFDIR )"
    515 			[ $? = 0 ] || bomb "getmakevar KERNCONFDIR failed"
    516 			KERNOBJDIR="$( getmakevar KERNOBJDIR )"
    517 			[ $? = 0 ] || bomb "getmakevar KERNOBJDIR failed"
    518 		fi
    519 		case "${kernconfname}" in
    520 		*/*)
    521 			kernconfpath="${kernconfname}"
    522 			kernconfbase="$( basename "${kernconfname}" )"
    523 			;;
    524 		*)
    525 			kernconfpath="${KERNCONFDIR}/${kernconfname}"
    526 			kernconfbase="${kernconfname}"
    527 			;;
    528 		esac
    529 		kernbuilddir="${KERNOBJDIR}/${kernconfbase}"
    530 		$runcmd echo "===> Kernel build directory: ${kernbuilddir}"
    531 		$runcmd mkdir -p "${kernbuilddir}" \
    532 			|| bomb "cannot mkdir: ${kernbuilddir}"
    533 		if [ "$UPDATE" = "" ]; then
    534 			$runcmd cd "${kernbuilddir}"
    535 			$runcmd "$makewrapper" cleandir \
    536 				|| bomb "make cleandir failed in " \
    537 					"${kernbuilddir}"
    538 			$runcmd cd "$TOP"
    539 		fi
    540 		$runcmd "${TOOLDIR}/bin/nbconfig" \
    541 			-b "${kernbuilddir}" \
    542 			-s "${TOP}/sys" "${kernconfpath}" \
    543 			|| bomb "nbconfig failed for ${kernconfname}"
    544 		$runcmd cd "${kernbuilddir}"
    545 		$runcmd "$makewrapper" depend \
    546 			|| bomb "make depend failed in ${kernbuilddir}"
    547 		$runcmd "$makewrapper" $parallel all \
    548 			|| bomb "make all failed in ${kernbuilddir}"
    549 		$runcmd echo "===> New kernel should be in ${kernbuilddir}"
    550 	fi
    551 fi
    552 
    553 if [ -n "$installworlddir" ]; then
    554 	${runcmd-exec} "$makewrapper" INSTALLWORLDDIR=${installworlddir} \
    555 		installworld || bomb "failed to make installworld"
    556 fi
    557