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

Annotation of src/build.sh, Revision 1.201

1.67      thorpej     1: #! /usr/bin/env sh
1.201   ! plunky      2: #      $NetBSD: build.sh,v 1.200 2009/01/03 08:23:00 apb Exp $
1.84      lukem       3: #
1.193     lukem       4: # Copyright (c) 2001-2008 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.168     apb       166:        # Find information about the build platform.  Note that "uname -p"
                    167:        # is not part of POSIX, but NetBSD's uname -p prints MACHINE_ARCH,
                    168:        # while uname -m prints MACHINE.
1.165     apb       169:        #
1.84      lukem     170:        uname_s=$(uname -s 2>/dev/null)
1.165     apb       171:        uname_r=$(uname -r 2>/dev/null)
1.84      lukem     172:        uname_m=$(uname -m 2>/dev/null)
1.168     apb       173:        uname_p=$(uname -p 2>/dev/null || uname -m 2>/dev/null)
1.84      lukem     174:
                    175:        # If $PWD is a valid name of the current directory, POSIX mandates
                    176:        # that pwd return it by default which causes problems in the
                    177:        # presence of symlinks.  Unsetting PWD is simpler than changing
                    178:        # every occurrence of pwd to use -P.
                    179:        #
1.147     dogcow    180:        # XXX Except that doesn't work on Solaris. Or many Linuces.
1.98      lukem     181:        #
1.84      lukem     182:        unset PWD
1.147     dogcow    183:        TOP=$(/bin/pwd -P 2>/dev/null || /bin/pwd 2>/dev/null)
1.84      lukem     184:
1.153     apb       185:        # The user can set HOST_SH in the environment, or we try to
                    186:        # guess an appropriate value.  Then we set several other
                    187:        # variables from HOST_SH.
                    188:        #
                    189:        set_HOST_SH
                    190:        setmakeenv HOST_SH "${HOST_SH}"
                    191:        setmakeenv BSHELL "${HOST_SH}"
                    192:        setmakeenv CONFIG_SHELL "${HOST_SH}"
                    193:
1.84      lukem     194:        # Set defaults.
1.98      lukem     195:        #
1.84      lukem     196:        toolprefix=nb
1.98      lukem     197:
1.95      thorpej   198:        # Some systems have a small ARG_MAX.  -X prevents make(1) from
                    199:        # exporting variables in the environment redundantly.
1.98      lukem     200:        #
1.95      thorpej   201:        case "${uname_s}" in
1.97      christos  202:        Darwin | FreeBSD | CYGWIN*)
1.95      thorpej   203:                MAKEFLAGS=-X
                    204:                ;;
                    205:        *)
                    206:                MAKEFLAGS=
                    207:                ;;
                    208:        esac
1.98      lukem     209:
1.171     apb       210:        # do_{operation}=true if given operation is requested.
                    211:        #
1.84      lukem     212:        do_expertmode=false
                    213:        do_rebuildmake=false
                    214:        do_removedirs=false
                    215:        do_tools=false
1.195     lukem     216:        do_cleandir=false
1.84      lukem     217:        do_obj=false
                    218:        do_build=false
                    219:        do_distribution=false
                    220:        do_release=false
                    221:        do_kernel=false
1.105     lukem     222:        do_releasekernel=false
1.84      lukem     223:        do_install=false
1.87      lukem     224:        do_sets=false
1.100     lukem     225:        do_sourcesets=false
1.142     apb       226:        do_syspkgs=false
1.146     apb       227:        do_iso_image=false
1.172     jnemeth   228:        do_iso_image_source=false
1.107     lukem     229:        do_params=false
1.98      lukem     230:
                    231:        # Create scratch directory
                    232:        #
                    233:        tmpdir="${TMPDIR-/tmp}/nbbuild$$"
                    234:        mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
                    235:        trap "cd /; rm -r -f \"${tmpdir}\"" 0
                    236:        results="${tmpdir}/build.sh.results"
1.116     jmmv      237:
                    238:        # Set source directories
                    239:        #
                    240:        setmakeenv NETBSDSRCDIR "${TOP}"
1.136     lukem     241:
1.194     lukem     242:        # Determine top-level obj directory.
                    243:        # Defaults to the top-level source directory.
                    244:        # If $MAKEOBJDIRPREFIX is set in the environment, use it.
                    245:        # We can't check $MAKEOBJDIR since that may be a make(1)
                    246:        # expression that we can't evaluate at this time.
                    247:        #
                    248:        TOP_objdir="${TOP}"
                    249:        if [ -n "${MAKEOBJDIRPREFIX}" ]; then
                    250:                TOP_objdir="${MAKEOBJDIRPREFIX}${TOP}"
                    251:        elif [ -n "${MAKEOBJDIR}" ]; then
                    252:                warning "Can't parse \$(MAKEOBJDIR) \"$MAKEOBJDIR\" to determine top objdir"
                    253:        fi
                    254:
1.165     apb       255:        # Find the version of NetBSD
                    256:        #
                    257:        DISTRIBVER="$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh)"
                    258:
1.190     perry     259:        # Set the BUILDSEED to NetBSD-"N"
                    260:        #
                    261:        setmakeenv BUILDSEED "NetBSD-$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh -m)"
                    262:
1.136     lukem     263:        # Set various environment variables to known defaults,
                    264:        # to minimize (cross-)build problems observed "in the field".
                    265:        #
                    266:        unsetmakeenv INFODIR
1.140     jmc       267:        unsetmakeenv LESSCHARSET
1.136     lukem     268:        setmakeenv LC_ALL C
1.84      lukem     269: }
1.29      jmc       270:
1.79      lukem     271: getarch()
                    272: {
1.158     apb       273:        # Translate some MACHINE name aliases (known only to build.sh)
                    274:        # into proper MACHINE and MACHINE_ARCH names.  Save the alias
                    275:        # name in makewrappermachine.
                    276:        #
                    277:        case "${MACHINE}" in
                    278:
                    279:        evbarm-e[bl])
                    280:                makewrappermachine=${MACHINE}
                    281:                # MACHINE_ARCH is "arm" or "armeb", not "armel"
                    282:                MACHINE_ARCH=arm${MACHINE##*-}
                    283:                MACHINE_ARCH=${MACHINE_ARCH%el}
                    284:                MACHINE=${MACHINE%-e[bl]}
                    285:                ;;
                    286:
                    287:        evbmips-e[bl]|sbmips-e[bl])
                    288:                makewrappermachine=${MACHINE}
                    289:                MACHINE_ARCH=mips${MACHINE##*-}
                    290:                MACHINE=${MACHINE%-e[bl]}
                    291:                ;;
                    292:
                    293:        evbmips64-e[bl]|sbmips64-e[bl])
                    294:                makewrappermachine=${MACHINE}
                    295:                MACHINE_ARCH=mips64${MACHINE##*-}
                    296:                MACHINE=${MACHINE%64-e[bl]}
                    297:                ;;
                    298:
                    299:        evbsh3-e[bl])
                    300:                makewrappermachine=${MACHINE}
                    301:                MACHINE_ARCH=sh3${MACHINE##*-}
                    302:                MACHINE=${MACHINE%-e[bl]}
                    303:                ;;
                    304:
                    305:        esac
                    306:
1.1       tv        307:        # Translate a MACHINE into a default MACHINE_ARCH.
1.84      lukem     308:        #
1.98      lukem     309:        case "${MACHINE}" in
1.1       tv        310:
1.162     briggs    311:        acorn26|acorn32|cats|hpcarm|iyonix|netwinder|shark|zaurus)
1.84      lukem     312:                MACHINE_ARCH=arm
                    313:                ;;
                    314:
1.162     briggs    315:        evbarm)         # unspecified MACHINE_ARCH gets LE
                    316:                MACHINE_ARCH=${MACHINE_ARCH:=arm}
                    317:                ;;
                    318:
1.98      lukem     319:        hp700)
                    320:                MACHINE_ARCH=hppa
                    321:                ;;
                    322:
1.84      lukem     323:        sun2)
                    324:                MACHINE_ARCH=m68000
                    325:                ;;
                    326:
1.98      lukem     327:        amiga|atari|cesfic|hp300|luna68k|mac68k|mvme68k|news68k|next68k|sun3|x68k)
1.84      lukem     328:                MACHINE_ARCH=m68k
                    329:                ;;
1.1       tv        330:
1.101     lukem     331:        evbmips|sbmips)         # no default MACHINE_ARCH
                    332:                ;;
                    333:
1.141     tsutsui   334:        ews4800mips|mipsco|newsmips|sgimips)
1.84      lukem     335:                MACHINE_ARCH=mipseb
                    336:                ;;
1.1       tv        337:
1.101     lukem     338:        algor|arc|cobalt|hpcmips|playstation2|pmax)
1.84      lukem     339:                MACHINE_ARCH=mipsel
                    340:                ;;
1.1       tv        341:
1.183     jmmv      342:        evbppc64|macppc64|ofppc64)
1.160     matt      343:                makewrappermachine=${MACHINE}
                    344:                MACHINE=${MACHINE%64}
                    345:                MACHINE_ARCH=powerpc64
                    346:                ;;
                    347:
1.181     garbled   348:        amigappc|bebox|evbppc|ibmnws|macppc|mvmeppc|ofppc|prep|rs6000|sandpoint)
1.84      lukem     349:                MACHINE_ARCH=powerpc
                    350:                ;;
1.1       tv        351:
1.103     lukem     352:        evbsh3)                 # no default MACHINE_ARCH
                    353:                ;;
                    354:
                    355:        mmeye)
1.84      lukem     356:                MACHINE_ARCH=sh3eb
                    357:                ;;
1.1       tv        358:
1.152     uwe       359:        dreamcast|hpcsh|landisk)
1.84      lukem     360:                MACHINE_ARCH=sh3el
                    361:                ;;
1.1       tv        362:
1.96      fvdl      363:        amd64)
                    364:                MACHINE_ARCH=x86_64
                    365:                ;;
1.62      fredette  366:
1.138     skrll     367:        alpha|i386|sparc|sparc64|vax|ia64)
1.98      lukem     368:                MACHINE_ARCH=${MACHINE}
1.84      lukem     369:                ;;
1.69      thorpej   370:
1.84      lukem     371:        *)
1.98      lukem     372:                bomb "Unknown target MACHINE: ${MACHINE}"
1.84      lukem     373:                ;;
1.4       tv        374:
1.1       tv        375:        esac
                    376: }
                    377:
1.79      lukem     378: validatearch()
                    379: {
1.47      tv        380:        # Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
1.84      lukem     381:        #
1.98      lukem     382:        case "${MACHINE_ARCH}" in
1.47      tv        383:
1.184     matt      384:        alpha|arm|armeb|hppa|i386|m68000|m68k|mipse[bl]|mips64e[bl]|powerpc|powerpc64|sh3e[bl]|sparc|sparc64|vax|x86_64|ia64)
1.84      lukem     385:                ;;
                    386:
1.101     lukem     387:        "")
                    388:                bomb "No MACHINE_ARCH provided"
                    389:                ;;
                    390:
1.84      lukem     391:        *)
1.98      lukem     392:                bomb "Unknown target MACHINE_ARCH: ${MACHINE_ARCH}"
                    393:                ;;
                    394:
                    395:        esac
                    396:
                    397:        # Determine valid MACHINE_ARCHs for MACHINE
                    398:        #
                    399:        case "${MACHINE}" in
                    400:
                    401:        evbarm)
                    402:                arches="arm armeb"
                    403:                ;;
                    404:
                    405:        evbmips|sbmips)
1.150     matt      406:                arches="mipseb mipsel mips64eb mips64el"
                    407:                ;;
                    408:
                    409:        sgimips)
                    410:                arches="mipseb mips64eb"
1.98      lukem     411:                ;;
                    412:
                    413:        evbsh3)
                    414:                arches="sh3eb sh3el"
                    415:                ;;
                    416:
1.183     jmmv      417:        macppc|evbppc|ofppc)
1.148     mrg       418:                arches="powerpc powerpc64"
                    419:                ;;
1.98      lukem     420:        *)
                    421:                oma="${MACHINE_ARCH}"
                    422:                getarch
                    423:                arches="${MACHINE_ARCH}"
                    424:                MACHINE_ARCH="${oma}"
1.84      lukem     425:                ;;
                    426:
1.47      tv        427:        esac
1.98      lukem     428:
                    429:        # Ensure that MACHINE_ARCH supports MACHINE
                    430:        #
                    431:        archok=false
                    432:        for a in ${arches}; do
                    433:                if [ "${a}" = "${MACHINE_ARCH}" ]; then
                    434:                        archok=true
                    435:                        break
                    436:                fi
                    437:        done
                    438:        ${archok} ||
                    439:            bomb "MACHINE_ARCH '${MACHINE_ARCH}' does not support MACHINE '${MACHINE}'"
1.47      tv        440: }
                    441:
1.168     apb       442: nobomb_getmakevar()
1.79      lukem     443: {
1.168     apb       444:        [ -x "${make}" ] || return 1
                    445:        "${make}" -m ${TOP}/share/mk -s -B -f- _x_ <<EOF || return 1
1.15      tv        446: _x_:
                    447:        echo \${$1}
                    448: .include <bsd.prog.mk>
1.70      lukem     449: .include <bsd.kernobj.mk>
1.15      tv        450: EOF
                    451: }
                    452:
1.168     apb       453: raw_getmakevar()
                    454: {
                    455:        [ -x "${make}" ] || bomb "raw_getmakevar $1: ${make} is not executable"
                    456:        nobomb_getmakevar "$1" || bomb "raw_getmakevar $1: ${make} failed"
                    457: }
                    458:
1.98      lukem     459: getmakevar()
1.82      lukem     460: {
1.98      lukem     461:        # raw_getmakevar() doesn't work properly if $make hasn't yet been
                    462:        # built, which can happen when running with the "-n" option.
                    463:        # getmakevar() deals with this by emitting a literal '$'
1.84      lukem     464:        # followed by the variable name, instead of trying to find the
                    465:        # variable's value.
                    466:        #
1.98      lukem     467:        if [ -x "${make}" ]; then
                    468:                raw_getmakevar "$1"
1.82      lukem     469:        else
                    470:                echo "\$$1"
                    471:        fi
                    472: }
                    473:
1.109     lukem     474: setmakeenv()
                    475: {
                    476:        eval "$1='$2'; export $1"
                    477:        makeenv="${makeenv} $1"
                    478: }
                    479:
1.111     lukem     480: unsetmakeenv()
                    481: {
                    482:        eval "unset $1"
                    483:        makeenv="${makeenv} $1"
                    484: }
                    485:
1.189     dyoung    486: # Convert possibly-relative paths to absolute paths by prepending
                    487: # ${TOP} if necessary.  Also delete trailing "/", if any.
                    488: resolvepaths()
                    489: {
                    490:        _OPTARG=
                    491:        for oa in ${OPTARG}; do
                    492:                case "${oa}" in
                    493:                /)
                    494:                        ;;
                    495:                /*)
                    496:                        oa="${oa%/}"
                    497:                        ;;
                    498:                *)
                    499:                        oa="${TOP}/${oa%/}"
                    500:                        ;;
                    501:                esac
                    502:                _OPTARG="${_OPTARG} ${oa}"
                    503:        done
                    504:        OPTARG="${_OPTARG}"
                    505: }
                    506:
1.131     junyoung  507: # Convert possibly-relative path to absolute path by prepending
                    508: # ${TOP} if necessary.  Also delete trailing "/", if any.
1.79      lukem     509: resolvepath()
                    510: {
1.98      lukem     511:        case "${OPTARG}" in
1.131     junyoung  512:        /)
                    513:                ;;
1.84      lukem     514:        /*)
1.131     junyoung  515:                OPTARG="${OPTARG%/}"
1.84      lukem     516:                ;;
                    517:        *)
1.131     junyoung  518:                OPTARG="${TOP}/${OPTARG%/}"
1.84      lukem     519:                ;;
1.10      tv        520:        esac
                    521: }
                    522:
1.79      lukem     523: usage()
                    524: {
1.84      lukem     525:        if [ -n "$*" ]; then
                    526:                echo ""
                    527:                echo "${progname}: $*"
                    528:        fi
1.70      lukem     529:        cat <<_usage_
1.83      lukem     530:
1.190     perry     531: Usage: ${progname} [-EnorUux] [-a arch] [-B buildid] [-C cdextras]
1.195     lukem     532:                 [-D dest] [-j njob] [-M obj] [-m mach] [-N noisy]
                    533:                 [-O obj] [-R release] [-S seed] [-T tools]
                    534:                 [-V var=[value]] [-w wrapper] [-X x11src] [-Z var]
                    535:                 operation [...]
1.84      lukem     536:
1.86      lukem     537:  Build operations (all imply "obj" and "tools"):
1.125     lukem     538:     build               Run "make build".
                    539:     distribution        Run "make distribution" (includes DESTDIR/etc/ files).
                    540:     release             Run "make release" (includes kernels & distrib media).
1.84      lukem     541:
                    542:  Other operations:
1.125     lukem     543:     help                Show this message and exit.
1.98      lukem     544:     makewrapper         Create ${toolprefix}make-\${MACHINE} wrapper and ${toolprefix}make.
1.125     lukem     545:                         Always performed.
1.195     lukem     546:     cleandir            Run "make cleandir".  [Default unless -u is used]
1.125     lukem     547:     obj                 Run "make obj".  [Default unless -o is used]
                    548:     tools               Build and install tools.
                    549:     install=idir        Run "make installworld" to \`idir' to install all sets
1.195     lukem     550:                         except \`etc'.  Useful after "distribution" or "release"
1.105     lukem     551:     kernel=conf         Build kernel with config file \`conf'
1.125     lukem     552:     releasekernel=conf  Install kernel built by kernel=conf to RELEASEDIR.
1.186     lukem     553:     sets                Create binary sets in
1.195     lukem     554:                         RELEASEDIR/RELEASEMACHINEDIR/binary/sets.
                    555:                         DESTDIR should be populated beforehand.
1.125     lukem     556:     sourcesets          Create source sets in RELEASEDIR/source/sets.
1.186     lukem     557:     syspkgs             Create syspkgs in
1.195     lukem     558:                         RELEASEDIR/RELEASEMACHINEDIR/binary/syspkgs.
1.172     jnemeth   559:     iso-image           Create CD-ROM image in RELEASEDIR/iso.
                    560:     iso-image-source    Create CD-ROM image with source in RELEASEDIR/iso.
1.125     lukem     561:     params              Display various make(1) parameters.
1.84      lukem     562:
                    563:  Options:
1.125     lukem     564:     -a arch     Set MACHINE_ARCH to arch.  [Default: deduced from MACHINE]
                    565:     -B buildId  Set BUILDID to buildId.
1.179     sborrill  566:     -C cdextras Set CDEXTRA to cdextras
1.119     lukem     567:     -D dest     Set DESTDIR to dest.  [Default: destdir.MACHINE]
1.98      lukem     568:     -E          Set "expert" mode; disables various safety checks.
1.127     lukem     569:                 Should not be used without expert knowledge of the build system.
1.129     wiz       570:     -h          Print this help message.
1.125     lukem     571:     -j njob     Run up to njob jobs in parallel; see make(1) -j.
                    572:     -M obj      Set obj root directory to obj; sets MAKEOBJDIRPREFIX.
1.111     lukem     573:                 Unsets MAKEOBJDIR.
1.125     lukem     574:     -m mach     Set MACHINE to mach; not required if NetBSD native.
1.195     lukem     575:     -N noisy    Set the noisyness (MAKEVERBOSE) level of the build:
1.199     apb       576:                     0   Minimal output ("quiet")
                    577:                     1   Describe what is occurring
                    578:                     2   Describe what is occurring and echo the actual command
                    579:                     3   Ignore the effect of the "@" prefix in make commands
                    580:                     4   Trace shell commands using the shell's -x flag
1.195     lukem     581:                 [Default: 2]
1.125     lukem     582:     -n          Show commands that would be executed, but do not execute them.
                    583:     -O obj      Set obj root directory to obj; sets a MAKEOBJDIR pattern.
1.111     lukem     584:                 Unsets MAKEOBJDIRPREFIX.
1.125     lukem     585:     -o          Set MKOBJDIRS=no; do not create objdirs at start of build.
1.119     lukem     586:     -R release  Set RELEASEDIR to release.  [Default: releasedir]
1.125     lukem     587:     -r          Remove contents of TOOLDIR and DESTDIR before building.
1.190     perry     588:     -S seed     Set BUILDSEED to seed.  [Default: NetBSD-majorversion]
1.98      lukem     589:     -T tools    Set TOOLDIR to tools.  If unset, and TOOLDIR is not set in
1.125     lukem     590:                 the environment, ${toolprefix}make will be (re)built unconditionally.
                    591:     -U          Set MKUNPRIVED=yes; build without requiring root privileges,
1.195     lukem     592:                 install from an UNPRIVED build with proper file permissions.
1.187     dholland  593:     -u          Set MKUPDATE=yes; do not run "make cleandir" first.
1.195     lukem     594:                 Without this, everything is rebuilt, including the tools.
1.125     lukem     595:     -V v=[val]  Set variable \`v' to \`val'.
                    596:     -w wrapper  Create ${toolprefix}make script as wrapper.
1.119     lukem     597:                 [Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE}]
1.127     lukem     598:     -X x11src   Set X11SRCDIR to x11src.  [Default: /usr/xsrc]
1.201   ! plunky    599:     -x          Set MKX11=yes; build X11 from X11SRCDIR
1.125     lukem     600:     -Z v        Unset ("zap") variable \`v'.
1.83      lukem     601:
1.70      lukem     602: _usage_
1.1       tv        603:        exit 1
                    604: }
                    605:
1.84      lukem     606: parseoptions()
                    607: {
1.197     lukem     608:        opts='a:B:C:D:Ehj:M:m:N:nO:oR:rS:T:UuV:w:xX:Z:'
1.84      lukem     609:        opt_a=no
                    610:
                    611:        if type getopts >/dev/null 2>&1; then
                    612:                # Use POSIX getopts.
1.98      lukem     613:                #
                    614:                getoptcmd='getopts ${opts} opt && opt=-${opt}'
1.84      lukem     615:                optargcmd=':'
1.98      lukem     616:                optremcmd='shift $((${OPTIND} -1))'
1.84      lukem     617:        else
                    618:                type getopt >/dev/null 2>&1 ||
                    619:                    bomb "/bin/sh shell is too old; try ksh or bash"
                    620:
                    621:                # Use old-style getopt(1) (doesn't handle whitespace in args).
1.98      lukem     622:                #
                    623:                args="$(getopt ${opts} $*)"
1.84      lukem     624:                [ $? = 0 ] || usage
1.98      lukem     625:                set -- ${args}
1.84      lukem     626:
                    627:                getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
                    628:                optargcmd='OPTARG="$1"; shift'
                    629:                optremcmd=':'
                    630:        fi
                    631:
                    632:        # Parse command line options.
                    633:        #
1.98      lukem     634:        while eval ${getoptcmd}; do
                    635:                case ${opt} in
1.84      lukem     636:
                    637:                -a)
1.98      lukem     638:                        eval ${optargcmd}
                    639:                        MACHINE_ARCH=${OPTARG}
1.84      lukem     640:                        opt_a=yes
                    641:                        ;;
                    642:
                    643:                -B)
1.98      lukem     644:                        eval ${optargcmd}
                    645:                        BUILDID=${OPTARG}
1.84      lukem     646:                        ;;
                    647:
1.174     jnemeth   648:                -C)
1.189     dyoung    649:                        eval ${optargcmd}; resolvepaths
1.174     jnemeth   650:                        iso_dir=${OPTARG}
                    651:                        ;;
                    652:
1.84      lukem     653:                -D)
1.98      lukem     654:                        eval ${optargcmd}; resolvepath
1.109     lukem     655:                        setmakeenv DESTDIR "${OPTARG}"
1.84      lukem     656:                        ;;
                    657:
                    658:                -E)
                    659:                        do_expertmode=true
                    660:                        ;;
                    661:
                    662:                -j)
1.98      lukem     663:                        eval ${optargcmd}
                    664:                        parallel="-j ${OPTARG}"
1.84      lukem     665:                        ;;
                    666:
                    667:                -M)
1.98      lukem     668:                        eval ${optargcmd}; resolvepath
1.194     lukem     669:                        TOP_objdir="${OPTARG}${TOP}"
1.111     lukem     670:                        unsetmakeenv MAKEOBJDIR
1.109     lukem     671:                        setmakeenv MAKEOBJDIRPREFIX "${OPTARG}"
1.84      lukem     672:                        ;;
                    673:
                    674:                        # -m overrides MACHINE_ARCH unless "-a" is specified
                    675:                -m)
1.98      lukem     676:                        eval ${optargcmd}
                    677:                        MACHINE="${OPTARG}"
                    678:                        [ "${opt_a}" != "yes" ] && getarch
1.84      lukem     679:                        ;;
                    680:
1.119     lukem     681:                -N)
                    682:                        eval ${optargcmd}
                    683:                        case "${OPTARG}" in
1.199     apb       684:                        0|1|2|3|4)
1.121     lukem     685:                                setmakeenv MAKEVERBOSE "${OPTARG}"
1.119     lukem     686:                                ;;
                    687:                        *)
                    688:                                usage "'${OPTARG}' is not a valid value for -N"
                    689:                                ;;
                    690:                        esac
                    691:                        ;;
                    692:
1.84      lukem     693:                -n)
                    694:                        runcmd=echo
                    695:                        ;;
                    696:
                    697:                -O)
1.98      lukem     698:                        eval ${optargcmd}; resolvepath
1.194     lukem     699:                        TOP_objdir="${OPTARG}"
1.111     lukem     700:                        unsetmakeenv MAKEOBJDIRPREFIX
1.109     lukem     701:                        setmakeenv MAKEOBJDIR "\${.CURDIR:C,^$TOP,$OPTARG,}"
1.84      lukem     702:                        ;;
                    703:
                    704:                -o)
                    705:                        MKOBJDIRS=no
                    706:                        ;;
                    707:
                    708:                -R)
1.98      lukem     709:                        eval ${optargcmd}; resolvepath
1.109     lukem     710:                        setmakeenv RELEASEDIR "${OPTARG}"
1.84      lukem     711:                        ;;
                    712:
                    713:                -r)
                    714:                        do_removedirs=true
                    715:                        do_rebuildmake=true
                    716:                        ;;
                    717:
1.190     perry     718:                -S)
                    719:                        eval ${optargcmd}
                    720:                        setmakeenv BUILDSEED "${OPTARG}"
                    721:                        ;;
                    722:
1.84      lukem     723:                -T)
1.98      lukem     724:                        eval ${optargcmd}; resolvepath
                    725:                        TOOLDIR="${OPTARG}"
1.84      lukem     726:                        export TOOLDIR
                    727:                        ;;
                    728:
                    729:                -U)
1.109     lukem     730:                        setmakeenv MKUNPRIVED yes
1.84      lukem     731:                        ;;
1.44      lukem     732:
1.84      lukem     733:                -u)
1.109     lukem     734:                        setmakeenv MKUPDATE yes
1.84      lukem     735:                        ;;
1.15      tv        736:
1.84      lukem     737:                -V)
1.98      lukem     738:                        eval ${optargcmd}
1.84      lukem     739:                        case "${OPTARG}" in
1.80      lukem     740:                    # XXX: consider restricting which variables can be changed?
1.84      lukem     741:                        [a-zA-Z_][a-zA-Z_0-9]*=*)
1.109     lukem     742:                                setmakeenv "${OPTARG%%=*}" "${OPTARG#*=}"
1.84      lukem     743:                                ;;
                    744:                        *)
                    745:                                usage "-V argument must be of the form 'var=[value]'"
                    746:                                ;;
                    747:                        esac
                    748:                        ;;
                    749:
                    750:                -w)
1.98      lukem     751:                        eval ${optargcmd}; resolvepath
                    752:                        makewrapper="${OPTARG}"
1.84      lukem     753:                        ;;
                    754:
1.127     lukem     755:                -X)
                    756:                        eval ${optargcmd}; resolvepath
                    757:                        setmakeenv X11SRCDIR "${OPTARG}"
                    758:                        ;;
                    759:
                    760:                -x)
                    761:                        setmakeenv MKX11 yes
                    762:                        ;;
                    763:
1.111     lukem     764:                -Z)
                    765:                        eval ${optargcmd}
                    766:                    # XXX: consider restricting which variables can be unset?
                    767:                        unsetmakeenv "${OPTARG}"
                    768:                        ;;
                    769:
1.84      lukem     770:                --)
                    771:                        break
                    772:                        ;;
                    773:
                    774:                -'?'|-h)
                    775:                        usage
                    776:                        ;;
                    777:
                    778:                esac
                    779:        done
                    780:
                    781:        # Validate operations.
                    782:        #
1.98      lukem     783:        eval ${optremcmd}
1.84      lukem     784:        while [ $# -gt 0 ]; do
                    785:                op=$1; shift
1.98      lukem     786:                operations="${operations} ${op}"
1.84      lukem     787:
1.98      lukem     788:                case "${op}" in
1.84      lukem     789:
1.87      lukem     790:                help)
                    791:                        usage
                    792:                        ;;
                    793:
1.195     lukem     794:                makewrapper|cleandir|obj|tools|build|distribution|release|sets|sourcesets|syspkgs|params)
1.80      lukem     795:                        ;;
1.84      lukem     796:
1.146     apb       797:                iso-image)
                    798:                        op=iso_image    # used as part of a variable name
                    799:                        ;;
                    800:
1.172     jnemeth   801:                iso-image-source)
                    802:                        op=iso_image_source   # used as part of a variable name
                    803:                        ;;
                    804:
1.105     lukem     805:                kernel=*|releasekernel=*)
1.84      lukem     806:                        arg=${op#*=}
                    807:                        op=${op%%=*}
1.98      lukem     808:                        [ -n "${arg}" ] ||
1.105     lukem     809:                            bomb "Must supply a kernel name with \`${op}=...'"
1.84      lukem     810:                        ;;
                    811:
                    812:                install=*)
                    813:                        arg=${op#*=}
                    814:                        op=${op%%=*}
1.98      lukem     815:                        [ -n "${arg}" ] ||
                    816:                            bomb "Must supply a directory with \`install=...'"
1.84      lukem     817:                        ;;
                    818:
1.80      lukem     819:                *)
1.84      lukem     820:                        usage "Unknown operation \`${op}'"
                    821:                        ;;
                    822:
1.80      lukem     823:                esac
1.98      lukem     824:                eval do_${op}=true
1.84      lukem     825:        done
1.98      lukem     826:        [ -n "${operations}" ] || usage "Missing operation to perform."
1.84      lukem     827:
                    828:        # Set up MACHINE*.  On a NetBSD host, these are allowed to be unset.
                    829:        #
1.98      lukem     830:        if [ -z "${MACHINE}" ]; then
                    831:                [ "${uname_s}" = "NetBSD" ] ||
                    832:                    bomb "MACHINE must be set, or -m must be used, for cross builds."
1.84      lukem     833:                MACHINE=${uname_m}
                    834:        fi
1.98      lukem     835:        [ -n "${MACHINE_ARCH}" ] || getarch
1.84      lukem     836:        validatearch
                    837:
                    838:        # Set up default make(1) environment.
                    839:        #
1.98      lukem     840:        makeenv="${makeenv} TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
                    841:        [ -z "${BUILDID}" ] || makeenv="${makeenv} BUILDID"
1.115     jmmv      842:        MAKEFLAGS="-de -m ${TOP}/share/mk ${MAKEFLAGS} MKOBJDIRS=${MKOBJDIRS-yes}"
1.84      lukem     843:        export MAKEFLAGS MACHINE MACHINE_ARCH
                    844: }
                    845:
1.163     apb       846: sanitycheck()
                    847: {
                    848:        # If the PATH contains any non-absolute components (including,
1.170     apb       849:        # but not limited to, "." or ""), then complain.  As an exception,
                    850:        # allow "" or "." as the last component of the PATH.  This is fatal
1.163     apb       851:        # if expert mode is not in effect.
                    852:        #
1.170     apb       853:        local path="${PATH}"
                    854:        path="${path%:}"        # delete trailing ":"
                    855:        path="${path%:.}"       # delete trailing ":."
                    856:        case ":${path}:/" in
                    857:        *:[!/]*)
1.163     apb       858:                if ${do_expertmode}; then
                    859:                        warning "PATH contains non-absolute components"
                    860:                else
1.164     apb       861:                        bomb "PATH environment variable must not" \
                    862:                             "contain non-absolute components"
1.163     apb       863:                fi
                    864:                ;;
                    865:        esac
                    866: }
                    867:
1.168     apb       868: # Try to set a value for TOOLDIR.  This is difficult because of a cyclic
                    869: # dependency: TOOLDIR may be affected by settings in /etc/mk.conf, so
                    870: # we would like to use getmakevar to get the value of TOOLDIR, but we
                    871: # can't use getmakevar before we have an up to date version of nbmake;
                    872: # we might already have an up to date version of nbmake in TOOLDIR, but
                    873: # we don't yet know where TOOLDIR is.
                    874: #
                    875: # In principle, we could break the cycle by building a copy of nbmake
                    876: # in a temporary directory.  However, people who use the default value
                    877: # of TOOLDIR do not like to have nbmake rebuilt every time they run
                    878: # build.sh.
                    879: #
                    880: # We try to please everybody as follows:
                    881: #
                    882: # * If TOOLDIR was set in the environment or on the command line, use
                    883: #   that value.
                    884: # * Otherwise try to guess what TOOLDIR would be if not overridden by
                    885: #   /etc/mk.conf, and check whether the resulting directory contains
                    886: #   a copy of ${toolprefix}make (this should work for everybody who
                    887: #   doesn't override TOOLDIR via /etc/mk.conf);
                    888: # * Failing that, search for ${toolprefix}make, nbmake, bmake, or make,
                    889: #   in the PATH (this might accidentally find a non-NetBSD version of
                    890: #   make, which will lead to failure in the next step);
                    891: # * If a copy of make was found above, try to use it with
                    892: #   nobomb_getmakevar to find the correct value for TOOLDIR;
                    893: # * If all else fails, leave TOOLDIR unset.  Our caller is expected to
                    894: #   be able to cope with this.
                    895: #
                    896: try_set_TOOLDIR()
                    897: {
                    898:        [ -n "${TOOLDIR}" ] && return
                    899:
1.198     apb       900:        # Set host_ostype to something like "NetBSD-4.5.6-i386".  This
                    901:        # is intended to match the HOST_OSTYPE variable in <bsd.own.mk>.
                    902:        #
1.168     apb       903:        local host_ostype="${uname_s}-$(
                    904:                echo "${uname_r}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
1.194     lukem     905:                )-$(
1.168     apb       906:                echo "${uname_p}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
                    907:                )"
                    908:
1.198     apb       909:        # Look in a few potential locations for
                    910:        # ${possible_TOOLDIR}/bin/${toolprefix}make.
                    911:        # If we find it, then set guess_make.
                    912:        #
                    913:        # In the usual case (without interference from environment
                    914:        # variables or /etc/mk.conf), <bsd.own.mk> should set TOOLDIR to
                    915:        # "${TOP_objdir}/tooldir.${host_ostype}".  However, in practice
                    916:        # we might have the wrong value of TOP_objdir, so we also try
                    917:        # some other possibilities.
                    918:        #
                    919:        local possible_TOP_OBJ
                    920:        local possible_TOOLDIR
                    921:        for possible_TOP_OBJ in "${TOP_objdir}" "${TOP}" "${TOP}/obj" \
                    922:                "${TOP}/obj.${MACHINE}"
                    923:        do
                    924:                possible_TOOLDIR="${possible_TOP_OBJ}/tooldir.${host_ostype}"
                    925:                guess_make="${possible_TOOLDIR}/bin/${toolprefix}make"
                    926:                if [ -x "${guess_make}" ]; then
                    927:                        break;
                    928:                else
                    929:                        unset guess_make
                    930:                fi
                    931:        done
                    932:
                    933:        # If the above didn't work, search the PATH for a suitable
                    934:        # ${toolprefix}make, nbmake, bmake, or make.
                    935:        #
1.168     apb       936:        : ${guess_make:=$(find_in_PATH ${toolprefix}make '')}
                    937:        : ${guess_make:=$(find_in_PATH nbmake '')}
                    938:        : ${guess_make:=$(find_in_PATH bmake '')}
                    939:        : ${guess_make:=$(find_in_PATH make '')}
                    940:
1.198     apb       941:        # Use ${guess_make} with nobomb_getmakevar to try to find
                    942:        # the value of TOOLDIR.  If this fails, unset TOOLDIR.
                    943:        #
                    944:        unset TOOLDIR
1.168     apb       945:        if [ -x "${guess_make}" ]; then
                    946:                TOOLDIR=$(make="${guess_make}" nobomb_getmakevar TOOLDIR)
1.194     lukem     947:                [ $? -eq 0 -a -n "${TOOLDIR}" ] || unset TOOLDIR
1.168     apb       948:        fi
                    949: }
                    950:
1.84      lukem     951: rebuildmake()
                    952: {
                    953:        # Test make source file timestamps against installed ${toolprefix}make
1.168     apb       954:        # binary, if TOOLDIR is pre-set or if try_set_TOOLDIR can set it.
1.84      lukem     955:        #
1.168     apb       956:        try_set_TOOLDIR
1.84      lukem     957:        make="${TOOLDIR-nonexistent}/bin/${toolprefix}make"
1.98      lukem     958:        if [ -x "${make}" ]; then
1.84      lukem     959:                for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
1.98      lukem     960:                        if [ "${f}" -nt "${make}" ]; then
1.122     lukem     961:                                statusmsg "${make} outdated (older than ${f}), needs building."
1.84      lukem     962:                                do_rebuildmake=true
                    963:                                break
                    964:                        fi
                    965:                done
                    966:        else
1.122     lukem     967:                statusmsg "No ${make}, needs building."
1.84      lukem     968:                do_rebuildmake=true
                    969:        fi
                    970:
                    971:        # Build bootstrap ${toolprefix}make if needed.
1.98      lukem     972:        if ${do_rebuildmake}; then
                    973:                statusmsg "Bootstrapping ${toolprefix}make"
                    974:                ${runcmd} cd "${tmpdir}"
                    975:                ${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
1.84      lukem     976:                        CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
1.153     apb       977:                        ${HOST_SH} "${TOP}/tools/make/configure" ||
1.98      lukem     978:                    bomb "Configure of ${toolprefix}make failed"
1.153     apb       979:                ${runcmd} ${HOST_SH} buildmake.sh ||
1.98      lukem     980:                    bomb "Build of ${toolprefix}make failed"
                    981:                make="${tmpdir}/${toolprefix}make"
                    982:                ${runcmd} cd "${TOP}"
                    983:                ${runcmd} rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
1.84      lukem     984:        fi
                    985: }
                    986:
                    987: validatemakeparams()
                    988: {
1.98      lukem     989:        if [ "${runcmd}" = "echo" ]; then
1.84      lukem     990:                TOOLCHAIN_MISSING=no
                    991:                EXTERNAL_TOOLCHAIN=""
                    992:        else
1.98      lukem     993:                TOOLCHAIN_MISSING=$(raw_getmakevar TOOLCHAIN_MISSING)
                    994:                EXTERNAL_TOOLCHAIN=$(raw_getmakevar EXTERNAL_TOOLCHAIN)
1.84      lukem     995:        fi
                    996:        if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
1.86      lukem     997:           [ -z "${EXTERNAL_TOOLCHAIN}" ]; then
1.98      lukem     998:                ${runcmd} echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
                    999:                ${runcmd} echo "        MACHINE:      ${MACHINE}"
                   1000:                ${runcmd} echo "        MACHINE_ARCH: ${MACHINE_ARCH}"
                   1001:                ${runcmd} echo ""
                   1002:                ${runcmd} echo "All builds for this platform should be done via a traditional make"
                   1003:                ${runcmd} echo "If you wish to use an external cross-toolchain, set"
                   1004:                ${runcmd} echo "        EXTERNAL_TOOLCHAIN=<path to toolchain root>"
                   1005:                ${runcmd} echo "in either the environment or mk.conf and rerun"
                   1006:                ${runcmd} echo "        ${progname} $*"
1.84      lukem    1007:                exit 1
                   1008:        fi
                   1009:
1.120     lukem    1010:        # Normalise MKOBJDIRS, MKUNPRIVED, and MKUPDATE
                   1011:        # These may be set as build.sh options or in "mk.conf".
                   1012:        # Don't export them as they're only used for tests in build.sh.
                   1013:        #
                   1014:        MKOBJDIRS=$(getmakevar MKOBJDIRS)
                   1015:        MKUNPRIVED=$(getmakevar MKUNPRIVED)
                   1016:        MKUPDATE=$(getmakevar MKUPDATE)
                   1017:
1.106     lukem    1018:        if [ "${MKOBJDIRS}" != "no" ]; then
1.191     apb      1019:                # Try to create the top level object directory before
                   1020:                # running "make obj", otherwise <bsd.own.mk> will not
                   1021:                # set the correct value for _SRC_TOP_OBJ_.
1.106     lukem    1022:                #
1.191     apb      1023:                # If either -M or -O was specified, then we have the
                   1024:                # directory name already.
                   1025:                #
                   1026:                # If neither -M nor -O was specified, then try to get
                   1027:                # the directory name from bsd.obj.mk's __usrobjdir
                   1028:                # variable, which is set using complex rules.  This
                   1029:                # works only if TOP = /usr/src.
                   1030:                #
1.194     lukem    1031:                top_obj_dir="${TOP_objdir}"
                   1032:                if [ -z "${top_obj_dir}" ]; then
1.191     apb      1033:                        if [ "$TOP" = "/usr/src" ]; then
                   1034:                                top_obj_dir="$(getmakevar __usrobjdir)"
                   1035:                        # else __usrobjdir is not actually used
                   1036:                        fi
                   1037:
1.106     lukem    1038:                fi
1.191     apb      1039:                case "$top_obj_dir" in
                   1040:                */*)
                   1041:                        ${runcmd} mkdir -p "${top_obj_dir}" \
                   1042:                        || bomb "Can't create object" \
                   1043:                                "directory ${top_obj_dir}"
                   1044:                        ;;
                   1045:                *)
1.192     apb      1046:                        # We don't know what the top level object
                   1047:                        # directory should be, so we can't create it.
                   1048:                        # A nonexistant directory might cause an error
                   1049:                        # when we "make obj" later, but we ignore it for
                   1050:                        # now.
1.191     apb      1051:                        ;;
                   1052:                esac
1.106     lukem    1053:
                   1054:                # make obj in tools to ensure that the objdir for the top-level
                   1055:                # of the source tree and for "tools" is available, in case the
                   1056:                # default TOOLDIR setting from <bsd.own.mk> is used, or the
                   1057:                # build.sh default DESTDIR and RELEASEDIR is to be used.
                   1058:                #
1.98      lukem    1059:                ${runcmd} cd tools
                   1060:                ${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
                   1061:                    bomb "Failed to make obj in tools"
                   1062:                ${runcmd} cd "${TOP}"
1.84      lukem    1063:        fi
1.80      lukem    1064:
1.186     lukem    1065:        # Find TOOLDIR, DESTDIR, RELEASEDIR, and RELEASEMACHINEDIR.
1.84      lukem    1066:        #
1.98      lukem    1067:        TOOLDIR=$(getmakevar TOOLDIR)
1.105     lukem    1068:        statusmsg "TOOLDIR path:     ${TOOLDIR}"
1.98      lukem    1069:        DESTDIR=$(getmakevar DESTDIR)
                   1070:        RELEASEDIR=$(getmakevar RELEASEDIR)
1.186     lukem    1071:        RELEASEMACHINEDIR=$(getmakevar RELEASEMACHINEDIR)
1.98      lukem    1072:        if ! $do_expertmode; then
1.105     lukem    1073:                _SRC_TOP_OBJ_=$(getmakevar _SRC_TOP_OBJ_)
                   1074:                : ${DESTDIR:=${_SRC_TOP_OBJ_}/destdir.${MACHINE}}
                   1075:                : ${RELEASEDIR:=${_SRC_TOP_OBJ_}/releasedir}
1.99      lukem    1076:                makeenv="${makeenv} DESTDIR RELEASEDIR"
1.98      lukem    1077:        fi
1.99      lukem    1078:        export TOOLDIR DESTDIR RELEASEDIR
1.105     lukem    1079:        statusmsg "DESTDIR path:     ${DESTDIR}"
                   1080:        statusmsg "RELEASEDIR path:  ${RELEASEDIR}"
1.84      lukem    1081:
                   1082:        # Check validity of TOOLDIR and DESTDIR.
                   1083:        #
1.98      lukem    1084:        if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = "/" ]; then
                   1085:                bomb "TOOLDIR '${TOOLDIR}' invalid"
1.84      lukem    1086:        fi
1.98      lukem    1087:        removedirs="${TOOLDIR}"
1.15      tv       1088:
1.98      lukem    1089:        if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = "/" ]; then
                   1090:                if ${do_build} || ${do_distribution} || ${do_release}; then
                   1091:                        if ! ${do_build} || \
1.84      lukem    1092:                           [ "${uname_s}" != "NetBSD" ] || \
1.98      lukem    1093:                           [ "${uname_m}" != "${MACHINE}" ]; then
1.84      lukem    1094:                                bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
                   1095:                        fi
1.98      lukem    1096:                        if ! ${do_expertmode}; then
1.84      lukem    1097:                                bomb "DESTDIR must != / for non -E (expert) builds"
                   1098:                        fi
1.98      lukem    1099:                        statusmsg "WARNING: Building to /, in expert mode."
                   1100:                        statusmsg "         This may cause your system to break!  Reasons include:"
                   1101:                        statusmsg "            - your kernel is not up to date"
                   1102:                        statusmsg "            - the libraries or toolchain have changed"
                   1103:                        statusmsg "         YOU HAVE BEEN WARNED!"
1.1       tv       1104:                fi
1.84      lukem    1105:        else
1.98      lukem    1106:                removedirs="${removedirs} ${DESTDIR}"
1.84      lukem    1107:        fi
1.98      lukem    1108:        if ${do_build} || ${do_distribution} || ${do_release}; then
                   1109:                if ! ${do_expertmode} && \
1.153     apb      1110:                    [ "$(id -u 2>/dev/null)" -ne 0 ] && \
1.124     lukem    1111:                    [ "${MKUNPRIVED}" = "no" ] ; then
1.86      lukem    1112:                        bomb "-U or -E must be set for build as an unprivileged user."
                   1113:                fi
1.185     apb      1114:        fi
1.105     lukem    1115:        if ${do_releasekernel} && [ -z "${RELEASEDIR}" ]; then
                   1116:                bomb "Must set RELEASEDIR with \`releasekernel=...'"
                   1117:        fi
1.185     apb      1118:
                   1119:        # Install as non-root is a bad idea.
                   1120:        #
                   1121:        if ${do_install} && [ "$(id -u 2>/dev/null)" -ne 0 ] ; then
                   1122:                if ${do_expertmode}; then
                   1123:                        warning "Will install as an unprivileged user."
                   1124:                else
                   1125:                        bomb "-E must be set for install as an unprivileged user."
                   1126:                fi
                   1127:        fi
                   1128:
                   1129:        # If a previous build.sh run used -U (and therefore created a
                   1130:        # METALOG file), then most subsequent build.sh runs must also
                   1131:        # use -U.  If DESTDIR is about to be removed, then don't perform
                   1132:        # this check.
                   1133:        #
                   1134:        case "${do_removedirs} ${removedirs} " in
                   1135:        true*" ${DESTDIR} "*)
                   1136:                # DESTDIR is about to be removed
                   1137:                ;;
                   1138:        *)
                   1139:                if ( ${do_build} || ${do_distribution} || ${do_release} || \
                   1140:                    ${do_install} ) && \
                   1141:                    [ -e "${DESTDIR}/METALOG" ] && \
                   1142:                    [ "${MKUNPRIVED}" = "no" ] ; then
                   1143:                        if $do_expertmode; then
                   1144:                                warning "A previous build.sh run specified -U."
                   1145:                        else
                   1146:                                bomb "A previous build.sh run specified -U; you must specify it again now."
                   1147:                        fi
                   1148:                fi
                   1149:                ;;
                   1150:        esac
1.84      lukem    1151: }
1.30      jmc      1152:
1.15      tv       1153:
1.84      lukem    1154: createmakewrapper()
                   1155: {
                   1156:        # Remove the target directories.
                   1157:        #
1.98      lukem    1158:        if ${do_removedirs}; then
                   1159:                for f in ${removedirs}; do
                   1160:                        statusmsg "Removing ${f}"
                   1161:                        ${runcmd} rm -r -f "${f}"
1.84      lukem    1162:                done
                   1163:        fi
1.15      tv       1164:
1.84      lukem    1165:        # Recreate $TOOLDIR.
                   1166:        #
1.98      lukem    1167:        ${runcmd} mkdir -p "${TOOLDIR}/bin" ||
                   1168:            bomb "mkdir of '${TOOLDIR}/bin' failed"
1.84      lukem    1169:
                   1170:        # Install ${toolprefix}make if it was built.
                   1171:        #
1.98      lukem    1172:        if ${do_rebuildmake}; then
                   1173:                ${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
                   1174:                ${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
                   1175:                    bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
                   1176:                make="${TOOLDIR}/bin/${toolprefix}make"
                   1177:                statusmsg "Created ${make}"
1.84      lukem    1178:        fi
1.15      tv       1179:
1.84      lukem    1180:        # Build a ${toolprefix}make wrapper script, usable by hand as
                   1181:        # well as by build.sh.
                   1182:        #
1.98      lukem    1183:        if [ -z "${makewrapper}" ]; then
1.102     lukem    1184:                makewrapper="${TOOLDIR}/bin/${toolprefix}make-${makewrappermachine:-${MACHINE}}"
1.98      lukem    1185:                [ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
1.52      thorpej  1186:        fi
1.4       tv       1187:
1.98      lukem    1188:        ${runcmd} rm -f "${makewrapper}"
                   1189:        if [ "${runcmd}" = "echo" ]; then
                   1190:                echo 'cat <<EOF >'${makewrapper}
1.84      lukem    1191:                makewrapout=
                   1192:        else
1.98      lukem    1193:                makewrapout=">>\${makewrapper}"
1.84      lukem    1194:        fi
1.18      tv       1195:
1.139     isaki    1196:        case "${KSH_VERSION:-${SH_VERSION}}" in
1.149     jnemeth  1197:        *PD\ KSH*|*MIRBSD\ KSH*)
1.135     isaki    1198:                set +o braceexpand
                   1199:                ;;
                   1200:        esac
                   1201:
1.98      lukem    1202:        eval cat <<EOF ${makewrapout}
1.153     apb      1203: #! ${HOST_SH}
1.4       tv       1204: # Set proper variables to allow easy "make" building of a NetBSD subtree.
1.201   ! plunky   1205: # Generated from:  \$NetBSD: build.sh,v 1.200 2009/01/03 08:23:00 apb Exp $
1.130     jmc      1206: # with these arguments: ${_args}
1.4       tv       1207: #
1.177     uebayasi 1208:
1.18      tv       1209: EOF
1.177     uebayasi 1210:        {
                   1211:                for f in ${makeenv}; do
                   1212:                        if eval "[ -z \"\${$f}\" -a \"\${${f}-X}\" = \"X\" ]"; then
                   1213:                                eval echo "unset ${f}"
                   1214:                        else
                   1215:                                eval echo "${f}=\'\$$(echo ${f})\'\;\ export\ ${f}"
                   1216:                        fi
                   1217:                done
1.18      tv       1218:
1.177     uebayasi 1219:                eval cat <<EOF
1.154     dyoung   1220: MAKEWRAPPERMACHINE=${makewrappermachine:-${MACHINE}}; export MAKEWRAPPERMACHINE
                   1221: USETOOLS=yes; export USETOOLS
1.177     uebayasi 1222: EOF
                   1223:        } | eval sort -u "${makewrapout}"
1.178     uebayasi 1224:        eval cat <<EOF "${makewrapout}"
1.18      tv       1225:
1.98      lukem    1226: exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
1.4       tv       1227: EOF
1.98      lukem    1228:        [ "${runcmd}" = "echo" ] && echo EOF
                   1229:        ${runcmd} chmod +x "${makewrapper}"
1.105     lukem    1230:        statusmsg "makewrapper:      ${makewrapper}"
1.98      lukem    1231:        statusmsg "Updated ${makewrapper}"
1.84      lukem    1232: }
                   1233:
                   1234: buildtools()
                   1235: {
1.98      lukem    1236:        if [ "${MKOBJDIRS}" != "no" ]; then
                   1237:                ${runcmd} "${makewrapper}" ${parallel} obj-tools ||
                   1238:                    bomb "Failed to make obj-tools"
1.84      lukem    1239:        fi
1.98      lukem    1240:        ${runcmd} cd tools
1.124     lukem    1241:        if [ "${MKUPDATE}" = "no" ]; then
1.144     dsl      1242:                ${runcmd} "${makewrapper}" ${parallel} cleandir ||
                   1243:                    bomb "Failed to make cleandir tools"
1.84      lukem    1244:        fi
1.144     dsl      1245:        ${runcmd} "${makewrapper}" ${parallel} dependall ||
                   1246:            bomb "Failed to make dependall tools"
                   1247:        ${runcmd} "${makewrapper}" ${parallel} install ||
                   1248:            bomb "Failed to make install tools"
1.98      lukem    1249:        statusmsg "Tools built to ${TOOLDIR}"
1.110     lukem    1250:        ${runcmd} cd "${TOP}"
1.84      lukem    1251: }
1.4       tv       1252:
1.105     lukem    1253: getkernelconf()
1.84      lukem    1254: {
1.105     lukem    1255:        kernelconf="$1"
1.114     lukem    1256:        if [ "${MKOBJDIRS}" != "no" ]; then
1.84      lukem    1257:                # The correct value of KERNOBJDIR might
                   1258:                # depend on a prior "make obj" in
                   1259:                # ${KERNSRCDIR}/${KERNARCHDIR}/compile.
                   1260:                #
1.98      lukem    1261:                KERNSRCDIR="$(getmakevar KERNSRCDIR)"
                   1262:                KERNARCHDIR="$(getmakevar KERNARCHDIR)"
                   1263:                ${runcmd} cd "${KERNSRCDIR}/${KERNARCHDIR}/compile"
1.143     dsl      1264:                ${runcmd} "${makewrapper}" ${parallel} obj ||
1.98      lukem    1265:                    bomb "Failed to make obj in ${KERNSRCDIR}/${KERNARCHDIR}/compile"
                   1266:                ${runcmd} cd "${TOP}"
1.84      lukem    1267:        fi
1.98      lukem    1268:        KERNCONFDIR="$(getmakevar KERNCONFDIR)"
                   1269:        KERNOBJDIR="$(getmakevar KERNOBJDIR)"
1.105     lukem    1270:        case "${kernelconf}" in
1.84      lukem    1271:        */*)
1.105     lukem    1272:                kernelconfpath="${kernelconf}"
                   1273:                kernelconfname="${kernelconf##*/}"
1.84      lukem    1274:                ;;
                   1275:        *)
1.105     lukem    1276:                kernelconfpath="${KERNCONFDIR}/${kernelconf}"
                   1277:                kernelconfname="${kernelconf}"
1.84      lukem    1278:                ;;
                   1279:        esac
1.105     lukem    1280:        kernelbuildpath="${KERNOBJDIR}/${kernelconfname}"
                   1281: }
                   1282:
                   1283: buildkernel()
                   1284: {
                   1285:        if ! ${do_tools} && ! ${buildkernelwarned:-false}; then
                   1286:                # Building tools every time we build a kernel is clearly
                   1287:                # unnecessary.  We could try to figure out whether rebuilding
                   1288:                # the tools is necessary this time, but it doesn't seem worth
                   1289:                # the trouble.  Instead, we say it's the user's responsibility
                   1290:                # to rebuild the tools if necessary.
                   1291:                #
                   1292:                statusmsg "Building kernel without building new tools"
                   1293:                buildkernelwarned=true
                   1294:        fi
                   1295:        getkernelconf $1
                   1296:        statusmsg "Building kernel:  ${kernelconf}"
                   1297:        statusmsg "Build directory:  ${kernelbuildpath}"
                   1298:        ${runcmd} mkdir -p "${kernelbuildpath}" ||
                   1299:            bomb "Cannot mkdir: ${kernelbuildpath}"
1.124     lukem    1300:        if [ "${MKUPDATE}" = "no" ]; then
1.105     lukem    1301:                ${runcmd} cd "${kernelbuildpath}"
1.143     dsl      1302:                ${runcmd} "${makewrapper}" ${parallel} cleandir ||
1.105     lukem    1303:                    bomb "Failed to make cleandir in ${kernelbuildpath}"
1.98      lukem    1304:                ${runcmd} cd "${TOP}"
1.16      thorpej  1305:        fi
1.157     rillig   1306:        [ -x "${TOOLDIR}/bin/${toolprefix}config" ] \
                   1307:        || bomb "${TOOLDIR}/bin/${toolprefix}config does not exist. You need to \"$0 tools\" first."
1.105     lukem    1308:        ${runcmd} "${TOOLDIR}/bin/${toolprefix}config" -b "${kernelbuildpath}" \
                   1309:                -s "${TOP}/sys" "${kernelconfpath}" ||
                   1310:            bomb "${toolprefix}config failed for ${kernelconf}"
                   1311:        ${runcmd} cd "${kernelbuildpath}"
1.143     dsl      1312:        ${runcmd} "${makewrapper}" ${parallel} depend ||
1.105     lukem    1313:            bomb "Failed to make depend in ${kernelbuildpath}"
1.98      lukem    1314:        ${runcmd} "${makewrapper}" ${parallel} all ||
1.105     lukem    1315:            bomb "Failed to make all in ${kernelbuildpath}"
1.98      lukem    1316:        ${runcmd} cd "${TOP}"
1.91      lukem    1317:
1.98      lukem    1318:        if [ "${runcmd}" != "echo" ]; then
1.105     lukem    1319:                statusmsg "Kernels built from ${kernelconf}:"
                   1320:                kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
1.91      lukem    1321:                for kern in ${kernlist:-netbsd}; do
1.105     lukem    1322:                        [ -f "${kernelbuildpath}/${kern}" ] && \
                   1323:                            echo "  ${kernelbuildpath}/${kern}"
1.98      lukem    1324:                done | tee -a "${results}"
1.91      lukem    1325:        fi
1.84      lukem    1326: }
                   1327:
1.105     lukem    1328: releasekernel()
                   1329: {
                   1330:        getkernelconf $1
1.186     lukem    1331:        kernelreldir="${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/kernel"
1.105     lukem    1332:        ${runcmd} mkdir -p "${kernelreldir}"
                   1333:        kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
                   1334:        for kern in ${kernlist:-netbsd}; do
                   1335:                builtkern="${kernelbuildpath}/${kern}"
                   1336:                [ -f "${builtkern}" ] || continue
                   1337:                releasekern="${kernelreldir}/${kern}-${kernelconfname}.gz"
                   1338:                statusmsg "Kernel copy:      ${releasekern}"
1.196     lukem    1339:                if [ "${runcmd}" = "echo" ]; then
                   1340:                        echo "gzip -c -9 < ${builtkern} > ${releasekern}"
                   1341:                else
                   1342:                        gzip -c -9 < "${builtkern}" > "${releasekern}"
                   1343:                fi
1.105     lukem    1344:        done
                   1345: }
                   1346:
1.84      lukem    1347: installworld()
                   1348: {
                   1349:        dir="$1"
1.98      lukem    1350:        ${runcmd} "${makewrapper}" INSTALLWORLDDIR="${dir}" installworld ||
                   1351:            bomb "Failed to make installworld to ${dir}"
                   1352:        statusmsg "Successful installworld to ${dir}"
1.84      lukem    1353: }
                   1354:
                   1355:
                   1356: main()
                   1357: {
                   1358:        initdefaults
1.130     jmc      1359:        _args=$@
1.84      lukem    1360:        parseoptions "$@"
1.93      lukem    1361:
1.163     apb      1362:        sanitycheck
                   1363:
1.93      lukem    1364:        build_start=$(date)
1.98      lukem    1365:        statusmsg "${progname} command: $0 $@"
                   1366:        statusmsg "${progname} started: ${build_start}"
1.165     apb      1367:        statusmsg "NetBSD version:   ${DISTRIBVER}"
                   1368:        statusmsg "MACHINE:          ${MACHINE}"
                   1369:        statusmsg "MACHINE_ARCH:     ${MACHINE_ARCH}"
                   1370:        statusmsg "Build platform:   ${uname_s} ${uname_r} ${uname_m}"
1.153     apb      1371:        statusmsg "HOST_SH:          ${HOST_SH}"
                   1372:
1.84      lukem    1373:        rebuildmake
                   1374:        validatemakeparams
                   1375:        createmakewrapper
                   1376:
                   1377:        # Perform the operations.
                   1378:        #
1.98      lukem    1379:        for op in ${operations}; do
                   1380:                case "${op}" in
1.86      lukem    1381:
                   1382:                makewrapper)
                   1383:                        # no-op
                   1384:                        ;;
1.84      lukem    1385:
                   1386:                tools)
                   1387:                        buildtools
                   1388:                        ;;
                   1389:
1.125     lukem    1390:                sets)
                   1391:                        statusmsg "Building sets from pre-populated ${DESTDIR}"
                   1392:                        ${runcmd} "${makewrapper}" ${parallel} ${op} ||
                   1393:                            bomb "Failed to make ${op}"
1.186     lukem    1394:                        setdir=${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
                   1395:                        statusmsg "Built sets to ${setdir}"
1.125     lukem    1396:                        ;;
1.142     apb      1397:
1.195     lukem    1398:                cleandir|obj|build|distribution|release|sourcesets|syspkgs|params)
1.98      lukem    1399:                        ${runcmd} "${makewrapper}" ${parallel} ${op} ||
                   1400:                            bomb "Failed to make ${op}"
                   1401:                        statusmsg "Successful make ${op}"
1.84      lukem    1402:                        ;;
                   1403:
1.173     jnemeth  1404:                iso-image|iso-image-source)
                   1405:                        ${runcmd} "${makewrapper}" ${parallel} \
1.189     dyoung   1406:                            CDEXTRA="$iso_dir" ${op} ||
1.173     jnemeth  1407:                            bomb "Failed to make ${op}"
                   1408:                        statusmsg "Successful make ${op}"
                   1409:                        ;;
                   1410:
1.84      lukem    1411:                kernel=*)
                   1412:                        arg=${op#*=}
                   1413:                        buildkernel "${arg}"
1.105     lukem    1414:                        ;;
                   1415:
                   1416:                releasekernel=*)
                   1417:                        arg=${op#*=}
                   1418:                        releasekernel "${arg}"
1.84      lukem    1419:                        ;;
                   1420:
                   1421:                install=*)
                   1422:                        arg=${op#*=}
1.85      lukem    1423:                        if [ "${arg}" = "/" ] && \
                   1424:                            (   [ "${uname_s}" != "NetBSD" ] || \
1.98      lukem    1425:                                [ "${uname_m}" != "${MACHINE}" ] ); then
1.85      lukem    1426:                                bomb "'${op}' must != / for cross builds."
                   1427:                        fi
1.84      lukem    1428:                        installworld "${arg}"
1.70      lukem    1429:                        ;;
1.84      lukem    1430:
1.70      lukem    1431:                *)
1.84      lukem    1432:                        bomb "Unknown operation \`${op}'"
1.70      lukem    1433:                        ;;
1.84      lukem    1434:
1.70      lukem    1435:                esac
1.84      lukem    1436:        done
1.93      lukem    1437:
1.98      lukem    1438:        statusmsg "${progname} ended:   $(date)"
                   1439:        if [ -s "${results}" ]; then
                   1440:                echo "===> Summary of results:"
                   1441:                sed -e 's/^===>//;s/^/  /' "${results}"
                   1442:                echo "===> ."
                   1443:        fi
1.84      lukem    1444: }
                   1445:
                   1446: main "$@"

CVSweb <webmaster@jp.NetBSD.org>