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