pullup #2873
[pkgsrc.git] / bootstrap / bootstrap
1 #! /bin/sh
2
3 # $NetBSD: bootstrap,v 1.148 2009/06/27 22:30:50 ahoka Exp $
4 #
5 #
6 # Copyright (c) 2001-2002 Alistair G. Crooks.  All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 # 1. Redistributions of source code must retain the above copyright
12 #    notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 #    notice, this list of conditions and the following disclaimer in the
15 #    documentation and/or other materials provided with the distribution.
16 # 3. All advertising materials mentioning features or use of this software
17 #    must display the following acknowledgement:
18 #       This product includes software developed by Alistair G. Crooks
19 #       for the NetBSD project.
20 # 4. The name of the author may not be used to endorse or promote
21 #    products derived from this software without specific prior written
22 #    permission.
23 #
24 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
25 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
28 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
30 # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #
36 #set -x
37
38 # the following environment variables are honored:
39 # compiler/linker flags: CFLAGS, CPPFLAGS, LDFLAGS
40 # tools: CP, GREP, ID, MKDIR, SH, TEST, TOUCH, XARGS
41
42
43 BOOTSTRAP_VERSION=20060721
44
45 # Don't let the bootstrap program get confused by a pre-existing mk.conf
46 # file.
47 MAKECONF=/dev/null
48 export MAKECONF
49
50 ignorecasecheck=no
51 unprivileged=no
52
53 preserve_path=no
54
55 # where the building takes place
56 bootstrapdir=`dirname "$0"`
57 bootstrapdir=`cd "${bootstrapdir}" && pwd`
58 pkgsrcdir=`dirname "${bootstrapdir}"`
59 wrkdir="`pwd`/work"
60
61 usage="Usage: $0 "'
62     [ --workdir <workdir> ]
63     [ --prefix <prefix> ]
64     [ --pkgdbdir <pkgdbdir> ]
65     [ --pkgmandir <pkgmandir> ]
66     [ --sysconfdir <sysconfdir> ]
67     [ --varbase <varbase> ]
68     [ --fetch-cmd <ftp command> ]
69     [ --ignore-case-check ]
70     [ --unprivileged | --ignore-user-check ]
71     [ --preserve-path ]
72     [ --compiler <compiler> ]
73     [ --abi [32|64] ]
74     [ --mk-fragment <mk.conf> ]
75     [ --binary-kit <tarball> ]
76     [ --gzip-binary-kit <tarball> ]
77     [ --binary-macpkg <pkg> ]
78     [ --full ]
79     [ --quiet ]
80     [ --help ]'
81
82 # this replicates some of the logic in bsd.prefs.mk. until
83 # bootstrap-pkgsrc is merged into pkgsrc, we need to determine the
84 # right value for OPSYS and MACHINE_ARCH.
85
86 # strip / for BSD/OS, strip - for HP-UX
87 opsys=`uname -s | tr -d /-`
88
89 mkbinarykit_macpkg()
90 {
91         local macdestdir
92         macdestdir=${wrkdir}/macpkg-destdir
93         rm -rf ${macdestdir} || die "cleanup destdir"
94
95         mkdir -p ${macdestdir}${prefix} || die "mkdir destprefix"
96         rmdir ${macdestdir}${prefix} || die "rmdir destprefix"
97         cp -Rp ${prefix} ${macdestdir}${prefix} || die "copy prefix"
98
99         if [ ! -d ${macdestdir}${pkgdbdir} ]; then
100                 mkdir -p ${macdestdir}${pkgdbdir} || die "mkdir destdbdir"
101                 rmdir ${macdestdir}${pkgdbdir} || die "rmdir destdbdir"
102                 cp -Rp ${pkgdbdir} ${macdestdir}${pkgdbdir} || die "copy dbdir"
103         fi
104
105         ${sedprog} -e "s|%WRKDIR%|${wrkdir}|g" \
106                 -e "s|%TARGETDIR%|${targetdir}|g" -e "s|%DATE%|${date}|g" \
107                 < macpkg.pmproj.in > ${wrkdir}/macpkg.pmproj
108         ${packagemaker} -build -proj ${wrkdir}/macpkg.pmproj -p "${binary_macpkg}"
109 }
110
111 mkbinarykit_tar()
112 {
113         # in case tar was built by bootstrap
114         PATH="$prefix/bin:$PATH"; export PATH
115         cd / && tar -hcf "${binary_kit}" .$prefix .$pkgdbdir .$etc_mk_conf
116 }
117
118 mkbinarykit_tgz()
119 {
120         # in case tar was built by bootstrap
121         PATH="$prefix/bin:$PATH"; export PATH
122         cd / && tar -hzcf "${binary_gzip_kit}" .$prefix .$pkgdbdir .$etc_mk_conf
123 }
124
125 die()
126 {
127         echo >&2 "$@"
128         exit 1
129 }
130
131 echo_msg()
132 {
133         echo "===> $@"
134 }
135
136 # see if we're using gcc.  If so, set $compiler_is_gnu to '1'.
137 get_compiler()
138 {
139         testcc="${CC}"
140         # normally, we'd just use 'cc', but certain configure tools look
141         # for gcc specifically, so we have to see if that comes first
142         if [ -z "${testcc}" ]; then
143                 save_IFS="${IFS}"
144                 IFS=':'
145                 for dir in ${PATH}; do
146                         test -z "$dir" && dir=.
147                         if [ -x "$dir/gcc" ]; then
148                                 testcc="$dir/gcc"
149                                 break
150                         fi
151                 done
152                 IFS="${save_IFS}"
153         fi
154
155         cat >${wrkdir}/$$.c <<EOF
156 #ifdef __GNUC__
157 indeed
158 #endif
159 EOF
160         compiler_is_gnu=`${testcc:-cc} -E ${wrkdir}/$$.c 2>/dev/null | grep -c indeed`
161         rm -f ${wrkdir}/$$.c
162
163 }
164 get_abi()
165 {
166         abi_opsys=$@
167
168         if [ -n "$abi" ]; then
169                 die "ERROR: $abi_opsys has special ABI handling, --abi not supported (yet)."
170         fi
171
172         case "$abi_opsys" in
173         IRIX)
174                 if [ `uname -r` -ge 6 ]; then
175                 abi=`sed -e 's/.*\(abi=\)\([on]*[36][24]\).*/\2/' /etc/compiler.defaults`
176                 isa=`sed -e 's/.*\(isa=mips\)\([1234]\).*/\2/' /etc/compiler.defaults`
177                 case "$abi" in
178                 o32)
179                         imakeopts="-DBuildO32 -DSgiISAo32=$isa"
180                         abi=""
181                         ;;
182                 n32)    imakeopts="-DBuildN32 -DSgiISA32=$isa"
183                         abi="32"
184                         ;;
185                 64 | n64)
186                         imakeopts="-DBuild64bit -DSgiISA64=$isa"
187                         abi="64"
188                         ;;
189                 esac
190                 else # IRIX before 6
191                 abi=32
192                 fi
193                 ;;
194         esac
195 }
196
197 get_machine_arch_aix()
198 {
199         _cpuid=`/usr/sbin/lsdev -C -c processor -S available | sed 1q | awk '{ print $1 }'`
200         if /usr/sbin/lsattr -El $_cpuid | grep ' POWER' >/dev/null 2>&1; then
201                 echo rs6000
202         else
203                 echo powerpc
204         fi
205 }
206
207 check_prog()
208 {
209         _var="$1"; _name="$2"
210
211         eval _tmp=\"\$$_var\"
212         if [ "x$_tmp" != "x" ]; then
213                 # Variable is already set (by the user, for example)
214                 return 0
215         fi
216
217         for _d in `echo $PATH | tr ':' ' '`; do
218                 if [ -f "$_d/$_name" ] && [ -x "$_d/$_name" ]; then
219                         # Program found
220                         eval $_var=\""$_d/$_name"\"
221                         return 1
222                 fi
223         done
224
225         die "$_name not found in path."
226 }
227
228 opsys_finish()
229 {
230         case "$opsys" in
231         IRIX)
232                 if [ ! -z "$imakeopts" ]; then
233                         echo "IMAKEOPTS+=               $imakeopts" >> ${TARGET_MKCONF}
234                 fi
235                 if [ `uname -r` -lt 6 ]; then
236                         echo_msg "Installing fake ldd script"
237                         run_cmd "$install_sh -c -o $user -g $group -m 755 $pkgsrcdir/pkgtools/bootstrap-extras/files/fakeldd $prefix/sbin"
238                         need_extras=yes
239                         echo "LDD=                      $prefix/sbin/fakeldd" >> ${TARGET_MKCONF}
240                 fi
241                 ;;
242         esac
243 }
244
245 is_root()
246 {
247         if [ `uname -s` = "IRIX" ]; then
248                 if [ `uname -r` -lt 6  -a -z "$ID" ]; then
249         # older version of IRIX have an id command with limited features
250                         if [ "`$idprog`" != "uid=0(root) gid=0(sys)" ]; then
251                                 return 0
252                         fi
253                         return 1
254                 fi
255         fi
256         if [ `$idprog -u` != 0 ]; then
257                 return 0
258         fi
259         return 1
260 }
261
262 # run a command, abort if it fails
263 run_cmd()
264 {
265         echo_msg "running: $@"
266         eval "$@"
267         ret=$?
268         if [ $ret -ne 0 ]; then
269                 echo_msg "exited with status $ret"
270                 die "aborted."
271         fi
272 }
273
274 # Some versions of mkdir (notably SunOS) bail out too easily, so use the
275 # install-sh wrapper instead.
276 mkdir_p()
277 {
278         for dir in $@; do
279                 run_cmd "$install_sh -d -o $user -g $group $dir"
280         done
281 }
282
283 mkdir_p_early()
284 {
285         [ -d "$1" ] && return 0 
286         mkdir -p "$1" 2> /dev/null && return 0
287         parent=`dirname "$1"`
288         mkdir_p_early "$parent"
289         if [ ! -d "$1" ] && mkdir "$1"; then
290                 echo_msg "mkdir $1 exited with status $?"
291                 die "aborted."
292         fi
293         return 0
294 }
295
296 copy_src()
297 {
298         _src="$1"; _dst="$2"
299         if [ ! -d $wrkdir/$_dst ]; then
300                 mkdir_p $wrkdir/$_dst
301         fi
302         $cpprog -r $_src/* $wrkdir/$_dst
303 }
304
305 get_optarg()
306 {
307         expr "x$1" : "x[^=]*=\\(.*\\)"
308 }
309
310 checkarg_sane_absolute_path() {
311         case "$1" in
312         "")     ;; # the default value will be used.
313         *[!-A-Za-z0-9_./]*)
314                 die "ERROR: Invalid characters in path $1 (from $2)." ;;
315         /*)     ;;
316         *)      die "ERROR: The argument to $2 must be an absolute path." ;;
317         esac
318 }
319
320 checkarg_sane_relative_path() {
321         case "$1" in
322         "")     ;; # the default value will be used.
323         *[!-A-Za-z0-9_./]*)
324                 die "ERROR: Invalid characters in path $1 (from $2)." ;;
325         /*)     die "ERROR: The argument to $2 must be a relative path." ;;
326         *)      ;;
327         esac
328 }
329
330 bootstrap_sh=${SH-/bin/sh}
331 bootstrap_sh_set=${SH+set}
332
333 # On some newer Ubuntu installations, /bin/sh is a symlink to /bin/dash,
334 # whose echo(1) is not BSD-compatible.
335 dash_echo_test=`$bootstrap_sh -c 'echo "\\100"'`
336 if [ "$dash_echo_test" = "@" ]; then
337         { echo "ERROR: Your shell's echo command is not BSD-compatible."
338           echo "ERROR: Please select another shell by setting the environment"
339           echo "ERROR: variable SH."
340         } 1>&2
341         exit 1;
342 fi
343
344 if [ -n "$PKG_PATH" ]; then
345         echo "ERROR: Please unset PKG_PATH before running bootstrap." 1>&2
346         exit 1;
347 fi
348
349 build_start=`date`
350 echo_msg "bootstrap command: $0 $@"
351 echo_msg "bootstrap started: $build_start"
352
353 # ensure system locations are empty; we will set them later when we know
354 # whether they will be system wide or user specific
355 prefix=
356 pkgdbdir=
357 pkgmandir=
358 sysconfdir=
359 varbase=
360
361 full=no
362 compiler=""
363 quiet=no
364 mk_fragment=
365
366 while [ $# -gt 0 ]; do
367         case $1 in
368         --workdir=*)    wrkdir=`get_optarg "$1"` ;;
369         --workdir)      wrkdir="$2"; shift ;;
370         --prefix=*)     prefix=`get_optarg "$1"` ;;
371         --prefix)       prefix="$2"; shift ;;
372         --pkgdbdir=*)   pkgdbdir=`get_optarg "$1"` ;;
373         --pkgdbdir)     pkgdbdir="$2"; shift ;;
374         --pkgmandir=*)  pkgmandir=`get_optarg "$1"` ;;
375         --pkgmandir)    pkgmandir="$2"; shift ;;
376         --sysconfdir=*) sysconfdir=`get_optarg "$1"` ;;
377         --sysconfdir)   sysconfdir="$2"; shift ;;
378         --varbase=*)    varbase=`get_optarg "$1"` ;;
379         --varbase)      varbase="$2"; shift ;;
380         --fetch-cmd=*)  fetch_cmd=`get_optarg "$1"` ;;
381         --fetch-cmd)    fetch_cmd="$2"; shift ;;
382         --compiler=*)   compiler=`get_optarg "$1"` ;;
383         --compiler)     compiler="$2"; shift ;;
384         --abi=*)        abi=`get_optarg "$1"` ;;
385         --abi)          abi="$2"; shift ;;
386         --ignore-case-check) ignorecasecheck=yes ;;
387         --unprivileged | --ignore-user-check) unprivileged=yes ;;
388         --preserve-path) preserve_path=yes ;;
389         --mk-fragment=*)
390                         mk_fragment=`get_optarg "$1"` ;;
391         --mk-fragment)
392                         mk_fragment="$2"; shift ;;
393
394         --binary-kit=*)
395                         binary_kit=`get_optarg "$1"` ;;
396         --binary-kit)
397                         binary_kit="$2"; shift ;;
398         --gzip-binary-kit=*)
399                         binary_gzip_kit=`get_optarg "$1"` ;;
400         --gzip-binary-kit)
401                         binary_gzip_kit="$2"; shift ;;
402         --binary-macpkg=*)
403                         binary_macpkg=`get_optarg "$1"` ;;
404         --binary-macpkg)
405                         binary_macpkg="$2"; shift ;;
406         --full)         full=yes ;;
407         --quiet)        quiet=yes ;;
408         --help)         echo "$usage"; exit ;;
409         -h)             echo "$usage"; exit ;;
410         --*)            echo "$usage"; exit 1 ;;
411         esac
412         shift
413 done
414
415 checkarg_sane_absolute_path "$wrkdir" "--workdir"
416 checkarg_sane_absolute_path "$prefix" "--prefix"
417 checkarg_sane_absolute_path "$pkgdbdir" "--pkgdbdir"
418 checkarg_sane_absolute_path "$sysconfdir" "--sysconfdir"
419 checkarg_sane_absolute_path "$varbase" "--varbase"
420 checkarg_sane_relative_path "$pkgmandir" "--pkgmandir"
421
422 # set defaults for system locations if not already set by the user
423 wrkobjdir=${wrkdir}/pkgsrc
424 if [ "$unprivileged" = "yes" ]; then
425         [ -z "$prefix" ] && prefix=${HOME}/pkg
426 elif [ -z "$prefix" -o "$prefix" = "/usr/pkg" ]; then
427         prefix=/usr/pkg
428         [ -z "$varbase" ] && varbase=/var
429 fi
430
431 [ -z "$varbase" ] && varbase=${prefix}/var
432 [ -z "$pkgdbdir" ] && pkgdbdir=${varbase}/db/pkg
433
434 if [ "$prefix" = "/usr" ]; then
435         [ -z "$pkgmandir" ] && pkgmandir=share/man
436 else
437         [ -z "$pkgmandir" ] && pkgmandir=man
438 fi
439 mandir=${prefix}/${pkgmandir}
440 [ -z "$sysconfdir" ] && sysconfdir=${prefix}/etc
441
442 if [ "x$preserve_path" != "xyes" ]; then
443         PATH="$PATH:/sbin:/usr/sbin"
444 fi
445
446 overpath=""
447 root_user=root
448 bmakexenv=
449 bmakexargs=
450 need_extras=no
451 case "$opsys" in
452 Darwin)
453         root_group=wheel
454         need_bsd_install=no
455         need_awk=no
456         need_sed=no
457         set_opsys=no
458         [ -z "$fetch_cmd" ] && fetch_cmd="/usr/bin/ftp"
459         machine_arch=`uname -p`
460         CC="gcc -isystem /usr/include"; export CC
461         osrev=`uname -r`
462         macosx_version=`echo $osrev | awk -F . '{ print "10."$1-4; }'`
463         case "$macosx_version" in
464         10.[0-4])
465                 packagemaker=/Developer/Tools/packagemaker
466                 ;;
467         *)
468                 packagemaker=/Developer/usr/bin/packagemaker
469                 ;;
470         esac
471         unset osrev macosx_version
472         ;;
473 DragonFly)
474         root_group=wheel
475         need_bsd_install=no
476         need_awk=no
477         need_sed=no
478         set_opsys=no
479         check_prog tarprog tar
480         machine_arch=`uname -p`
481         case `uname -r` in
482         1.1[0-9]*)
483                 [ -z "$fetch_cmd" ] && fetch_cmd="/usr/bin/ftp"
484                 ;;
485         1.0* | 1.1 | 1.2.* | 1.3.*)
486                 ;;
487         *)
488                 [ -z "$fetch_cmd" ] && fetch_cmd="/usr/bin/ftp"
489                 ;;
490         esac
491         ;;
492 FreeBSD)
493         root_group=wheel
494         need_bsd_install=no
495         need_awk=no
496         need_sed=no
497         set_opsys=no
498         machine_arch=`uname -p`
499         ;;
500 HPUX)
501         root_group=sys
502         need_bsd_install=yes
503         need_awk=yes
504         need_sed=yes
505         set_opsys=no
506         machine_arch=`uname -m | sed 's/^9000.*$/hppa/'`
507         ;;
508 IRIX*)
509         if [ -d "/usr/freeware/bin" ]; then
510                 overpath="/usr/freeware/bin:$overpath"
511         fi
512         if [ -d "/usr/bsd/bin" ]; then
513                 overpath="/usr/bsd/bin:$overpath"
514         fi
515         root_group=sys
516         need_bsd_install=yes
517         get_abi "IRIX"
518         opsys=IRIX
519         need_awk=yes
520         need_sed=yes
521         set_opsys=yes
522         machine_arch=mipseb
523         bmakexargs="MACHINE_ARCH=$machine_arch"
524         bmakexenv="MAKE=pmake"
525         check_compiler=yes
526         if [ `uname -r` -lt 6 ]; then
527 # IRIX 5's mkdir bails out with an error when trying to create with the -p
528 # option an already existing directory
529                 need_mkdir=yes
530         fi
531         ;;
532 Linux)
533         if [ -f /etc/debian_version ]; then
534                 DEBIAN=yes
535         fi
536         root_group=root
537         need_bsd_install=no
538         need_awk=no
539         need_sed=no
540         set_opsys=no
541         machine_arch=`uname -m | sed -e 's/i.86/i386/'`
542         ;;
543 NetBSD)
544         root_group=wheel
545         need_bsd_install=no
546         need_awk=no
547         need_sed=no
548         set_opsys=no
549         machine_arch=`uname -p`
550         ;;
551 OpenBSD)
552         root_group=wheel
553         need_bsd_install=no
554         need_awk=no
555         need_sed=no
556         set_opsys=no
557         machine_arch=`uname -m`
558         ;;
559 SunOS)
560         if [ -d "/usr/xpg4/bin" ]; then
561                 overpath="/usr/xpg4/bin:$overpath"
562         fi
563         root_group=root
564         need_bsd_install=yes
565         need_awk=yes
566         need_sed=yes
567         need_ksh=yes
568         set_opsys=no
569         groupsprog="/usr/xpg4/bin/id -gn"
570         whoamiprog="/usr/xpg4/bin/id -un"
571         machine_arch=`uname -p | sed -e 's/i86pc/i386/'`
572         check_compiler=yes
573         ;;
574 AIX)
575         root_group=system
576         need_bsd_install=yes
577         need_awk=yes
578         need_sed=yes
579         need_fixed_strip=yes
580         set_opsys=no
581         machine_arch=`get_machine_arch_aix`
582         ;;
583 Interix)
584         is_root () {
585                 if id -G | grep -q 131616; then
586                         return 1
587                 fi
588                 return 0
589         }
590         mkdir_p () {
591                 mkdir -p "$@" # allows umask to take effect
592         }
593         default_install_mode=0775
594         root_user=`id -u`
595         root_group=131616
596         need_bsd_install=yes
597         need_awk=yes
598         need_sed=yes
599         set_opsys=no
600         need_xargs=yes
601         # only used for unprivileged builds
602         groupsprog="id -gn"
603         # for bootstrap only; pkgsrc uses CPPFLAGS
604         CC="gcc -D_ALL_SOURCE"; export CC
605         ac_cv_header_poll_h=no; export ac_cv_header_poll_h
606         ac_cv_func_poll=no; export ac_cv_func_poll
607         ;;
608 UnixWare)
609         root_group=sys
610         need_bsd_install=no
611         BSTRAP_ENV="INSTALL=/usr/ucb/install $BSTRAP_ENV"
612         need_mkdir=yes
613         need_awk=yes
614         need_sed=yes
615         whoamiprog=/usr/ucb/whoami
616         set_opsys=no
617         CC="gcc -DUNIXWARE"; export CC
618         ;;
619 OSF1)
620         root_group=system
621         need_bsd_install=yes
622         need_awk=yes
623         need_sed=yes
624         need_ksh=yes
625         set_opsys=no
626         ;;
627 QNX)
628         root_group=root
629         need_bsd_install=yes
630         set_opsys=no
631         groupsprog="id -gn"
632         whoamiprog="id -un"
633         fetch_cmd="/usr/bin/ftp"
634         machine_arch=`uname -p | sed -e 's/x86/i386/'`
635         ;;
636 *)
637         echo "This platform ($opsys) is untried - good luck, and thanks for using pkgsrc"
638         root_group=wheel
639         need_bsd_install=yes
640         need_awk=yes
641         need_sed=yes
642         set_opsys=no
643         ;;
644 esac
645
646 # If "--full" is specified, then install all of the platform-independent
647 # bootstrap software.
648 #
649 case "$full" in
650 yes)
651         need_bsd_install=yes
652         need_awk=yes
653         need_sed=yes
654         need_ksh=yes
655         ;;
656 esac
657
658 case "$quiet" in
659 yes)
660         configure_quiet_flags="--quiet"
661         make_quiet_flags="-s"
662         ;;
663 no)
664         configure_quiet_flags=""
665         make_quiet_flags=""
666 esac
667
668 # export OPSYS and MACHINE_ARCH for pkg_install. we only set
669 # MACHINE_ARCH on platforms where we override bmake's value.
670 OPSYS=${opsys}
671 export OPSYS
672 if [ "${machine_arch}" != "" ]; then
673         MACHINE_ARCH=${machine_arch}
674         export MACHINE_ARCH
675 fi
676
677 if [ "x$preserve_path" != "xyes" ]; then
678         PATH="$overpath:$PATH"
679 fi
680
681 check_prog awkprog awk
682 check_prog chmodprog chmod
683 if [ -n "$CP" ]; then
684         cpprog="$CP"
685 else
686         check_prog cpprog cp
687 fi
688 if [ -n "$ID" ]; then
689         idprog="$ID"
690 else
691         check_prog idprog id
692 fi
693 check_prog groupsprog groups
694 check_prog lnprog ln
695 check_prog lsprog ls
696 check_prog rmdirprog rmdir
697 check_prog sedprog sed
698 check_prog shprog sh
699 check_prog whoamiprog whoami
700
701 if [ -d "${wrkdir}" ] || [ -f "${wrkdir}" ]; then
702         echo "\"${wrkdir}\" already exists, please remove it or use --workdir.";
703         exit 1
704 fi
705
706 mkdir_p_early ${wrkdir}
707 if touch ${wrkdir}/.writeable; then
708         :
709 else
710         echo "\"${wrkdir}\" is not writeable. Try $0 -h.";
711         exit 1
712 fi
713 echo "Working directory is: ${wrkdir}"
714
715 if [ "$compiler" = "" ] && [ x"$check_compiler" = x"yes" ]; then
716         get_compiler
717         if [ $compiler_is_gnu -gt 0 ]; then
718                 compiler="gcc"
719         else
720                 case "$opsys" in
721                 IRIX)
722                         if [ `uname -r` -ge 6 ]; then
723                                 compiler="mipspro"
724                         else
725                                 compiler="ido"
726                         fi
727                         test -n "$CC" || CC=cc
728                         ;;
729                 SunOS)  compiler="sunpro"
730                         test -n "$CC" || CC=cc
731                         ;;
732                 esac
733         fi
734 fi
735
736 mkdir_p_early ${wrkdir}/bin
737
738 # build install-sh
739 run_cmd "$sedprog -e 's|@DEFAULT_INSTALL_MODE@|'${default_install_mode-0755}'|' $pkgsrcdir/sysutils/install-sh/files/install-sh.in > $wrkdir/bin/install-sh"
740 run_cmd "$chmodprog +x $wrkdir/bin/install-sh"
741 install_sh="$shprog $wrkdir/bin/install-sh"
742
743 is_root
744 if [ $? = 1 ]; then
745         user=$root_user
746         group=$root_group
747 else
748         if [ $unprivileged = "no" ]; then
749                 die "You must be either root to install bootstrap-pkgsrc or use the --unprivileged option."
750         fi
751
752         user=`$whoamiprog`
753         group=`$groupsprog | $awkprog '{print $1}'`
754         echo_msg "building as unprivileged user $user/$group"
755
756         # force bmake install target to use $user and $group
757         echo "BINOWN=$user
758 BINGRP=$group
759 LIBOWN=$user
760 LIBGRP=$group
761 MANOWN=$user
762 MANGRP=$group" > ${wrkdir}/Makefile.inc
763 fi
764
765 # make sure we're using a case-sensitive file system
766 if [ $ignorecasecheck = "no" ]; then
767 case "$opsys" in
768 Interix)
769         echo_msg "Testing file system case sensitivity"
770         for fs in "$prefix"; do
771                 testdir="pkgsrc-REQUIRES-case-SENSITIVE-filesystem"
772                 testdir_mangled="PKGSRC-requires-CASE-sensitive-FILESYSTEM"
773                 mkdir_p "$fs/$testdir" || die "can't verify filesystem ($fs) case-sensitivity"
774                 if [ -d "$fs/$testdir_mangled" ]; then
775                         $rmdirprog "$fs/$testdir"
776                         die "\"$fs\" needs to be on a case-sensitive filesystem (see README.$opsys)"
777                 fi
778                 $rmdirprog "$fs/$testdir"
779         done
780         ;;
781 esac
782 fi
783
784 # export the proper environment
785 PATH=$prefix/bin:$prefix/sbin:${PATH}; export PATH
786 if [ -d /usr/ccs/bin -a -x /usr/ccs/bin/make ]; then
787         PATH=${PATH}:/usr/ccs/bin; export PATH
788 fi
789 PKG_DBDIR=$pkgdbdir; export PKG_DBDIR
790 LOCALBASE=$prefix; export LOCALBASE
791 VARBASE=$varbase; export VARBASE
792
793 # set up an example mk.conf file
794 TARGET_MKCONF=${wrkdir}/mk.conf.example
795 echo_msg "Creating default mk.conf. in ${wrkdir}"
796 echo "# Example ${sysconfdir}/mk.conf file produced by bootstrap-pkgsrc" > ${TARGET_MKCONF}
797 echo "# `date`" >> ${TARGET_MKCONF}
798 echo "" >> ${TARGET_MKCONF}
799 echo ".ifdef BSD_PKG_MK # begin pkgsrc settings" >> ${TARGET_MKCONF}
800 echo "" >> ${TARGET_MKCONF}
801
802 # IRIX64 needs to be set to IRIX, for example
803 if [ "$set_opsys" = "yes" ]; then
804         echo "OPSYS=                    $opsys" >> ${TARGET_MKCONF}
805 fi
806
807 if [ ! -z "$abi" ]; then
808         echo "ABI=                      $abi" >> ${TARGET_MKCONF}
809 fi
810 if [ "$compiler" != "" ]; then
811         echo "PKGSRC_COMPILER=  $compiler" >> ${TARGET_MKCONF}
812 fi
813 case "$compiler" in
814 sunpro)
815         echo "CC=                       cc"        >> ${TARGET_MKCONF}
816         echo "CXX=                      CC"        >> ${TARGET_MKCONF}
817         echo "CPP=                      \${CC} -E" >> ${TARGET_MKCONF}
818         ;;
819 esac
820 if [ -n "$SUNWSPROBASE" ]; then
821         echo "SUNWSPROBASE=             $SUNWSPROBASE" >> ${TARGET_MKCONF}
822 fi
823 echo "" >> ${TARGET_MKCONF}
824
825 # enable unprivileged builds if not root
826 if [ "$unprivileged" = "yes" ]; then
827         echo "UNPRIVILEGED=             yes" >> ${TARGET_MKCONF}
828 fi
829
830 # save environment in example mk.conf
831 echo "PKG_DBDIR=                $pkgdbdir" >> ${TARGET_MKCONF}
832 echo "LOCALBASE=                $prefix" >> ${TARGET_MKCONF}
833 echo "VARBASE=          $varbase" >> ${TARGET_MKCONF}
834 if [ "${sysconfdir}" != "${prefix}/etc" ]; then
835         echo "PKG_SYSCONFBASE=  $sysconfdir" >> ${TARGET_MKCONF}
836 fi
837 echo "PKG_TOOLS_BIN=            $prefix/sbin" >> ${TARGET_MKCONF}
838 echo "PKGMANDIR=                $pkgmandir" >> ${TARGET_MKCONF}
839 echo "" >> ${TARGET_MKCONF}
840
841 BOOTSTRAP_MKCONF=${wrkdir}/mk.conf
842 cp ${TARGET_MKCONF} ${BOOTSTRAP_MKCONF}
843
844 # sbin is used by pkg_install, share/mk by bootstrap-mk-files
845 mkdir_p $wrkdir/sbin $wrkdir/share/mk
846 mkdir_p_early ${wrkdir}
847
848 if [ "$need_bsd_install" = "yes" ]; then
849         BSTRAP_ENV="INSTALL='$prefix/bin/install-sh -c' $BSTRAP_ENV"
850         echo "TOOLS_PLATFORM.install?=  $prefix/bin/install-sh" >> ${TARGET_MKCONF}
851         echo "TOOLS_PLATFORM.install?=  $wrkdir/bin/install-sh" >> ${BOOTSTRAP_MKCONF}
852 fi
853
854 if [ "$need_fixed_strip" = "yes" ] ; then
855         echo_msg "Installing fixed strip script"
856         run_cmd "$install_sh -c -o $user -g $group -m 755 $pkgsrcdir/pkgtools/bootstrap-extras/files/strip-sh $wrkdir/bin/strip"
857         echo "TOOLS_PLATFORM.strip?=            $prefix/bin/strip" >> ${TARGET_MKCONF}
858         echo "TOOLS_PLATFORM.strip?=            $wrkdir/bin/strip" >> ${BOOTSTRAP_MKCONF}
859         need_extras=yes
860 fi
861
862 if [ "$need_mkdir" = "yes" -a -z "$MKDIR" ]; then
863         echo_msg "Installing fixed mkdir script \"mkdir-sh\""
864         run_cmd "$install_sh -c -o $user -g $group -m 755 $pkgsrcdir/pkgtools/bootstrap-extras/files/mkdir-sh $wrkdir/bin/mkdir-sh"
865         echo "TOOLS_PLATFORM.mkdir?=            $prefix/bin/mkdir-sh -p" >> ${TARGET_MKCONF}
866         echo "TOOLS_PLATFORM.mkdir?=            $wrkdir/bin/mkdir-sh -p" >> ${BOOTSTRAP_MKCONF}
867         need_extras=yes
868 fi
869
870 if [ "$need_xargs" = "yes" ]; then
871         echo_msg "Installing fixed xargs script"
872         run_cmd "$install_sh -c -o $user -g $group -m 755 $pkgsrcdir/pkgtools/bootstrap-extras/files/xargs-sh $wrkdir/bin/xargs"
873         echo "TOOLS_PLATFORM.xargs?=            $prefix/bin/xargs" >> ${TARGET_MKCONF}
874         echo "TOOLS_PLATFORM.xargs?=            $wrkdir/bin/xargs" >> ${BOOTSTRAP_MKCONF}
875         need_extras=yes
876 fi
877
878 echo_msg "Bootstrapping mk-files"
879 run_cmd "(cd ${pkgsrcdir}/pkgtools/bootstrap-mk-files/files && env CP=${cpprog} \
880  OPSYS=${opsys} MK_DST=${wrkdir}/share/mk ROOT_GROUP=${root_group} \
881 ROOT_USER=${root_user} SED=${sedprog} SYSCONFDIR=${sysconfdir} \
882 $shprog ./bootstrap.sh)"
883
884 bootstrap_bmake() {
885         echo_msg "Bootstrapping bmake"
886         copy_src $pkgsrcdir/devel/bmake/files bmake
887         run_cmd "(cd $wrkdir/bmake && env $bmakexenv $shprog ./boot-strap $configure_quiet_flags -q -o $opsys --prefix=$wrkdir --sysconfdir=$wrkdir --mksrc none --with-default-sys-path="$wrkdir/share/mk" $bmakexargs)"
888         run_cmd "$install_sh -c -o $user -g $group -m 755 $wrkdir/bmake/$opsys/bmake $wrkdir/bin/bmake"
889 }
890 bootstrap_bmake
891
892 bmake="$wrkdir/bin/bmake $make_quiet_flags"
893
894 # build libnbcompat
895 echo_msg "Building libnbcompat"
896 copy_src $pkgsrcdir/pkgtools/libnbcompat/files libnbcompat
897 run_cmd "(cd $wrkdir/libnbcompat; $shprog ./configure $configure_quiet_flags -C --prefix=$prefix --mandir=$mandir --sysconfdir=$sysconfdir --enable-bsd-getopt --enable-db && $bmake $make_quiet_flags)"
898
899 # bootstrap ksh if necessary
900 case "$need_ksh" in
901 yes)    echo_msg "Bootstrapping ksh"
902         copy_src $pkgsrcdir/shells/pdksh/files ksh
903         test -n "$CC" || CC=gcc # default to gcc if no compiler is specified
904         run_cmd "(cd $wrkdir/ksh && env $BSTRAP_ENV $shprog ./configure $configure_quiet_flags --prefix=$prefix --mandir=$mandir --sysconfdir=$sysconfdir && $bmake)"
905         run_cmd "$install_sh -c -o $user -g $group -m 755 $wrkdir/ksh/ksh $wrkdir/bin/pdksh"
906         echo "TOOLS_PLATFORM.sh?=               $prefix/bin/pdksh" >> ${TARGET_MKCONF}
907         echo "TOOLS_PLATFORM.sh?=               $wrkdir/bin/pdksh" >> ${BOOTSTRAP_MKCONF}
908         echo "TOOLS_PLATFORM.ksh?=              $prefix/bin/pdksh" >> ${TARGET_MKCONF}
909         echo "TOOLS_PLATFORM.ksh?=              $wrkdir/bin/pdksh" >> ${BOOTSTRAP_MKCONF}
910 # Now rebootstrap bmake for ksh
911         echo_msg "Rebootstrapping bmake for ksh"
912         bmakexargs="$bmakexargs --with-defshell=$wrkdir/bin/pdksh"
913         bootstrap_bmake
914         ;;
915 esac
916
917 # bootstrap awk if necessary
918 case "$need_awk" in
919 yes)    echo_msg "Bootstrapping awk"
920         copy_src $pkgsrcdir/lang/nawk/files awk
921         test -n "$CC" || CC=gcc # default to gcc if no compiler is specified
922         run_cmd "(cd $wrkdir/awk && $bmake -f Makefile CC=\"${CC}\" CFLAGS=\"${CFLAGS}\")"
923         run_cmd "$install_sh -c -o $user -g $group -m 755 $wrkdir/awk/a.out $wrkdir/bin/nawk"
924         echo "TOOLS_PLATFORM.awk?=              $prefix/bin/nawk" >> ${TARGET_MKCONF}
925         echo "TOOLS_PLATFORM.awk?=              $wrkdir/bin/nawk" >> ${BOOTSTRAP_MKCONF}
926         ;;
927 esac
928
929 # bootstrap sed if necessary
930 case "$need_sed" in
931 yes)    echo_msg "Bootstrapping sed"
932         copy_src $pkgsrcdir/textproc/nbsed/files sed
933         run_cmd "(cd $wrkdir/sed; env $BSTRAP_ENV CPPFLAGS='$CPPFLAGS -I../libnbcompat' LDFLAGS='$LDFLAGS -L../libnbcompat' LIBS='-lnbcompat' $shprog ./configure $configure_quiet_flags -C --prefix=$prefix --mandir=$mandir --sysconfdir=$sysconfdir --program-transform-name='s,sed,nbsed,' && $bmake)"
934         run_cmd "$install_sh -c -o $user -g $group -m 755 $wrkdir/sed/sed $wrkdir/bin/sed"
935         echo "TOOLS_PLATFORM.sed?=              $prefix/bin/nbsed" >> ${TARGET_MKCONF}
936         echo "TOOLS_PLATFORM.sed?=              $wrkdir/bin/sed" >> ${BOOTSTRAP_MKCONF}
937         ;;
938 esac
939
940 # bootstrap pkg_install
941 echo_msg "Bootstrapping pkgtools"
942 copy_src $pkgsrcdir/pkgtools/pkg_install/files pkg_install
943 run_cmd "(cd $wrkdir/pkg_install; env $BSTRAP_ENV \
944 CPPFLAGS='$CPPFLAGS -I../libnbcompat -I../../libnbcompat' \
945 LDFLAGS='$LDFLAGS -L../libnbcompat -L../../libnbcompat' \
946 LIBS='-lnbcompat' $shprog ./configure $configure_quiet_flags -C \
947 --enable-bootstrap --prefix=$prefix --sysconfdir=$sysconfdir \
948 --with-pkgdbdir=$pkgdbdir --mandir=$mandir $pkg_install_args && $bmake)"
949 run_cmd "$install_sh -c -o $user -g $group -m 755 $wrkdir/pkg_install/admin/pkg_admin $wrkdir/sbin/pkg_admin"
950 run_cmd "$install_sh -c -o $user -g $group -m 755 $wrkdir/pkg_install/create/pkg_create $wrkdir/sbin/pkg_create"
951 run_cmd "$install_sh -c -o $user -g $group -m 755 $wrkdir/pkg_install/info/pkg_info $wrkdir/sbin/pkg_info"
952 echo "PKG_ADMIN_CMD?=                   $wrkdir/sbin/pkg_admin" >> ${BOOTSTRAP_MKCONF}
953 echo "PKG_CREATE_CMD?=                  $wrkdir/sbin/pkg_create" >> ${BOOTSTRAP_MKCONF}
954 echo "PKG_INFO_CMD?=                    $wrkdir/sbin/pkg_info" >> ${BOOTSTRAP_MKCONF}
955
956 if [ -n "${fetch_cmd}" ]; then
957         echo "FETCH_CMD=                $fetch_cmd" >> ${TARGET_MKCONF}
958 fi
959
960 MAKECONF=$wrkdir/mk.conf
961 export MAKECONF
962
963 if [ "$bootstrap_sh_set" = "set" ]; then
964         echo "TOOLS_PLATFORM.sh?=               ${bootstrap_sh}" >> ${TARGET_MKCONF}
965         echo "TOOLS_PLATFORM.sh?=               ${bootstrap_sh}" >> ${BOOTSTRAP_MKCONF}
966 fi
967
968 # preserve compiler and tool environment variables settings
969 if test -n "$CP"; then
970         echo "TOOLS_PLATFORM.cp?=               $CP" >> ${TARGET_MKCONF}
971         echo "TOOLS_PLATFORM.cp?=               $CP" >> ${BOOTSTRAP_MKCONF}
972 fi
973 if test -n "$GREP"; then
974         echo "TOOLS_PLATFORM.grep?=             $GREP" >> ${TARGET_MKCONF}
975         echo "TOOLS_PLATFORM.grep?=             $GREP" >> ${BOOTSTRAP_MKCONF}
976 fi
977 if test -n "$ID"; then
978         echo "TOOLS_PLATFORM.id?=               $ID" >> ${TARGET_MKCONF}
979         echo "TOOLS_PLATFORM.id?=               $ID" >> ${BOOTSTRAP_MKCONF}
980 fi
981 if test -n "$MKDIR"; then
982         echo "TOOLS_PLATFORM.mkdir?=            $MKDIR" >> ${TARGET_MKCONF}
983         echo "TOOLS_PLATFORM.mkdir?=            $MKDIR" >> ${BOOTSTRAP_MKCONF}
984 fi
985 if test -n "$TEST"; then
986         echo "TOOLS_PLATFORM.test?=             $TEST" >> ${TARGET_MKCONF}
987         echo "TOOLS_PLATFORM.test?=             $TEST" >> ${BOOTSTRAP_MKCONF}
988 fi
989 if test -n "$TOUCH"; then
990         echo "TOOLS_PLATFORM.touch?=            $TOUCH" >> ${TARGET_MKCONF}
991         echo "TOOLS_PLATFORM.touch?=            $TOUCH" >> ${BOOTSTRAP_MKCONF}
992 fi
993 if test -n "$XARGS"; then
994         echo "TOOLS_PLATFORM.xargs?=            $XARGS" >> ${TARGET_MKCONF}
995         echo "TOOLS_PLATFORM.xargs?=            $XARGS" >> ${BOOTSTRAP_MKCONF}
996 fi
997 if test -n "$CFLAGS"; then
998         echo "CFLAGS+=          $CFLAGS" >> ${TARGET_MKCONF}
999         echo "DBG=                      # prevent DBG from adding default optimizer flags" >> ${TARGET_MKCONF}
1000         echo "DBG=                      # prevent DBG from adding default optimizer flags" >> ${BOOTSTRAP_MKCONF}
1001 fi
1002 if test -n "$CPPFLAGS"; then
1003         echo "CPPFLAGS+=                $CPPFLAGS" >> ${TARGET_MKCONF}
1004 fi
1005 if test -n "$LDFLAGS"; then
1006         echo "LDFLAGS+=         $LDFLAGS" >> ${TARGET_MKCONF}
1007 fi
1008
1009 # opsys specific fiddling
1010 opsys_finish
1011
1012 echo "WRKOBJDIR=                ${wrkdir}/wrk" >> ${BOOTSTRAP_MKCONF}
1013
1014 echo "" >> ${TARGET_MKCONF}
1015 echo "" >> ${BOOTSTRAP_MKCONF}
1016 if test -n "${mk_fragment}"; then
1017         cat "${mk_fragment}" >> ${TARGET_MKCONF}
1018         echo "" >> ${TARGET_MKCONF}
1019 fi
1020 echo ".endif                    # end pkgsrc settings" >> ${TARGET_MKCONF}
1021 echo ".endif                    # end pkgsrc settings" >> ${BOOTSTRAP_MKCONF}
1022
1023 # register packages
1024 # usage: register_package <packagedirectory> [additional arguments]
1025 build_package() {
1026         run_cmd "(cd $pkgsrcdir/$1 && $bmake -DPKG_PRESERVE MAKECONF=${BOOTSTRAP_MKCONF} install)"
1027 }
1028
1029 #
1030 # Please make sure that the following packages and
1031 # only the following packages set BOOTSTRAP_PKG=yes.
1032 #
1033 echo_msg "Installing packages"
1034 build_package "pkgtools/bootstrap-mk-files"
1035 case "$need_bsd_install" in
1036 yes)    build_package "sysutils/install-sh";;
1037 esac
1038 case "$need_ksh" in
1039 yes)    build_package "shells/pdksh";;
1040 esac
1041 build_package "devel/bmake"
1042 case "$need_awk" in
1043 yes)    build_package "lang/nawk";;
1044 esac
1045 case "$need_sed" in
1046 yes)    build_package "textproc/nbsed";;
1047 esac
1048 case "$need_extras" in
1049 yes)    build_package "pkgtools/bootstrap-extras";;
1050 esac
1051 build_package "pkgtools/pkg_install"
1052
1053 etc_mk_conf="$sysconfdir/mk.conf"
1054
1055 # Install the example mk.conf so that it is used, but only if it doesn't
1056 # exist yet. This can happen with non-default sysconfdir settings.
1057 mkdir_p "$sysconfdir"
1058 if [ ! -f "$etc_mk_conf" ]; then
1059         cp "$TARGET_MKCONF" "$etc_mk_conf"
1060         TARGET_MKCONF="$etc_mk_conf"
1061 fi
1062
1063 hline="==========================================================================="
1064 echo ""
1065 echo "$hline"
1066 echo ""
1067 echo "Please remember to add $prefix/bin to your PATH environment variable"
1068 echo "and $mandir to your MANPATH environment variable, if necessary."
1069 echo ""
1070 echo "An example mk.conf file with the settings you provided to \"bootstrap\""
1071 echo "has been created for you. It can be found in:"
1072 echo ""
1073 echo "      ${TARGET_MKCONF}"
1074 echo ""
1075 if [ "$TARGET_MKCONF" != "$etc_mk_conf" ]; then
1076         echo "Please copy it to $etc_mk_conf to use it."
1077         echo ""
1078 fi
1079 echo "You can find extensive documentation of the NetBSD Packages Collection"
1080 echo "in $pkgsrcdir/doc/pkgsrc.txt."
1081 echo ""
1082 echo "Hopefully everything is now complete."
1083 echo "Thank you"
1084 echo ""
1085 echo "$hline"
1086 echo ""
1087
1088 [ ! -z "${binary_kit}" ] && mkbinarykit_tar
1089 [ ! -z "${binary_gzip_kit}" ] && mkbinarykit_tgz
1090 [ ! -z "${binary_macpkg}" ] && mkbinarykit_macpkg
1091
1092 echo_msg "bootstrap started: $build_start"
1093 echo_msg "bootstrap ended:   `date`"
1094
1095 exit 0