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