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