build.sh revision 1.153.2.4 1 #! /usr/bin/env sh
2 # $NetBSD: build.sh,v 1.153.2.4 2007/09/29 11:10:56 xtraeme 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 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 # Find information about the build platform.
158 #
159 uname_s=$(uname -s 2>/dev/null)
160 uname_r=$(uname -r 2>/dev/null)
161 uname_m=$(uname -m 2>/dev/null)
162
163 # If $PWD is a valid name of the current directory, POSIX mandates
164 # that pwd return it by default which causes problems in the
165 # presence of symlinks. Unsetting PWD is simpler than changing
166 # every occurrence of pwd to use -P.
167 #
168 # XXX Except that doesn't work on Solaris. Or many Linuces.
169 #
170 unset PWD
171 TOP=$(/bin/pwd -P 2>/dev/null || /bin/pwd 2>/dev/null)
172
173 # The user can set HOST_SH in the environment, or we try to
174 # guess an appropriate value. Then we set several other
175 # variables from HOST_SH.
176 #
177 set_HOST_SH
178 setmakeenv HOST_SH "${HOST_SH}"
179 setmakeenv BSHELL "${HOST_SH}"
180 setmakeenv CONFIG_SHELL "${HOST_SH}"
181
182 # Set defaults.
183 #
184 toolprefix=nb
185
186 # Some systems have a small ARG_MAX. -X prevents make(1) from
187 # exporting variables in the environment redundantly.
188 #
189 case "${uname_s}" in
190 Darwin | FreeBSD | CYGWIN*)
191 MAKEFLAGS=-X
192 ;;
193 *)
194 MAKEFLAGS=
195 ;;
196 esac
197
198 makeenv=
199 makewrapper=
200 makewrappermachine=
201 runcmd=
202 operations=
203 removedirs=
204 do_expertmode=false
205 do_rebuildmake=false
206 do_removedirs=false
207
208 # do_{operation}=true if given operation is requested.
209 #
210 do_tools=false
211 do_obj=false
212 do_build=false
213 do_distribution=false
214 do_release=false
215 do_kernel=false
216 do_releasekernel=false
217 do_install=false
218 do_sets=false
219 do_sourcesets=false
220 do_syspkgs=false
221 do_iso_image=false
222 do_iso_image_source=false
223 do_params=false
224
225 # Create scratch directory
226 #
227 tmpdir="${TMPDIR-/tmp}/nbbuild$$"
228 mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
229 trap "cd /; rm -r -f \"${tmpdir}\"" 0
230 results="${tmpdir}/build.sh.results"
231
232 # Set source directories
233 #
234 setmakeenv NETBSDSRCDIR "${TOP}"
235
236 # Find the version of NetBSD
237 #
238 DISTRIBVER="$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh)"
239
240 # Set various environment variables to known defaults,
241 # to minimize (cross-)build problems observed "in the field".
242 #
243 unsetmakeenv INFODIR
244 unsetmakeenv LESSCHARSET
245 setmakeenv LC_ALL C
246 }
247
248 getarch()
249 {
250 # Translate a MACHINE into a default MACHINE_ARCH.
251 #
252 case "${MACHINE}" in
253
254 acorn26|acorn32|cats|evbarm|hpcarm|iyonix|netwinder|shark)
255 MACHINE_ARCH=arm
256 ;;
257
258 hp700)
259 MACHINE_ARCH=hppa
260 ;;
261
262 sun2)
263 MACHINE_ARCH=m68000
264 ;;
265
266 amiga|atari|cesfic|hp300|luna68k|mac68k|mvme68k|news68k|next68k|sun3|x68k)
267 MACHINE_ARCH=m68k
268 ;;
269
270 evbmips-e[bl]|sbmips-e[bl])
271 MACHINE_ARCH=mips${MACHINE##*-}
272 makewrappermachine=${MACHINE}
273 MACHINE=${MACHINE%-e[bl]}
274 ;;
275
276 evbmips64-e[bl]|sbmips64-e[bl])
277 MACHINE_ARCH=mips64${MACHINE##*-}
278 makewrappermachine=${MACHINE}
279 MACHINE=${MACHINE%64-e[bl]}
280 ;;
281
282 evbmips|sbmips) # no default MACHINE_ARCH
283 ;;
284
285 ews4800mips|mipsco|newsmips|sgimips)
286 MACHINE_ARCH=mipseb
287 ;;
288
289 algor|arc|cobalt|hpcmips|playstation2|pmax)
290 MACHINE_ARCH=mipsel
291 ;;
292
293 pc532)
294 MACHINE_ARCH=ns32k
295 ;;
296
297 amigappc|bebox|evbppc|ibmnws|macppc|mvmeppc|ofppc|pmppc|prep|sandpoint)
298 MACHINE_ARCH=powerpc
299 ;;
300
301 evbsh3-e[bl])
302 MACHINE_ARCH=sh3${MACHINE##*-}
303 makewrappermachine=${MACHINE}
304 MACHINE=${MACHINE%-e[bl]}
305 ;;
306
307 evbsh3) # no default MACHINE_ARCH
308 ;;
309
310 mmeye)
311 MACHINE_ARCH=sh3eb
312 ;;
313
314 dreamcast|hpcsh|landisk)
315 MACHINE_ARCH=sh3el
316 ;;
317
318 amd64)
319 MACHINE_ARCH=x86_64
320 ;;
321
322 alpha|i386|sparc|sparc64|vax|ia64)
323 MACHINE_ARCH=${MACHINE}
324 ;;
325
326 *)
327 bomb "Unknown target MACHINE: ${MACHINE}"
328 ;;
329
330 esac
331 }
332
333 validatearch()
334 {
335 # Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
336 #
337 case "${MACHINE_ARCH}" in
338
339 alpha|arm|armeb|hppa|i386|m68000|m68k|mipse[bl]|mips64e[bl]|ns32k|powerpc|powerpc64|sh[35]e[bl]|sparc|sparc64|vax|x86_64|ia64)
340 ;;
341
342 "")
343 bomb "No MACHINE_ARCH provided"
344 ;;
345
346 *)
347 bomb "Unknown target MACHINE_ARCH: ${MACHINE_ARCH}"
348 ;;
349
350 esac
351
352 # Determine valid MACHINE_ARCHs for MACHINE
353 #
354 case "${MACHINE}" in
355
356 evbarm)
357 arches="arm armeb"
358 ;;
359
360 evbmips|sbmips)
361 arches="mipseb mipsel mips64eb mips64el"
362 ;;
363
364 sgimips)
365 arches="mipseb mips64eb"
366 ;;
367
368 evbsh3)
369 arches="sh3eb sh3el"
370 ;;
371
372 macppc|evbppc)
373 arches="powerpc powerpc64"
374 ;;
375 *)
376 oma="${MACHINE_ARCH}"
377 getarch
378 arches="${MACHINE_ARCH}"
379 MACHINE_ARCH="${oma}"
380 ;;
381
382 esac
383
384 # Ensure that MACHINE_ARCH supports MACHINE
385 #
386 archok=false
387 for a in ${arches}; do
388 if [ "${a}" = "${MACHINE_ARCH}" ]; then
389 archok=true
390 break
391 fi
392 done
393 ${archok} ||
394 bomb "MACHINE_ARCH '${MACHINE_ARCH}' does not support MACHINE '${MACHINE}'"
395 }
396
397 raw_getmakevar()
398 {
399 [ -x "${make}" ] || bomb "raw_getmakevar $1: ${make} is not executable"
400 "${make}" -m ${TOP}/share/mk -s -B -f- _x_ <<EOF
401 _x_:
402 echo \${$1}
403 .include <bsd.prog.mk>
404 .include <bsd.kernobj.mk>
405 EOF
406 }
407
408 getmakevar()
409 {
410 # raw_getmakevar() doesn't work properly if $make hasn't yet been
411 # built, which can happen when running with the "-n" option.
412 # getmakevar() deals with this by emitting a literal '$'
413 # followed by the variable name, instead of trying to find the
414 # variable's value.
415 #
416 if [ -x "${make}" ]; then
417 raw_getmakevar "$1"
418 else
419 echo "\$$1"
420 fi
421 }
422
423 setmakeenv()
424 {
425 eval "$1='$2'; export $1"
426 makeenv="${makeenv} $1"
427 }
428
429 unsetmakeenv()
430 {
431 eval "unset $1"
432 makeenv="${makeenv} $1"
433 }
434
435 # Convert possibly-relative path to absolute path by prepending
436 # ${TOP} if necessary. Also delete trailing "/", if any.
437 resolvepath()
438 {
439 case "${OPTARG}" in
440 /)
441 ;;
442 /*)
443 OPTARG="${OPTARG%/}"
444 ;;
445 *)
446 OPTARG="${TOP}/${OPTARG%/}"
447 ;;
448 esac
449 }
450
451 usage()
452 {
453 if [ -n "$*" ]; then
454 echo ""
455 echo "${progname}: $*"
456 fi
457 cat <<_usage_
458
459 Usage: ${progname} [-EnorUux] [-a arch] [-B buildid] [-C cddir] [-D dest]
460 [-j njob] [-M obj] [-m mach] [-N noisy] [-O obj] [-R release]
461 [-T tools] [-V var=[value]] [-w wrapper] [-X x11src] [-Z var]
462 operation [...]
463
464 Build operations (all imply "obj" and "tools"):
465 build Run "make build".
466 distribution Run "make distribution" (includes DESTDIR/etc/ files).
467 release Run "make release" (includes kernels & distrib media).
468
469 Other operations:
470 help Show this message and exit.
471 makewrapper Create ${toolprefix}make-\${MACHINE} wrapper and ${toolprefix}make.
472 Always performed.
473 obj Run "make obj". [Default unless -o is used]
474 tools Build and install tools.
475 install=idir Run "make installworld" to \`idir' to install all sets
476 except \`etc'. Useful after "distribution" or "release"
477 kernel=conf Build kernel with config file \`conf'
478 releasekernel=conf Install kernel built by kernel=conf to RELEASEDIR.
479 sets Create binary sets in RELEASEDIR/MACHINE/binary/sets.
480 DESTDIR should be populated beforehand.
481 sourcesets Create source sets in RELEASEDIR/source/sets.
482 syspkgs Create syspkgs in RELEASEDIR/MACHINE/binary/syspkgs.
483 iso-image Create CD-ROM image in RELEASEDIR/iso.
484 iso-image-source Create CD-ROM image with source in RELEASEDIR/iso.
485 iso-dir=cddir Add the contents of \`cddir' to a CD-ROM image.
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.4 2007/09/29 11:10:56 xtraeme 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