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