[BACK]Return to build.sh CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src

Annotation of src/build.sh, Revision 1.237

1.67      thorpej     1: #! /usr/bin/env sh
1.237   ! pooka       2: #      $NetBSD: build.sh,v 1.236 2010/05/27 05:41:54 pgoyette Exp $
1.84      lukem       3: #
1.203     lukem       4: # Copyright (c) 2001-2009 The NetBSD Foundation, Inc.
1.84      lukem       5: # All rights reserved.
                      6: #
                      7: # This code is derived from software contributed to The NetBSD Foundation
                      8: # by Todd Vierling and Luke Mewburn.
                      9: #
                     10: # Redistribution and use in source and binary forms, with or without
                     11: # modification, are permitted provided that the following conditions
                     12: # are met:
                     13: # 1. Redistributions of source code must retain the above copyright
                     14: #    notice, this list of conditions and the following disclaimer.
                     15: # 2. Redistributions in binary form must reproduce the above copyright
                     16: #    notice, this list of conditions and the following disclaimer in the
                     17: #    documentation and/or other materials provided with the distribution.
                     18: #
                     19: # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     20: # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     21: # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     22: # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     23: # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     24: # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     25: # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     26: # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     27: # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     28: # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     29: # POSSIBILITY OF SUCH DAMAGE.
                     30: #
1.1       tv         31: #
                     32: # Top level build wrapper, for a system containing no tools.
                     33: #
1.153     apb        34: # This script should run on any POSIX-compliant shell.  If the
                     35: # first "sh" found in the PATH is a POSIX-compliant shell, then
                     36: # you should not need to take any special action.  Otherwise, you
                     37: # should set the environment variable HOST_SH to a POSIX-compliant
                     38: # shell, and invoke build.sh with that shell.  (Depending on your
                     39: # system, one of /bin/ksh, /usr/local/bin/bash, or /usr/xpg4/bin/sh
                     40: # might be a suitable shell.)
1.67      thorpej    41: #
1.1       tv         42:
1.84      lukem      43: progname=${0##*/}
                     44: toppid=$$
1.98      lukem      45: results=/dev/null
1.200     apb        46: tab='  '
1.82      lukem      47: trap "exit 1" 1 2 3 15
1.84      lukem      48:
1.79      lukem      49: bomb()
                     50: {
1.82      lukem      51:        cat >&2 <<ERRORMESSAGE
                     52:
                     53: ERROR: $@
                     54: *** BUILD ABORTED ***
                     55: ERRORMESSAGE
1.98      lukem      56:        kill ${toppid}          # in case we were invoked from a subshell
1.1       tv         57:        exit 1
                     58: }
1.78      lukem      59:
1.84      lukem      60:
1.98      lukem      61: statusmsg()
                     62: {
                     63:        ${runcmd} echo "===> $@" | tee -a "${results}"
                     64: }
                     65:
1.163     apb        66: warning()
                     67: {
                     68:        statusmsg "Warning: $@"
                     69: }
                     70:
1.168     apb        71: # Find a program in the PATH, and print the result.  If not found,
                     72: # print a default.  If $2 is defined (even if it is an empty string),
                     73: # then that is the default; otherwise, $1 is used as the default.
1.153     apb        74: find_in_PATH()
                     75: {
                     76:        local prog="$1"
1.168     apb        77:        local result="${2-"$1"}"
1.153     apb        78:        local oldIFS="${IFS}"
                     79:        local dir
                     80:        IFS=":"
                     81:        for dir in ${PATH}; do
                     82:                if [ -x "${dir}/${prog}" ]; then
1.168     apb        83:                        result="${dir}/${prog}"
1.153     apb        84:                        break
                     85:                fi
                     86:        done
                     87:        IFS="${oldIFS}"
1.168     apb        88:        echo "${result}"
1.153     apb        89: }
                     90:
                     91: # Try to find a working POSIX shell, and set HOST_SH to refer to it.
                     92: # Assumes that uname_s, uname_m, and PWD have been set.
                     93: set_HOST_SH()
                     94: {
                     95:        # Even if ${HOST_SH} is already defined, we still do the
                     96:        # sanity checks at the end.
                     97:
                     98:        # Solaris has /usr/xpg4/bin/sh.
                     99:        #
                    100:        [ -z "${HOST_SH}" ] && [ x"${uname_s}" = x"SunOS" ] && \
                    101:                [ -x /usr/xpg4/bin/sh ] && HOST_SH="/usr/xpg4/bin/sh"
                    102:
                    103:        # Try to get the name of the shell that's running this script,
                    104:        # by parsing the output from "ps".  We assume that, if the host
                    105:        # system's ps command supports -o comm at all, it will do so
                    106:        # in the usual way: a one-line header followed by a one-line
                    107:        # result, possibly including trailing white space.  And if the
                    108:        # host system's ps command doesn't support -o comm, we assume
                    109:        # that we'll get an error message on stderr and nothing on
                    110:        # stdout.  (We don't try to use ps -o 'comm=' to suppress the
                    111:        # header line, because that is less widely supported.)
                    112:        #
                    113:        # If we get the wrong result here, the user can override it by
                    114:        # specifying HOST_SH in the environment.
                    115:        #
                    116:        [ -z "${HOST_SH}" ] && HOST_SH="$(
1.200     apb       117:                (ps -p $$ -o comm | sed -ne "2s/[ ${tab}]*\$//p") 2>/dev/null )"
1.153     apb       118:
                    119:        # If nothing above worked, use "sh".  We will later find the
                    120:        # first directory in the PATH that has a "sh" program.
                    121:        #
                    122:        [ -z "${HOST_SH}" ] && HOST_SH="sh"
                    123:
                    124:        # If the result so far is not an absolute path, try to prepend
                    125:        # PWD or search the PATH.
                    126:        #
                    127:        case "${HOST_SH}" in
                    128:        /*)     :
                    129:                ;;
                    130:        */*)    HOST_SH="${PWD}/${HOST_SH}"
                    131:                ;;
                    132:        *)      HOST_SH="$(find_in_PATH "${HOST_SH}")"
                    133:                ;;
                    134:        esac
                    135:
                    136:        # If we don't have an absolute path by now, bomb.
                    137:        #
                    138:        case "${HOST_SH}" in
                    139:        /*)     :
                    140:                ;;
                    141:        *)      bomb "HOST_SH=\"${HOST_SH}\" is not an absolute path."
                    142:                ;;
                    143:        esac
                    144:
                    145:        # If HOST_SH is not executable, bomb.
                    146:        #
                    147:        [ -x "${HOST_SH}" ] ||
                    148:            bomb "HOST_SH=\"${HOST_SH}\" is not executable."
                    149: }
                    150:
1.84      lukem     151: initdefaults()
                    152: {
1.171     apb       153:        makeenv=
                    154:        makewrapper=
                    155:        makewrappermachine=
                    156:        runcmd=
                    157:        operations=
                    158:        removedirs=
                    159:
1.156     dsl       160:        [ -d usr.bin/make ] || cd "$(dirname $0)"
1.84      lukem     161:        [ -d usr.bin/make ] ||
                    162:            bomb "build.sh must be run from the top source level"
                    163:        [ -f share/mk/bsd.own.mk ] ||
                    164:            bomb "src/share/mk is missing; please re-fetch the source tree"
                    165:
1.218     apb       166:        # Set LC_ALL=C before we try to parse the output from any command
                    167:        setmakeenv LC_ALL C
                    168:
1.204     apb       169:        # Find information about the build platform.  This should be
                    170:        # kept in sync with _HOST_OSNAME, _HOST_OSREL, and _HOST_ARCH
                    171:        # variables in share/mk/bsd.sys.mk.
                    172:        #
                    173:        # Note that "uname -p" is not part of POSIX, but we want uname_p
                    174:        # to be set to the host MACHINE_ARCH, if possible.  On systems
                    175:        # where "uname -p" fails, prints "unknown", or prints a string
                    176:        # that does not look like an identifier, fall back to using the
                    177:        # output from "uname -m" instead.
1.165     apb       178:        #
1.84      lukem     179:        uname_s=$(uname -s 2>/dev/null)
1.165     apb       180:        uname_r=$(uname -r 2>/dev/null)
1.84      lukem     181:        uname_m=$(uname -m 2>/dev/null)
1.204     apb       182:        uname_p=$(uname -p 2>/dev/null || echo "unknown")
                    183:        case "${uname_p}" in
1.205     apb       184:        ''|unknown|*[^-_A-Za-z0-9]*) uname_p="${uname_m}" ;;
1.204     apb       185:        esac
1.84      lukem     186:
1.202     sketch    187:        id_u=$(id -u 2>/dev/null || /usr/xpg4/bin/id -u 2>/dev/null)
                    188:
1.84      lukem     189:        # If $PWD is a valid name of the current directory, POSIX mandates
                    190:        # that pwd return it by default which causes problems in the
                    191:        # presence of symlinks.  Unsetting PWD is simpler than changing
                    192:        # every occurrence of pwd to use -P.
                    193:        #
1.147     dogcow    194:        # XXX Except that doesn't work on Solaris. Or many Linuces.
1.98      lukem     195:        #
1.84      lukem     196:        unset PWD
1.147     dogcow    197:        TOP=$(/bin/pwd -P 2>/dev/null || /bin/pwd 2>/dev/null)
1.84      lukem     198:
1.153     apb       199:        # The user can set HOST_SH in the environment, or we try to
                    200:        # guess an appropriate value.  Then we set several other
                    201:        # variables from HOST_SH.
                    202:        #
                    203:        set_HOST_SH
                    204:        setmakeenv HOST_SH "${HOST_SH}"
                    205:        setmakeenv BSHELL "${HOST_SH}"
                    206:        setmakeenv CONFIG_SHELL "${HOST_SH}"
                    207:
1.84      lukem     208:        # Set defaults.
1.98      lukem     209:        #
1.84      lukem     210:        toolprefix=nb
1.98      lukem     211:
1.95      thorpej   212:        # Some systems have a small ARG_MAX.  -X prevents make(1) from
                    213:        # exporting variables in the environment redundantly.
1.98      lukem     214:        #
1.95      thorpej   215:        case "${uname_s}" in
1.97      christos  216:        Darwin | FreeBSD | CYGWIN*)
1.95      thorpej   217:                MAKEFLAGS=-X
                    218:                ;;
                    219:        *)
                    220:                MAKEFLAGS=
                    221:                ;;
                    222:        esac
1.98      lukem     223:
1.171     apb       224:        # do_{operation}=true if given operation is requested.
                    225:        #
1.84      lukem     226:        do_expertmode=false
                    227:        do_rebuildmake=false
                    228:        do_removedirs=false
                    229:        do_tools=false
1.195     lukem     230:        do_cleandir=false
1.84      lukem     231:        do_obj=false
                    232:        do_build=false
                    233:        do_distribution=false
                    234:        do_release=false
                    235:        do_kernel=false
1.105     lukem     236:        do_releasekernel=false
1.207     jnemeth   237:        do_modules=false
1.84      lukem     238:        do_install=false
1.87      lukem     239:        do_sets=false
1.100     lukem     240:        do_sourcesets=false
1.142     apb       241:        do_syspkgs=false
1.146     apb       242:        do_iso_image=false
1.172     jnemeth   243:        do_iso_image_source=false
1.107     lukem     244:        do_params=false
1.219     pooka     245:        do_rump=false
1.98      lukem     246:
1.211     apb       247:        # done_{operation}=true if given operation has been done.
                    248:        #
                    249:        done_rebuildmake=false
                    250:
1.98      lukem     251:        # Create scratch directory
                    252:        #
                    253:        tmpdir="${TMPDIR-/tmp}/nbbuild$$"
                    254:        mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
                    255:        trap "cd /; rm -r -f \"${tmpdir}\"" 0
                    256:        results="${tmpdir}/build.sh.results"
1.116     jmmv      257:
                    258:        # Set source directories
                    259:        #
                    260:        setmakeenv NETBSDSRCDIR "${TOP}"
1.136     lukem     261:
1.233     cegger    262:        # Make sure KERNOBJDIR is an absolute path if defined
                    263:        #
                    264:        case "${KERNOBJDIR}" in
                    265:        ''|/*)  ;;
                    266:        *)      KERNOBJDIR="${TOP}/${KERNOBJDIR}"
                    267:                setmakeenv KERNOBJDIR "${KERNOBJDIR}"
                    268:                ;;
                    269:        esac
                    270:
1.165     apb       271:        # Find the version of NetBSD
                    272:        #
                    273:        DISTRIBVER="$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh)"
                    274:
1.190     perry     275:        # Set the BUILDSEED to NetBSD-"N"
                    276:        #
                    277:        setmakeenv BUILDSEED "NetBSD-$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh -m)"
                    278:
1.206     perry     279:        # Set MKARZERO to "yes"
                    280:        #
                    281:        setmakeenv MKARZERO "yes"
                    282:
1.136     lukem     283:        # Set various environment variables to known defaults,
                    284:        # to minimize (cross-)build problems observed "in the field".
                    285:        #
                    286:        unsetmakeenv INFODIR
1.140     jmc       287:        unsetmakeenv LESSCHARSET
1.84      lukem     288: }
1.29      jmc       289:
1.79      lukem     290: getarch()
                    291: {
1.158     apb       292:        # Translate some MACHINE name aliases (known only to build.sh)
                    293:        # into proper MACHINE and MACHINE_ARCH names.  Save the alias
                    294:        # name in makewrappermachine.
                    295:        #
                    296:        case "${MACHINE}" in
                    297:
                    298:        evbarm-e[bl])
                    299:                makewrappermachine=${MACHINE}
                    300:                # MACHINE_ARCH is "arm" or "armeb", not "armel"
                    301:                MACHINE_ARCH=arm${MACHINE##*-}
                    302:                MACHINE_ARCH=${MACHINE_ARCH%el}
                    303:                MACHINE=${MACHINE%-e[bl]}
                    304:                ;;
                    305:
                    306:        evbmips-e[bl]|sbmips-e[bl])
                    307:                makewrappermachine=${MACHINE}
                    308:                MACHINE_ARCH=mips${MACHINE##*-}
                    309:                MACHINE=${MACHINE%-e[bl]}
                    310:                ;;
                    311:
                    312:        evbmips64-e[bl]|sbmips64-e[bl])
                    313:                makewrappermachine=${MACHINE}
                    314:                MACHINE_ARCH=mips64${MACHINE##*-}
                    315:                MACHINE=${MACHINE%64-e[bl]}
                    316:                ;;
                    317:
                    318:        evbsh3-e[bl])
                    319:                makewrappermachine=${MACHINE}
                    320:                MACHINE_ARCH=sh3${MACHINE##*-}
                    321:                MACHINE=${MACHINE%-e[bl]}
                    322:                ;;
                    323:
                    324:        esac
                    325:
1.1       tv        326:        # Translate a MACHINE into a default MACHINE_ARCH.
1.84      lukem     327:        #
1.98      lukem     328:        case "${MACHINE}" in
1.1       tv        329:
1.162     briggs    330:        acorn26|acorn32|cats|hpcarm|iyonix|netwinder|shark|zaurus)
1.84      lukem     331:                MACHINE_ARCH=arm
                    332:                ;;
                    333:
1.162     briggs    334:        evbarm)         # unspecified MACHINE_ARCH gets LE
                    335:                MACHINE_ARCH=${MACHINE_ARCH:=arm}
                    336:                ;;
                    337:
1.98      lukem     338:        hp700)
                    339:                MACHINE_ARCH=hppa
                    340:                ;;
                    341:
1.84      lukem     342:        sun2)
                    343:                MACHINE_ARCH=m68000
                    344:                ;;
                    345:
1.98      lukem     346:        amiga|atari|cesfic|hp300|luna68k|mac68k|mvme68k|news68k|next68k|sun3|x68k)
1.84      lukem     347:                MACHINE_ARCH=m68k
                    348:                ;;
1.1       tv        349:
1.101     lukem     350:        evbmips|sbmips)         # no default MACHINE_ARCH
                    351:                ;;
                    352:
1.224     matt      353:        sgimips64)
                    354:                makewrappermachine=${MACHINE}
                    355:                MACHINE=${MACHINE%64}
                    356:                MACHINE_ARCH=mips64eb
                    357:                ;;
                    358:
1.141     tsutsui   359:        ews4800mips|mipsco|newsmips|sgimips)
1.84      lukem     360:                MACHINE_ARCH=mipseb
                    361:                ;;
1.1       tv        362:
1.225     matt      363:        algor64|cobalt64|pmax64)
1.224     matt      364:                makewrappermachine=${MACHINE}
                    365:                MACHINE=${MACHINE%64}
                    366:                MACHINE_ARCH=mips64el
                    367:                ;;
                    368:
1.223     pooka     369:        algor|arc|cobalt|hpcmips|pmax)
1.84      lukem     370:                MACHINE_ARCH=mipsel
                    371:                ;;
1.1       tv        372:
1.183     jmmv      373:        evbppc64|macppc64|ofppc64)
1.160     matt      374:                makewrappermachine=${MACHINE}
                    375:                MACHINE=${MACHINE%64}
                    376:                MACHINE_ARCH=powerpc64
                    377:                ;;
                    378:
1.181     garbled   379:        amigappc|bebox|evbppc|ibmnws|macppc|mvmeppc|ofppc|prep|rs6000|sandpoint)
1.84      lukem     380:                MACHINE_ARCH=powerpc
                    381:                ;;
1.1       tv        382:
1.103     lukem     383:        evbsh3)                 # no default MACHINE_ARCH
                    384:                ;;
                    385:
                    386:        mmeye)
1.84      lukem     387:                MACHINE_ARCH=sh3eb
                    388:                ;;
1.1       tv        389:
1.152     uwe       390:        dreamcast|hpcsh|landisk)
1.84      lukem     391:                MACHINE_ARCH=sh3el
                    392:                ;;
1.1       tv        393:
1.96      fvdl      394:        amd64)
                    395:                MACHINE_ARCH=x86_64
                    396:                ;;
1.62      fredette  397:
1.138     skrll     398:        alpha|i386|sparc|sparc64|vax|ia64)
1.98      lukem     399:                MACHINE_ARCH=${MACHINE}
1.84      lukem     400:                ;;
1.69      thorpej   401:
1.84      lukem     402:        *)
1.98      lukem     403:                bomb "Unknown target MACHINE: ${MACHINE}"
1.84      lukem     404:                ;;
1.4       tv        405:
1.1       tv        406:        esac
                    407: }
                    408:
1.79      lukem     409: validatearch()
                    410: {
1.47      tv        411:        # Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
1.84      lukem     412:        #
1.98      lukem     413:        case "${MACHINE_ARCH}" in
1.47      tv        414:
1.184     matt      415:        alpha|arm|armeb|hppa|i386|m68000|m68k|mipse[bl]|mips64e[bl]|powerpc|powerpc64|sh3e[bl]|sparc|sparc64|vax|x86_64|ia64)
1.84      lukem     416:                ;;
                    417:
1.101     lukem     418:        "")
                    419:                bomb "No MACHINE_ARCH provided"
                    420:                ;;
                    421:
1.84      lukem     422:        *)
1.98      lukem     423:                bomb "Unknown target MACHINE_ARCH: ${MACHINE_ARCH}"
                    424:                ;;
                    425:
                    426:        esac
                    427:
                    428:        # Determine valid MACHINE_ARCHs for MACHINE
                    429:        #
                    430:        case "${MACHINE}" in
                    431:
                    432:        evbarm)
                    433:                arches="arm armeb"
                    434:                ;;
                    435:
1.225     matt      436:        algor|cobalt|pmax)
1.224     matt      437:                arches="mipsel mips64el"
                    438:                ;;
                    439:
1.98      lukem     440:        evbmips|sbmips)
1.150     matt      441:                arches="mipseb mipsel mips64eb mips64el"
                    442:                ;;
                    443:
                    444:        sgimips)
                    445:                arches="mipseb mips64eb"
1.98      lukem     446:                ;;
                    447:
                    448:        evbsh3)
                    449:                arches="sh3eb sh3el"
                    450:                ;;
                    451:
1.183     jmmv      452:        macppc|evbppc|ofppc)
1.148     mrg       453:                arches="powerpc powerpc64"
                    454:                ;;
1.98      lukem     455:        *)
                    456:                oma="${MACHINE_ARCH}"
                    457:                getarch
                    458:                arches="${MACHINE_ARCH}"
                    459:                MACHINE_ARCH="${oma}"
1.84      lukem     460:                ;;
                    461:
1.47      tv        462:        esac
1.98      lukem     463:
                    464:        # Ensure that MACHINE_ARCH supports MACHINE
                    465:        #
                    466:        archok=false
                    467:        for a in ${arches}; do
                    468:                if [ "${a}" = "${MACHINE_ARCH}" ]; then
                    469:                        archok=true
                    470:                        break
                    471:                fi
                    472:        done
                    473:        ${archok} ||
                    474:            bomb "MACHINE_ARCH '${MACHINE_ARCH}' does not support MACHINE '${MACHINE}'"
1.47      tv        475: }
                    476:
1.210     apb       477: # nobomb_getmakevar --
                    478: # Given the name of a make variable in $1, print make's idea of the
                    479: # value of that variable, or return 1 if there's an error.
                    480: #
1.168     apb       481: nobomb_getmakevar()
1.79      lukem     482: {
1.168     apb       483:        [ -x "${make}" ] || return 1
                    484:        "${make}" -m ${TOP}/share/mk -s -B -f- _x_ <<EOF || return 1
1.15      tv        485: _x_:
                    486:        echo \${$1}
                    487: .include <bsd.prog.mk>
1.70      lukem     488: .include <bsd.kernobj.mk>
1.15      tv        489: EOF
                    490: }
                    491:
1.210     apb       492: # nobomb_getmakevar --
                    493: # Given the name of a make variable in $1, print make's idea of the
                    494: # value of that variable, or bomb if there's an error.
                    495: #
                    496: bomb_getmakevar()
1.168     apb       497: {
1.210     apb       498:        [ -x "${make}" ] || bomb "bomb_getmakevar $1: ${make} is not executable"
                    499:        nobomb_getmakevar "$1" || bomb "bomb_getmakevar $1: ${make} failed"
1.168     apb       500: }
                    501:
1.210     apb       502: # nobomb_getmakevar --
                    503: # Given the name of a make variable in $1, print make's idea of the
                    504: # value of that variable, or print a literal '$' followed by the
                    505: # variable name if ${make} is not executable.  This is intended for use in
                    506: # messages that need to be readable even if $make hasn't been built,
                    507: # such as when build.sh is run with the "-n" option.
                    508: #
1.98      lukem     509: getmakevar()
1.82      lukem     510: {
1.98      lukem     511:        if [ -x "${make}" ]; then
1.210     apb       512:                bomb_getmakevar "$1"
1.82      lukem     513:        else
                    514:                echo "\$$1"
                    515:        fi
                    516: }
                    517:
1.109     lukem     518: setmakeenv()
                    519: {
                    520:        eval "$1='$2'; export $1"
                    521:        makeenv="${makeenv} $1"
                    522: }
                    523:
1.111     lukem     524: unsetmakeenv()
                    525: {
                    526:        eval "unset $1"
                    527:        makeenv="${makeenv} $1"
                    528: }
                    529:
1.208     apb       530: # Given a variable name in $1, modify the variable in place as follows:
                    531: # For each space-separated word in the variable, call resolvepath.
1.189     dyoung    532: resolvepaths()
                    533: {
1.208     apb       534:        local var="$1"
                    535:        local val
                    536:        eval val=\"\${${var}}\"
                    537:        local newval=''
                    538:        local word
                    539:        for word in ${val}; do
                    540:                resolvepath word
                    541:                newval="${newval}${newval:+ }${word}"
1.189     dyoung    542:        done
1.208     apb       543:        eval ${var}=\"\${newval}\"
1.189     dyoung    544: }
                    545:
1.208     apb       546: # Given a variable name in $1, modify the variable in place as follows:
1.131     junyoung  547: # Convert possibly-relative path to absolute path by prepending
                    548: # ${TOP} if necessary.  Also delete trailing "/", if any.
1.79      lukem     549: resolvepath()
                    550: {
1.208     apb       551:        local var="$1"
                    552:        local val
                    553:        eval val=\"\${${var}}\"
                    554:        case "${val}" in
1.131     junyoung  555:        /)
                    556:                ;;
1.84      lukem     557:        /*)
1.208     apb       558:                val="${val%/}"
1.84      lukem     559:                ;;
                    560:        *)
1.208     apb       561:                val="${TOP}/${val%/}"
1.84      lukem     562:                ;;
1.10      tv        563:        esac
1.208     apb       564:        eval ${var}=\"\${val}\"
1.10      tv        565: }
                    566:
1.79      lukem     567: usage()
                    568: {
1.84      lukem     569:        if [ -n "$*" ]; then
                    570:                echo ""
                    571:                echo "${progname}: $*"
                    572:        fi
1.70      lukem     573:        cat <<_usage_
1.83      lukem     574:
1.190     perry     575: Usage: ${progname} [-EnorUux] [-a arch] [-B buildid] [-C cdextras]
1.195     lukem     576:                 [-D dest] [-j njob] [-M obj] [-m mach] [-N noisy]
                    577:                 [-O obj] [-R release] [-S seed] [-T tools]
1.222     uebayasi  578:                 [-V var=[value]] [-w wrapper] [-X x11src] [-Y extsrcsrc]
                    579:                 [-Z var]
1.195     lukem     580:                 operation [...]
1.84      lukem     581:
1.86      lukem     582:  Build operations (all imply "obj" and "tools"):
1.125     lukem     583:     build               Run "make build".
                    584:     distribution        Run "make distribution" (includes DESTDIR/etc/ files).
                    585:     release             Run "make release" (includes kernels & distrib media).
1.84      lukem     586:
                    587:  Other operations:
1.125     lukem     588:     help                Show this message and exit.
1.98      lukem     589:     makewrapper         Create ${toolprefix}make-\${MACHINE} wrapper and ${toolprefix}make.
1.125     lukem     590:                         Always performed.
1.195     lukem     591:     cleandir            Run "make cleandir".  [Default unless -u is used]
1.125     lukem     592:     obj                 Run "make obj".  [Default unless -o is used]
                    593:     tools               Build and install tools.
                    594:     install=idir        Run "make installworld" to \`idir' to install all sets
1.195     lukem     595:                         except \`etc'.  Useful after "distribution" or "release"
1.105     lukem     596:     kernel=conf         Build kernel with config file \`conf'
1.125     lukem     597:     releasekernel=conf  Install kernel built by kernel=conf to RELEASEDIR.
1.226     mbalmer   598:     modules             Build kernel modules.
1.219     pooka     599:     rumptest            Do a linktest for rump (for developers).
1.186     lukem     600:     sets                Create binary sets in
1.195     lukem     601:                         RELEASEDIR/RELEASEMACHINEDIR/binary/sets.
                    602:                         DESTDIR should be populated beforehand.
1.125     lukem     603:     sourcesets          Create source sets in RELEASEDIR/source/sets.
1.186     lukem     604:     syspkgs             Create syspkgs in
1.195     lukem     605:                         RELEASEDIR/RELEASEMACHINEDIR/binary/syspkgs.
1.172     jnemeth   606:     iso-image           Create CD-ROM image in RELEASEDIR/iso.
                    607:     iso-image-source    Create CD-ROM image with source in RELEASEDIR/iso.
1.125     lukem     608:     params              Display various make(1) parameters.
1.84      lukem     609:
                    610:  Options:
1.125     lukem     611:     -a arch     Set MACHINE_ARCH to arch.  [Default: deduced from MACHINE]
                    612:     -B buildId  Set BUILDID to buildId.
1.209     apb       613:     -C cdextras Append cdextras to CDEXTRA variable for inclusion on CD-ROM.
1.119     lukem     614:     -D dest     Set DESTDIR to dest.  [Default: destdir.MACHINE]
1.98      lukem     615:     -E          Set "expert" mode; disables various safety checks.
1.127     lukem     616:                 Should not be used without expert knowledge of the build system.
1.129     wiz       617:     -h          Print this help message.
1.125     lukem     618:     -j njob     Run up to njob jobs in parallel; see make(1) -j.
                    619:     -M obj      Set obj root directory to obj; sets MAKEOBJDIRPREFIX.
1.111     lukem     620:                 Unsets MAKEOBJDIR.
1.125     lukem     621:     -m mach     Set MACHINE to mach; not required if NetBSD native.
1.195     lukem     622:     -N noisy    Set the noisyness (MAKEVERBOSE) level of the build:
1.199     apb       623:                     0   Minimal output ("quiet")
                    624:                     1   Describe what is occurring
                    625:                     2   Describe what is occurring and echo the actual command
                    626:                     3   Ignore the effect of the "@" prefix in make commands
                    627:                     4   Trace shell commands using the shell's -x flag
1.195     lukem     628:                 [Default: 2]
1.125     lukem     629:     -n          Show commands that would be executed, but do not execute them.
                    630:     -O obj      Set obj root directory to obj; sets a MAKEOBJDIR pattern.
1.111     lukem     631:                 Unsets MAKEOBJDIRPREFIX.
1.125     lukem     632:     -o          Set MKOBJDIRS=no; do not create objdirs at start of build.
1.119     lukem     633:     -R release  Set RELEASEDIR to release.  [Default: releasedir]
1.125     lukem     634:     -r          Remove contents of TOOLDIR and DESTDIR before building.
1.190     perry     635:     -S seed     Set BUILDSEED to seed.  [Default: NetBSD-majorversion]
1.98      lukem     636:     -T tools    Set TOOLDIR to tools.  If unset, and TOOLDIR is not set in
1.125     lukem     637:                 the environment, ${toolprefix}make will be (re)built unconditionally.
                    638:     -U          Set MKUNPRIVED=yes; build without requiring root privileges,
1.195     lukem     639:                 install from an UNPRIVED build with proper file permissions.
1.187     dholland  640:     -u          Set MKUPDATE=yes; do not run "make cleandir" first.
1.195     lukem     641:                 Without this, everything is rebuilt, including the tools.
1.125     lukem     642:     -V v=[val]  Set variable \`v' to \`val'.
                    643:     -w wrapper  Create ${toolprefix}make script as wrapper.
1.119     lukem     644:                 [Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE}]
1.127     lukem     645:     -X x11src   Set X11SRCDIR to x11src.  [Default: /usr/xsrc]
1.201     plunky    646:     -x          Set MKX11=yes; build X11 from X11SRCDIR
1.222     uebayasi  647:     -Y extsrcsrc
                    648:                 Set EXTSRCSRCDIR to extsrcsrc.  [Default: /usr/extsrc]
                    649:     -y          Set MKEXTSRC=yes; build extsrc from EXTSRCSRCDIR
1.125     lukem     650:     -Z v        Unset ("zap") variable \`v'.
1.83      lukem     651:
1.70      lukem     652: _usage_
1.1       tv        653:        exit 1
                    654: }
                    655:
1.84      lukem     656: parseoptions()
                    657: {
1.222     uebayasi  658:        opts='a:B:C:D:Ehj:M:m:N:nO:oR:rS:T:UuV:w:xX:yY:Z:'
1.84      lukem     659:        opt_a=no
                    660:
                    661:        if type getopts >/dev/null 2>&1; then
                    662:                # Use POSIX getopts.
1.98      lukem     663:                #
                    664:                getoptcmd='getopts ${opts} opt && opt=-${opt}'
1.84      lukem     665:                optargcmd=':'
1.98      lukem     666:                optremcmd='shift $((${OPTIND} -1))'
1.84      lukem     667:        else
                    668:                type getopt >/dev/null 2>&1 ||
                    669:                    bomb "/bin/sh shell is too old; try ksh or bash"
                    670:
                    671:                # Use old-style getopt(1) (doesn't handle whitespace in args).
1.98      lukem     672:                #
                    673:                args="$(getopt ${opts} $*)"
1.84      lukem     674:                [ $? = 0 ] || usage
1.98      lukem     675:                set -- ${args}
1.84      lukem     676:
                    677:                getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
                    678:                optargcmd='OPTARG="$1"; shift'
                    679:                optremcmd=':'
                    680:        fi
                    681:
                    682:        # Parse command line options.
                    683:        #
1.98      lukem     684:        while eval ${getoptcmd}; do
                    685:                case ${opt} in
1.84      lukem     686:
                    687:                -a)
1.98      lukem     688:                        eval ${optargcmd}
                    689:                        MACHINE_ARCH=${OPTARG}
1.84      lukem     690:                        opt_a=yes
                    691:                        ;;
                    692:
                    693:                -B)
1.98      lukem     694:                        eval ${optargcmd}
                    695:                        BUILDID=${OPTARG}
1.84      lukem     696:                        ;;
                    697:
1.174     jnemeth   698:                -C)
1.208     apb       699:                        eval ${optargcmd}; resolvepaths OPTARG
1.209     apb       700:                        CDEXTRA="${CDEXTRA}${CDEXTRA:+ }${OPTARG}"
1.174     jnemeth   701:                        ;;
                    702:
1.84      lukem     703:                -D)
1.208     apb       704:                        eval ${optargcmd}; resolvepath OPTARG
1.109     lukem     705:                        setmakeenv DESTDIR "${OPTARG}"
1.84      lukem     706:                        ;;
                    707:
                    708:                -E)
                    709:                        do_expertmode=true
                    710:                        ;;
                    711:
                    712:                -j)
1.98      lukem     713:                        eval ${optargcmd}
                    714:                        parallel="-j ${OPTARG}"
1.84      lukem     715:                        ;;
                    716:
                    717:                -M)
1.208     apb       718:                        eval ${optargcmd}; resolvepath OPTARG
1.212     apb       719:                        case "${OPTARG}" in
                    720:                        \$*)    usage "-M argument must not begin with '$'"
                    721:                                ;;
                    722:                        *\$*)   # can use resolvepath, but can't set TOP_objdir
                    723:                                resolvepath OPTARG
                    724:                                ;;
                    725:                        *)      resolvepath OPTARG
                    726:                                TOP_objdir="${OPTARG}${TOP}"
                    727:                                ;;
                    728:                        esac
1.111     lukem     729:                        unsetmakeenv MAKEOBJDIR
1.109     lukem     730:                        setmakeenv MAKEOBJDIRPREFIX "${OPTARG}"
1.84      lukem     731:                        ;;
                    732:
                    733:                        # -m overrides MACHINE_ARCH unless "-a" is specified
                    734:                -m)
1.98      lukem     735:                        eval ${optargcmd}
                    736:                        MACHINE="${OPTARG}"
                    737:                        [ "${opt_a}" != "yes" ] && getarch
1.84      lukem     738:                        ;;
                    739:
1.119     lukem     740:                -N)
                    741:                        eval ${optargcmd}
                    742:                        case "${OPTARG}" in
1.199     apb       743:                        0|1|2|3|4)
1.121     lukem     744:                                setmakeenv MAKEVERBOSE "${OPTARG}"
1.119     lukem     745:                                ;;
                    746:                        *)
                    747:                                usage "'${OPTARG}' is not a valid value for -N"
                    748:                                ;;
                    749:                        esac
                    750:                        ;;
                    751:
1.84      lukem     752:                -n)
                    753:                        runcmd=echo
                    754:                        ;;
                    755:
                    756:                -O)
1.212     apb       757:                        eval ${optargcmd}
                    758:                        case "${OPTARG}" in
                    759:                        *\$*)   usage "-O argument must not contain '$'"
                    760:                                ;;
                    761:                        *)      resolvepath OPTARG
                    762:                                TOP_objdir="${OPTARG}"
                    763:                                ;;
                    764:                        esac
1.111     lukem     765:                        unsetmakeenv MAKEOBJDIRPREFIX
1.109     lukem     766:                        setmakeenv MAKEOBJDIR "\${.CURDIR:C,^$TOP,$OPTARG,}"
1.84      lukem     767:                        ;;
                    768:
                    769:                -o)
                    770:                        MKOBJDIRS=no
                    771:                        ;;
                    772:
                    773:                -R)
1.208     apb       774:                        eval ${optargcmd}; resolvepath OPTARG
1.109     lukem     775:                        setmakeenv RELEASEDIR "${OPTARG}"
1.84      lukem     776:                        ;;
                    777:
                    778:                -r)
                    779:                        do_removedirs=true
                    780:                        do_rebuildmake=true
                    781:                        ;;
                    782:
1.190     perry     783:                -S)
                    784:                        eval ${optargcmd}
                    785:                        setmakeenv BUILDSEED "${OPTARG}"
                    786:                        ;;
                    787:
1.84      lukem     788:                -T)
1.208     apb       789:                        eval ${optargcmd}; resolvepath OPTARG
1.98      lukem     790:                        TOOLDIR="${OPTARG}"
1.84      lukem     791:                        export TOOLDIR
                    792:                        ;;
                    793:
                    794:                -U)
1.109     lukem     795:                        setmakeenv MKUNPRIVED yes
1.84      lukem     796:                        ;;
1.44      lukem     797:
1.84      lukem     798:                -u)
1.109     lukem     799:                        setmakeenv MKUPDATE yes
1.84      lukem     800:                        ;;
1.15      tv        801:
1.84      lukem     802:                -V)
1.98      lukem     803:                        eval ${optargcmd}
1.84      lukem     804:                        case "${OPTARG}" in
1.80      lukem     805:                    # XXX: consider restricting which variables can be changed?
1.84      lukem     806:                        [a-zA-Z_][a-zA-Z_0-9]*=*)
1.109     lukem     807:                                setmakeenv "${OPTARG%%=*}" "${OPTARG#*=}"
1.84      lukem     808:                                ;;
                    809:                        *)
                    810:                                usage "-V argument must be of the form 'var=[value]'"
                    811:                                ;;
                    812:                        esac
                    813:                        ;;
                    814:
                    815:                -w)
1.208     apb       816:                        eval ${optargcmd}; resolvepath OPTARG
1.98      lukem     817:                        makewrapper="${OPTARG}"
1.84      lukem     818:                        ;;
                    819:
1.127     lukem     820:                -X)
1.208     apb       821:                        eval ${optargcmd}; resolvepath OPTARG
1.127     lukem     822:                        setmakeenv X11SRCDIR "${OPTARG}"
                    823:                        ;;
                    824:
                    825:                -x)
                    826:                        setmakeenv MKX11 yes
                    827:                        ;;
                    828:
1.222     uebayasi  829:                -Y)
                    830:                        eval ${optargcmd}; resolvepath OPTARG
                    831:                        setmakeenv EXTSRCSRCDIR "${OPTARG}"
                    832:                        ;;
                    833:
                    834:                -y)
                    835:                        setmakeenv MKEXTSRC yes
                    836:                        ;;
                    837:
1.111     lukem     838:                -Z)
                    839:                        eval ${optargcmd}
                    840:                    # XXX: consider restricting which variables can be unset?
                    841:                        unsetmakeenv "${OPTARG}"
                    842:                        ;;
                    843:
1.84      lukem     844:                --)
                    845:                        break
                    846:                        ;;
                    847:
                    848:                -'?'|-h)
                    849:                        usage
                    850:                        ;;
                    851:
                    852:                esac
                    853:        done
                    854:
                    855:        # Validate operations.
                    856:        #
1.98      lukem     857:        eval ${optremcmd}
1.84      lukem     858:        while [ $# -gt 0 ]; do
                    859:                op=$1; shift
1.98      lukem     860:                operations="${operations} ${op}"
1.84      lukem     861:
1.98      lukem     862:                case "${op}" in
1.84      lukem     863:
1.87      lukem     864:                help)
                    865:                        usage
                    866:                        ;;
                    867:
1.195     lukem     868:                makewrapper|cleandir|obj|tools|build|distribution|release|sets|sourcesets|syspkgs|params)
1.80      lukem     869:                        ;;
1.84      lukem     870:
1.146     apb       871:                iso-image)
                    872:                        op=iso_image    # used as part of a variable name
                    873:                        ;;
                    874:
1.172     jnemeth   875:                iso-image-source)
                    876:                        op=iso_image_source   # used as part of a variable name
                    877:                        ;;
                    878:
1.105     lukem     879:                kernel=*|releasekernel=*)
1.84      lukem     880:                        arg=${op#*=}
                    881:                        op=${op%%=*}
1.98      lukem     882:                        [ -n "${arg}" ] ||
1.105     lukem     883:                            bomb "Must supply a kernel name with \`${op}=...'"
1.84      lukem     884:                        ;;
                    885:
1.207     jnemeth   886:                modules)
                    887:                        op=modules
                    888:                        ;;
                    889:
1.84      lukem     890:                install=*)
                    891:                        arg=${op#*=}
                    892:                        op=${op%%=*}
1.98      lukem     893:                        [ -n "${arg}" ] ||
                    894:                            bomb "Must supply a directory with \`install=...'"
1.84      lukem     895:                        ;;
                    896:
1.219     pooka     897:                rump|rumptest)
                    898:                        op=${op}
                    899:                        ;;
                    900:
1.80      lukem     901:                *)
1.84      lukem     902:                        usage "Unknown operation \`${op}'"
                    903:                        ;;
                    904:
1.80      lukem     905:                esac
1.98      lukem     906:                eval do_${op}=true
1.84      lukem     907:        done
1.98      lukem     908:        [ -n "${operations}" ] || usage "Missing operation to perform."
1.84      lukem     909:
                    910:        # Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
                    911:        #
1.98      lukem     912:        if [ -z "${MACHINE}" ]; then
                    913:                [ "${uname_s}" = "NetBSD" ] ||
                    914:                    bomb "MACHINE must be set, or -m must be used, for cross builds."
1.84      lukem     915:                MACHINE=${uname_m}
                    916:        fi
1.98      lukem     917:        [ -n "${MACHINE_ARCH}" ] || getarch
1.84      lukem     918:        validatearch
                    919:
                    920:        # Set up default make(1) environment.
                    921:        #
1.98      lukem     922:        makeenv="${makeenv} TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
                    923:        [ -z "${BUILDID}" ] || makeenv="${makeenv} BUILDID"
1.115     jmmv      924:        MAKEFLAGS="-de -m ${TOP}/share/mk ${MAKEFLAGS} MKOBJDIRS=${MKOBJDIRS-yes}"
1.84      lukem     925:        export MAKEFLAGS MACHINE MACHINE_ARCH
                    926: }
                    927:
1.163     apb       928: sanitycheck()
                    929: {
                    930:        # If the PATH contains any non-absolute components (including,
1.170     apb       931:        # but not limited to, "." or ""), then complain.  As an exception,
                    932:        # allow "" or "." as the last component of the PATH.  This is fatal
1.163     apb       933:        # if expert mode is not in effect.
                    934:        #
1.170     apb       935:        local path="${PATH}"
                    936:        path="${path%:}"        # delete trailing ":"
                    937:        path="${path%:.}"       # delete trailing ":."
                    938:        case ":${path}:/" in
                    939:        *:[!/]*)
1.163     apb       940:                if ${do_expertmode}; then
                    941:                        warning "PATH contains non-absolute components"
                    942:                else
1.164     apb       943:                        bomb "PATH environment variable must not" \
                    944:                             "contain non-absolute components"
1.163     apb       945:                fi
                    946:                ;;
                    947:        esac
                    948: }
                    949:
1.213     apb       950: # print_tooldir_make --
                    951: # Try to find and print a path to an existing
                    952: # ${TOOLDIR}/bin/${toolprefix}make, for use by rebuildmake() before a
                    953: # new version of ${toolprefix}make has been built.
1.168     apb       954: #
                    955: # * If TOOLDIR was set in the environment or on the command line, use
                    956: #   that value.
                    957: # * Otherwise try to guess what TOOLDIR would be if not overridden by
                    958: #   /etc/mk.conf, and check whether the resulting directory contains
                    959: #   a copy of ${toolprefix}make (this should work for everybody who
                    960: #   doesn't override TOOLDIR via /etc/mk.conf);
                    961: # * Failing that, search for ${toolprefix}make, nbmake, bmake, or make,
                    962: #   in the PATH (this might accidentally find a non-NetBSD version of
                    963: #   make, which will lead to failure in the next step);
                    964: # * If a copy of make was found above, try to use it with
1.213     apb       965: #   nobomb_getmakevar to find the correct value for TOOLDIR, and believe the
                    966: #   result only if it's a directory that already exists;
                    967: # * If a value of TOOLDIR was found above, and if
                    968: #   ${TOOLDIR}/bin/${toolprefix}make exists, print that value.
1.168     apb       969: #
1.213     apb       970: print_tooldir_make()
1.168     apb       971: {
1.213     apb       972:        local possible_TOP_OBJ
                    973:        local possible_TOOLDIR
                    974:        local possible_make
                    975:        local tooldir_make
                    976:
                    977:        if [ -n "${TOOLDIR}" ]; then
                    978:                echo "${TOOLDIR}/bin/${toolprefix}make"
                    979:                return 0
                    980:        fi
1.168     apb       981:
1.198     apb       982:        # Set host_ostype to something like "NetBSD-4.5.6-i386".  This
                    983:        # is intended to match the HOST_OSTYPE variable in <bsd.own.mk>.
                    984:        #
1.168     apb       985:        local host_ostype="${uname_s}-$(
                    986:                echo "${uname_r}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
1.194     lukem     987:                )-$(
1.168     apb       988:                echo "${uname_p}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
                    989:                )"
                    990:
1.198     apb       991:        # Look in a few potential locations for
                    992:        # ${possible_TOOLDIR}/bin/${toolprefix}make.
1.213     apb       993:        # If we find it, then set possible_make.
1.198     apb       994:        #
                    995:        # In the usual case (without interference from environment
                    996:        # variables or /etc/mk.conf), <bsd.own.mk> should set TOOLDIR to
1.212     apb       997:        # "${_SRC_TOP_OBJ_}/tooldir.${host_ostype}".
                    998:        #
                    999:        # In practice it's difficult to figure out the correct value
                   1000:        # for _SRC_TOP_OBJ_.  In the easiest case, when the -M or -O
                   1001:        # options were passed to build.sh, then ${TOP_objdir} will be
                   1002:        # the correct value.  We also try a few other possibilities, but
                   1003:        # we do not replicate all the logic of <bsd.obj.mk>.
1.198     apb      1004:        #
1.212     apb      1005:        for possible_TOP_OBJ in \
                   1006:                "${TOP_objdir}" \
                   1007:                "${MAKEOBJDIRPREFIX:+${MAKEOBJDIRPREFIX}${TOP}}" \
                   1008:                "${TOP}" \
                   1009:                "${TOP}/obj" \
1.198     apb      1010:                "${TOP}/obj.${MACHINE}"
                   1011:        do
1.212     apb      1012:                [ -n "${possible_TOP_OBJ}" ] || continue
1.198     apb      1013:                possible_TOOLDIR="${possible_TOP_OBJ}/tooldir.${host_ostype}"
1.213     apb      1014:                possible_make="${possible_TOOLDIR}/bin/${toolprefix}make"
                   1015:                if [ -x "${possible_make}" ]; then
1.212     apb      1016:                        break
1.198     apb      1017:                else
1.213     apb      1018:                        unset possible_make
1.198     apb      1019:                fi
                   1020:        done
                   1021:
                   1022:        # If the above didn't work, search the PATH for a suitable
                   1023:        # ${toolprefix}make, nbmake, bmake, or make.
                   1024:        #
1.213     apb      1025:        : ${possible_make:=$(find_in_PATH ${toolprefix}make '')}
                   1026:        : ${possible_make:=$(find_in_PATH nbmake '')}
                   1027:        : ${possible_make:=$(find_in_PATH bmake '')}
                   1028:        : ${possible_make:=$(find_in_PATH make '')}
                   1029:
                   1030:        # At this point, we don't care whether possible_make is in the
                   1031:        # correct TOOLDIR or not; we simply want it to be usable by
                   1032:        # getmakevar to help us find the correct TOOLDIR.
                   1033:        #
                   1034:        # Use ${possible_make} with nobomb_getmakevar to try to find
                   1035:        # the value of TOOLDIR.  Believe the result only if it's
                   1036:        # a directory that already exists and contains bin/${toolprefix}make.
                   1037:        #
                   1038:        if [ -x "${possible_make}" ]; then
                   1039:                possible_TOOLDIR="$(
                   1040:                        make="${possible_make}" nobomb_getmakevar TOOLDIR
                   1041:                        )"
                   1042:                if [ $? = 0 ] && [ -n "${possible_TOOLDIR}" ] \
                   1043:                    && [ -d "${possible_TOOLDIR}" ];
                   1044:                then
                   1045:                        tooldir_make="${possible_TOOLDIR}/bin/${toolprefix}make"
                   1046:                        if [ -x "${tooldir_make}" ]; then
                   1047:                                echo "${tooldir_make}"
                   1048:                                return 0
                   1049:                        fi
                   1050:                fi
1.168     apb      1051:        fi
1.213     apb      1052:        return 1
1.168     apb      1053: }
                   1054:
1.213     apb      1055: # rebuildmake --
                   1056: # Rebuild nbmake in a temporary directory if necessary.  Sets $make
                   1057: # to a path to the nbmake executable.  Sets done_rebuildmake=true
                   1058: # if nbmake was rebuilt.
                   1059: #
                   1060: # There is a cyclic dependency between building nbmake and choosing
                   1061: # TOOLDIR: TOOLDIR may be affected by settings in /etc/mk.conf, so we
                   1062: # would like to use getmakevar to get the value of TOOLDIR; but we can't
                   1063: # use getmakevar before we have an up to date version of nbmake; we
                   1064: # might already have an up to date version of nbmake in TOOLDIR, but we
                   1065: # don't yet know where TOOLDIR is.
                   1066: #
                   1067: # The default value of TOOLDIR also depends on the location of the top
                   1068: # level object directory, so $(getmakevar TOOLDIR) invoked before or
                   1069: # after making the top level object directory may produce different
                   1070: # results.
                   1071: #
                   1072: # Strictly speaking, we should do the following:
                   1073: #
                   1074: #    1. build a new version of nbmake in a temporary directory;
                   1075: #    2. use the temporary nbmake to create the top level obj directory;
                   1076: #    3. use $(getmakevar TOOLDIR) with the temporary nbmake to
                   1077: #       get the corect value of TOOLDIR;
1.214     apb      1078: #    4. move the temporary nbmake to ${TOOLDIR}/bin/nbmake.
1.213     apb      1079: #
                   1080: # However, people don't like building nbmake unnecessarily if their
                   1081: # TOOLDIR has not changed since an earlier build.  We try to avoid
                   1082: # rebuilding a temporary version of nbmake by taking some shortcuts to
                   1083: # guess a value for TOOLDIR, looking for an existing version of nbmake
                   1084: # in that TOOLDIR, and checking whether that nbmake is newer than the
                   1085: # sources used to build it.
                   1086: #
1.84      lukem    1087: rebuildmake()
                   1088: {
1.213     apb      1089:        make="$(print_tooldir_make)"
                   1090:        if [ -n "${make}" ] && [ -x "${make}" ]; then
1.84      lukem    1091:                for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
1.98      lukem    1092:                        if [ "${f}" -nt "${make}" ]; then
1.213     apb      1093:                                statusmsg "${make} outdated" \
                   1094:                                        "(older than ${f}), needs building."
1.84      lukem    1095:                                do_rebuildmake=true
                   1096:                                break
                   1097:                        fi
                   1098:                done
                   1099:        else
1.213     apb      1100:                statusmsg "No \$TOOLDIR/bin/${toolprefix}make, needs building."
1.84      lukem    1101:                do_rebuildmake=true
                   1102:        fi
                   1103:
                   1104:        # Build bootstrap ${toolprefix}make if needed.
1.98      lukem    1105:        if ${do_rebuildmake}; then
                   1106:                statusmsg "Bootstrapping ${toolprefix}make"
                   1107:                ${runcmd} cd "${tmpdir}"
                   1108:                ${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
1.84      lukem    1109:                        CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
1.153     apb      1110:                        ${HOST_SH} "${TOP}/tools/make/configure" ||
1.98      lukem    1111:                    bomb "Configure of ${toolprefix}make failed"
1.153     apb      1112:                ${runcmd} ${HOST_SH} buildmake.sh ||
1.98      lukem    1113:                    bomb "Build of ${toolprefix}make failed"
                   1114:                make="${tmpdir}/${toolprefix}make"
                   1115:                ${runcmd} cd "${TOP}"
                   1116:                ${runcmd} rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
1.211     apb      1117:                done_rebuildmake=true
1.84      lukem    1118:        fi
                   1119: }
                   1120:
                   1121: validatemakeparams()
                   1122: {
1.98      lukem    1123:        if [ "${runcmd}" = "echo" ]; then
1.84      lukem    1124:                TOOLCHAIN_MISSING=no
                   1125:                EXTERNAL_TOOLCHAIN=""
                   1126:        else
1.210     apb      1127:                TOOLCHAIN_MISSING=$(bomb_getmakevar TOOLCHAIN_MISSING)
                   1128:                EXTERNAL_TOOLCHAIN=$(bomb_getmakevar EXTERNAL_TOOLCHAIN)
1.84      lukem    1129:        fi
                   1130:        if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
1.86      lukem    1131:           [ -z "${EXTERNAL_TOOLCHAIN}" ]; then
1.98      lukem    1132:                ${runcmd} echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
                   1133:                ${runcmd} echo "        MACHINE:      ${MACHINE}"
                   1134:                ${runcmd} echo "        MACHINE_ARCH: ${MACHINE_ARCH}"
                   1135:                ${runcmd} echo ""
                   1136:                ${runcmd} echo "All builds for this platform should be done via a traditional make"
                   1137:                ${runcmd} echo "If you wish to use an external cross-toolchain, set"
                   1138:                ${runcmd} echo "        EXTERNAL_TOOLCHAIN=<path to toolchain root>"
                   1139:                ${runcmd} echo "in either the environment or mk.conf and rerun"
                   1140:                ${runcmd} echo "        ${progname} $*"
1.84      lukem    1141:                exit 1
                   1142:        fi
                   1143:
1.120     lukem    1144:        # Normalise MKOBJDIRS, MKUNPRIVED, and MKUPDATE
                   1145:        # These may be set as build.sh options or in "mk.conf".
                   1146:        # Don't export them as they're only used for tests in build.sh.
                   1147:        #
                   1148:        MKOBJDIRS=$(getmakevar MKOBJDIRS)
                   1149:        MKUNPRIVED=$(getmakevar MKUNPRIVED)
                   1150:        MKUPDATE=$(getmakevar MKUPDATE)
                   1151:
1.106     lukem    1152:        if [ "${MKOBJDIRS}" != "no" ]; then
1.212     apb      1153:                # Create the top-level object directory.
1.106     lukem    1154:                #
1.212     apb      1155:                # "make obj NOSUBDIR=" can handle most cases, but it
                   1156:                # can't handle the case where MAKEOBJDIRPREFIX is set
                   1157:                # while the corresponding directory does not exist
                   1158:                # (rules in <bsd.obj.mk> would abort the build).  We
                   1159:                # therefore have to handle the MAKEOBJDIRPREFIX case
                   1160:                # without invoking "make obj".  The MAKEOBJDIR case
                   1161:                # could be handled either way, but we choose to handle
                   1162:                # it similarly to MAKEOBJDIRPREFIX.
1.191     apb      1163:                #
1.212     apb      1164:                if [ -n "${TOP_obj}" ]; then
                   1165:                        # It must have been set by the "-M" or "-O"
                   1166:                        # command line options, so there's no need to
                   1167:                        # use getmakevar
                   1168:                        :
                   1169:                elif [ -n "$MAKEOBJDIRPREFIX" ]; then
                   1170:                        TOP_obj="$(getmakevar MAKEOBJDIRPREFIX)${TOP}"
                   1171:                elif [ -n "$MAKEOBJDIR" ]; then
                   1172:                        TOP_obj="$(getmakevar MAKEOBJDIR)"
                   1173:                fi
                   1174:                if [ -n "$TOP_obj" ]; then
                   1175:                        ${runcmd} mkdir -p "${TOP_obj}" ||
                   1176:                            bomb "Can't create top level object directory" \
                   1177:                                        "${TOP_obj}"
                   1178:                else
                   1179:                        ${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
                   1180:                            bomb "Can't create top level object directory" \
                   1181:                                        "using make obj"
1.106     lukem    1182:                fi
                   1183:
1.212     apb      1184:                # make obj in tools to ensure that the objdir for "tools"
                   1185:                # is available.
1.106     lukem    1186:                #
1.98      lukem    1187:                ${runcmd} cd tools
                   1188:                ${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
                   1189:                    bomb "Failed to make obj in tools"
                   1190:                ${runcmd} cd "${TOP}"
1.84      lukem    1191:        fi
1.80      lukem    1192:
1.215     apb      1193:        # Find TOOLDIR, DESTDIR, and RELEASEDIR, according to getmakevar,
                   1194:        # and bomb if they have changed from the values we had from the
                   1195:        # command line or environment.
                   1196:        #
1.212     apb      1197:        # This must be done after creating the top-level object directory.
1.84      lukem    1198:        #
1.215     apb      1199:        for var in TOOLDIR DESTDIR RELEASEDIR
                   1200:        do
                   1201:                eval oldval=\"\$${var}\"
                   1202:                newval="$(getmakevar $var)"
                   1203:                if ! $do_expertmode; then
1.216     enami    1204:                        : ${_SRC_TOP_OBJ_:=$(getmakevar _SRC_TOP_OBJ_)}
1.215     apb      1205:                        case "$var" in
                   1206:                        DESTDIR)
                   1207:                                : ${newval:=${_SRC_TOP_OBJ_}/destdir.${MACHINE}}
1.217     apb      1208:                                makeenv="${makeenv} DESTDIR"
1.215     apb      1209:                                ;;
                   1210:                        RELEASEDIR)
                   1211:                                : ${newval:=${_SRC_TOP_OBJ_}/releasedir}
1.217     apb      1212:                                makeenv="${makeenv} RELEASEDIR"
1.215     apb      1213:                                ;;
                   1214:                        esac
                   1215:                fi
                   1216:                if [ -n "$oldval" ] && [ "$oldval" != "$newval" ]; then
                   1217:                        bomb "Value of ${var} has changed" \
                   1218:                                "(was \"${oldval}\", now \"${newval}\")"
                   1219:                fi
                   1220:                eval ${var}=\"\${newval}\"
                   1221:                eval export ${var}
                   1222:                statusmsg "${var} path:     ${newval}"
                   1223:        done
                   1224:
                   1225:        # RELEASEMACHINEDIR is just a subdir name, e.g. "i386".
1.186     lukem    1226:        RELEASEMACHINEDIR=$(getmakevar RELEASEMACHINEDIR)
1.84      lukem    1227:
                   1228:        # Check validity of TOOLDIR and DESTDIR.
                   1229:        #
1.98      lukem    1230:        if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = "/" ]; then
                   1231:                bomb "TOOLDIR '${TOOLDIR}' invalid"
1.84      lukem    1232:        fi
1.98      lukem    1233:        removedirs="${TOOLDIR}"
1.15      tv       1234:
1.98      lukem    1235:        if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = "/" ]; then
                   1236:                if ${do_build} || ${do_distribution} || ${do_release}; then
                   1237:                        if ! ${do_build} || \
1.84      lukem    1238:                           [ "${uname_s}" != "NetBSD" ] || \
1.98      lukem    1239:                           [ "${uname_m}" != "${MACHINE}" ]; then
1.84      lukem    1240:                                bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
                   1241:                        fi
1.98      lukem    1242:                        if ! ${do_expertmode}; then
1.84      lukem    1243:                                bomb "DESTDIR must != / for non -E (expert) builds"
                   1244:                        fi
1.98      lukem    1245:                        statusmsg "WARNING: Building to /, in expert mode."
                   1246:                        statusmsg "         This may cause your system to break!  Reasons include:"
                   1247:                        statusmsg "            - your kernel is not up to date"
                   1248:                        statusmsg "            - the libraries or toolchain have changed"
                   1249:                        statusmsg "         YOU HAVE BEEN WARNED!"
1.1       tv       1250:                fi
1.84      lukem    1251:        else
1.98      lukem    1252:                removedirs="${removedirs} ${DESTDIR}"
1.84      lukem    1253:        fi
1.98      lukem    1254:        if ${do_build} || ${do_distribution} || ${do_release}; then
                   1255:                if ! ${do_expertmode} && \
1.202     sketch   1256:                    [ "$id_u" -ne 0 ] && \
1.124     lukem    1257:                    [ "${MKUNPRIVED}" = "no" ] ; then
1.86      lukem    1258:                        bomb "-U or -E must be set for build as an unprivileged user."
                   1259:                fi
1.185     apb      1260:        fi
1.105     lukem    1261:        if ${do_releasekernel} && [ -z "${RELEASEDIR}" ]; then
                   1262:                bomb "Must set RELEASEDIR with \`releasekernel=...'"
                   1263:        fi
1.185     apb      1264:
                   1265:        # Install as non-root is a bad idea.
                   1266:        #
1.202     sketch   1267:        if ${do_install} && [ "$id_u" -ne 0 ] ; then
1.185     apb      1268:                if ${do_expertmode}; then
                   1269:                        warning "Will install as an unprivileged user."
                   1270:                else
                   1271:                        bomb "-E must be set for install as an unprivileged user."
                   1272:                fi
                   1273:        fi
                   1274:
                   1275:        # If a previous build.sh run used -U (and therefore created a
                   1276:        # METALOG file), then most subsequent build.sh runs must also
                   1277:        # use -U.  If DESTDIR is about to be removed, then don't perform
                   1278:        # this check.
                   1279:        #
                   1280:        case "${do_removedirs} ${removedirs} " in
                   1281:        true*" ${DESTDIR} "*)
                   1282:                # DESTDIR is about to be removed
                   1283:                ;;
                   1284:        *)
                   1285:                if ( ${do_build} || ${do_distribution} || ${do_release} || \
                   1286:                    ${do_install} ) && \
                   1287:                    [ -e "${DESTDIR}/METALOG" ] && \
                   1288:                    [ "${MKUNPRIVED}" = "no" ] ; then
                   1289:                        if $do_expertmode; then
                   1290:                                warning "A previous build.sh run specified -U."
                   1291:                        else
                   1292:                                bomb "A previous build.sh run specified -U; you must specify it again now."
                   1293:                        fi
                   1294:                fi
                   1295:                ;;
                   1296:        esac
1.84      lukem    1297: }
1.30      jmc      1298:
1.15      tv       1299:
1.84      lukem    1300: createmakewrapper()
                   1301: {
                   1302:        # Remove the target directories.
                   1303:        #
1.98      lukem    1304:        if ${do_removedirs}; then
                   1305:                for f in ${removedirs}; do
                   1306:                        statusmsg "Removing ${f}"
                   1307:                        ${runcmd} rm -r -f "${f}"
1.84      lukem    1308:                done
                   1309:        fi
1.15      tv       1310:
1.84      lukem    1311:        # Recreate $TOOLDIR.
                   1312:        #
1.98      lukem    1313:        ${runcmd} mkdir -p "${TOOLDIR}/bin" ||
                   1314:            bomb "mkdir of '${TOOLDIR}/bin' failed"
1.84      lukem    1315:
1.214     apb      1316:        # If we did not previously rebuild ${toolprefix}make, then
                   1317:        # check whether $make is still valid and the same as the output
                   1318:        # from print_tooldir_make.  If not, then rebuild make now.  A
                   1319:        # possible reason for this being necessary is that the actual
                   1320:        # value of TOOLDIR might be different from the value guessed
                   1321:        # before the top level obj dir was created.
                   1322:        #
                   1323:        if ! ${done_rebuildmake} && \
                   1324:            ( [ ! -x "$make" ] || [ "$make" != "$(print_tooldir_make)" ] )
                   1325:        then
                   1326:                rebuildmake
                   1327:        fi
                   1328:
1.84      lukem    1329:        # Install ${toolprefix}make if it was built.
                   1330:        #
1.211     apb      1331:        if ${done_rebuildmake}; then
1.98      lukem    1332:                ${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
                   1333:                ${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
                   1334:                    bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
                   1335:                make="${TOOLDIR}/bin/${toolprefix}make"
                   1336:                statusmsg "Created ${make}"
1.84      lukem    1337:        fi
1.15      tv       1338:
1.84      lukem    1339:        # Build a ${toolprefix}make wrapper script, usable by hand as
                   1340:        # well as by build.sh.
                   1341:        #
1.98      lukem    1342:        if [ -z "${makewrapper}" ]; then
1.102     lukem    1343:                makewrapper="${TOOLDIR}/bin/${toolprefix}make-${makewrappermachine:-${MACHINE}}"
1.98      lukem    1344:                [ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
1.52      thorpej  1345:        fi
1.4       tv       1346:
1.98      lukem    1347:        ${runcmd} rm -f "${makewrapper}"
                   1348:        if [ "${runcmd}" = "echo" ]; then
                   1349:                echo 'cat <<EOF >'${makewrapper}
1.84      lukem    1350:                makewrapout=
                   1351:        else
1.98      lukem    1352:                makewrapout=">>\${makewrapper}"
1.84      lukem    1353:        fi
1.18      tv       1354:
1.139     isaki    1355:        case "${KSH_VERSION:-${SH_VERSION}}" in
1.149     jnemeth  1356:        *PD\ KSH*|*MIRBSD\ KSH*)
1.135     isaki    1357:                set +o braceexpand
                   1358:                ;;
                   1359:        esac
                   1360:
1.98      lukem    1361:        eval cat <<EOF ${makewrapout}
1.153     apb      1362: #! ${HOST_SH}
1.4       tv       1363: # Set proper variables to allow easy "make" building of a NetBSD subtree.
1.237   ! pooka    1364: # Generated from:  \$NetBSD: build.sh,v 1.236 2010/05/27 05:41:54 pgoyette Exp $
1.130     jmc      1365: # with these arguments: ${_args}
1.4       tv       1366: #
1.177     uebayasi 1367:
1.18      tv       1368: EOF
1.177     uebayasi 1369:        {
                   1370:                for f in ${makeenv}; do
                   1371:                        if eval "[ -z \"\${$f}\" -a \"\${${f}-X}\" = \"X\" ]"; then
                   1372:                                eval echo "unset ${f}"
                   1373:                        else
                   1374:                                eval echo "${f}=\'\$$(echo ${f})\'\;\ export\ ${f}"
                   1375:                        fi
                   1376:                done
1.18      tv       1377:
1.177     uebayasi 1378:                eval cat <<EOF
1.154     dyoung   1379: MAKEWRAPPERMACHINE=${makewrappermachine:-${MACHINE}}; export MAKEWRAPPERMACHINE
                   1380: USETOOLS=yes; export USETOOLS
1.177     uebayasi 1381: EOF
                   1382:        } | eval sort -u "${makewrapout}"
1.178     uebayasi 1383:        eval cat <<EOF "${makewrapout}"
1.18      tv       1384:
1.98      lukem    1385: exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
1.4       tv       1386: EOF
1.98      lukem    1387:        [ "${runcmd}" = "echo" ] && echo EOF
                   1388:        ${runcmd} chmod +x "${makewrapper}"
1.105     lukem    1389:        statusmsg "makewrapper:      ${makewrapper}"
1.98      lukem    1390:        statusmsg "Updated ${makewrapper}"
1.84      lukem    1391: }
                   1392:
1.203     lukem    1393: make_in_dir()
                   1394: {
                   1395:        dir="$1"
                   1396:        op="$2"
                   1397:        ${runcmd} cd "${dir}" ||
                   1398:            bomb "Failed to cd to \"${dir}\""
                   1399:        ${runcmd} "${makewrapper}" ${parallel} ${op} ||
                   1400:            bomb "Failed to make ${op} in \"${dir}\""
                   1401:        ${runcmd} cd "${TOP}" ||
                   1402:            bomb "Failed to cd back to \"${TOP}\""
                   1403: }
                   1404:
1.84      lukem    1405: buildtools()
                   1406: {
1.98      lukem    1407:        if [ "${MKOBJDIRS}" != "no" ]; then
                   1408:                ${runcmd} "${makewrapper}" ${parallel} obj-tools ||
                   1409:                    bomb "Failed to make obj-tools"
1.84      lukem    1410:        fi
1.124     lukem    1411:        if [ "${MKUPDATE}" = "no" ]; then
1.203     lukem    1412:                make_in_dir tools cleandir
1.84      lukem    1413:        fi
1.203     lukem    1414:        make_in_dir tools dependall
                   1415:        make_in_dir tools install
1.98      lukem    1416:        statusmsg "Tools built to ${TOOLDIR}"
1.84      lukem    1417: }
1.4       tv       1418:
1.105     lukem    1419: getkernelconf()
1.84      lukem    1420: {
1.105     lukem    1421:        kernelconf="$1"
1.114     lukem    1422:        if [ "${MKOBJDIRS}" != "no" ]; then
1.84      lukem    1423:                # The correct value of KERNOBJDIR might
                   1424:                # depend on a prior "make obj" in
                   1425:                # ${KERNSRCDIR}/${KERNARCHDIR}/compile.
                   1426:                #
1.98      lukem    1427:                KERNSRCDIR="$(getmakevar KERNSRCDIR)"
                   1428:                KERNARCHDIR="$(getmakevar KERNARCHDIR)"
1.203     lukem    1429:                make_in_dir "${KERNSRCDIR}/${KERNARCHDIR}/compile" obj
1.84      lukem    1430:        fi
1.98      lukem    1431:        KERNCONFDIR="$(getmakevar KERNCONFDIR)"
                   1432:        KERNOBJDIR="$(getmakevar KERNOBJDIR)"
1.105     lukem    1433:        case "${kernelconf}" in
1.84      lukem    1434:        */*)
1.105     lukem    1435:                kernelconfpath="${kernelconf}"
                   1436:                kernelconfname="${kernelconf##*/}"
1.84      lukem    1437:                ;;
                   1438:        *)
1.105     lukem    1439:                kernelconfpath="${KERNCONFDIR}/${kernelconf}"
                   1440:                kernelconfname="${kernelconf}"
1.84      lukem    1441:                ;;
                   1442:        esac
1.105     lukem    1443:        kernelbuildpath="${KERNOBJDIR}/${kernelconfname}"
                   1444: }
                   1445:
                   1446: buildkernel()
                   1447: {
                   1448:        if ! ${do_tools} && ! ${buildkernelwarned:-false}; then
                   1449:                # Building tools every time we build a kernel is clearly
                   1450:                # unnecessary.  We could try to figure out whether rebuilding
                   1451:                # the tools is necessary this time, but it doesn't seem worth
                   1452:                # the trouble.  Instead, we say it's the user's responsibility
                   1453:                # to rebuild the tools if necessary.
                   1454:                #
                   1455:                statusmsg "Building kernel without building new tools"
                   1456:                buildkernelwarned=true
                   1457:        fi
                   1458:        getkernelconf $1
                   1459:        statusmsg "Building kernel:  ${kernelconf}"
                   1460:        statusmsg "Build directory:  ${kernelbuildpath}"
                   1461:        ${runcmd} mkdir -p "${kernelbuildpath}" ||
                   1462:            bomb "Cannot mkdir: ${kernelbuildpath}"
1.124     lukem    1463:        if [ "${MKUPDATE}" = "no" ]; then
1.203     lukem    1464:                make_in_dir "${kernelbuildpath}" cleandir
1.16      thorpej  1465:        fi
1.157     rillig   1466:        [ -x "${TOOLDIR}/bin/${toolprefix}config" ] \
                   1467:        || bomb "${TOOLDIR}/bin/${toolprefix}config does not exist. You need to \"$0 tools\" first."
1.105     lukem    1468:        ${runcmd} "${TOOLDIR}/bin/${toolprefix}config" -b "${kernelbuildpath}" \
                   1469:                -s "${TOP}/sys" "${kernelconfpath}" ||
                   1470:            bomb "${toolprefix}config failed for ${kernelconf}"
1.203     lukem    1471:        make_in_dir "${kernelbuildpath}" depend
                   1472:        make_in_dir "${kernelbuildpath}" all
1.91      lukem    1473:
1.98      lukem    1474:        if [ "${runcmd}" != "echo" ]; then
1.105     lukem    1475:                statusmsg "Kernels built from ${kernelconf}:"
                   1476:                kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
1.91      lukem    1477:                for kern in ${kernlist:-netbsd}; do
1.105     lukem    1478:                        [ -f "${kernelbuildpath}/${kern}" ] && \
                   1479:                            echo "  ${kernelbuildpath}/${kern}"
1.98      lukem    1480:                done | tee -a "${results}"
1.91      lukem    1481:        fi
1.84      lukem    1482: }
                   1483:
1.105     lukem    1484: releasekernel()
                   1485: {
                   1486:        getkernelconf $1
1.186     lukem    1487:        kernelreldir="${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/kernel"
1.105     lukem    1488:        ${runcmd} mkdir -p "${kernelreldir}"
                   1489:        kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
                   1490:        for kern in ${kernlist:-netbsd}; do
                   1491:                builtkern="${kernelbuildpath}/${kern}"
                   1492:                [ -f "${builtkern}" ] || continue
                   1493:                releasekern="${kernelreldir}/${kern}-${kernelconfname}.gz"
                   1494:                statusmsg "Kernel copy:      ${releasekern}"
1.196     lukem    1495:                if [ "${runcmd}" = "echo" ]; then
                   1496:                        echo "gzip -c -9 < ${builtkern} > ${releasekern}"
                   1497:                else
                   1498:                        gzip -c -9 < "${builtkern}" > "${releasekern}"
                   1499:                fi
1.105     lukem    1500:        done
                   1501: }
                   1502:
1.207     jnemeth  1503: buildmodules()
                   1504: {
1.234     morr     1505:        setmakeenv MKBINUTILS no
1.207     jnemeth  1506:        if ! ${do_tools} && ! ${buildmoduleswarned:-false}; then
                   1507:                # Building tools every time we build modules is clearly
                   1508:                # unnecessary as well as a kernel.
                   1509:                #
                   1510:                statusmsg "Building modules without building new tools"
                   1511:                buildmoduleswarned=true
                   1512:        fi
                   1513:
                   1514:        statusmsg "Building kernel modules for NetBSD/${MACHINE} ${DISTRIBVER}"
                   1515:        if [ "${MKOBJDIRS}" != "no" ]; then
                   1516:                make_in_dir sys/modules obj ||
                   1517:                    bomb "Failed to make obj in sys/modules"
                   1518:        fi
                   1519:        if [ "${MKUPDATE}" = "no" ]; then
                   1520:                make_in_dir sys/modules cleandir
                   1521:        fi
                   1522:        ${runcmd} "${makewrapper}" ${parallel} do-sys-modules ||
                   1523:            bomb "Failed to make do-sys-modules"
                   1524:
1.236     pgoyette 1525:        statusmsg "Successful build of kernel modules for NetBSD/${MACHINE} ${DISTRIBVER}"
1.207     jnemeth  1526: }
                   1527:
1.84      lukem    1528: installworld()
                   1529: {
                   1530:        dir="$1"
1.98      lukem    1531:        ${runcmd} "${makewrapper}" INSTALLWORLDDIR="${dir}" installworld ||
                   1532:            bomb "Failed to make installworld to ${dir}"
                   1533:        statusmsg "Successful installworld to ${dir}"
1.84      lukem    1534: }
                   1535:
1.219     pooka    1536: # Run rump build&link tests.
                   1537: #
                   1538: # To make this feasible for running without having to install includes and
                   1539: # libraries into destdir (i.e. quick), we only run ld.  This is possible
                   1540: # since the rump kernel is a closed namespace apart from calls to rumpuser.
                   1541: # Therefore, if ld complains only about rumpuser symbols, rump kernel
                   1542: # linking was successful.
                   1543: #
                   1544: # We test that rump links with a number of component configurations.
                   1545: # These attempt to mimic what is encountered in the full build.
                   1546: # See list below.  The list should probably be either autogenerated
                   1547: # or managed elsewhere.  But keep it here until a better idea arises.
                   1548: #
                   1549: # Above all, note that THIS IS NOT A SUBSTITUTE FOR A FULL BUILD.
                   1550: #
                   1551:
                   1552: RUMP_LIBSETS='
                   1553:        -lrump,
                   1554:        -lrumpvfs -lrump,
1.230     pooka    1555:        -lrumpdev -lrump,
1.235     pooka    1556:        -lrumpnet -lrump,
1.237   ! pooka    1557:        -lrumpkern_tty -lrumpvfs -lrump,
1.219     pooka    1558:        -lrumpfs_tmpfs -lrumpvfs -lrump,
1.230     pooka    1559:        -lrumpfs_ffs -lrumpfs_msdos -lrumpvfs -lrumpdev_disk -lrumpdev -lrump,
1.219     pooka    1560:        -lrumpnet_virtif -lrumpnet_netinet -lrumpnet_net -lrumpnet -lrump,
                   1561:        -lrumpnet_sockin -lrumpfs_smbfs -lrumpdev_netsmb
                   1562:            -lrumpcrypto -lrumpdev -lrumpnet -lrumpvfs -lrump,
1.220     pooka    1563:        -lrumpnet_sockin -lrumpfs_nfs -lrumpnet -lrumpvfs -lrump,
                   1564:        -lrumpdev_cgd -lrumpdev_raidframe -lrumpdev_disk -lrumpdev_rnd
                   1565:            -lrumpdev -lrumpvfs -lrumpcrypto -lrump'
1.219     pooka    1566: dorump()
                   1567: {
                   1568:        local doclean=""
                   1569:        local doobjs=""
                   1570:
                   1571:        # we cannot link libs without building csu, and that leads to lossage
                   1572:        [ "${1}" != "rumptest" ] && bomb 'build.sh rump not yet functional. ' \
                   1573:            'did you mean "rumptest"?'
                   1574:
1.227     pooka    1575:        # create obj and distrib dirs
1.228     pooka    1576:        if [ "${MKOBJDIRS}" != "no" ]; then
                   1577:                make_in_dir "${NETBSDSRCDIR}/etc/mtree" obj
                   1578:                make_in_dir "${NETBSDSRCDIR}/sys/rump" obj
                   1579:        fi
1.227     pooka    1580:        ${runcmd} "${makewrapper}" ${parallel} do-distrib-dirs \
                   1581:            || bomb 'could not create distrib-dirs'
                   1582:
1.219     pooka    1583:        [ "${MKUPDATE}" = "no" ] && doclean="cleandir"
                   1584:        targlist="${doclean} ${doobjs} dependall install"
                   1585:        # optimize: for test we build only static libs (3x test speedup)
                   1586:        if [ "${1}" = "rumptest" ] ; then
                   1587:                setmakeenv NOPIC 1
                   1588:                setmakeenv NOPROFILE 1
                   1589:        fi
                   1590:        for cmd in ${targlist} ; do
                   1591:                make_in_dir "${NETBSDSRCDIR}/sys/rump" ${cmd}
                   1592:        done
                   1593:
                   1594:        # if we just wanted to build & install rump, we're done
                   1595:        [ "${1}" != "rumptest" ] && return
                   1596:
1.221     pooka    1597:        ${runcmd} cd "${NETBSDSRCDIR}/sys/rump/librump/rumpkern" \
                   1598:            || bomb "cd to rumpkern failed"
                   1599:        md_quirks=`${runcmd} "${makewrapper}" -V '${_SYMQUIRK}'`
                   1600:        # one little, two little, three little backslashes ...
1.231     hans     1601:        md_quirks="$(echo ${md_quirks} | sed 's,\\,\\\\,g'";s/'//g" )"
1.221     pooka    1602:        ${runcmd} cd "${TOP}" || bomb "cd to ${TOP} failed"
1.219     pooka    1603:        tool_ld=`${runcmd} "${makewrapper}" -V '${LD}'`
1.221     pooka    1604:
1.219     pooka    1605:        local oIFS="${IFS}"
                   1606:        IFS=","
                   1607:        for set in ${RUMP_LIBSETS} ; do
                   1608:                IFS="${oIFS}"
                   1609:                ${runcmd} ${tool_ld} -nostdlib -L${DESTDIR}/usr/lib     \
1.221     pooka    1610:                    -static --whole-archive ${set} 2>&1 |               \
                   1611:                      awk -v quirks="${md_quirks}" '
1.219     pooka    1612:                        /undefined reference/ &&
                   1613:                            !/more undefined references.*follow/{
1.221     pooka    1614:                                if (match($NF,
                   1615:                                    "`(rumpuser_|__" quirks ")") == 0)
1.219     pooka    1616:                                        fails[NR] = $0
1.221     pooka    1617:                        }
1.230     pooka    1618:                        /cannot find -l/{fails[NR] = $0}
1.219     pooka    1619:                        END{
                   1620:                                for (x in fails)
                   1621:                                        print fails[x]
                   1622:                                exit x!=0
                   1623:                        }'
                   1624:                [ $? -ne 0 ] && bomb "Testlink of rump failed: ${set}"
                   1625:        done
                   1626:        statusmsg "Rump build&link tests successful"
                   1627: }
1.84      lukem    1628:
                   1629: main()
                   1630: {
                   1631:        initdefaults
1.130     jmc      1632:        _args=$@
1.84      lukem    1633:        parseoptions "$@"
1.93      lukem    1634:
1.163     apb      1635:        sanitycheck
                   1636:
1.93      lukem    1637:        build_start=$(date)
1.98      lukem    1638:        statusmsg "${progname} command: $0 $@"
                   1639:        statusmsg "${progname} started: ${build_start}"
1.165     apb      1640:        statusmsg "NetBSD version:   ${DISTRIBVER}"
                   1641:        statusmsg "MACHINE:          ${MACHINE}"
                   1642:        statusmsg "MACHINE_ARCH:     ${MACHINE_ARCH}"
                   1643:        statusmsg "Build platform:   ${uname_s} ${uname_r} ${uname_m}"
1.153     apb      1644:        statusmsg "HOST_SH:          ${HOST_SH}"
                   1645:
1.84      lukem    1646:        rebuildmake
                   1647:        validatemakeparams
                   1648:        createmakewrapper
                   1649:
                   1650:        # Perform the operations.
                   1651:        #
1.98      lukem    1652:        for op in ${operations}; do
                   1653:                case "${op}" in
1.86      lukem    1654:
                   1655:                makewrapper)
                   1656:                        # no-op
                   1657:                        ;;
1.84      lukem    1658:
                   1659:                tools)
                   1660:                        buildtools
                   1661:                        ;;
                   1662:
1.125     lukem    1663:                sets)
                   1664:                        statusmsg "Building sets from pre-populated ${DESTDIR}"
                   1665:                        ${runcmd} "${makewrapper}" ${parallel} ${op} ||
                   1666:                            bomb "Failed to make ${op}"
1.186     lukem    1667:                        setdir=${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
                   1668:                        statusmsg "Built sets to ${setdir}"
1.125     lukem    1669:                        ;;
1.142     apb      1670:
1.195     lukem    1671:                cleandir|obj|build|distribution|release|sourcesets|syspkgs|params)
1.98      lukem    1672:                        ${runcmd} "${makewrapper}" ${parallel} ${op} ||
                   1673:                            bomb "Failed to make ${op}"
                   1674:                        statusmsg "Successful make ${op}"
1.84      lukem    1675:                        ;;
                   1676:
1.173     jnemeth  1677:                iso-image|iso-image-source)
                   1678:                        ${runcmd} "${makewrapper}" ${parallel} \
1.209     apb      1679:                            CDEXTRA="$CDEXTRA" ${op} ||
1.173     jnemeth  1680:                            bomb "Failed to make ${op}"
                   1681:                        statusmsg "Successful make ${op}"
                   1682:                        ;;
                   1683:
1.84      lukem    1684:                kernel=*)
                   1685:                        arg=${op#*=}
                   1686:                        buildkernel "${arg}"
1.105     lukem    1687:                        ;;
                   1688:
                   1689:                releasekernel=*)
                   1690:                        arg=${op#*=}
                   1691:                        releasekernel "${arg}"
1.84      lukem    1692:                        ;;
                   1693:
1.207     jnemeth  1694:                modules)
                   1695:                        buildmodules
                   1696:                        ;;
                   1697:
1.84      lukem    1698:                install=*)
                   1699:                        arg=${op#*=}
1.85      lukem    1700:                        if [ "${arg}" = "/" ] && \
                   1701:                            (   [ "${uname_s}" != "NetBSD" ] || \
1.98      lukem    1702:                                [ "${uname_m}" != "${MACHINE}" ] ); then
1.85      lukem    1703:                                bomb "'${op}' must != / for cross builds."
                   1704:                        fi
1.84      lukem    1705:                        installworld "${arg}"
1.70      lukem    1706:                        ;;
1.84      lukem    1707:
1.219     pooka    1708:                rump|rumptest)
                   1709:                        dorump "${op}"
                   1710:                        ;;
                   1711:
1.70      lukem    1712:                *)
1.84      lukem    1713:                        bomb "Unknown operation \`${op}'"
1.70      lukem    1714:                        ;;
1.84      lukem    1715:
1.70      lukem    1716:                esac
1.84      lukem    1717:        done
1.93      lukem    1718:
1.98      lukem    1719:        statusmsg "${progname} ended:   $(date)"
                   1720:        if [ -s "${results}" ]; then
                   1721:                echo "===> Summary of results:"
                   1722:                sed -e 's/^===>//;s/^/  /' "${results}"
                   1723:                echo "===> ."
                   1724:        fi
1.84      lukem    1725: }
                   1726:
                   1727: main "$@"

CVSweb <webmaster@jp.NetBSD.org>