build.sh revision 1.157 1 #! /usr/bin/env sh
2 # $NetBSD: build.sh,v 1.157 2007/01/17 03:43:18 rillig Exp $
3 #
4 # Copyright (c) 2001-2005 The NetBSD Foundation, Inc.
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 # 3. All advertising materials mentioning features or use of this software
19 # must display the following acknowledgement:
20 # This product includes software developed by the NetBSD
21 # Foundation, Inc. and its contributors.
22 # 4. Neither the name of The NetBSD Foundation nor the names of its
23 # contributors may be used to endorse or promote products derived
24 # from this software without specific prior written permission.
25 #
26 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 # POSSIBILITY OF SUCH DAMAGE.
37 #
38 #
39 # Top level build wrapper, for a system containing no tools.
40 #
41 # This script should run on any POSIX-compliant shell. If the
42 # first "sh" found in the PATH is a POSIX-compliant shell, then
43 # you should not need to take any special action. Otherwise, you
44 # should set the environment variable HOST_SH to a POSIX-compliant
45 # shell, and invoke build.sh with that shell. (Depending on your
46 # system, one of /bin/ksh, /usr/local/bin/bash, or /usr/xpg4/bin/sh
47 # might be a suitable shell.)
48 #
49
50 progname=${0##*/}
51 toppid=$$
52 results=/dev/null
53 trap "exit 1" 1 2 3 15
54
55 bomb()
56 {
57 cat >&2 <<ERRORMESSAGE
58
59 ERROR: $@
60 *** BUILD ABORTED ***
61 ERRORMESSAGE
62 kill ${toppid} # in case we were invoked from a subshell
63 exit 1
64 }
65
66
67 statusmsg()
68 {
69 ${runcmd} echo "===> $@" | tee -a "${results}"
70 }
71
72 # Find a program in the PATH
73 find_in_PATH()
74 {
75 local prog="$1"
76 local oldIFS="${IFS}"
77 local dir
78 IFS=":"
79 for dir in ${PATH}; do
80 if [ -x "${dir}/${prog}" ]; then
81 prog="${dir}/${prog}"
82 break
83 fi
84 done
85 IFS="${oldIFS}"
86 echo "${prog}"
87 }
88
89 # Try to find a working POSIX shell, and set HOST_SH to refer to it.
90 # Assumes that uname_s, uname_m, and PWD have been set.
91 set_HOST_SH()
92 {
93 # Even if ${HOST_SH} is already defined, we still do the
94 # sanity checks at the end.
95
96 # Solaris has /usr/xpg4/bin/sh.
97 #
98 [ -z "${HOST_SH}" ] && [ x"${uname_s}" = x"SunOS" ] && \
99 [ -x /usr/xpg4/bin/sh ] && HOST_SH="/usr/xpg4/bin/sh"
100
101 # Try to get the name of the shell that's running this script,
102 # by parsing the output from "ps". We assume that, if the host
103 # system's ps command supports -o comm at all, it will do so
104 # in the usual way: a one-line header followed by a one-line
105 # result, possibly including trailing white space. And if the
106 # host system's ps command doesn't support -o comm, we assume
107 # that we'll get an error message on stderr and nothing on
108 # stdout. (We don't try to use ps -o 'comm=' to suppress the
109 # header line, because that is less widely supported.)
110 #
111 # If we get the wrong result here, the user can override it by
112 # specifying HOST_SH in the environment.
113 #
114 [ -z "${HOST_SH}" ] && HOST_SH="$(
115 (ps -p $$ -o comm | sed -ne '2s/[ \t]*$//p') 2>/dev/null )"
116
117 # If nothing above worked, use "sh". We will later find the
118 # first directory in the PATH that has a "sh" program.
119 #
120 [ -z "${HOST_SH}" ] && HOST_SH="sh"
121
122 # If the result so far is not an absolute path, try to prepend
123 # PWD or search the PATH.
124 #
125 case "${HOST_SH}" in
126 /*) :
127 ;;
128 */*) HOST_SH="${PWD}/${HOST_SH}"
129 ;;
130 *) HOST_SH="$(find_in_PATH "${HOST_SH}")"
131 ;;
132 esac
133
134 # If we don't have an absolute path by now, bomb.
135 #
136 case "${HOST_SH}" in
137 /*) :
138 ;;
139 *) bomb "HOST_SH=\"${HOST_SH}\" is not an absolute path."
140 ;;
141 esac
142
143 # If HOST_SH is not executable, bomb.
144 #
145 [ -x "${HOST_SH}" ] ||
146 bomb "HOST_SH=\"${HOST_SH}\" is not executable."
147 }
148
149 initdefaults()
150 {
151 [ -d usr.bin/make ] || cd "$(dirname $0)"
152 [ -d usr.bin/make ] ||
153 bomb "build.sh must be run from the top source level"
154 [ -f share/mk/bsd.own.mk ] ||
155 bomb "src/share/mk is missing; please re-fetch the source tree"
156
157 uname_s=$(uname -s 2>/dev/null)
158 uname_m=$(uname -m 2>/dev/null)
159
160 # If $PWD is a valid name of the current directory, POSIX mandates
161 # that pwd return it by default which causes problems in the
162 # presence of symlinks. Unsetting PWD is simpler than changing
163 # every occurrence of pwd to use -P.
164 #
165 # XXX Except that doesn't work on Solaris. Or many Linuces.
166 #
167 unset PWD
168 TOP=$(/bin/pwd -P 2>/dev/null || /bin/pwd 2>/dev/null)
169
170 # The user can set HOST_SH in the environment, or we try to
171 # guess an appropriate value. Then we set several other
172 # variables from HOST_SH.
173 #
174 set_HOST_SH
175 setmakeenv HOST_SH "${HOST_SH}"
176 setmakeenv BSHELL "${HOST_SH}"
177 setmakeenv CONFIG_SHELL "${HOST_SH}"
178
179 # Set defaults.
180 #
181 toolprefix=nb
182
183 # Some systems have a small ARG_MAX. -X prevents make(1) from
184 # exporting variables in the environment redundantly.
185 #
186 case "${uname_s}" in
187 Darwin | FreeBSD | CYGWIN*)
188 MAKEFLAGS=-X
189 ;;
190 *)
191 MAKEFLAGS=
192 ;;
193 esac
194
195 makeenv=
196 makewrapper=
197 makewrappermachine=
198 runcmd=
199 operations=
200 removedirs=
201 do_expertmode=false
202 do_rebuildmake=false
203 do_removedirs=false
204
205 # do_{operation}=true if given operation is requested.
206 #
207 do_tools=false
208 do_obj=false
209 do_build=false
210 do_distribution=false
211 do_release=false
212 do_kernel=false
213 do_releasekernel=false
214 do_install=false
215 do_sets=false
216 do_sourcesets=false
217 do_syspkgs=false
218 do_iso_image=false
219 do_params=false
220
221 # Create scratch directory
222 #
223 tmpdir="${TMPDIR-/tmp}/nbbuild$$"
224 mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
225 trap "cd /; rm -r -f \"${tmpdir}\"" 0
226 results="${tmpdir}/build.sh.results"
227
228 # Set source directories
229 #
230 setmakeenv NETBSDSRCDIR "${TOP}"
231
232 # Set various environment variables to known defaults,
233 # to minimize (cross-)build problems observed "in the field".
234 #
235 unsetmakeenv INFODIR
236 unsetmakeenv LESSCHARSET
237 setmakeenv LC_ALL C
238 }
239
240 getarch()
241 {
242 # Translate a MACHINE into a default MACHINE_ARCH.
243 #
244 case "${MACHINE}" in
245
246 acorn26|acorn32|cats|evbarm|hpcarm|iyonix|netwinder|shark|zaurus)
247 MACHINE_ARCH=arm
248 ;;
249
250 hp700)
251 MACHINE_ARCH=hppa
252 ;;
253
254 sun2)
255 MACHINE_ARCH=m68000
256 ;;
257
258 amiga|atari|cesfic|hp300|luna68k|mac68k|mvme68k|news68k|next68k|sun3|x68k)
259 MACHINE_ARCH=m68k
260 ;;
261
262 evbmips-e[bl]|sbmips-e[bl])
263 MACHINE_ARCH=mips${MACHINE##*-}
264 makewrappermachine=${MACHINE}
265 MACHINE=${MACHINE%-e[bl]}
266 ;;
267
268 evbmips64-e[bl]|sbmips64-e[bl])
269 MACHINE_ARCH=mips64${MACHINE##*-}
270 makewrappermachine=${MACHINE}
271 MACHINE=${MACHINE%64-e[bl]}
272 ;;
273
274 evbmips|sbmips) # no default MACHINE_ARCH
275 ;;
276
277 ews4800mips|mipsco|newsmips|sgimips)
278 MACHINE_ARCH=mipseb
279 ;;
280
281 algor|arc|cobalt|hpcmips|playstation2|pmax)
282 MACHINE_ARCH=mipsel
283 ;;
284
285 pc532)
286 MACHINE_ARCH=ns32k
287 ;;
288
289 amigappc|bebox|evbppc|ibmnws|macppc|mvmeppc|ofppc|pmppc|prep|sandpoint)
290 MACHINE_ARCH=powerpc
291 ;;
292
293 evbsh3-e[bl])
294 MACHINE_ARCH=sh3${MACHINE##*-}
295 makewrappermachine=${MACHINE}
296 MACHINE=${MACHINE%-e[bl]}
297 ;;
298
299 evbsh3) # no default MACHINE_ARCH
300 ;;
301
302 mmeye)
303 MACHINE_ARCH=sh3eb
304 ;;
305
306 dreamcast|hpcsh|landisk)
307 MACHINE_ARCH=sh3el
308 ;;
309
310 evbsh5)
311 MACHINE_ARCH=sh5el
312 ;;
313 amd64)
314 MACHINE_ARCH=x86_64
315 ;;
316
317 alpha|i386|sparc|sparc64|vax|ia64)
318 MACHINE_ARCH=${MACHINE}
319 ;;
320
321 *)
322 bomb "Unknown target MACHINE: ${MACHINE}"
323 ;;
324
325 esac
326 }
327
328 validatearch()
329 {
330 # Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
331 #
332 case "${MACHINE_ARCH}" in
333
334 alpha|arm|armeb|hppa|i386|m68000|m68k|mipse[bl]|mips64e[bl]|ns32k|powerpc|powerpc64|sh[35]e[bl]|sparc|sparc64|vax|x86_64|ia64)
335 ;;
336
337 "")
338 bomb "No MACHINE_ARCH provided"
339 ;;
340
341 *)
342 bomb "Unknown target MACHINE_ARCH: ${MACHINE_ARCH}"
343 ;;
344
345 esac
346
347 # Determine valid MACHINE_ARCHs for MACHINE
348 #
349 case "${MACHINE}" in
350
351 evbarm)
352 arches="arm armeb"
353 ;;
354
355 evbmips|sbmips)
356 arches="mipseb mipsel mips64eb mips64el"
357 ;;
358
359 sgimips)
360 arches="mipseb mips64eb"
361 ;;
362
363 evbsh3)
364 arches="sh3eb sh3el"
365 ;;
366
367 evbsh5)
368 arches="sh5eb sh5el"
369 ;;
370
371 macppc|evbppc)
372 arches="powerpc powerpc64"
373 ;;
374 *)
375 oma="${MACHINE_ARCH}"
376 getarch
377 arches="${MACHINE_ARCH}"
378 MACHINE_ARCH="${oma}"
379 ;;
380
381 esac
382
383 # Ensure that MACHINE_ARCH supports MACHINE
384 #
385 archok=false
386 for a in ${arches}; do
387 if [ "${a}" = "${MACHINE_ARCH}" ]; then
388 archok=true
389 break
390 fi
391 done
392 ${archok} ||
393 bomb "MACHINE_ARCH '${MACHINE_ARCH}' does not support MACHINE '${MACHINE}'"
394 }
395
396 raw_getmakevar()
397 {
398 [ -x "${make}" ] || bomb "raw_getmakevar $1: ${make} is not executable"
399 "${make}" -m ${TOP}/share/mk -s -f- _x_ <<EOF || bomb "raw_getmakevar $1: ${make} failed"
400 _x_:
401 echo \${$1}
402 .include <bsd.prog.mk>
403 .include <bsd.kernobj.mk>
404 EOF
405 }
406
407 getmakevar()
408 {
409 # raw_getmakevar() doesn't work properly if $make hasn't yet been
410 # built, which can happen when running with the "-n" option.
411 # getmakevar() deals with this by emitting a literal '$'
412 # followed by the variable name, instead of trying to find the
413 # variable's value.
414 #
415 if [ -x "${make}" ]; then
416 raw_getmakevar "$1"
417 else
418 echo "\$$1"
419 fi
420 }
421
422 setmakeenv()
423 {
424 eval "$1='$2'; export $1"
425 makeenv="${makeenv} $1"
426 }
427
428 unsetmakeenv()
429 {
430 eval "unset $1"
431 makeenv="${makeenv} $1"
432 }
433
434 # Convert possibly-relative path to absolute path by prepending
435 # ${TOP} if necessary. Also delete trailing "/", if any.
436 resolvepath()
437 {
438 case "${OPTARG}" in
439 /)
440 ;;
441 /*)
442 OPTARG="${OPTARG%/}"
443 ;;
444 *)
445 OPTARG="${TOP}/${OPTARG%/}"
446 ;;
447 esac
448 }
449
450 usage()
451 {
452 if [ -n "$*" ]; then
453 echo ""
454 echo "${progname}: $*"
455 fi
456 cat <<_usage_
457
458 Usage: ${progname} [-EnorUux] [-a arch] [-B buildid] [-D dest] [-j njob]
459 [-M obj] [-m mach] [-N noisy] [-O obj] [-R release] [-T tools]
460 [-V var=[value]] [-w wrapper] [-X x11src] [-Z var]
461 operation [...]
462
463 Build operations (all imply "obj" and "tools"):
464 build Run "make build".
465 distribution Run "make distribution" (includes DESTDIR/etc/ files).
466 release Run "make release" (includes kernels & distrib media).
467
468 Other operations:
469 help Show this message and exit.
470 makewrapper Create ${toolprefix}make-\${MACHINE} wrapper and ${toolprefix}make.
471 Always performed.
472 obj Run "make obj". [Default unless -o is used]
473 tools Build and install tools.
474 install=idir Run "make installworld" to \`idir' to install all sets
475 except \`etc'. Useful after "distribution" or "release"
476 kernel=conf Build kernel with config file \`conf'
477 releasekernel=conf Install kernel built by kernel=conf to RELEASEDIR.
478 sets Create binary sets in RELEASEDIR/MACHINE/binary/sets.
479 DESTDIR should be populated beforehand.
480 sourcesets Create source sets in RELEASEDIR/source/sets.
481 syspkgs Create syspkgs in RELEASEDIR/MACHINE/binary/syspkgs.
482 iso-image Create CD-ROM image in RELEASEDIR/MACHINE/installation.
483 params Display various make(1) parameters.
484
485 Options:
486 -a arch Set MACHINE_ARCH to arch. [Default: deduced from MACHINE]
487 -B buildId Set BUILDID to buildId.
488 -D dest Set DESTDIR to dest. [Default: destdir.MACHINE]
489 -E Set "expert" mode; disables various safety checks.
490 Should not be used without expert knowledge of the build system.
491 -h Print this help message.
492 -j njob Run up to njob jobs in parallel; see make(1) -j.
493 -M obj Set obj root directory to obj; sets MAKEOBJDIRPREFIX.
494 Unsets MAKEOBJDIR.
495 -m mach Set MACHINE to mach; not required if NetBSD native.
496 -N noisy Set the noisyness (MAKEVERBOSE) level of the build:
497 0 Quiet
498 1 Operations are described, commands are suppressed
499 2 Full output
500 [Default: 2]
501 -n Show commands that would be executed, but do not execute them.
502 -O obj Set obj root directory to obj; sets a MAKEOBJDIR pattern.
503 Unsets MAKEOBJDIRPREFIX.
504 -o Set MKOBJDIRS=no; do not create objdirs at start of build.
505 -R release Set RELEASEDIR to release. [Default: releasedir]
506 -r Remove contents of TOOLDIR and DESTDIR before building.
507 -T tools Set TOOLDIR to tools. If unset, and TOOLDIR is not set in
508 the environment, ${toolprefix}make will be (re)built unconditionally.
509 -U Set MKUNPRIVED=yes; build without requiring root privileges,
510 install from an UNPRIVED build with proper file permissions.
511 -u Set MKUPDATE=yes; do not run "make clean" first.
512 Without this, everything is rebuilt, including the tools.
513 -V v=[val] Set variable \`v' to \`val'.
514 -w wrapper Create ${toolprefix}make script as wrapper.
515 [Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE}]
516 -X x11src Set X11SRCDIR to x11src. [Default: /usr/xsrc]
517 -x Set MKX11=yes; build X11R6 from X11SRCDIR
518 -Z v Unset ("zap") variable \`v'.
519
520 _usage_
521 exit 1
522 }
523
524 parseoptions()
525 {
526 opts='a:B:bD:dEhi:j:k:M:m:N:nO:oR:rT:tUuV:w:xX:Z:'
527 opt_a=no
528
529 if type getopts >/dev/null 2>&1; then
530 # Use POSIX getopts.
531 #
532 getoptcmd='getopts ${opts} opt && opt=-${opt}'
533 optargcmd=':'
534 optremcmd='shift $((${OPTIND} -1))'
535 else
536 type getopt >/dev/null 2>&1 ||
537 bomb "/bin/sh shell is too old; try ksh or bash"
538
539 # Use old-style getopt(1) (doesn't handle whitespace in args).
540 #
541 args="$(getopt ${opts} $*)"
542 [ $? = 0 ] || usage
543 set -- ${args}
544
545 getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
546 optargcmd='OPTARG="$1"; shift'
547 optremcmd=':'
548 fi
549
550 # Parse command line options.
551 #
552 while eval ${getoptcmd}; do
553 case ${opt} in
554
555 -a)
556 eval ${optargcmd}
557 MACHINE_ARCH=${OPTARG}
558 opt_a=yes
559 ;;
560
561 -B)
562 eval ${optargcmd}
563 BUILDID=${OPTARG}
564 ;;
565
566 -b)
567 usage "'-b' has been replaced by 'makewrapper'"
568 ;;
569
570 -D)
571 eval ${optargcmd}; resolvepath
572 setmakeenv DESTDIR "${OPTARG}"
573 ;;
574
575 -d)
576 usage "'-d' has been replaced by 'distribution'"
577 ;;
578
579 -E)
580 do_expertmode=true
581 ;;
582
583 -i)
584 usage "'-i idir' has been replaced by 'install=idir'"
585 ;;
586
587 -j)
588 eval ${optargcmd}
589 parallel="-j ${OPTARG}"
590 ;;
591
592 -k)
593 usage "'-k conf' has been replaced by 'kernel=conf'"
594 ;;
595
596 -M)
597 eval ${optargcmd}; resolvepath
598 makeobjdir="${OPTARG}"
599 unsetmakeenv MAKEOBJDIR
600 setmakeenv MAKEOBJDIRPREFIX "${OPTARG}"
601 ;;
602
603 # -m overrides MACHINE_ARCH unless "-a" is specified
604 -m)
605 eval ${optargcmd}
606 MACHINE="${OPTARG}"
607 [ "${opt_a}" != "yes" ] && getarch
608 ;;
609
610 -N)
611 eval ${optargcmd}
612 case "${OPTARG}" in
613 0|1|2)
614 setmakeenv MAKEVERBOSE "${OPTARG}"
615 ;;
616 *)
617 usage "'${OPTARG}' is not a valid value for -N"
618 ;;
619 esac
620 ;;
621
622 -n)
623 runcmd=echo
624 ;;
625
626 -O)
627 eval ${optargcmd}; resolvepath
628 makeobjdir="${OPTARG}"
629 unsetmakeenv MAKEOBJDIRPREFIX
630 setmakeenv MAKEOBJDIR "\${.CURDIR:C,^$TOP,$OPTARG,}"
631 ;;
632
633 -o)
634 MKOBJDIRS=no
635 ;;
636
637 -R)
638 eval ${optargcmd}; resolvepath
639 setmakeenv RELEASEDIR "${OPTARG}"
640 ;;
641
642 -r)
643 do_removedirs=true
644 do_rebuildmake=true
645 ;;
646
647 -T)
648 eval ${optargcmd}; resolvepath
649 TOOLDIR="${OPTARG}"
650 export TOOLDIR
651 ;;
652
653 -t)
654 usage "'-t' has been replaced by 'tools'"
655 ;;
656
657 -U)
658 setmakeenv MKUNPRIVED yes
659 ;;
660
661 -u)
662 setmakeenv MKUPDATE yes
663 ;;
664
665 -V)
666 eval ${optargcmd}
667 case "${OPTARG}" in
668 # XXX: consider restricting which variables can be changed?
669 [a-zA-Z_][a-zA-Z_0-9]*=*)
670 setmakeenv "${OPTARG%%=*}" "${OPTARG#*=}"
671 ;;
672 *)
673 usage "-V argument must be of the form 'var=[value]'"
674 ;;
675 esac
676 ;;
677
678 -w)
679 eval ${optargcmd}; resolvepath
680 makewrapper="${OPTARG}"
681 ;;
682
683 -X)
684 eval ${optargcmd}; resolvepath
685 setmakeenv X11SRCDIR "${OPTARG}"
686 ;;
687
688 -x)
689 setmakeenv MKX11 yes
690 ;;
691
692 -Z)
693 eval ${optargcmd}
694 # XXX: consider restricting which variables can be unset?
695 unsetmakeenv "${OPTARG}"
696 ;;
697
698 --)
699 break
700 ;;
701
702 -'?'|-h)
703 usage
704 ;;
705
706 esac
707 done
708
709 # Validate operations.
710 #
711 eval ${optremcmd}
712 while [ $# -gt 0 ]; do
713 op=$1; shift
714 operations="${operations} ${op}"
715
716 case "${op}" in
717
718 help)
719 usage
720 ;;
721
722 makewrapper|obj|tools|build|distribution|release|sets|sourcesets|syspkgs|params)
723 ;;
724
725 iso-image)
726 op=iso_image # used as part of a variable name
727 ;;
728
729 kernel=*|releasekernel=*)
730 arg=${op#*=}
731 op=${op%%=*}
732 [ -n "${arg}" ] ||
733 bomb "Must supply a kernel name with \`${op}=...'"
734 ;;
735
736 install=*)
737 arg=${op#*=}
738 op=${op%%=*}
739 [ -n "${arg}" ] ||
740 bomb "Must supply a directory with \`install=...'"
741 ;;
742
743 *)
744 usage "Unknown operation \`${op}'"
745 ;;
746
747 esac
748 eval do_${op}=true
749 done
750 [ -n "${operations}" ] || usage "Missing operation to perform."
751
752 # Set up MACHINE*. On a NetBSD host, these are allowed to be unset.
753 #
754 if [ -z "${MACHINE}" ]; then
755 [ "${uname_s}" = "NetBSD" ] ||
756 bomb "MACHINE must be set, or -m must be used, for cross builds."
757 MACHINE=${uname_m}
758 fi
759 [ -n "${MACHINE_ARCH}" ] || getarch
760 validatearch
761
762 # Set up default make(1) environment.
763 #
764 makeenv="${makeenv} TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
765 [ -z "${BUILDID}" ] || makeenv="${makeenv} BUILDID"
766 MAKEFLAGS="-de -m ${TOP}/share/mk ${MAKEFLAGS} MKOBJDIRS=${MKOBJDIRS-yes}"
767 export MAKEFLAGS MACHINE MACHINE_ARCH
768 }
769
770 rebuildmake()
771 {
772 # Test make source file timestamps against installed ${toolprefix}make
773 # binary, if TOOLDIR is pre-set.
774 #
775 # Note that we do NOT try to grovel "mk.conf" here to find out if
776 # TOOLDIR is set there, because it can contain make variable
777 # expansions and other stuff only parseable *after* we have a working
778 # ${toolprefix}make. So this logic can only work if the user has
779 # pre-set TOOLDIR in the environment or used the -T option to build.sh.
780 #
781 make="${TOOLDIR-nonexistent}/bin/${toolprefix}make"
782 if [ -x "${make}" ]; then
783 for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
784 if [ "${f}" -nt "${make}" ]; then
785 statusmsg "${make} outdated (older than ${f}), needs building."
786 do_rebuildmake=true
787 break
788 fi
789 done
790 else
791 statusmsg "No ${make}, needs building."
792 do_rebuildmake=true
793 fi
794
795 # Build bootstrap ${toolprefix}make if needed.
796 if ${do_rebuildmake}; then
797 statusmsg "Bootstrapping ${toolprefix}make"
798 ${runcmd} cd "${tmpdir}"
799 ${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
800 CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
801 ${HOST_SH} "${TOP}/tools/make/configure" ||
802 bomb "Configure of ${toolprefix}make failed"
803 ${runcmd} ${HOST_SH} buildmake.sh ||
804 bomb "Build of ${toolprefix}make failed"
805 make="${tmpdir}/${toolprefix}make"
806 ${runcmd} cd "${TOP}"
807 ${runcmd} rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
808 fi
809 }
810
811 validatemakeparams()
812 {
813 if [ "${runcmd}" = "echo" ]; then
814 TOOLCHAIN_MISSING=no
815 EXTERNAL_TOOLCHAIN=""
816 else
817 TOOLCHAIN_MISSING=$(raw_getmakevar TOOLCHAIN_MISSING)
818 EXTERNAL_TOOLCHAIN=$(raw_getmakevar EXTERNAL_TOOLCHAIN)
819 fi
820 if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
821 [ -z "${EXTERNAL_TOOLCHAIN}" ]; then
822 ${runcmd} echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
823 ${runcmd} echo " MACHINE: ${MACHINE}"
824 ${runcmd} echo " MACHINE_ARCH: ${MACHINE_ARCH}"
825 ${runcmd} echo ""
826 ${runcmd} echo "All builds for this platform should be done via a traditional make"
827 ${runcmd} echo "If you wish to use an external cross-toolchain, set"
828 ${runcmd} echo " EXTERNAL_TOOLCHAIN=<path to toolchain root>"
829 ${runcmd} echo "in either the environment or mk.conf and rerun"
830 ${runcmd} echo " ${progname} $*"
831 exit 1
832 fi
833
834 # Normalise MKOBJDIRS, MKUNPRIVED, and MKUPDATE
835 # These may be set as build.sh options or in "mk.conf".
836 # Don't export them as they're only used for tests in build.sh.
837 #
838 MKOBJDIRS=$(getmakevar MKOBJDIRS)
839 MKUNPRIVED=$(getmakevar MKUNPRIVED)
840 MKUPDATE=$(getmakevar MKUPDATE)
841
842 if [ "${MKOBJDIRS}" != "no" ]; then
843 # If setting -M or -O to the root of an obj dir, make sure
844 # the base directory is made before continuing as <bsd.own.mk>
845 # will need this to pick up _SRC_TOP_OBJ_
846 #
847 if [ ! -z "${makeobjdir}" ]; then
848 ${runcmd} mkdir -p "${makeobjdir}"
849 fi
850
851 # make obj in tools to ensure that the objdir for the top-level
852 # of the source tree and for "tools" is available, in case the
853 # default TOOLDIR setting from <bsd.own.mk> is used, or the
854 # build.sh default DESTDIR and RELEASEDIR is to be used.
855 #
856 ${runcmd} cd tools
857 ${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
858 bomb "Failed to make obj in tools"
859 ${runcmd} cd "${TOP}"
860 fi
861
862 statusmsg "MACHINE: ${MACHINE}"
863 statusmsg "MACHINE_ARCH: ${MACHINE_ARCH}"
864
865 # Find TOOLDIR, DESTDIR, and RELEASEDIR.
866 #
867 TOOLDIR=$(getmakevar TOOLDIR)
868 statusmsg "TOOLDIR path: ${TOOLDIR}"
869 DESTDIR=$(getmakevar DESTDIR)
870 RELEASEDIR=$(getmakevar RELEASEDIR)
871 if ! $do_expertmode; then
872 _SRC_TOP_OBJ_=$(getmakevar _SRC_TOP_OBJ_)
873 : ${DESTDIR:=${_SRC_TOP_OBJ_}/destdir.${MACHINE}}
874 : ${RELEASEDIR:=${_SRC_TOP_OBJ_}/releasedir}
875 makeenv="${makeenv} DESTDIR RELEASEDIR"
876 fi
877 export TOOLDIR DESTDIR RELEASEDIR
878 statusmsg "DESTDIR path: ${DESTDIR}"
879 statusmsg "RELEASEDIR path: ${RELEASEDIR}"
880
881 # Check validity of TOOLDIR and DESTDIR.
882 #
883 if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = "/" ]; then
884 bomb "TOOLDIR '${TOOLDIR}' invalid"
885 fi
886 removedirs="${TOOLDIR}"
887
888 if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = "/" ]; then
889 if ${do_build} || ${do_distribution} || ${do_release}; then
890 if ! ${do_build} || \
891 [ "${uname_s}" != "NetBSD" ] || \
892 [ "${uname_m}" != "${MACHINE}" ]; then
893 bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
894 fi
895 if ! ${do_expertmode}; then
896 bomb "DESTDIR must != / for non -E (expert) builds"
897 fi
898 statusmsg "WARNING: Building to /, in expert mode."
899 statusmsg " This may cause your system to break! Reasons include:"
900 statusmsg " - your kernel is not up to date"
901 statusmsg " - the libraries or toolchain have changed"
902 statusmsg " YOU HAVE BEEN WARNED!"
903 fi
904 else
905 removedirs="${removedirs} ${DESTDIR}"
906 fi
907 if ${do_build} || ${do_distribution} || ${do_release}; then
908 if ! ${do_expertmode} && \
909 [ "$(id -u 2>/dev/null)" -ne 0 ] && \
910 [ "${MKUNPRIVED}" = "no" ] ; then
911 bomb "-U or -E must be set for build as an unprivileged user."
912 fi
913 fi
914 if ${do_releasekernel} && [ -z "${RELEASEDIR}" ]; then
915 bomb "Must set RELEASEDIR with \`releasekernel=...'"
916 fi
917 }
918
919
920 createmakewrapper()
921 {
922 # Remove the target directories.
923 #
924 if ${do_removedirs}; then
925 for f in ${removedirs}; do
926 statusmsg "Removing ${f}"
927 ${runcmd} rm -r -f "${f}"
928 done
929 fi
930
931 # Recreate $TOOLDIR.
932 #
933 ${runcmd} mkdir -p "${TOOLDIR}/bin" ||
934 bomb "mkdir of '${TOOLDIR}/bin' failed"
935
936 # Install ${toolprefix}make if it was built.
937 #
938 if ${do_rebuildmake}; then
939 ${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
940 ${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
941 bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
942 make="${TOOLDIR}/bin/${toolprefix}make"
943 statusmsg "Created ${make}"
944 fi
945
946 # Build a ${toolprefix}make wrapper script, usable by hand as
947 # well as by build.sh.
948 #
949 if [ -z "${makewrapper}" ]; then
950 makewrapper="${TOOLDIR}/bin/${toolprefix}make-${makewrappermachine:-${MACHINE}}"
951 [ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
952 fi
953
954 ${runcmd} rm -f "${makewrapper}"
955 if [ "${runcmd}" = "echo" ]; then
956 echo 'cat <<EOF >'${makewrapper}
957 makewrapout=
958 else
959 makewrapout=">>\${makewrapper}"
960 fi
961
962 case "${KSH_VERSION:-${SH_VERSION}}" in
963 *PD\ KSH*|*MIRBSD\ KSH*)
964 set +o braceexpand
965 ;;
966 esac
967
968 eval cat <<EOF ${makewrapout}
969 #! ${HOST_SH}
970 # Set proper variables to allow easy "make" building of a NetBSD subtree.
971 # Generated from: \$NetBSD: build.sh,v 1.157 2007/01/17 03:43:18 rillig Exp $
972 # with these arguments: ${_args}
973 #
974 EOF
975 for f in ${makeenv}; do
976 if eval "[ -z \"\${$f}\" -a \"\${${f}-X}\" = \"X\" ]"; then
977 eval echo "unset ${f}" ${makewrapout}
978 else
979 eval echo "${f}=\'\$$(echo ${f})\'\;\ export\ ${f}" ${makewrapout}
980 fi
981 done
982
983 eval cat <<EOF ${makewrapout}
984 MAKEWRAPPERMACHINE=${makewrappermachine:-${MACHINE}}; export MAKEWRAPPERMACHINE
985 USETOOLS=yes; export USETOOLS
986
987 exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
988 EOF
989 [ "${runcmd}" = "echo" ] && echo EOF
990 ${runcmd} chmod +x "${makewrapper}"
991 statusmsg "makewrapper: ${makewrapper}"
992 statusmsg "Updated ${makewrapper}"
993 }
994
995 buildtools()
996 {
997 if [ "${MKOBJDIRS}" != "no" ]; then
998 ${runcmd} "${makewrapper}" ${parallel} obj-tools ||
999 bomb "Failed to make obj-tools"
1000 fi
1001 ${runcmd} cd tools
1002 if [ "${MKUPDATE}" = "no" ]; then
1003 ${runcmd} "${makewrapper}" ${parallel} cleandir ||
1004 bomb "Failed to make cleandir tools"
1005 fi
1006 ${runcmd} "${makewrapper}" ${parallel} dependall ||
1007 bomb "Failed to make dependall tools"
1008 ${runcmd} "${makewrapper}" ${parallel} install ||
1009 bomb "Failed to make install tools"
1010 statusmsg "Tools built to ${TOOLDIR}"
1011 ${runcmd} cd "${TOP}"
1012 }
1013
1014 getkernelconf()
1015 {
1016 kernelconf="$1"
1017 if [ "${MKOBJDIRS}" != "no" ]; then
1018 # The correct value of KERNOBJDIR might
1019 # depend on a prior "make obj" in
1020 # ${KERNSRCDIR}/${KERNARCHDIR}/compile.
1021 #
1022 KERNSRCDIR="$(getmakevar KERNSRCDIR)"
1023 KERNARCHDIR="$(getmakevar KERNARCHDIR)"
1024 ${runcmd} cd "${KERNSRCDIR}/${KERNARCHDIR}/compile"
1025 ${runcmd} "${makewrapper}" ${parallel} obj ||
1026 bomb "Failed to make obj in ${KERNSRCDIR}/${KERNARCHDIR}/compile"
1027 ${runcmd} cd "${TOP}"
1028 fi
1029 KERNCONFDIR="$(getmakevar KERNCONFDIR)"
1030 KERNOBJDIR="$(getmakevar KERNOBJDIR)"
1031 case "${kernelconf}" in
1032 */*)
1033 kernelconfpath="${kernelconf}"
1034 kernelconfname="${kernelconf##*/}"
1035 ;;
1036 *)
1037 kernelconfpath="${KERNCONFDIR}/${kernelconf}"
1038 kernelconfname="${kernelconf}"
1039 ;;
1040 esac
1041 kernelbuildpath="${KERNOBJDIR}/${kernelconfname}"
1042 }
1043
1044 buildkernel()
1045 {
1046 if ! ${do_tools} && ! ${buildkernelwarned:-false}; then
1047 # Building tools every time we build a kernel is clearly
1048 # unnecessary. We could try to figure out whether rebuilding
1049 # the tools is necessary this time, but it doesn't seem worth
1050 # the trouble. Instead, we say it's the user's responsibility
1051 # to rebuild the tools if necessary.
1052 #
1053 statusmsg "Building kernel without building new tools"
1054 buildkernelwarned=true
1055 fi
1056 getkernelconf $1
1057 statusmsg "Building kernel: ${kernelconf}"
1058 statusmsg "Build directory: ${kernelbuildpath}"
1059 ${runcmd} mkdir -p "${kernelbuildpath}" ||
1060 bomb "Cannot mkdir: ${kernelbuildpath}"
1061 if [ "${MKUPDATE}" = "no" ]; then
1062 ${runcmd} cd "${kernelbuildpath}"
1063 ${runcmd} "${makewrapper}" ${parallel} cleandir ||
1064 bomb "Failed to make cleandir in ${kernelbuildpath}"
1065 ${runcmd} cd "${TOP}"
1066 fi
1067 [ -x "${TOOLDIR}/bin/${toolprefix}config" ] \
1068 || bomb "${TOOLDIR}/bin/${toolprefix}config does not exist. You need to \"$0 tools\" first."
1069 ${runcmd} "${TOOLDIR}/bin/${toolprefix}config" -b "${kernelbuildpath}" \
1070 -s "${TOP}/sys" "${kernelconfpath}" ||
1071 bomb "${toolprefix}config failed for ${kernelconf}"
1072 ${runcmd} cd "${kernelbuildpath}"
1073 ${runcmd} "${makewrapper}" ${parallel} depend ||
1074 bomb "Failed to make depend in ${kernelbuildpath}"
1075 ${runcmd} "${makewrapper}" ${parallel} all ||
1076 bomb "Failed to make all in ${kernelbuildpath}"
1077 ${runcmd} cd "${TOP}"
1078
1079 if [ "${runcmd}" != "echo" ]; then
1080 statusmsg "Kernels built from ${kernelconf}:"
1081 kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
1082 for kern in ${kernlist:-netbsd}; do
1083 [ -f "${kernelbuildpath}/${kern}" ] && \
1084 echo " ${kernelbuildpath}/${kern}"
1085 done | tee -a "${results}"
1086 fi
1087 }
1088
1089 releasekernel()
1090 {
1091 getkernelconf $1
1092 kernelreldir="${RELEASEDIR}/${MACHINE}/binary/kernel"
1093 ${runcmd} mkdir -p "${kernelreldir}"
1094 kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
1095 for kern in ${kernlist:-netbsd}; do
1096 builtkern="${kernelbuildpath}/${kern}"
1097 [ -f "${builtkern}" ] || continue
1098 releasekern="${kernelreldir}/${kern}-${kernelconfname}.gz"
1099 statusmsg "Kernel copy: ${releasekern}"
1100 ${runcmd} gzip -c -9 < "${builtkern}" > "${releasekern}"
1101 done
1102 }
1103
1104 installworld()
1105 {
1106 dir="$1"
1107 ${runcmd} "${makewrapper}" INSTALLWORLDDIR="${dir}" installworld ||
1108 bomb "Failed to make installworld to ${dir}"
1109 statusmsg "Successful installworld to ${dir}"
1110 }
1111
1112
1113 main()
1114 {
1115 initdefaults
1116 _args=$@
1117 parseoptions "$@"
1118
1119 build_start=$(date)
1120 statusmsg "${progname} command: $0 $@"
1121 statusmsg "${progname} started: ${build_start}"
1122
1123 statusmsg "HOST_SH: ${HOST_SH}"
1124
1125 rebuildmake
1126 validatemakeparams
1127 createmakewrapper
1128
1129 # Perform the operations.
1130 #
1131 for op in ${operations}; do
1132 case "${op}" in
1133
1134 makewrapper)
1135 # no-op
1136 ;;
1137
1138 tools)
1139 buildtools
1140 ;;
1141
1142 sets)
1143 statusmsg "Building sets from pre-populated ${DESTDIR}"
1144 ${runcmd} "${makewrapper}" ${parallel} ${op} ||
1145 bomb "Failed to make ${op}"
1146 statusmsg "Successful make ${op}"
1147 ;;
1148
1149 obj|build|distribution|iso-image|release|sourcesets|syspkgs|params)
1150 ${runcmd} "${makewrapper}" ${parallel} ${op} ||
1151 bomb "Failed to make ${op}"
1152 statusmsg "Successful make ${op}"
1153 ;;
1154
1155 kernel=*)
1156 arg=${op#*=}
1157 buildkernel "${arg}"
1158 ;;
1159
1160 releasekernel=*)
1161 arg=${op#*=}
1162 releasekernel "${arg}"
1163 ;;
1164
1165 install=*)
1166 arg=${op#*=}
1167 if [ "${arg}" = "/" ] && \
1168 ( [ "${uname_s}" != "NetBSD" ] || \
1169 [ "${uname_m}" != "${MACHINE}" ] ); then
1170 bomb "'${op}' must != / for cross builds."
1171 fi
1172 installworld "${arg}"
1173 ;;
1174
1175 *)
1176 bomb "Unknown operation \`${op}'"
1177 ;;
1178
1179 esac
1180 done
1181
1182 statusmsg "${progname} started: ${build_start}"
1183 statusmsg "${progname} ended: $(date)"
1184 if [ -s "${results}" ]; then
1185 echo "===> Summary of results:"
1186 sed -e 's/^===>//;s/^/ /' "${results}"
1187 echo "===> ."
1188 fi
1189 }
1190
1191 main "$@"
1192