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