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

Annotation of src/build.sh, Revision 1.203

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

CVSweb <webmaster@jp.NetBSD.org>