build.sh revision 1.238 1 #! /usr/bin/env sh
2 # $NetBSD: build.sh,v 1.238 2010/08/24 13:35:32 pgoyette Exp $
3 #
4 # Copyright (c) 2001-2009 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 #
19 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
30 #
31 #
32 # Top level build wrapper, for a system containing no tools.
33 #
34 # This script should run on any POSIX-compliant shell. If the
35 # first "sh" found in the PATH is a POSIX-compliant shell, then
36 # you should not need to take any special action. Otherwise, you
37 # should set the environment variable HOST_SH to a POSIX-compliant
38 # shell, and invoke build.sh with that shell. (Depending on your
39 # system, one of /bin/ksh, /usr/local/bin/bash, or /usr/xpg4/bin/sh
40 # might be a suitable shell.)
41 #
42
43 progname=${0##*/}
44 toppid=$$
45 results=/dev/null
46 tab=' '
47 trap "exit 1" 1 2 3 15
48
49 bomb()
50 {
51 cat >&2 <<ERRORMESSAGE
52
53 ERROR: $@
54 *** BUILD ABORTED ***
55 ERRORMESSAGE
56 kill ${toppid} # in case we were invoked from a subshell
57 exit 1
58 }
59
60
61 statusmsg()
62 {
63 ${runcmd} echo "===> $@" | tee -a "${results}"
64 }
65
66 statusmsg2()
67 {
68 msg="${1}"
69 shift
70 case ${msg} in
71 ????????????????*) ;;
72 ??????????*) msg="${msg} ";;
73 ?????*) msg="${msg} ";;
74 *) msg="${msg} ";;
75 esac
76 case ${msg} in
77 ?????????????????????*) ;;
78 ????????????????????) msg="${msg} ";;
79 ???????????????????) msg="${msg} ";;
80 ??????????????????) msg="${msg} ";;
81 ?????????????????) msg="${msg} ";;
82 ????????????????) msg="${msg} ";;
83 esac
84 statusmsg "${msg}$@"
85 }
86
87 warning()
88 {
89 statusmsg "Warning: $@"
90 }
91
92 # Find a program in the PATH, and print the result. If not found,
93 # print a default. If $2 is defined (even if it is an empty string),
94 # then that is the default; otherwise, $1 is used as the default.
95 find_in_PATH()
96 {
97 local prog="$1"
98 local result="${2-"$1"}"
99 local oldIFS="${IFS}"
100 local dir
101 IFS=":"
102 for dir in ${PATH}; do
103 if [ -x "${dir}/${prog}" ]; then
104 result="${dir}/${prog}"
105 break
106 fi
107 done
108 IFS="${oldIFS}"
109 echo "${result}"
110 }
111
112 # Try to find a working POSIX shell, and set HOST_SH to refer to it.
113 # Assumes that uname_s, uname_m, and PWD have been set.
114 set_HOST_SH()
115 {
116 # Even if ${HOST_SH} is already defined, we still do the
117 # sanity checks at the end.
118
119 # Solaris has /usr/xpg4/bin/sh.
120 #
121 [ -z "${HOST_SH}" ] && [ x"${uname_s}" = x"SunOS" ] && \
122 [ -x /usr/xpg4/bin/sh ] && HOST_SH="/usr/xpg4/bin/sh"
123
124 # Try to get the name of the shell that's running this script,
125 # by parsing the output from "ps". We assume that, if the host
126 # system's ps command supports -o comm at all, it will do so
127 # in the usual way: a one-line header followed by a one-line
128 # result, possibly including trailing white space. And if the
129 # host system's ps command doesn't support -o comm, we assume
130 # that we'll get an error message on stderr and nothing on
131 # stdout. (We don't try to use ps -o 'comm=' to suppress the
132 # header line, because that is less widely supported.)
133 #
134 # If we get the wrong result here, the user can override it by
135 # specifying HOST_SH in the environment.
136 #
137 [ -z "${HOST_SH}" ] && HOST_SH="$(
138 (ps -p $$ -o comm | sed -ne "2s/[ ${tab}]*\$//p") 2>/dev/null )"
139
140 # If nothing above worked, use "sh". We will later find the
141 # first directory in the PATH that has a "sh" program.
142 #
143 [ -z "${HOST_SH}" ] && HOST_SH="sh"
144
145 # If the result so far is not an absolute path, try to prepend
146 # PWD or search the PATH.
147 #
148 case "${HOST_SH}" in
149 /*) :
150 ;;
151 */*) HOST_SH="${PWD}/${HOST_SH}"
152 ;;
153 *) HOST_SH="$(find_in_PATH "${HOST_SH}")"
154 ;;
155 esac
156
157 # If we don't have an absolute path by now, bomb.
158 #
159 case "${HOST_SH}" in
160 /*) :
161 ;;
162 *) bomb "HOST_SH=\"${HOST_SH}\" is not an absolute path."
163 ;;
164 esac
165
166 # If HOST_SH is not executable, bomb.
167 #
168 [ -x "${HOST_SH}" ] ||
169 bomb "HOST_SH=\"${HOST_SH}\" is not executable."
170 }
171
172 initdefaults()
173 {
174 makeenv=
175 makewrapper=
176 makewrappermachine=
177 runcmd=
178 operations=
179 removedirs=
180
181 [ -d usr.bin/make ] || cd "$(dirname $0)"
182 [ -d usr.bin/make ] ||
183 bomb "build.sh must be run from the top source level"
184 [ -f share/mk/bsd.own.mk ] ||
185 bomb "src/share/mk is missing; please re-fetch the source tree"
186
187 # Set LC_ALL=C before we try to parse the output from any command
188 setmakeenv LC_ALL C
189
190 # Find information about the build platform. This should be
191 # kept in sync with _HOST_OSNAME, _HOST_OSREL, and _HOST_ARCH
192 # variables in share/mk/bsd.sys.mk.
193 #
194 # Note that "uname -p" is not part of POSIX, but we want uname_p
195 # to be set to the host MACHINE_ARCH, if possible. On systems
196 # where "uname -p" fails, prints "unknown", or prints a string
197 # that does not look like an identifier, fall back to using the
198 # output from "uname -m" instead.
199 #
200 uname_s=$(uname -s 2>/dev/null)
201 uname_r=$(uname -r 2>/dev/null)
202 uname_m=$(uname -m 2>/dev/null)
203 uname_p=$(uname -p 2>/dev/null || echo "unknown")
204 case "${uname_p}" in
205 ''|unknown|*[^-_A-Za-z0-9]*) uname_p="${uname_m}" ;;
206 esac
207
208 id_u=$(id -u 2>/dev/null || /usr/xpg4/bin/id -u 2>/dev/null)
209
210 # If $PWD is a valid name of the current directory, POSIX mandates
211 # that pwd return it by default which causes problems in the
212 # presence of symlinks. Unsetting PWD is simpler than changing
213 # every occurrence of pwd to use -P.
214 #
215 # XXX Except that doesn't work on Solaris. Or many Linuces.
216 #
217 unset PWD
218 TOP=$(/bin/pwd -P 2>/dev/null || /bin/pwd 2>/dev/null)
219
220 # The user can set HOST_SH in the environment, or we try to
221 # guess an appropriate value. Then we set several other
222 # variables from HOST_SH.
223 #
224 set_HOST_SH
225 setmakeenv HOST_SH "${HOST_SH}"
226 setmakeenv BSHELL "${HOST_SH}"
227 setmakeenv CONFIG_SHELL "${HOST_SH}"
228
229 # Set defaults.
230 #
231 toolprefix=nb
232
233 # Some systems have a small ARG_MAX. -X prevents make(1) from
234 # exporting variables in the environment redundantly.
235 #
236 case "${uname_s}" in
237 Darwin | FreeBSD | CYGWIN*)
238 MAKEFLAGS=-X
239 ;;
240 *)
241 MAKEFLAGS=
242 ;;
243 esac
244
245 # do_{operation}=true if given operation is requested.
246 #
247 do_expertmode=false
248 do_rebuildmake=false
249 do_removedirs=false
250 do_tools=false
251 do_cleandir=false
252 do_obj=false
253 do_build=false
254 do_distribution=false
255 do_release=false
256 do_kernel=false
257 do_releasekernel=false
258 do_modules=false
259 do_install=false
260 do_sets=false
261 do_sourcesets=false
262 do_syspkgs=false
263 do_iso_image=false
264 do_iso_image_source=false
265 do_params=false
266 do_rump=false
267
268 # done_{operation}=true if given operation has been done.
269 #
270 done_rebuildmake=false
271
272 # Create scratch directory
273 #
274 tmpdir="${TMPDIR-/tmp}/nbbuild$$"
275 mkdir "${tmpdir}" || bomb "Cannot mkdir: ${tmpdir}"
276 trap "cd /; rm -r -f \"${tmpdir}\"" 0
277 results="${tmpdir}/build.sh.results"
278
279 # Set source directories
280 #
281 setmakeenv NETBSDSRCDIR "${TOP}"
282
283 # Make sure KERNOBJDIR is an absolute path if defined
284 #
285 case "${KERNOBJDIR}" in
286 ''|/*) ;;
287 *) KERNOBJDIR="${TOP}/${KERNOBJDIR}"
288 setmakeenv KERNOBJDIR "${KERNOBJDIR}"
289 ;;
290 esac
291
292 # Find the version of NetBSD
293 #
294 DISTRIBVER="$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh)"
295
296 # Set the BUILDSEED to NetBSD-"N"
297 #
298 setmakeenv BUILDSEED "NetBSD-$(${HOST_SH} ${TOP}/sys/conf/osrelease.sh -m)"
299
300 # Set MKARZERO to "yes"
301 #
302 setmakeenv MKARZERO "yes"
303
304 # Set various environment variables to known defaults,
305 # to minimize (cross-)build problems observed "in the field".
306 #
307 unsetmakeenv INFODIR
308 unsetmakeenv LESSCHARSET
309 }
310
311 getarch()
312 {
313 # Translate some MACHINE name aliases (known only to build.sh)
314 # into proper MACHINE and MACHINE_ARCH names. Save the alias
315 # name in makewrappermachine.
316 #
317 case "${MACHINE}" in
318
319 evbarm-e[bl])
320 makewrappermachine=${MACHINE}
321 # MACHINE_ARCH is "arm" or "armeb", not "armel"
322 MACHINE_ARCH=arm${MACHINE##*-}
323 MACHINE_ARCH=${MACHINE_ARCH%el}
324 MACHINE=${MACHINE%-e[bl]}
325 ;;
326
327 evbmips-e[bl]|sbmips-e[bl])
328 makewrappermachine=${MACHINE}
329 MACHINE_ARCH=mips${MACHINE##*-}
330 MACHINE=${MACHINE%-e[bl]}
331 ;;
332
333 evbmips64-e[bl]|sbmips64-e[bl])
334 makewrappermachine=${MACHINE}
335 MACHINE_ARCH=mips64${MACHINE##*-}
336 MACHINE=${MACHINE%64-e[bl]}
337 ;;
338
339 evbsh3-e[bl])
340 makewrappermachine=${MACHINE}
341 MACHINE_ARCH=sh3${MACHINE##*-}
342 MACHINE=${MACHINE%-e[bl]}
343 ;;
344
345 esac
346
347 # Translate a MACHINE into a default MACHINE_ARCH.
348 #
349 case "${MACHINE}" in
350
351 acorn26|acorn32|cats|hpcarm|iyonix|netwinder|shark|zaurus)
352 MACHINE_ARCH=arm
353 ;;
354
355 evbarm) # unspecified MACHINE_ARCH gets LE
356 MACHINE_ARCH=${MACHINE_ARCH:=arm}
357 ;;
358
359 hp700)
360 MACHINE_ARCH=hppa
361 ;;
362
363 sun2)
364 MACHINE_ARCH=m68000
365 ;;
366
367 amiga|atari|cesfic|hp300|luna68k|mac68k|mvme68k|news68k|next68k|sun3|x68k)
368 MACHINE_ARCH=m68k
369 ;;
370
371 evbmips|sbmips) # no default MACHINE_ARCH
372 ;;
373
374 sgimips64)
375 makewrappermachine=${MACHINE}
376 MACHINE=${MACHINE%64}
377 MACHINE_ARCH=mips64eb
378 ;;
379
380 ews4800mips|mipsco|newsmips|sgimips)
381 MACHINE_ARCH=mipseb
382 ;;
383
384 algor64|cobalt64|pmax64)
385 makewrappermachine=${MACHINE}
386 MACHINE=${MACHINE%64}
387 MACHINE_ARCH=mips64el
388 ;;
389
390 algor|arc|cobalt|hpcmips|pmax)
391 MACHINE_ARCH=mipsel
392 ;;
393
394 evbppc64|macppc64|ofppc64)
395 makewrappermachine=${MACHINE}
396 MACHINE=${MACHINE%64}
397 MACHINE_ARCH=powerpc64
398 ;;
399
400 amigappc|bebox|evbppc|ibmnws|macppc|mvmeppc|ofppc|prep|rs6000|sandpoint)
401 MACHINE_ARCH=powerpc
402 ;;
403
404 evbsh3) # no default MACHINE_ARCH
405 ;;
406
407 mmeye)
408 MACHINE_ARCH=sh3eb
409 ;;
410
411 dreamcast|hpcsh|landisk)
412 MACHINE_ARCH=sh3el
413 ;;
414
415 amd64)
416 MACHINE_ARCH=x86_64
417 ;;
418
419 alpha|i386|sparc|sparc64|vax|ia64)
420 MACHINE_ARCH=${MACHINE}
421 ;;
422
423 *)
424 bomb "Unknown target MACHINE: ${MACHINE}"
425 ;;
426
427 esac
428 }
429
430 validatearch()
431 {
432 # Ensure that the MACHINE_ARCH exists (and is supported by build.sh).
433 #
434 case "${MACHINE_ARCH}" in
435
436 alpha|arm|armeb|hppa|i386|m68000|m68k|mipse[bl]|mips64e[bl]|powerpc|powerpc64|sh3e[bl]|sparc|sparc64|vax|x86_64|ia64)
437 ;;
438
439 "")
440 bomb "No MACHINE_ARCH provided"
441 ;;
442
443 *)
444 bomb "Unknown target MACHINE_ARCH: ${MACHINE_ARCH}"
445 ;;
446
447 esac
448
449 # Determine valid MACHINE_ARCHs for MACHINE
450 #
451 case "${MACHINE}" in
452
453 evbarm)
454 arches="arm armeb"
455 ;;
456
457 algor|cobalt|pmax)
458 arches="mipsel mips64el"
459 ;;
460
461 evbmips|sbmips)
462 arches="mipseb mipsel mips64eb mips64el"
463 ;;
464
465 sgimips)
466 arches="mipseb mips64eb"
467 ;;
468
469 evbsh3)
470 arches="sh3eb sh3el"
471 ;;
472
473 macppc|evbppc|ofppc)
474 arches="powerpc powerpc64"
475 ;;
476 *)
477 oma="${MACHINE_ARCH}"
478 getarch
479 arches="${MACHINE_ARCH}"
480 MACHINE_ARCH="${oma}"
481 ;;
482
483 esac
484
485 # Ensure that MACHINE_ARCH supports MACHINE
486 #
487 archok=false
488 for a in ${arches}; do
489 if [ "${a}" = "${MACHINE_ARCH}" ]; then
490 archok=true
491 break
492 fi
493 done
494 ${archok} ||
495 bomb "MACHINE_ARCH '${MACHINE_ARCH}' does not support MACHINE '${MACHINE}'"
496 }
497
498 # nobomb_getmakevar --
499 # Given the name of a make variable in $1, print make's idea of the
500 # value of that variable, or return 1 if there's an error.
501 #
502 nobomb_getmakevar()
503 {
504 [ -x "${make}" ] || return 1
505 "${make}" -m ${TOP}/share/mk -s -B -f- _x_ <<EOF || return 1
506 _x_:
507 echo \${$1}
508 .include <bsd.prog.mk>
509 .include <bsd.kernobj.mk>
510 EOF
511 }
512
513 # nobomb_getmakevar --
514 # Given the name of a make variable in $1, print make's idea of the
515 # value of that variable, or bomb if there's an error.
516 #
517 bomb_getmakevar()
518 {
519 [ -x "${make}" ] || bomb "bomb_getmakevar $1: ${make} is not executable"
520 nobomb_getmakevar "$1" || bomb "bomb_getmakevar $1: ${make} failed"
521 }
522
523 # nobomb_getmakevar --
524 # Given the name of a make variable in $1, print make's idea of the
525 # value of that variable, or print a literal '$' followed by the
526 # variable name if ${make} is not executable. This is intended for use in
527 # messages that need to be readable even if $make hasn't been built,
528 # such as when build.sh is run with the "-n" option.
529 #
530 getmakevar()
531 {
532 if [ -x "${make}" ]; then
533 bomb_getmakevar "$1"
534 else
535 echo "\$$1"
536 fi
537 }
538
539 setmakeenv()
540 {
541 eval "$1='$2'; export $1"
542 makeenv="${makeenv} $1"
543 }
544
545 unsetmakeenv()
546 {
547 eval "unset $1"
548 makeenv="${makeenv} $1"
549 }
550
551 # Given a variable name in $1, modify the variable in place as follows:
552 # For each space-separated word in the variable, call resolvepath.
553 resolvepaths()
554 {
555 local var="$1"
556 local val
557 eval val=\"\${${var}}\"
558 local newval=''
559 local word
560 for word in ${val}; do
561 resolvepath word
562 newval="${newval}${newval:+ }${word}"
563 done
564 eval ${var}=\"\${newval}\"
565 }
566
567 # Given a variable name in $1, modify the variable in place as follows:
568 # Convert possibly-relative path to absolute path by prepending
569 # ${TOP} if necessary. Also delete trailing "/", if any.
570 resolvepath()
571 {
572 local var="$1"
573 local val
574 eval val=\"\${${var}}\"
575 case "${val}" in
576 /)
577 ;;
578 /*)
579 val="${val%/}"
580 ;;
581 *)
582 val="${TOP}/${val%/}"
583 ;;
584 esac
585 eval ${var}=\"\${val}\"
586 }
587
588 usage()
589 {
590 if [ -n "$*" ]; then
591 echo ""
592 echo "${progname}: $*"
593 fi
594 cat <<_usage_
595
596 Usage: ${progname} [-EnorUux] [-a arch] [-B buildid] [-C cdextras]
597 [-D dest] [-j njob] [-M obj] [-m mach] [-N noisy]
598 [-O obj] [-R release] [-S seed] [-T tools]
599 [-V var=[value]] [-w wrapper] [-X x11src] [-Y extsrcsrc]
600 [-Z var]
601 operation [...]
602
603 Build operations (all imply "obj" and "tools"):
604 build Run "make build".
605 distribution Run "make distribution" (includes DESTDIR/etc/ files).
606 release Run "make release" (includes kernels & distrib media).
607
608 Other operations:
609 help Show this message and exit.
610 makewrapper Create ${toolprefix}make-\${MACHINE} wrapper and ${toolprefix}make.
611 Always performed.
612 cleandir Run "make cleandir". [Default unless -u is used]
613 obj Run "make obj". [Default unless -o is used]
614 tools Build and install tools.
615 install=idir Run "make installworld" to \`idir' to install all sets
616 except \`etc'. Useful after "distribution" or "release"
617 kernel=conf Build kernel with config file \`conf'
618 releasekernel=conf Install kernel built by kernel=conf to RELEASEDIR.
619 modules Build kernel modules.
620 rumptest Do a linktest for rump (for developers).
621 sets Create binary sets in
622 RELEASEDIR/RELEASEMACHINEDIR/binary/sets.
623 DESTDIR should be populated beforehand.
624 sourcesets Create source sets in RELEASEDIR/source/sets.
625 syspkgs Create syspkgs in
626 RELEASEDIR/RELEASEMACHINEDIR/binary/syspkgs.
627 iso-image Create CD-ROM image in RELEASEDIR/iso.
628 iso-image-source Create CD-ROM image with source in RELEASEDIR/iso.
629 params Display various make(1) parameters.
630
631 Options:
632 -a arch Set MACHINE_ARCH to arch. [Default: deduced from MACHINE]
633 -B buildId Set BUILDID to buildId.
634 -C cdextras Append cdextras to CDEXTRA variable for inclusion on CD-ROM.
635 -D dest Set DESTDIR to dest. [Default: destdir.MACHINE]
636 -E Set "expert" mode; disables various safety checks.
637 Should not be used without expert knowledge of the build system.
638 -h Print this help message.
639 -j njob Run up to njob jobs in parallel; see make(1) -j.
640 -M obj Set obj root directory to obj; sets MAKEOBJDIRPREFIX.
641 Unsets MAKEOBJDIR.
642 -m mach Set MACHINE to mach; not required if NetBSD native.
643 -N noisy Set the noisyness (MAKEVERBOSE) level of the build:
644 0 Minimal output ("quiet")
645 1 Describe what is occurring
646 2 Describe what is occurring and echo the actual command
647 3 Ignore the effect of the "@" prefix in make commands
648 4 Trace shell commands using the shell's -x flag
649 [Default: 2]
650 -n Show commands that would be executed, but do not execute them.
651 -O obj Set obj root directory to obj; sets a MAKEOBJDIR pattern.
652 Unsets MAKEOBJDIRPREFIX.
653 -o Set MKOBJDIRS=no; do not create objdirs at start of build.
654 -R release Set RELEASEDIR to release. [Default: releasedir]
655 -r Remove contents of TOOLDIR and DESTDIR before building.
656 -S seed Set BUILDSEED to seed. [Default: NetBSD-majorversion]
657 -T tools Set TOOLDIR to tools. If unset, and TOOLDIR is not set in
658 the environment, ${toolprefix}make will be (re)built unconditionally.
659 -U Set MKUNPRIVED=yes; build without requiring root privileges,
660 install from an UNPRIVED build with proper file permissions.
661 -u Set MKUPDATE=yes; do not run "make cleandir" first.
662 Without this, everything is rebuilt, including the tools.
663 -V v=[val] Set variable \`v' to \`val'.
664 -w wrapper Create ${toolprefix}make script as wrapper.
665 [Default: \${TOOLDIR}/bin/${toolprefix}make-\${MACHINE}]
666 -X x11src Set X11SRCDIR to x11src. [Default: /usr/xsrc]
667 -x Set MKX11=yes; build X11 from X11SRCDIR
668 -Y extsrcsrc
669 Set EXTSRCSRCDIR to extsrcsrc. [Default: /usr/extsrc]
670 -y Set MKEXTSRC=yes; build extsrc from EXTSRCSRCDIR
671 -Z v Unset ("zap") variable \`v'.
672
673 _usage_
674 exit 1
675 }
676
677 parseoptions()
678 {
679 opts='a:B:C:D:Ehj:M:m:N:nO:oR:rS:T:UuV:w:xX:yY:Z:'
680 opt_a=no
681
682 if type getopts >/dev/null 2>&1; then
683 # Use POSIX getopts.
684 #
685 getoptcmd='getopts ${opts} opt && opt=-${opt}'
686 optargcmd=':'
687 optremcmd='shift $((${OPTIND} -1))'
688 else
689 type getopt >/dev/null 2>&1 ||
690 bomb "/bin/sh shell is too old; try ksh or bash"
691
692 # Use old-style getopt(1) (doesn't handle whitespace in args).
693 #
694 args="$(getopt ${opts} $*)"
695 [ $? = 0 ] || usage
696 set -- ${args}
697
698 getoptcmd='[ $# -gt 0 ] && opt="$1" && shift'
699 optargcmd='OPTARG="$1"; shift'
700 optremcmd=':'
701 fi
702
703 # Parse command line options.
704 #
705 while eval ${getoptcmd}; do
706 case ${opt} in
707
708 -a)
709 eval ${optargcmd}
710 MACHINE_ARCH=${OPTARG}
711 opt_a=yes
712 ;;
713
714 -B)
715 eval ${optargcmd}
716 BUILDID=${OPTARG}
717 ;;
718
719 -C)
720 eval ${optargcmd}; resolvepaths OPTARG
721 CDEXTRA="${CDEXTRA}${CDEXTRA:+ }${OPTARG}"
722 ;;
723
724 -D)
725 eval ${optargcmd}; resolvepath OPTARG
726 setmakeenv DESTDIR "${OPTARG}"
727 ;;
728
729 -E)
730 do_expertmode=true
731 ;;
732
733 -j)
734 eval ${optargcmd}
735 parallel="-j ${OPTARG}"
736 ;;
737
738 -M)
739 eval ${optargcmd}; resolvepath OPTARG
740 case "${OPTARG}" in
741 \$*) usage "-M argument must not begin with '$'"
742 ;;
743 *\$*) # can use resolvepath, but can't set TOP_objdir
744 resolvepath OPTARG
745 ;;
746 *) resolvepath OPTARG
747 TOP_objdir="${OPTARG}${TOP}"
748 ;;
749 esac
750 unsetmakeenv MAKEOBJDIR
751 setmakeenv MAKEOBJDIRPREFIX "${OPTARG}"
752 ;;
753
754 # -m overrides MACHINE_ARCH unless "-a" is specified
755 -m)
756 eval ${optargcmd}
757 MACHINE="${OPTARG}"
758 [ "${opt_a}" != "yes" ] && getarch
759 ;;
760
761 -N)
762 eval ${optargcmd}
763 case "${OPTARG}" in
764 0|1|2|3|4)
765 setmakeenv MAKEVERBOSE "${OPTARG}"
766 ;;
767 *)
768 usage "'${OPTARG}' is not a valid value for -N"
769 ;;
770 esac
771 ;;
772
773 -n)
774 runcmd=echo
775 ;;
776
777 -O)
778 eval ${optargcmd}
779 case "${OPTARG}" in
780 *\$*) usage "-O argument must not contain '$'"
781 ;;
782 *) resolvepath OPTARG
783 TOP_objdir="${OPTARG}"
784 ;;
785 esac
786 unsetmakeenv MAKEOBJDIRPREFIX
787 setmakeenv MAKEOBJDIR "\${.CURDIR:C,^$TOP,$OPTARG,}"
788 ;;
789
790 -o)
791 MKOBJDIRS=no
792 ;;
793
794 -R)
795 eval ${optargcmd}; resolvepath OPTARG
796 setmakeenv RELEASEDIR "${OPTARG}"
797 ;;
798
799 -r)
800 do_removedirs=true
801 do_rebuildmake=true
802 ;;
803
804 -S)
805 eval ${optargcmd}
806 setmakeenv BUILDSEED "${OPTARG}"
807 ;;
808
809 -T)
810 eval ${optargcmd}; resolvepath OPTARG
811 TOOLDIR="${OPTARG}"
812 export TOOLDIR
813 ;;
814
815 -U)
816 setmakeenv MKUNPRIVED yes
817 ;;
818
819 -u)
820 setmakeenv MKUPDATE yes
821 ;;
822
823 -V)
824 eval ${optargcmd}
825 case "${OPTARG}" in
826 # XXX: consider restricting which variables can be changed?
827 [a-zA-Z_][a-zA-Z_0-9]*=*)
828 setmakeenv "${OPTARG%%=*}" "${OPTARG#*=}"
829 ;;
830 *)
831 usage "-V argument must be of the form 'var=[value]'"
832 ;;
833 esac
834 ;;
835
836 -w)
837 eval ${optargcmd}; resolvepath OPTARG
838 makewrapper="${OPTARG}"
839 ;;
840
841 -X)
842 eval ${optargcmd}; resolvepath OPTARG
843 setmakeenv X11SRCDIR "${OPTARG}"
844 ;;
845
846 -x)
847 setmakeenv MKX11 yes
848 ;;
849
850 -Y)
851 eval ${optargcmd}; resolvepath OPTARG
852 setmakeenv EXTSRCSRCDIR "${OPTARG}"
853 ;;
854
855 -y)
856 setmakeenv MKEXTSRC yes
857 ;;
858
859 -Z)
860 eval ${optargcmd}
861 # XXX: consider restricting which variables can be unset?
862 unsetmakeenv "${OPTARG}"
863 ;;
864
865 --)
866 break
867 ;;
868
869 -'?'|-h)
870 usage
871 ;;
872
873 esac
874 done
875
876 # Validate operations.
877 #
878 eval ${optremcmd}
879 while [ $# -gt 0 ]; do
880 op=$1; shift
881 operations="${operations} ${op}"
882
883 case "${op}" in
884
885 help)
886 usage
887 ;;
888
889 makewrapper|cleandir|obj|tools|build|distribution|release|sets|sourcesets|syspkgs|params)
890 ;;
891
892 iso-image)
893 op=iso_image # used as part of a variable name
894 ;;
895
896 iso-image-source)
897 op=iso_image_source # used as part of a variable name
898 ;;
899
900 kernel=*|releasekernel=*)
901 arg=${op#*=}
902 op=${op%%=*}
903 [ -n "${arg}" ] ||
904 bomb "Must supply a kernel name with \`${op}=...'"
905 ;;
906
907 modules)
908 op=modules
909 ;;
910
911 install=*)
912 arg=${op#*=}
913 op=${op%%=*}
914 [ -n "${arg}" ] ||
915 bomb "Must supply a directory with \`install=...'"
916 ;;
917
918 rump|rumptest)
919 op=${op}
920 ;;
921
922 *)
923 usage "Unknown operation \`${op}'"
924 ;;
925
926 esac
927 eval do_${op}=true
928 done
929 [ -n "${operations}" ] || usage "Missing operation to perform."
930
931 # Set up MACHINE*. On a NetBSD host, these are allowed to be unset.
932 #
933 if [ -z "${MACHINE}" ]; then
934 [ "${uname_s}" = "NetBSD" ] ||
935 bomb "MACHINE must be set, or -m must be used, for cross builds."
936 MACHINE=${uname_m}
937 fi
938 [ -n "${MACHINE_ARCH}" ] || getarch
939 validatearch
940
941 # Set up default make(1) environment.
942 #
943 makeenv="${makeenv} TOOLDIR MACHINE MACHINE_ARCH MAKEFLAGS"
944 [ -z "${BUILDID}" ] || makeenv="${makeenv} BUILDID"
945 MAKEFLAGS="-de -m ${TOP}/share/mk ${MAKEFLAGS} MKOBJDIRS=${MKOBJDIRS-yes}"
946 export MAKEFLAGS MACHINE MACHINE_ARCH
947 }
948
949 sanitycheck()
950 {
951 # If the PATH contains any non-absolute components (including,
952 # but not limited to, "." or ""), then complain. As an exception,
953 # allow "" or "." as the last component of the PATH. This is fatal
954 # if expert mode is not in effect.
955 #
956 local path="${PATH}"
957 path="${path%:}" # delete trailing ":"
958 path="${path%:.}" # delete trailing ":."
959 case ":${path}:/" in
960 *:[!/]*)
961 if ${do_expertmode}; then
962 warning "PATH contains non-absolute components"
963 else
964 bomb "PATH environment variable must not" \
965 "contain non-absolute components"
966 fi
967 ;;
968 esac
969 }
970
971 # print_tooldir_make --
972 # Try to find and print a path to an existing
973 # ${TOOLDIR}/bin/${toolprefix}make, for use by rebuildmake() before a
974 # new version of ${toolprefix}make has been built.
975 #
976 # * If TOOLDIR was set in the environment or on the command line, use
977 # that value.
978 # * Otherwise try to guess what TOOLDIR would be if not overridden by
979 # /etc/mk.conf, and check whether the resulting directory contains
980 # a copy of ${toolprefix}make (this should work for everybody who
981 # doesn't override TOOLDIR via /etc/mk.conf);
982 # * Failing that, search for ${toolprefix}make, nbmake, bmake, or make,
983 # in the PATH (this might accidentally find a non-NetBSD version of
984 # make, which will lead to failure in the next step);
985 # * If a copy of make was found above, try to use it with
986 # nobomb_getmakevar to find the correct value for TOOLDIR, and believe the
987 # result only if it's a directory that already exists;
988 # * If a value of TOOLDIR was found above, and if
989 # ${TOOLDIR}/bin/${toolprefix}make exists, print that value.
990 #
991 print_tooldir_make()
992 {
993 local possible_TOP_OBJ
994 local possible_TOOLDIR
995 local possible_make
996 local tooldir_make
997
998 if [ -n "${TOOLDIR}" ]; then
999 echo "${TOOLDIR}/bin/${toolprefix}make"
1000 return 0
1001 fi
1002
1003 # Set host_ostype to something like "NetBSD-4.5.6-i386". This
1004 # is intended to match the HOST_OSTYPE variable in <bsd.own.mk>.
1005 #
1006 local host_ostype="${uname_s}-$(
1007 echo "${uname_r}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
1008 )-$(
1009 echo "${uname_p}" | sed -e 's/([^)]*)//g' -e 's/ /_/g'
1010 )"
1011
1012 # Look in a few potential locations for
1013 # ${possible_TOOLDIR}/bin/${toolprefix}make.
1014 # If we find it, then set possible_make.
1015 #
1016 # In the usual case (without interference from environment
1017 # variables or /etc/mk.conf), <bsd.own.mk> should set TOOLDIR to
1018 # "${_SRC_TOP_OBJ_}/tooldir.${host_ostype}".
1019 #
1020 # In practice it's difficult to figure out the correct value
1021 # for _SRC_TOP_OBJ_. In the easiest case, when the -M or -O
1022 # options were passed to build.sh, then ${TOP_objdir} will be
1023 # the correct value. We also try a few other possibilities, but
1024 # we do not replicate all the logic of <bsd.obj.mk>.
1025 #
1026 for possible_TOP_OBJ in \
1027 "${TOP_objdir}" \
1028 "${MAKEOBJDIRPREFIX:+${MAKEOBJDIRPREFIX}${TOP}}" \
1029 "${TOP}" \
1030 "${TOP}/obj" \
1031 "${TOP}/obj.${MACHINE}"
1032 do
1033 [ -n "${possible_TOP_OBJ}" ] || continue
1034 possible_TOOLDIR="${possible_TOP_OBJ}/tooldir.${host_ostype}"
1035 possible_make="${possible_TOOLDIR}/bin/${toolprefix}make"
1036 if [ -x "${possible_make}" ]; then
1037 break
1038 else
1039 unset possible_make
1040 fi
1041 done
1042
1043 # If the above didn't work, search the PATH for a suitable
1044 # ${toolprefix}make, nbmake, bmake, or make.
1045 #
1046 : ${possible_make:=$(find_in_PATH ${toolprefix}make '')}
1047 : ${possible_make:=$(find_in_PATH nbmake '')}
1048 : ${possible_make:=$(find_in_PATH bmake '')}
1049 : ${possible_make:=$(find_in_PATH make '')}
1050
1051 # At this point, we don't care whether possible_make is in the
1052 # correct TOOLDIR or not; we simply want it to be usable by
1053 # getmakevar to help us find the correct TOOLDIR.
1054 #
1055 # Use ${possible_make} with nobomb_getmakevar to try to find
1056 # the value of TOOLDIR. Believe the result only if it's
1057 # a directory that already exists and contains bin/${toolprefix}make.
1058 #
1059 if [ -x "${possible_make}" ]; then
1060 possible_TOOLDIR="$(
1061 make="${possible_make}" nobomb_getmakevar TOOLDIR
1062 )"
1063 if [ $? = 0 ] && [ -n "${possible_TOOLDIR}" ] \
1064 && [ -d "${possible_TOOLDIR}" ];
1065 then
1066 tooldir_make="${possible_TOOLDIR}/bin/${toolprefix}make"
1067 if [ -x "${tooldir_make}" ]; then
1068 echo "${tooldir_make}"
1069 return 0
1070 fi
1071 fi
1072 fi
1073 return 1
1074 }
1075
1076 # rebuildmake --
1077 # Rebuild nbmake in a temporary directory if necessary. Sets $make
1078 # to a path to the nbmake executable. Sets done_rebuildmake=true
1079 # if nbmake was rebuilt.
1080 #
1081 # There is a cyclic dependency between building nbmake and choosing
1082 # TOOLDIR: TOOLDIR may be affected by settings in /etc/mk.conf, so we
1083 # would like to use getmakevar to get the value of TOOLDIR; but we can't
1084 # use getmakevar before we have an up to date version of nbmake; we
1085 # might already have an up to date version of nbmake in TOOLDIR, but we
1086 # don't yet know where TOOLDIR is.
1087 #
1088 # The default value of TOOLDIR also depends on the location of the top
1089 # level object directory, so $(getmakevar TOOLDIR) invoked before or
1090 # after making the top level object directory may produce different
1091 # results.
1092 #
1093 # Strictly speaking, we should do the following:
1094 #
1095 # 1. build a new version of nbmake in a temporary directory;
1096 # 2. use the temporary nbmake to create the top level obj directory;
1097 # 3. use $(getmakevar TOOLDIR) with the temporary nbmake to
1098 # get the corect value of TOOLDIR;
1099 # 4. move the temporary nbmake to ${TOOLDIR}/bin/nbmake.
1100 #
1101 # However, people don't like building nbmake unnecessarily if their
1102 # TOOLDIR has not changed since an earlier build. We try to avoid
1103 # rebuilding a temporary version of nbmake by taking some shortcuts to
1104 # guess a value for TOOLDIR, looking for an existing version of nbmake
1105 # in that TOOLDIR, and checking whether that nbmake is newer than the
1106 # sources used to build it.
1107 #
1108 rebuildmake()
1109 {
1110 make="$(print_tooldir_make)"
1111 if [ -n "${make}" ] && [ -x "${make}" ]; then
1112 for f in usr.bin/make/*.[ch] usr.bin/make/lst.lib/*.[ch]; do
1113 if [ "${f}" -nt "${make}" ]; then
1114 statusmsg "${make} outdated" \
1115 "(older than ${f}), needs building."
1116 do_rebuildmake=true
1117 break
1118 fi
1119 done
1120 else
1121 statusmsg "No \$TOOLDIR/bin/${toolprefix}make, needs building."
1122 do_rebuildmake=true
1123 fi
1124
1125 # Build bootstrap ${toolprefix}make if needed.
1126 if ${do_rebuildmake}; then
1127 statusmsg "Bootstrapping ${toolprefix}make"
1128 ${runcmd} cd "${tmpdir}"
1129 ${runcmd} env CC="${HOST_CC-cc}" CPPFLAGS="${HOST_CPPFLAGS}" \
1130 CFLAGS="${HOST_CFLAGS--O}" LDFLAGS="${HOST_LDFLAGS}" \
1131 ${HOST_SH} "${TOP}/tools/make/configure" ||
1132 bomb "Configure of ${toolprefix}make failed"
1133 ${runcmd} ${HOST_SH} buildmake.sh ||
1134 bomb "Build of ${toolprefix}make failed"
1135 make="${tmpdir}/${toolprefix}make"
1136 ${runcmd} cd "${TOP}"
1137 ${runcmd} rm -f usr.bin/make/*.o usr.bin/make/lst.lib/*.o
1138 done_rebuildmake=true
1139 fi
1140 }
1141
1142 validatemakeparams()
1143 {
1144 if [ "${runcmd}" = "echo" ]; then
1145 TOOLCHAIN_MISSING=no
1146 EXTERNAL_TOOLCHAIN=""
1147 else
1148 TOOLCHAIN_MISSING=$(bomb_getmakevar TOOLCHAIN_MISSING)
1149 EXTERNAL_TOOLCHAIN=$(bomb_getmakevar EXTERNAL_TOOLCHAIN)
1150 fi
1151 if [ "${TOOLCHAIN_MISSING}" = "yes" ] && \
1152 [ -z "${EXTERNAL_TOOLCHAIN}" ]; then
1153 ${runcmd} echo "ERROR: build.sh (in-tree cross-toolchain) is not yet available for"
1154 ${runcmd} echo " MACHINE: ${MACHINE}"
1155 ${runcmd} echo " MACHINE_ARCH: ${MACHINE_ARCH}"
1156 ${runcmd} echo ""
1157 ${runcmd} echo "All builds for this platform should be done via a traditional make"
1158 ${runcmd} echo "If you wish to use an external cross-toolchain, set"
1159 ${runcmd} echo " EXTERNAL_TOOLCHAIN=<path to toolchain root>"
1160 ${runcmd} echo "in either the environment or mk.conf and rerun"
1161 ${runcmd} echo " ${progname} $*"
1162 exit 1
1163 fi
1164
1165 # Normalise MKOBJDIRS, MKUNPRIVED, and MKUPDATE
1166 # These may be set as build.sh options or in "mk.conf".
1167 # Don't export them as they're only used for tests in build.sh.
1168 #
1169 MKOBJDIRS=$(getmakevar MKOBJDIRS)
1170 MKUNPRIVED=$(getmakevar MKUNPRIVED)
1171 MKUPDATE=$(getmakevar MKUPDATE)
1172
1173 if [ "${MKOBJDIRS}" != "no" ]; then
1174 # Create the top-level object directory.
1175 #
1176 # "make obj NOSUBDIR=" can handle most cases, but it
1177 # can't handle the case where MAKEOBJDIRPREFIX is set
1178 # while the corresponding directory does not exist
1179 # (rules in <bsd.obj.mk> would abort the build). We
1180 # therefore have to handle the MAKEOBJDIRPREFIX case
1181 # without invoking "make obj". The MAKEOBJDIR case
1182 # could be handled either way, but we choose to handle
1183 # it similarly to MAKEOBJDIRPREFIX.
1184 #
1185 if [ -n "${TOP_obj}" ]; then
1186 # It must have been set by the "-M" or "-O"
1187 # command line options, so there's no need to
1188 # use getmakevar
1189 :
1190 elif [ -n "$MAKEOBJDIRPREFIX" ]; then
1191 TOP_obj="$(getmakevar MAKEOBJDIRPREFIX)${TOP}"
1192 elif [ -n "$MAKEOBJDIR" ]; then
1193 TOP_obj="$(getmakevar MAKEOBJDIR)"
1194 fi
1195 if [ -n "$TOP_obj" ]; then
1196 ${runcmd} mkdir -p "${TOP_obj}" ||
1197 bomb "Can't create top level object directory" \
1198 "${TOP_obj}"
1199 else
1200 ${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
1201 bomb "Can't create top level object directory" \
1202 "using make obj"
1203 fi
1204
1205 # make obj in tools to ensure that the objdir for "tools"
1206 # is available.
1207 #
1208 ${runcmd} cd tools
1209 ${runcmd} "${make}" -m ${TOP}/share/mk obj NOSUBDIR= ||
1210 bomb "Failed to make obj in tools"
1211 ${runcmd} cd "${TOP}"
1212 fi
1213
1214 # Find TOOLDIR, DESTDIR, and RELEASEDIR, according to getmakevar,
1215 # and bomb if they have changed from the values we had from the
1216 # command line or environment.
1217 #
1218 # This must be done after creating the top-level object directory.
1219 #
1220 for var in TOOLDIR DESTDIR RELEASEDIR
1221 do
1222 eval oldval=\"\$${var}\"
1223 newval="$(getmakevar $var)"
1224 if ! $do_expertmode; then
1225 : ${_SRC_TOP_OBJ_:=$(getmakevar _SRC_TOP_OBJ_)}
1226 case "$var" in
1227 DESTDIR)
1228 : ${newval:=${_SRC_TOP_OBJ_}/destdir.${MACHINE}}
1229 makeenv="${makeenv} DESTDIR"
1230 ;;
1231 RELEASEDIR)
1232 : ${newval:=${_SRC_TOP_OBJ_}/releasedir}
1233 makeenv="${makeenv} RELEASEDIR"
1234 ;;
1235 esac
1236 fi
1237 if [ -n "$oldval" ] && [ "$oldval" != "$newval" ]; then
1238 bomb "Value of ${var} has changed" \
1239 "(was \"${oldval}\", now \"${newval}\")"
1240 fi
1241 eval ${var}=\"\${newval}\"
1242 eval export ${var}
1243 statusmsg2 "${var} path:" "${newval}"
1244 done
1245
1246 # RELEASEMACHINEDIR is just a subdir name, e.g. "i386".
1247 RELEASEMACHINEDIR=$(getmakevar RELEASEMACHINEDIR)
1248
1249 # Check validity of TOOLDIR and DESTDIR.
1250 #
1251 if [ -z "${TOOLDIR}" ] || [ "${TOOLDIR}" = "/" ]; then
1252 bomb "TOOLDIR '${TOOLDIR}' invalid"
1253 fi
1254 removedirs="${TOOLDIR}"
1255
1256 if [ -z "${DESTDIR}" ] || [ "${DESTDIR}" = "/" ]; then
1257 if ${do_build} || ${do_distribution} || ${do_release}; then
1258 if ! ${do_build} || \
1259 [ "${uname_s}" != "NetBSD" ] || \
1260 [ "${uname_m}" != "${MACHINE}" ]; then
1261 bomb "DESTDIR must != / for cross builds, or ${progname} 'distribution' or 'release'."
1262 fi
1263 if ! ${do_expertmode}; then
1264 bomb "DESTDIR must != / for non -E (expert) builds"
1265 fi
1266 statusmsg "WARNING: Building to /, in expert mode."
1267 statusmsg " This may cause your system to break! Reasons include:"
1268 statusmsg " - your kernel is not up to date"
1269 statusmsg " - the libraries or toolchain have changed"
1270 statusmsg " YOU HAVE BEEN WARNED!"
1271 fi
1272 else
1273 removedirs="${removedirs} ${DESTDIR}"
1274 fi
1275 if ${do_build} || ${do_distribution} || ${do_release}; then
1276 if ! ${do_expertmode} && \
1277 [ "$id_u" -ne 0 ] && \
1278 [ "${MKUNPRIVED}" = "no" ] ; then
1279 bomb "-U or -E must be set for build as an unprivileged user."
1280 fi
1281 fi
1282 if ${do_releasekernel} && [ -z "${RELEASEDIR}" ]; then
1283 bomb "Must set RELEASEDIR with \`releasekernel=...'"
1284 fi
1285
1286 # Install as non-root is a bad idea.
1287 #
1288 if ${do_install} && [ "$id_u" -ne 0 ] ; then
1289 if ${do_expertmode}; then
1290 warning "Will install as an unprivileged user."
1291 else
1292 bomb "-E must be set for install as an unprivileged user."
1293 fi
1294 fi
1295
1296 # If a previous build.sh run used -U (and therefore created a
1297 # METALOG file), then most subsequent build.sh runs must also
1298 # use -U. If DESTDIR is about to be removed, then don't perform
1299 # this check.
1300 #
1301 case "${do_removedirs} ${removedirs} " in
1302 true*" ${DESTDIR} "*)
1303 # DESTDIR is about to be removed
1304 ;;
1305 *)
1306 if ( ${do_build} || ${do_distribution} || ${do_release} || \
1307 ${do_install} ) && \
1308 [ -e "${DESTDIR}/METALOG" ] && \
1309 [ "${MKUNPRIVED}" = "no" ] ; then
1310 if $do_expertmode; then
1311 warning "A previous build.sh run specified -U."
1312 else
1313 bomb "A previous build.sh run specified -U; you must specify it again now."
1314 fi
1315 fi
1316 ;;
1317 esac
1318 }
1319
1320
1321 createmakewrapper()
1322 {
1323 # Remove the target directories.
1324 #
1325 if ${do_removedirs}; then
1326 for f in ${removedirs}; do
1327 statusmsg "Removing ${f}"
1328 ${runcmd} rm -r -f "${f}"
1329 done
1330 fi
1331
1332 # Recreate $TOOLDIR.
1333 #
1334 ${runcmd} mkdir -p "${TOOLDIR}/bin" ||
1335 bomb "mkdir of '${TOOLDIR}/bin' failed"
1336
1337 # If we did not previously rebuild ${toolprefix}make, then
1338 # check whether $make is still valid and the same as the output
1339 # from print_tooldir_make. If not, then rebuild make now. A
1340 # possible reason for this being necessary is that the actual
1341 # value of TOOLDIR might be different from the value guessed
1342 # before the top level obj dir was created.
1343 #
1344 if ! ${done_rebuildmake} && \
1345 ( [ ! -x "$make" ] || [ "$make" != "$(print_tooldir_make)" ] )
1346 then
1347 rebuildmake
1348 fi
1349
1350 # Install ${toolprefix}make if it was built.
1351 #
1352 if ${done_rebuildmake}; then
1353 ${runcmd} rm -f "${TOOLDIR}/bin/${toolprefix}make"
1354 ${runcmd} cp "${make}" "${TOOLDIR}/bin/${toolprefix}make" ||
1355 bomb "Failed to install \$TOOLDIR/bin/${toolprefix}make"
1356 make="${TOOLDIR}/bin/${toolprefix}make"
1357 statusmsg "Created ${make}"
1358 fi
1359
1360 # Build a ${toolprefix}make wrapper script, usable by hand as
1361 # well as by build.sh.
1362 #
1363 if [ -z "${makewrapper}" ]; then
1364 makewrapper="${TOOLDIR}/bin/${toolprefix}make-${makewrappermachine:-${MACHINE}}"
1365 [ -z "${BUILDID}" ] || makewrapper="${makewrapper}-${BUILDID}"
1366 fi
1367
1368 ${runcmd} rm -f "${makewrapper}"
1369 if [ "${runcmd}" = "echo" ]; then
1370 echo 'cat <<EOF >'${makewrapper}
1371 makewrapout=
1372 else
1373 makewrapout=">>\${makewrapper}"
1374 fi
1375
1376 case "${KSH_VERSION:-${SH_VERSION}}" in
1377 *PD\ KSH*|*MIRBSD\ KSH*)
1378 set +o braceexpand
1379 ;;
1380 esac
1381
1382 eval cat <<EOF ${makewrapout}
1383 #! ${HOST_SH}
1384 # Set proper variables to allow easy "make" building of a NetBSD subtree.
1385 # Generated from: \$NetBSD: build.sh,v 1.238 2010/08/24 13:35:32 pgoyette Exp $
1386 # with these arguments: ${_args}
1387 #
1388
1389 EOF
1390 {
1391 for f in ${makeenv}; do
1392 if eval "[ -z \"\${$f}\" -a \"\${${f}-X}\" = \"X\" ]"; then
1393 eval echo "unset ${f}"
1394 else
1395 eval echo "${f}=\'\$$(echo ${f})\'\;\ export\ ${f}"
1396 fi
1397 done
1398
1399 eval cat <<EOF
1400 MAKEWRAPPERMACHINE=${makewrappermachine:-${MACHINE}}; export MAKEWRAPPERMACHINE
1401 USETOOLS=yes; export USETOOLS
1402 EOF
1403 } | eval sort -u "${makewrapout}"
1404 eval cat <<EOF "${makewrapout}"
1405
1406 exec "\${TOOLDIR}/bin/${toolprefix}make" \${1+"\$@"}
1407 EOF
1408 [ "${runcmd}" = "echo" ] && echo EOF
1409 ${runcmd} chmod +x "${makewrapper}"
1410 statusmsg2 "Updated makewrapper:" "${makewrapper}"
1411 }
1412
1413 make_in_dir()
1414 {
1415 dir="$1"
1416 op="$2"
1417 ${runcmd} cd "${dir}" ||
1418 bomb "Failed to cd to \"${dir}\""
1419 ${runcmd} "${makewrapper}" ${parallel} ${op} ||
1420 bomb "Failed to make ${op} in \"${dir}\""
1421 ${runcmd} cd "${TOP}" ||
1422 bomb "Failed to cd back to \"${TOP}\""
1423 }
1424
1425 buildtools()
1426 {
1427 if [ "${MKOBJDIRS}" != "no" ]; then
1428 ${runcmd} "${makewrapper}" ${parallel} obj-tools ||
1429 bomb "Failed to make obj-tools"
1430 fi
1431 if [ "${MKUPDATE}" = "no" ]; then
1432 make_in_dir tools cleandir
1433 fi
1434 make_in_dir tools dependall
1435 make_in_dir tools install
1436 statusmsg "Tools built to ${TOOLDIR}"
1437 }
1438
1439 getkernelconf()
1440 {
1441 kernelconf="$1"
1442 if [ "${MKOBJDIRS}" != "no" ]; then
1443 # The correct value of KERNOBJDIR might
1444 # depend on a prior "make obj" in
1445 # ${KERNSRCDIR}/${KERNARCHDIR}/compile.
1446 #
1447 KERNSRCDIR="$(getmakevar KERNSRCDIR)"
1448 KERNARCHDIR="$(getmakevar KERNARCHDIR)"
1449 make_in_dir "${KERNSRCDIR}/${KERNARCHDIR}/compile" obj
1450 fi
1451 KERNCONFDIR="$(getmakevar KERNCONFDIR)"
1452 KERNOBJDIR="$(getmakevar KERNOBJDIR)"
1453 case "${kernelconf}" in
1454 */*)
1455 kernelconfpath="${kernelconf}"
1456 kernelconfname="${kernelconf##*/}"
1457 ;;
1458 *)
1459 kernelconfpath="${KERNCONFDIR}/${kernelconf}"
1460 kernelconfname="${kernelconf}"
1461 ;;
1462 esac
1463 kernelbuildpath="${KERNOBJDIR}/${kernelconfname}"
1464 }
1465
1466 buildkernel()
1467 {
1468 if ! ${do_tools} && ! ${buildkernelwarned:-false}; then
1469 # Building tools every time we build a kernel is clearly
1470 # unnecessary. We could try to figure out whether rebuilding
1471 # the tools is necessary this time, but it doesn't seem worth
1472 # the trouble. Instead, we say it's the user's responsibility
1473 # to rebuild the tools if necessary.
1474 #
1475 statusmsg "Building kernel without building new tools"
1476 buildkernelwarned=true
1477 fi
1478 getkernelconf $1
1479 statusmsg2 "Building kernel:" "${kernelconf}"
1480 statusmsg2 "Build directory:" "${kernelbuildpath}"
1481 ${runcmd} mkdir -p "${kernelbuildpath}" ||
1482 bomb "Cannot mkdir: ${kernelbuildpath}"
1483 if [ "${MKUPDATE}" = "no" ]; then
1484 make_in_dir "${kernelbuildpath}" cleandir
1485 fi
1486 [ -x "${TOOLDIR}/bin/${toolprefix}config" ] \
1487 || bomb "${TOOLDIR}/bin/${toolprefix}config does not exist. You need to \"$0 tools\" first."
1488 ${runcmd} "${TOOLDIR}/bin/${toolprefix}config" -b "${kernelbuildpath}" \
1489 -s "${TOP}/sys" "${kernelconfpath}" ||
1490 bomb "${toolprefix}config failed for ${kernelconf}"
1491 make_in_dir "${kernelbuildpath}" depend
1492 make_in_dir "${kernelbuildpath}" all
1493
1494 if [ "${runcmd}" != "echo" ]; then
1495 statusmsg "Kernels built from ${kernelconf}:"
1496 kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
1497 for kern in ${kernlist:-netbsd}; do
1498 [ -f "${kernelbuildpath}/${kern}" ] && \
1499 echo " ${kernelbuildpath}/${kern}"
1500 done | tee -a "${results}"
1501 fi
1502 }
1503
1504 releasekernel()
1505 {
1506 getkernelconf $1
1507 kernelreldir="${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/kernel"
1508 ${runcmd} mkdir -p "${kernelreldir}"
1509 kernlist=$(awk '$1 == "config" { print $2 }' ${kernelconfpath})
1510 for kern in ${kernlist:-netbsd}; do
1511 builtkern="${kernelbuildpath}/${kern}"
1512 [ -f "${builtkern}" ] || continue
1513 releasekern="${kernelreldir}/${kern}-${kernelconfname}.gz"
1514 statusmsg2 "Kernel copy:" "${releasekern}"
1515 if [ "${runcmd}" = "echo" ]; then
1516 echo "gzip -c -9 < ${builtkern} > ${releasekern}"
1517 else
1518 gzip -c -9 < "${builtkern}" > "${releasekern}"
1519 fi
1520 done
1521 }
1522
1523 buildmodules()
1524 {
1525 setmakeenv MKBINUTILS no
1526 if ! ${do_tools} && ! ${buildmoduleswarned:-false}; then
1527 # Building tools every time we build modules is clearly
1528 # unnecessary as well as a kernel.
1529 #
1530 statusmsg "Building modules without building new tools"
1531 buildmoduleswarned=true
1532 fi
1533
1534 statusmsg "Building kernel modules for NetBSD/${MACHINE} ${DISTRIBVER}"
1535 if [ "${MKOBJDIRS}" != "no" ]; then
1536 make_in_dir sys/modules obj ||
1537 bomb "Failed to make obj in sys/modules"
1538 fi
1539 if [ "${MKUPDATE}" = "no" ]; then
1540 make_in_dir sys/modules cleandir
1541 fi
1542 ${runcmd} "${makewrapper}" ${parallel} do-sys-modules ||
1543 bomb "Failed to make do-sys-modules"
1544
1545 statusmsg "Successful build of kernel modules for NetBSD/${MACHINE} ${DISTRIBVER}"
1546 }
1547
1548 installworld()
1549 {
1550 dir="$1"
1551 ${runcmd} "${makewrapper}" INSTALLWORLDDIR="${dir}" installworld ||
1552 bomb "Failed to make installworld to ${dir}"
1553 statusmsg "Successful installworld to ${dir}"
1554 }
1555
1556 # Run rump build&link tests.
1557 #
1558 # To make this feasible for running without having to install includes and
1559 # libraries into destdir (i.e. quick), we only run ld. This is possible
1560 # since the rump kernel is a closed namespace apart from calls to rumpuser.
1561 # Therefore, if ld complains only about rumpuser symbols, rump kernel
1562 # linking was successful.
1563 #
1564 # We test that rump links with a number of component configurations.
1565 # These attempt to mimic what is encountered in the full build.
1566 # See list below. The list should probably be either autogenerated
1567 # or managed elsewhere. But keep it here until a better idea arises.
1568 #
1569 # Above all, note that THIS IS NOT A SUBSTITUTE FOR A FULL BUILD.
1570 #
1571
1572 RUMP_LIBSETS='
1573 -lrump,
1574 -lrumpvfs -lrump,
1575 -lrumpdev -lrump,
1576 -lrumpnet -lrump,
1577 -lrumpkern_tty -lrumpvfs -lrump,
1578 -lrumpfs_tmpfs -lrumpvfs -lrump,
1579 -lrumpfs_ffs -lrumpfs_msdos -lrumpvfs -lrumpdev_disk -lrumpdev -lrump,
1580 -lrumpnet_virtif -lrumpnet_netinet -lrumpnet_net -lrumpnet -lrump,
1581 -lrumpnet_sockin -lrumpfs_smbfs -lrumpdev_netsmb
1582 -lrumpcrypto -lrumpdev -lrumpnet -lrumpvfs -lrump,
1583 -lrumpnet_sockin -lrumpfs_nfs -lrumpnet -lrumpvfs -lrump,
1584 -lrumpdev_cgd -lrumpdev_raidframe -lrumpdev_disk -lrumpdev_rnd
1585 -lrumpdev -lrumpvfs -lrumpcrypto -lrump'
1586 dorump()
1587 {
1588 local doclean=""
1589 local doobjs=""
1590
1591 # we cannot link libs without building csu, and that leads to lossage
1592 [ "${1}" != "rumptest" ] && bomb 'build.sh rump not yet functional. ' \
1593 'did you mean "rumptest"?'
1594
1595 # create obj and distrib dirs
1596 if [ "${MKOBJDIRS}" != "no" ]; then
1597 make_in_dir "${NETBSDSRCDIR}/etc/mtree" obj
1598 make_in_dir "${NETBSDSRCDIR}/sys/rump" obj
1599 fi
1600 ${runcmd} "${makewrapper}" ${parallel} do-distrib-dirs \
1601 || bomb 'could not create distrib-dirs'
1602
1603 [ "${MKUPDATE}" = "no" ] && doclean="cleandir"
1604 targlist="${doclean} ${doobjs} dependall install"
1605 # optimize: for test we build only static libs (3x test speedup)
1606 if [ "${1}" = "rumptest" ] ; then
1607 setmakeenv NOPIC 1
1608 setmakeenv NOPROFILE 1
1609 fi
1610 for cmd in ${targlist} ; do
1611 make_in_dir "${NETBSDSRCDIR}/sys/rump" ${cmd}
1612 done
1613
1614 # if we just wanted to build & install rump, we're done
1615 [ "${1}" != "rumptest" ] && return
1616
1617 ${runcmd} cd "${NETBSDSRCDIR}/sys/rump/librump/rumpkern" \
1618 || bomb "cd to rumpkern failed"
1619 md_quirks=`${runcmd} "${makewrapper}" -V '${_SYMQUIRK}'`
1620 # one little, two little, three little backslashes ...
1621 md_quirks="$(echo ${md_quirks} | sed 's,\\,\\\\,g'";s/'//g" )"
1622 ${runcmd} cd "${TOP}" || bomb "cd to ${TOP} failed"
1623 tool_ld=`${runcmd} "${makewrapper}" -V '${LD}'`
1624
1625 local oIFS="${IFS}"
1626 IFS=","
1627 for set in ${RUMP_LIBSETS} ; do
1628 IFS="${oIFS}"
1629 ${runcmd} ${tool_ld} -nostdlib -L${DESTDIR}/usr/lib \
1630 -static --whole-archive ${set} 2>&1 | \
1631 awk -v quirks="${md_quirks}" '
1632 /undefined reference/ &&
1633 !/more undefined references.*follow/{
1634 if (match($NF,
1635 "`(rumpuser_|__" quirks ")") == 0)
1636 fails[NR] = $0
1637 }
1638 /cannot find -l/{fails[NR] = $0}
1639 END{
1640 for (x in fails)
1641 print fails[x]
1642 exit x!=0
1643 }'
1644 [ $? -ne 0 ] && bomb "Testlink of rump failed: ${set}"
1645 done
1646 statusmsg "Rump build&link tests successful"
1647 }
1648
1649 main()
1650 {
1651 initdefaults
1652 _args=$@
1653 parseoptions "$@"
1654
1655 sanitycheck
1656
1657 build_start=$(date)
1658 statusmsg2 "${progname} command:" "$0 $@"
1659 statusmsg2 "${progname} started:" "${build_start}"
1660 statusmsg2 "NetBSD version:" "${DISTRIBVER}"
1661 statusmsg2 "MACHINE:" "${MACHINE}"
1662 statusmsg2 "MACHINE_ARCH:" "${MACHINE_ARCH}"
1663 statusmsg2 "Build platform:" "${uname_s} ${uname_r} ${uname_m}"
1664 statusmsg2 "HOST_SH:" "${HOST_SH}"
1665
1666 rebuildmake
1667 validatemakeparams
1668 createmakewrapper
1669
1670 # Perform the operations.
1671 #
1672 for op in ${operations}; do
1673 case "${op}" in
1674
1675 makewrapper)
1676 # no-op
1677 ;;
1678
1679 tools)
1680 buildtools
1681 ;;
1682
1683 sets)
1684 statusmsg "Building sets from pre-populated ${DESTDIR}"
1685 ${runcmd} "${makewrapper}" ${parallel} ${op} ||
1686 bomb "Failed to make ${op}"
1687 setdir=${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
1688 statusmsg "Built sets to ${setdir}"
1689 ;;
1690
1691 cleandir|obj|build|distribution|release|sourcesets|syspkgs|params)
1692 ${runcmd} "${makewrapper}" ${parallel} ${op} ||
1693 bomb "Failed to make ${op}"
1694 statusmsg "Successful make ${op}"
1695 ;;
1696
1697 iso-image|iso-image-source)
1698 ${runcmd} "${makewrapper}" ${parallel} \
1699 CDEXTRA="$CDEXTRA" ${op} ||
1700 bomb "Failed to make ${op}"
1701 statusmsg "Successful make ${op}"
1702 ;;
1703
1704 kernel=*)
1705 arg=${op#*=}
1706 buildkernel "${arg}"
1707 ;;
1708
1709 releasekernel=*)
1710 arg=${op#*=}
1711 releasekernel "${arg}"
1712 ;;
1713
1714 modules)
1715 buildmodules
1716 ;;
1717
1718 install=*)
1719 arg=${op#*=}
1720 if [ "${arg}" = "/" ] && \
1721 ( [ "${uname_s}" != "NetBSD" ] || \
1722 [ "${uname_m}" != "${MACHINE}" ] ); then
1723 bomb "'${op}' must != / for cross builds."
1724 fi
1725 installworld "${arg}"
1726 ;;
1727
1728 rump|rumptest)
1729 dorump "${op}"
1730 ;;
1731
1732 *)
1733 bomb "Unknown operation \`${op}'"
1734 ;;
1735
1736 esac
1737 done
1738
1739 statusmsg2 "${progname} ended:" "$(date)"
1740 if [ -s "${results}" ]; then
1741 echo "===> Summary of results:"
1742 sed -e 's/^===>//;s/^/ /' "${results}"
1743 echo "===> ."
1744 fi
1745 }
1746
1747 main "$@"
1748