build.sh revision 1.136 1 #! /usr/bin/env sh
2 # $NetBSD: build.sh,v 1.136 2005/05/19 01:37:30 lukem 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.
88 #
89 unset PWD
90 TOP=$(/bin/pwd -P 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_params=false
131
132 # Create scratch directory
133 #
134 tmpdir="${TMPDIR-/tmp}/nbbuild$$"
135 mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
136 trap "cd /; rm -r -f \"${tmpdir}\"" 0
137 results="${tmpdir}/build.sh.results"
138
139 # Set source directories
140 #
141 setmakeenv NETBSDSRCDIR "${TOP}"
142
143 # Set various environment variables to known defaults,
144 # to minimize (cross-)build problems observed "in the field".
145 #
146 unsetmakeenv INFODIR
147 setmakeenv LC_ALL C
148 }
149
150 getarch()
151 {
152 # Translate a MACHINE into a default MACHINE_ARCH.
153 #
154 case "${MACHINE}" in
155
156 acorn26|acorn32|cats|evbarm|hpcarm|iyonix|netwinder|shark)
157 MACHINE_ARCH=arm
158 ;;
159
160 hp700)
161 MACHINE_ARCH=hppa
162 ;;
163
164 sun2)
165 MACHINE_ARCH=m68000
166 ;;
167
168 amiga|atari|cesfic|hp300|luna68k|mac68k|mvme68k|news68k|next68k|sun3|x68k)
169 MACHINE_ARCH=m68k
170 ;;
171
172 evbmips-e[bl]|sbmips-e[bl])
173 MACHINE_ARCH=mips${MACHINE##*-}
174 makewrappermachine=${MACHINE}
175 MACHINE=${MACHINE%-e[bl]}
176 ;;
177
178 evbmips|sbmips) # no default MACHINE_ARCH
179 ;;
180
181 mipsco|newsmips|sgimips)
182 MACHINE_ARCH=mipseb
183 ;;
184
185 algor|arc|cobalt|hpcmips|playstation2|pmax)
186 MACHINE_ARCH=mipsel
187 ;;
188
189 pc532)
190 MACHINE_ARCH=ns32k
191 ;;
192
193 amigappc|bebox|evbppc|ibmnws|macppc|mvmeppc|ofppc|pmppc|prep|sandpoint)
194 MACHINE_ARCH=powerpc
195 ;;
196
197 evbsh3-e[bl])
198 MACHINE_ARCH=sh3${MACHINE##*-}
199 makewrappermachine=${MACHINE}
200 MACHINE=${MACHINE%-e[bl]}
201 ;;
202
203 evbsh3) # no default MACHINE_ARCH
204 ;;
205
206 mmeye)
207 MACHINE_ARCH=sh3eb
208 ;;
209
210 dreamcast|hpcsh)
211 MACHINE_ARCH=sh3el
212 ;;
213
214 evbsh5)
215 MACHINE_ARCH=sh5el
216 ;;
217 amd64)
218 MACHINE_ARCH=x86_64
219 ;;
220
221 xen-*)
222 setmakeenv XEN_BUILD "${MACHINE##*-}"
223 setmakeenv KERNARCHDIR "arch/xen"
224 setmakeenv RELEASEMACHINE "xen"
225 setmakeenv RELEASEMACHINEDIR "${MACHINE}"
226 makewrappermachine=${MACHINE}
227 MACHINE=${MACHINE##*-}
228 getarch
229 ;;
230
231 xen) # no default MACHINE_ARCH
232 ;;
233
234 alpha|i386|sparc|sparc64|vax)
235 MACHINE_ARCH=${MACHINE}
236 ;;
237
238 *)
239 bomb "Unknown target MACHINE: ${MACHINE}"
240 ;;
241
242 esac
243 }
244
245 validatearch()
246 {
247 # Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
248 #
249 case "${MACHINE_ARCH}" in
250
251 alpha|arm|armeb|hppa|i386|m68000|m68k|mipse[bl]|ns32k|powerpc|sh[35]e[bl]|sparc|sparc64|vax|x86_64)
252 ;;
253
254 "")
255 bomb "No MACHINE_ARCH provided"
256 ;;
257
258 *)
259 bomb "Unknown target MACHINE_ARCH: ${MACHINE_ARCH}"
260 ;;
261
262 esac
263
264 # Determine valid MACHINE_ARCHs for MACHINE
265 #
266 case "${MACHINE}" in
267
268 evbarm)
269 arches="arm armeb"
270 ;;
271
272 evbmips|sbmips)
273 arches="mipseb mipsel"
274 ;;
275
276 evbsh3)
277 arches="sh3eb sh3el"
278 ;;
279
280 evbsh5)
281 arches="sh5eb sh5el"
282 ;;
283
284 *)
285 oma="${MACHINE_ARCH}"
286 getarch
287 arches="${MACHINE_ARCH}"
288 MACHINE_ARCH="${oma}"
289 ;;
290
291 esac
292
293 # Ensure that MACHINE_ARCH supports MACHINE
294 #
295 archok=false
296 for a in ${arches}; do
297 if [ "${a}" = "${MACHINE_ARCH}" ]; then
298 archok=true
299 break
300 fi
301 done
302 ${archok} ||
303 bomb "MACHINE_ARCH '${MACHINE_ARCH}' does not support MACHINE '${MACHINE}'"
304 }
305
306 raw_getmakevar()
307 {
308 [ -x "${make}" ] || bomb "raw_getmakevar $1: ${make} is not executable"
309 "${make}" -m ${TOP}/share/mk -s -f- _x_ <<EOF
310 _x_:
311 echo \${$1}
312 .include <bsd.prog.mk>
313 .include <bsd.kernobj.mk>
314 EOF
315 }
316
317 getmakevar()
318 {
319 # raw_getmakevar() doesn't work properly if $make hasn't yet been
320 # built, which can happen when running with the "-n" option.
321 # getmakevar() deals with this by emitting a literal '$'
322 # followed by the variable name, instead of trying to find the
323 # variable's value.
324 #
325 if [ -x "${make}" ]; then
326 raw_getmakevar "$1"
327 else
328 echo "\$$1"
329 fi
330 }
331
332 setmakeenv()
333 {
334 eval "$1='$2'; export $1"
335 makeenv="${makeenv} $1"
336 }
337
338 unsetmakeenv()
339 {
340 eval "unset $1"
341 makeenv="${makeenv} $1"
342 }
343
344 # Convert possibly-relative path to absolute path by prepending
345 # ${TOP} if necessary. Also delete trailing "/", if any.
346 resolvepath()
347 {
348 case "${OPTARG}" in
349 /)
350 ;;
351 /*)
352 OPTARG="${OPTARG%/}"
353 ;;
354 *)
355 OPTARG="${TOP}/${OPTARG%/}"
356 ;;
357 esac
358 }
359
360 usage()
361 {
362 if [ -n "$*" ]; then
363 echo ""
364 echo "${progname}: $*"
365 fi
366 cat <<_usage_
367
368 Usage: ${progname} [-EnorUux] [-a arch] [-B buildid] [-D dest] [-j njob]
369 [-M obj] [-m mach] [-N noisy] [-O obj] [-R release] [-T tools]
370 [-V var=[value]] [-w wrapper] [-X x11src] [-Z var]
371 operation [...]
372
373 Build operations (all imply "obj" and "tools"):
374 build Run "make build".
375 distribution Run "make distribution" (includes DESTDIR/etc/ files).
376 release Run "make release" (includes kernels & distrib media).
377
378 Other operations:
379 help Show this message and exit.
380 makewrapper Create ${toolprefix}make-\${MACHINE} wrapper and ${toolprefix}make.
381 Always performed.
382 obj Run "make obj". [Default unless -o is used]
383 tools Build and install tools.
384 install=idir Run "make installworld" to \`idir' to install all sets
385 except \`etc'. Useful after "distribution" or "release"
386 kernel=conf Build kernel with config file \`conf'
387 releasekernel=conf Install kernel built by kernel=conf to RELEASEDIR.
388 sets Create binary sets in RELEASEDIR/MACHINE/binary/sets.
389 DESTDIR should be populated beforehand.
390 sourcesets Create source sets in RELEASEDIR/source/sets.
391 params Display various make(1) parameters.
392
393 Options:
394 -a arch Set MACHINE_ARCH to arch. [Default: deduced from MACHINE]
395 -B buildId Set BUILDID to buildId.
396 -D dest Set DESTDIR to dest. [Default: destdir.MACHINE]
397 -E Set "expert" mode; disables various safety checks.
398 Should not be used without expert knowledge of the build system.
399 -h Print this help message.
400 -j njob Run up to njob jobs in parallel; see make(1) -j.
401 -M obj Set obj root directory to obj; sets MAKEOBJDIRPREFIX.
402 Unsets MAKEOBJDIR.
403 -m mach Set MACHINE to mach; not required if NetBSD native.
404 -N noisy Set the noisyness (MAKEVERBOSE) level of the build:
405 0 Quiet
406 1 Operations are described, commands are suppressed
407 2 Full output
408 [Default: 2]
409 -n Show commands that would be executed, but do not execute them.
410 -O obj Set obj root directory to obj; sets a MAKEOBJDIR pattern.
411 Unsets MAKEOBJDIRPREFIX.
412 -o Set MKOBJDIRS=no; do not create objdirs at start of build.
413 -R release Set RELEASEDIR to release. [Default: releasedir]
414 -r Remove contents of TOOLDIR and DESTDIR before building.
415 -T tools Set TOOLDIR to tools. If unset, and TOOLDIR is not set in
416 the environment, ${toolprefix}make will be (re)built unconditionally.
417 -U Set MKUNPRIVED=yes; build without requiring root privileges,
418 install from an UNPRIVED build with proper file permissions.
419 -u Set MKUPDATE=yes; do not run "make clean" first.
420 Without this, everything is rebuilt, including the tools.
421 -V v=[val] Set variable \`v' to \`val'.
422 -w wrapper Create ${toolprefix}make script as wrapper.
423 [Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE}]
424 -X x11src Set X11SRCDIR to x11src. [Default: /usr/xsrc]
425 -x Set MKX11=yes; build X11R6 from X11SRCDIR
426 -Z v Unset ("zap") variable \`v'.
427
428 _usage_
429 exit 1
430 }
431
432 parseoptions()
433 {
434 opts='a:B:bD:dEhi:j:k:M:m:N:nO:oR:rT:tUuV:w:xX:Z:'
435 opt_a=no
436
437 if type getopts >/dev/null 2>&1; then
438 # Use POSIX getopts.
439 #
440 getoptcmd='getopts ${opts} opt && opt=-${opt}'
441 optargcmd=':'
442 optremcmd='shift $((${OPTIND} -1))'
443 else
444 type getopt >/dev/null 2>&1 ||
445 bomb "/bin/sh shell is too old; try ksh or bash"
446
447 # Use old-style getopt(1) (doesn't handle whitespace in args).
448 #
449 args="$(getopt ${opts} $*)"
450 [ $? = 0 ] || usage
451 set -- ${args}
452
453 getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
454 optargcmd='OPTARG="$1"; shift'
455 optremcmd=':'
456 fi
457
458 # Parse command line options.
459 #
460 while eval ${getoptcmd}; do
461 case ${opt} in
462
463 -a)
464 eval ${optargcmd}
465 MACHINE_ARCH=${OPTARG}
466 opt_a=yes
467 ;;
468
469 -B)
470 eval ${optargcmd}
471 BUILDID=${OPTARG}
472 ;;
473
474 -b)
475 usage "'-b' has been replaced by 'makewrapper'"
476 ;;
477
478 -D)
479 eval ${optargcmd}; resolvepath
480 setmakeenv DESTDIR "${OPTARG}"
481 ;;
482
483 -d)
484 usage "'-d' has been replaced by 'distribution'"
485 ;;
486
487 -E)
488 do_expertmode=true
489 ;;
490
491 -i)
492 usage "'-i idir' has been replaced by 'install=idir'"
493 ;;
494
495 -j)
496 eval ${optargcmd}
497 parallel="-j ${OPTARG}"
498 ;;
499
500 -k)
501 usage "'-k conf' has been replaced by 'kernel=conf'"
502 ;;
503
504 -M)
505 eval ${optargcmd}; resolvepath
506 makeobjdir="${OPTARG}"
507 unsetmakeenv MAKEOBJDIR
508 setmakeenv MAKEOBJDIRPREFIX "${OPTARG}"
509 ;;
510
511 # -m overrides MACHINE_ARCH unless "-a" is specified
512 -m)
513 eval ${optargcmd}
514 MACHINE="${OPTARG}"
515 [ "${opt_a}" != "yes" ] && getarch
516 ;;
517
518 -N)
519 eval ${optargcmd}
520 case "${OPTARG}" in
521 0|1|2)
522 setmakeenv MAKEVERBOSE "${OPTARG}"
523 ;;
524 *)
525 usage "'${OPTARG}' is not a valid value for -N"
526 ;;
527 esac
528 ;;
529
530 -n)
531 runcmd=echo
532 ;;
533
534 -O)
535 eval ${optargcmd}; resolvepath
536 makeobjdir="${OPTARG}"
537 unsetmakeenv MAKEOBJDIRPREFIX
538 setmakeenv MAKEOBJDIR "\${.CURDIR:C,^$TOP,$OPTARG,}"
539 ;;
540
541 -o)
542 MKOBJDIRS=no
543 ;;
544
545 -R)
546 eval ${optargcmd}; resolvepath
547 setmakeenv RELEASEDIR "${OPTARG}"
548 ;;
549
550 -r)
551 do_removedirs=true
552 do_rebuildmake=true
553 ;;
554
555 -T)
556 eval ${optargcmd}; resolvepath
557 TOOLDIR="${OPTARG}"
558 export TOOLDIR
559 ;;
560
561 -t)
562 usage "'-t' has been replaced by 'tools'"
563 ;;
564
565 -U)
566 setmakeenv MKUNPRIVED yes
567 ;;
568
569 -u)
570 setmakeenv MKUPDATE yes
571 ;;
572
573 -V)
574 eval ${optargcmd}
575 case "${OPTARG}" in
576 # XXX: consider restricting which variables can be changed?
577 [a-zA-Z_][a-zA-Z_0-9]*=*)
578 setmakeenv "${OPTARG%%=*}" "${OPTARG#*=}"
579 ;;
580 *)
581 usage "-V argument must be of the form 'var=[value]'"
582 ;;
583 esac
584 ;;
585
586 -w)
587 eval ${optargcmd}; resolvepath
588 makewrapper="${OPTARG}"
589 ;;
590
591 -X)
592 eval ${optargcmd}; resolvepath
593 setmakeenv X11SRCDIR "${OPTARG}"
594 ;;
595
596 -x)
597 setmakeenv MKX11 yes
598 ;;
599
600 -Z)
601 eval ${optargcmd}
602 # XXX: consider restricting which variables can be unset?
603 unsetmakeenv "${OPTARG}"
604 ;;
605
606 --)
607 break
608 ;;
609
610 -'?'|-h)
611 usage
612 ;;
613
614 esac
615 done
616
617 # Validate operations.
618 #
619 eval ${optremcmd}
620 while [ $# -gt 0 ]; do
621 op=$1; shift
622 operations="${operations} ${op}"
623
624 case "${op}" in
625
626 help)
627 usage
628 ;;
629
630 makewrapper|obj|tools|build|distribution|release|sets|sourcesets|params)
631 ;;
632
633 kernel=*|releasekernel=*)
634 arg=${op#*=}
635 op=${op%%=*}
636 [ -n "${arg}" ] ||
637 bomb "Must supply a kernel name with \`${op}=...'"
638 ;;
639
640 install=*)
641 arg=${op#*=}
642 op=${op%%=*}
643 [ -n "${arg}" ] ||
644 bomb "Must supply a directory with \`install=...'"
645 ;;
646
647 *)
648 usage "Unknown operation \`${op}'"
649 ;;
650
651 esac
652 eval do_${op}=true
653 done
654 [ -n "${operations}" ] || usage "Missing operation to perform."
655
656 # Set up MACHINE*. On a NetBSD host, these are allowed to be unset.
657 #
658 if [ -z "${MACHINE}" ]; then
659 [ "${uname_s}" = "NetBSD" ] ||
660 bomb "MACHINE must be set, or -m must be used, for cross builds."
661 MACHINE=${uname_m}
662 fi
663 [ -n "${MACHINE_ARCH}" ] || getarch
664 validatearch
665
666 # Set up default make(1) environment.
667 #
668 makeenv="${makeenv} TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
669 [ -z "${BUILDID}" ] || makeenv="${makeenv} BUILDID"
670 MAKEFLAGS="-de -m ${TOP}/share/mk ${MAKEFLAGS} MKOBJDIRS=${MKOBJDIRS-yes}"
671 export MAKEFLAGS MACHINE MACHINE_ARCH
672 }
673
674 rebuildmake()
675 {
676 # Test make source file timestamps against installed ${toolprefix}make
677 # binary, if TOOLDIR is pre-set.
678 #
679 # Note that we do NOT try to grovel "mk.conf" here to find out if
680 # TOOLDIR is set there, because it can contain make variable
681 # expansions and other stuff only parseable *after* we have a working
682 # ${toolprefix}make. So this logic can only work if the user has
683 # pre-set TOOLDIR in the environment or used the -T option to build.sh.
684 #
685 make="${TOOLDIR-nonexistent}/bin/${toolprefix}make"
686 if [ -x "${make}" ]; then
687 for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
688 if [ "${f}" -nt "${make}" ]; then
689 statusmsg "${make} outdated (older than ${f}), needs building."
690 do_rebuildmake=true
691 break
692 fi
693 done
694 else
695 statusmsg "No ${make}, needs building."
696 do_rebuildmake=true
697 fi
698
699 # Build bootstrap ${toolprefix}make if needed.
700 if ${do_rebuildmake}; then
701 statusmsg "Bootstrapping ${toolprefix}make"
702 ${runcmd} cd "${tmpdir}"
703 ${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
704 CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
705 sh "${TOP}/tools/make/configure" ||
706 bomb "Configure of ${toolprefix}make failed"
707 ${runcmd} sh buildmake.sh ||
708 bomb "Build of ${toolprefix}make failed"
709 make="${tmpdir}/${toolprefix}make"
710 ${runcmd} cd "${TOP}"
711 ${runcmd} rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
712 fi
713 }
714
715 validatemakeparams()
716 {
717 if [ "${runcmd}" = "echo" ]; then
718 TOOLCHAIN_MISSING=no
719 EXTERNAL_TOOLCHAIN=""
720 else
721 TOOLCHAIN_MISSING=$(raw_getmakevar TOOLCHAIN_MISSING)
722 EXTERNAL_TOOLCHAIN=$(raw_getmakevar EXTERNAL_TOOLCHAIN)
723 fi
724 if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
725 [ -z "${EXTERNAL_TOOLCHAIN}" ]; then
726 ${runcmd} echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
727 ${runcmd} echo " MACHINE: ${MACHINE}"
728 ${runcmd} echo " MACHINE_ARCH: ${MACHINE_ARCH}"
729 ${runcmd} echo ""
730 ${runcmd} echo "All builds for this platform should be done via a traditional make"
731 ${runcmd} echo "If you wish to use an external cross-toolchain, set"
732 ${runcmd} echo " EXTERNAL_TOOLCHAIN=<path to toolchain root>"
733 ${runcmd} echo "in either the environment or mk.conf and rerun"
734 ${runcmd} echo " ${progname} $*"
735 exit 1
736 fi
737
738 # Normalise MKOBJDIRS, MKUNPRIVED, and MKUPDATE
739 # These may be set as build.sh options or in "mk.conf".
740 # Don't export them as they're only used for tests in build.sh.
741 #
742 MKOBJDIRS=$(getmakevar MKOBJDIRS)
743 MKUNPRIVED=$(getmakevar MKUNPRIVED)
744 MKUPDATE=$(getmakevar MKUPDATE)
745
746 if [ "${MKOBJDIRS}" != "no" ]; then
747 # If setting -M or -O to the root of an obj dir, make sure
748 # the base directory is made before continuing as <bsd.own.mk>
749 # will need this to pick up _SRC_TOP_OBJ_
750 #
751 if [ ! -z "${makeobjdir}" ]; then
752 ${runcmd} mkdir -p "${makeobjdir}"
753 fi
754
755 # make obj in tools to ensure that the objdir for the top-level
756 # of the source tree and for "tools" is available, in case the
757 # default TOOLDIR setting from <bsd.own.mk> is used, or the
758 # build.sh default DESTDIR and RELEASEDIR is to be used.
759 #
760 ${runcmd} cd tools
761 ${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
762 bomb "Failed to make obj in tools"
763 ${runcmd} cd "${TOP}"
764 fi
765
766 statusmsg "MACHINE: ${MACHINE}"
767 statusmsg "MACHINE_ARCH: ${MACHINE_ARCH}"
768
769 # Find TOOLDIR, DESTDIR, and RELEASEDIR.
770 #
771 TOOLDIR=$(getmakevar TOOLDIR)
772 statusmsg "TOOLDIR path: ${TOOLDIR}"
773 DESTDIR=$(getmakevar DESTDIR)
774 RELEASEDIR=$(getmakevar RELEASEDIR)
775 if ! $do_expertmode; then
776 _SRC_TOP_OBJ_=$(getmakevar _SRC_TOP_OBJ_)
777 : ${DESTDIR:=${_SRC_TOP_OBJ_}/destdir.${MACHINE}}
778 : ${RELEASEDIR:=${_SRC_TOP_OBJ_}/releasedir}
779 makeenv="${makeenv} DESTDIR RELEASEDIR"
780 fi
781 export TOOLDIR DESTDIR RELEASEDIR
782 statusmsg "DESTDIR path: ${DESTDIR}"
783 statusmsg "RELEASEDIR path: ${RELEASEDIR}"
784
785 # Check validity of TOOLDIR and DESTDIR.
786 #
787 if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = "/" ]; then
788 bomb "TOOLDIR '${TOOLDIR}' invalid"
789 fi
790 removedirs="${TOOLDIR}"
791
792 if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = "/" ]; then
793 if ${do_build} || ${do_distribution} || ${do_release}; then
794 if ! ${do_build} || \
795 [ "${uname_s}" != "NetBSD" ] || \
796 [ "${uname_m}" != "${MACHINE}" ]; then
797 bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
798 fi
799 if ! ${do_expertmode}; then
800 bomb "DESTDIR must != / for non -E (expert) builds"
801 fi
802 statusmsg "WARNING: Building to /, in expert mode."
803 statusmsg " This may cause your system to break! Reasons include:"
804 statusmsg " - your kernel is not up to date"
805 statusmsg " - the libraries or toolchain have changed"
806 statusmsg " YOU HAVE BEEN WARNED!"
807 fi
808 else
809 removedirs="${removedirs} ${DESTDIR}"
810 fi
811 if ${do_build} || ${do_distribution} || ${do_release}; then
812 if ! ${do_expertmode} && \
813 [ $(id -u 2>/dev/null) -ne 0 ] && \
814 [ "${MKUNPRIVED}" = "no" ] ; then
815 bomb "-U or -E must be set for build as an unprivileged user."
816 fi
817 fi
818 if ${do_releasekernel} && [ -z "${RELEASEDIR}" ]; then
819 bomb "Must set RELEASEDIR with \`releasekernel=...'"
820 fi
821 }
822
823
824 createmakewrapper()
825 {
826 # Remove the target directories.
827 #
828 if ${do_removedirs}; then
829 for f in ${removedirs}; do
830 statusmsg "Removing ${f}"
831 ${runcmd} rm -r -f "${f}"
832 done
833 fi
834
835 # Recreate $TOOLDIR.
836 #
837 ${runcmd} mkdir -p "${TOOLDIR}/bin" ||
838 bomb "mkdir of '${TOOLDIR}/bin' failed"
839
840 # Install ${toolprefix}make if it was built.
841 #
842 if ${do_rebuildmake}; then
843 ${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
844 ${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
845 bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
846 make="${TOOLDIR}/bin/${toolprefix}make"
847 statusmsg "Created ${make}"
848 fi
849
850 # Build a ${toolprefix}make wrapper script, usable by hand as
851 # well as by build.sh.
852 #
853 if [ -z "${makewrapper}" ]; then
854 makewrapper="${TOOLDIR}/bin/${toolprefix}make-${makewrappermachine:-${MACHINE}}"
855 [ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
856 fi
857
858 ${runcmd} rm -f "${makewrapper}"
859 if [ "${runcmd}" = "echo" ]; then
860 echo 'cat <<EOF >'${makewrapper}
861 makewrapout=
862 else
863 makewrapout=">>\${makewrapper}"
864 fi
865
866 case "${uname_s}" in
867 OpenBSD)
868 set +o braceexpand
869 ;;
870 esac
871
872 eval cat <<EOF ${makewrapout}
873 #! /bin/sh
874 # Set proper variables to allow easy "make" building of a NetBSD subtree.
875 # Generated from: \$NetBSD: build.sh,v 1.136 2005/05/19 01:37:30 lukem Exp $
876 # with these arguments: ${_args}
877 #
878 EOF
879 for f in ${makeenv}; do
880 if eval "[ -z \"\${$f}\" -a \"\${${f}-X}\" = \"X\" ]"; then
881 eval echo "unset ${f}" ${makewrapout}
882 else
883 eval echo "${f}=\'\$$(echo ${f})\'\;\ export\ ${f}" ${makewrapout}
884 fi
885 done
886 eval echo "USETOOLS=yes\; export USETOOLS" ${makewrapout}
887
888 eval cat <<EOF ${makewrapout}
889
890 exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
891 EOF
892 [ "${runcmd}" = "echo" ] && echo EOF
893 ${runcmd} chmod +x "${makewrapper}"
894 statusmsg "makewrapper: ${makewrapper}"
895 statusmsg "Updated ${makewrapper}"
896 }
897
898 buildtools()
899 {
900 if [ "${MKOBJDIRS}" != "no" ]; then
901 ${runcmd} "${makewrapper}" ${parallel} obj-tools ||
902 bomb "Failed to make obj-tools"
903 fi
904 ${runcmd} cd tools
905 if [ "${MKUPDATE}" = "no" ]; then
906 cleandir=cleandir
907 else
908 cleandir=
909 fi
910 ${runcmd} "${makewrapper}" ${cleandir} dependall install ||
911 bomb "Failed to make 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}" 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}" 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}" 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|release|sourcesets|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