Merge from vendor branch TNF:
[pkgsrcv2.git] / bootstrap / bootstrap
1 #! /bin/sh
2
3 # $NetBSD: bootstrap,v 1.156 2010/02/06 10:28:54 obache 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, LIBS
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         Haiku)
243                 need_extras=yes
244                 echo "LDD=                      $prefix/sbin/fakeldd" >> ${TARGET_MKCONF}
245                 ;;
246         esac
247 }
248
249 is_root()
250 {
251         if [ `uname -s` = "IRIX" ]; then
252                 if [ `uname -r` -lt 6  -a -z "$ID" ]; then
253         # older version of IRIX have an id command with limited features
254                         if [ "`$idprog`" != "uid=0(root) gid=0(sys)" ]; then
255                                 return 0
256                         fi
257                         return 1
258                 fi
259         fi
260         if [ `$idprog -u` != 0 ]; then
261                 return 0
262         fi
263         return 1
264 }
265
266 # run a command, abort if it fails
267 run_cmd()
268 {
269         echo_msg "running: $@"
270         eval "$@"
271         ret=$?
272         if [ $ret -ne 0 ]; then
273                 echo_msg "exited with status $ret"
274                 die "aborted."
275         fi
276 }
277
278 # Some versions of mkdir (notably SunOS) bail out too easily, so use the
279 # install-sh wrapper instead.
280 mkdir_p()
281 {
282         for dir in $@; do
283                 run_cmd "$install_sh -d -o $user -g $group $dir"
284         done
285 }
286
287 mkdir_p_early()
288 {
289         [ -d "$1" ] && return 0 
290         mkdir -p "$1" 2> /dev/null && return 0
291         parent=`dirname "$1"`
292         mkdir_p_early "$parent"
293         if [ ! -d "$1" ] && mkdir "$1"; then
294                 echo_msg "mkdir $1 exited with status $?"
295                 die "aborted."
296         fi
297         return 0
298 }
299
300 copy_src()
301 {
302         _src="$1"; _dst="$2"
303         if [ ! -d $wrkdir/$_dst ]; then
304                 mkdir_p $wrkdir/$_dst
305         fi
306         $cpprog -r $_src/* $wrkdir/$_dst
307 }
308
309 get_optarg()
310 {
311         expr "x$1" : "x[^=]*=\\(.*\\)"
312 }
313
314 checkarg_sane_absolute_path() {
315         case "$1" in
316         "")     ;; # the default value will be used.
317         *[!-A-Za-z0-9_./]*)
318                 die "ERROR: Invalid characters in path $1 (from $2)." ;;
319         /*)     ;;
320         *)      die "ERROR: The argument to $2 must be an absolute path." ;;
321         esac
322 }
323
324 checkarg_sane_relative_path() {
325         case "$1" in
326         "")     ;; # the default value will be used.
327         *[!-A-Za-z0-9_./]*)
328                 die "ERROR: Invalid characters in path $1 (from $2)." ;;
329         /*)     die "ERROR: The argument to $2 must be a relative path." ;;
330         *)      ;;
331         esac
332 }
333
334 bootstrap_sh=${SH-/bin/sh}
335 bootstrap_sh_set=${SH+set}
336
337 # On some newer Ubuntu installations, /bin/sh is a symlink to /bin/dash,
338 # whose echo(1) is not BSD-compatible.
339 dash_echo_test=`$bootstrap_sh -c 'echo "\\100"'`
340 if [ "$dash_echo_test" = "@" ]; then
341         { echo "ERROR: Your shell's echo command is not BSD-compatible."
342           echo "ERROR: Please select another shell by setting the environment"
343           echo "ERROR: variable SH."
344         } 1>&2
345         exit 1;
346 fi
347
348 if [ -n "$PKG_PATH" ]; then
349         echo "ERROR: Please unset PKG_PATH before running bootstrap." 1>&2
350         exit 1;
351 fi
352
353 build_start=`date`
354 echo_msg "bootstrap command: $0 $@"
355 echo_msg "bootstrap started: $build_start"
356
357 # ensure system locations are empty; we will set them later when we know
358 # whether they will be system wide or user specific
359 prefix=
360 pkgdbdir=
361 pkgmandir=
362 sysconfdir=
363 varbase=
364
365 full=no
366 compiler=""
367 quiet=no
368 mk_fragment=
369
370 while [ $# -gt 0 ]; do
371         case $1 in
372         --workdir=*)    wrkdir=`get_optarg "$1"` ;;
373         --workdir)      wrkdir="$2"; shift ;;
374         --prefix=*)     prefix=`get_optarg "$1"` ;;
375         --prefix)       prefix="$2"; shift ;;
376         --pkgdbdir=*)   pkgdbdir=`get_optarg "$1"` ;;
377         --pkgdbdir)     pkgdbdir="$2"; shift ;;
378         --pkgmandir=*)  pkgmandir=`get_optarg "$1"` ;;
379         --pkgmandir)    pkgmandir="$2"; shift ;;
380         --sysconfdir=*) sysconfdir=`get_optarg "$1"` ;;
381         --sysconfdir)   sysconfdir="$2"; shift ;;
382         --varbase=*)    varbase=`get_optarg "$1"` ;;
383         --varbase)      varbase="$2"; shift ;;
384         --fetch-cmd=*)  fetch_cmd=`get_optarg "$1"` ;;
385         --fetch-cmd)    fetch_cmd="$2"; shift ;;
386         --compiler=*)   compiler=`get_optarg "$1"` ;;
387         --compiler)     compiler="$2"; shift ;;
388         --abi=*)        abi=`get_optarg "$1"` ;;
389         --abi)          abi="$2"; shift ;;
390         --ignore-case-check) ignorecasecheck=yes ;;
391         --unprivileged | --ignore-user-check) unprivileged=yes ;;
392         --preserve-path) preserve_path=yes ;;
393         --mk-fragment=*)
394                         mk_fragment=`get_optarg "$1"` ;;
395         --mk-fragment)
396                         mk_fragment="$2"; shift ;;
397
398         --binary-kit=*)
399                         binary_kit=`get_optarg "$1"` ;;
400         --binary-kit)
401                         binary_kit="$2"; shift ;;
402         --gzip-binary-kit=*)
403                         binary_gzip_kit=`get_optarg "$1"` ;;
404         --gzip-binary-kit)
405                         binary_gzip_kit="$2"; shift ;;
406         --binary-macpkg=*)
407                         binary_macpkg=`get_optarg "$1"` ;;
408         --binary-macpkg)
409                         binary_macpkg="$2"; shift ;;
410         --full)         full=yes ;;
411         --quiet)        quiet=yes ;;
412         --help)         echo "$usage"; exit ;;
413         -h)             echo "$usage"; exit ;;
414         --*)            echo "$usage"; exit 1 ;;
415         esac
416         shift
417 done
418
419 checkarg_sane_absolute_path "$wrkdir" "--workdir"
420 checkarg_sane_absolute_path "$prefix" "--prefix"
421 checkarg_sane_absolute_path "$pkgdbdir" "--pkgdbdir"
422 checkarg_sane_absolute_path "$sysconfdir" "--sysconfdir"
423 checkarg_sane_absolute_path "$varbase" "--varbase"
424 checkarg_sane_relative_path "$pkgmandir" "--pkgmandir"
425
426 # set defaults for system locations if not already set by the user
427 wrkobjdir=${wrkdir}/pkgsrc
428 if [ "$unprivileged" = "yes" ]; then
429         [ -z "$prefix" ] && prefix=${HOME}/pkg
430 elif [ -z "$prefix" -o "$prefix" = "/usr/pkg" ]; then
431         prefix=/usr/pkg
432         [ -z "$varbase" ] && varbase=/var
433 fi
434
435 [ -z "$varbase" ] && varbase=${prefix}/var
436 [ -z "$pkgdbdir" ] && pkgdbdir=${varbase}/db/pkg
437
438 if [ "$prefix" = "/usr" ]; then
439         [ -z "$pkgmandir" ] && pkgmandir=share/man
440 else
441         [ -z "$pkgmandir" ] && pkgmandir=man
442 fi
443 mandir=${prefix}/${pkgmandir}
444 [ -z "$sysconfdir" ] && sysconfdir=${prefix}/etc
445
446 if [ "x$preserve_path" != "xyes" ]; then
447         PATH="$PATH:/sbin:/usr/sbin"
448 fi
449
450 overpath=""
451 root_user=root
452 bmakexenv=
453 bmakexargs=
454 need_extras=no
455 case "$opsys" in
456 AIX)
457         root_group=system
458         need_bsd_install=yes
459         need_awk=yes
460         need_sed=yes
461         need_fixed_strip=yes
462         set_opsys=no
463         machine_arch=`get_machine_arch_aix`
464         ;;
465 Darwin)
466         root_group=wheel
467         need_bsd_install=no
468         need_awk=no
469         need_sed=no
470         set_opsys=no
471         [ -z "$fetch_cmd" ] && fetch_cmd="/usr/bin/ftp"
472         machine_arch=`uname -p`
473         CC="gcc -isystem /usr/include"; export CC
474         osrev=`uname -r`
475         macosx_version=`echo $osrev | awk -F . '{ print "10."$1-4; }'`
476         case "$macosx_version" in
477         10.[0-4])
478                 packagemaker=/Developer/Tools/packagemaker
479                 ;;
480         *)
481                 packagemaker=/Developer/usr/bin/packagemaker
482                 ;;
483         esac
484         unset osrev macosx_version
485         ;;
486 DragonFly)
487         root_group=wheel
488         need_bsd_install=no
489         need_awk=no
490         need_sed=no
491         set_opsys=no
492         check_prog tarprog tar
493         machine_arch=`uname -p`
494         case `uname -r` in
495         1.1[0-9]*)
496                 [ -z "$fetch_cmd" ] && fetch_cmd="/usr/bin/ftp"
497                 ;;
498         1.0* | 1.1 | 1.2.* | 1.3.*)
499                 ;;
500         *)
501                 [ -z "$fetch_cmd" ] && fetch_cmd="/usr/bin/ftp"
502                 ;;
503         esac
504         ;;
505 FreeBSD)
506         root_group=wheel
507         need_bsd_install=no
508         need_awk=no
509         need_sed=no
510         set_opsys=no
511         machine_arch=`uname -p`
512         ;;
513 Haiku)
514         root_user=user
515         root_group=root
516         need_bsd_install=no
517         need_awk=no
518         need_sed=no
519         set_opsys=no
520         case `uname -m` in
521         BeMac)
522                 machine_arch=powerpc
523                 ;;
524         BePC)
525                 machine_arch=i386
526                 ;;
527         *)
528                 machine_arch=`uname -p`
529                 ;;
530         esac
531         ;;
532 HPUX)
533         root_group=sys
534         need_bsd_install=yes
535         need_awk=yes
536         need_sed=yes
537         set_opsys=no
538         machine_arch=`uname -m | sed 's/^9000.*$/hppa/'`
539         ;;
540 Interix)
541         is_root () {
542                 if id -G | grep -q 131616; then
543                         return 1
544                 fi
545                 return 0
546         }
547         mkdir_p () {
548                 mkdir -p "$@" # allows umask to take effect
549         }
550         default_install_mode=0775
551         root_user=`id -u`
552         root_group=131616
553         case `uname -r` in
554         3.* | 5.*)
555                 need_bsd_install=yes
556                 need_awk=yes
557                 need_sed=yes
558                 set_opsys=no
559                 need_xargs=yes
560                 ;;
561         *)
562                 need_bsd_install=no
563                 need_awk=no
564                 need_sed=no
565                 set_opsys=no
566                 need_xargs=no
567                 ;;
568         esac
569         # only used for unprivileged builds
570         groupsprog="id -gn"
571         # for bootstrap only; pkgsrc uses CPPFLAGS
572         CC="gcc -D_ALL_SOURCE"; export CC
573         ac_cv_header_poll_h=no; export ac_cv_header_poll_h
574         ac_cv_func_poll=no; export ac_cv_func_poll
575         ;;
576 IRIX*)
577         if [ -d "/usr/freeware/bin" ]; then
578                 overpath="/usr/freeware/bin:$overpath"
579         fi
580         if [ -d "/usr/bsd" ]; then
581                 overpath="/usr/bsd:$overpath"
582         fi
583         if [ -d "/usr/bsd/bin" ]; then
584                 overpath="/usr/bsd/bin:$overpath"
585         fi
586         root_group=sys
587         need_bsd_install=yes
588         get_abi "IRIX"
589         opsys=IRIX
590         need_awk=yes
591         need_sed=yes
592         set_opsys=yes
593         machine_arch=mipseb
594         bmakexargs="MACHINE_ARCH=$machine_arch"
595         bmakexenv="MAKE=pmake"
596         check_compiler=yes
597         if [ `uname -r` -lt 6 ]; then
598 # IRIX 5's mkdir bails out with an error when trying to create with the -p
599 # option an already existing directory
600                 need_mkdir=yes
601         fi
602         ;;
603 Linux)
604         if [ -f /etc/ssdlinux_version ]; then
605                 root_group=wheel
606         else
607                 root_group=root
608         fi
609         need_bsd_install=no
610         need_awk=no
611         need_sed=no
612         set_opsys=no
613         machine_arch=`uname -m | sed -e 's/i.86/i386/'`
614         ;;
615 NetBSD)
616         root_group=wheel
617         need_bsd_install=no
618         need_awk=no
619         need_sed=no
620         set_opsys=no
621         machine_arch=`uname -p`
622         ;;
623 OpenBSD)
624         root_group=wheel
625         need_bsd_install=no
626         need_awk=no
627         need_sed=no
628         set_opsys=no
629         machine_arch=`uname -m`
630         ;;
631 OSF1)
632         root_group=system
633         need_bsd_install=yes
634         need_awk=yes
635         need_sed=yes
636         need_ksh=yes
637         set_opsys=no
638         ;;
639 QNX)
640         root_group=root
641         need_bsd_install=yes
642         need_awk=yes
643         need_sed=yes
644         set_opsys=no
645         groupsprog="id -gn"
646         whoamiprog="id -un"
647         fetch_cmd="/usr/bin/ftp"
648         machine_arch=`uname -p | sed -e 's/x86/i386/'`
649         ;;
650 SunOS)
651         if [ -d "/usr/xpg4/bin" ]; then
652                 overpath="/usr/xpg4/bin:$overpath"
653         fi
654         root_group=root
655         need_bsd_install=yes
656         need_awk=yes
657         need_sed=yes
658         need_ksh=yes
659         set_opsys=no
660         groupsprog="/usr/xpg4/bin/id -gn"
661         whoamiprog="/usr/xpg4/bin/id -un"
662         machine_arch=`uname -p | sed -e 's/i86pc/i386/'`
663         check_compiler=yes
664         ;;
665 UnixWare)
666         root_group=sys
667         need_bsd_install=no
668         BSTRAP_ENV="INSTALL=/usr/ucb/install $BSTRAP_ENV"
669         need_mkdir=yes
670         need_awk=yes
671         need_sed=yes
672         whoamiprog=/usr/ucb/whoami
673         set_opsys=no
674         CC="gcc -DUNIXWARE"; export CC
675         ;;
676 *)
677         echo "This platform ($opsys) is untried - good luck, and thanks for using pkgsrc"
678         root_group=wheel
679         need_bsd_install=yes
680         need_awk=yes
681         need_sed=yes
682         set_opsys=no
683         ;;
684 esac
685
686 # If "--full" is specified, then install all of the platform-independent
687 # bootstrap software.
688 #
689 case "$full" in
690 yes)
691         need_bsd_install=yes
692         need_awk=yes
693         need_sed=yes
694         need_ksh=yes
695         ;;
696 esac
697
698 case "$quiet" in
699 yes)
700         configure_quiet_flags="--quiet"
701         make_quiet_flags="-s"
702         ;;
703 no)
704         configure_quiet_flags=""
705         make_quiet_flags=""
706 esac
707
708 # export OPSYS and MACHINE_ARCH for pkg_install. we only set
709 # MACHINE_ARCH on platforms where we override bmake's value.
710 OPSYS=${opsys}
711 export OPSYS
712 if [ "${machine_arch}" != "" ]; then
713         MACHINE_ARCH=${machine_arch}
714         export MACHINE_ARCH
715 fi
716
717 if [ "x$preserve_path" != "xyes" ]; then
718         PATH="$overpath:$PATH"
719 fi
720
721 check_prog awkprog awk
722 check_prog chmodprog chmod
723 if [ -n "$CP" ]; then
724         cpprog="$CP"
725 else
726         check_prog cpprog cp
727 fi
728 if [ -n "$ID" ]; then
729         idprog="$ID"
730 else
731         check_prog idprog id
732 fi
733 check_prog groupsprog groups
734 check_prog lnprog ln
735 check_prog lsprog ls
736 check_prog rmdirprog rmdir
737 check_prog sedprog sed
738 check_prog shprog sh
739 check_prog whoamiprog whoami
740
741 if [ -d "${wrkdir}" ] || [ -f "${wrkdir}" ]; then
742         echo "\"${wrkdir}\" already exists, please remove it or use --workdir.";
743         exit 1
744 fi
745
746 mkdir_p_early ${wrkdir}
747 if touch ${wrkdir}/.writeable; then
748         :
749 else
750         echo "\"${wrkdir}\" is not writeable. Try $0 -h.";
751         exit 1
752 fi
753 echo "Working directory is: ${wrkdir}"
754
755 if [ "$compiler" = "" ] && [ x"$check_compiler" = x"yes" ]; then
756         get_compiler
757         if [ $compiler_is_gnu -gt 0 ]; then
758                 compiler="gcc"
759         else
760                 case "$opsys" in
761                 IRIX)
762                         if [ `uname -r` -ge 6 ]; then
763                                 compiler="mipspro"
764                         else
765                                 compiler="ido"
766                         fi
767                         test -n "$CC" || CC=cc
768                         ;;
769                 SunOS)  compiler="sunpro"
770                         test -n "$CC" || CC=cc
771                         ;;
772                 esac
773         fi
774 fi
775
776 mkdir_p_early ${wrkdir}/bin
777
778 # build install-sh
779 run_cmd "$sedprog -e 's|@DEFAULT_INSTALL_MODE@|'${default_install_mode-0755}'|' $pkgsrcdir/sysutils/install-sh/files/install-sh.in > $wrkdir/bin/install-sh"
780 run_cmd "$chmodprog +x $wrkdir/bin/install-sh"
781 install_sh="$shprog $wrkdir/bin/install-sh"
782
783 is_root
784 if [ $? = 1 ]; then
785         user=$root_user
786         group=$root_group
787 else
788         if [ $unprivileged = "no" ]; then
789                 die "You must be either root to install bootstrap-pkgsrc or use the --unprivileged option."
790         fi
791
792         user=`$whoamiprog`
793         group=`$groupsprog | $awkprog '{print $1}'`
794         echo_msg "building as unprivileged user $user/$group"
795
796         # force bmake install target to use $user and $group
797         echo "BINOWN=$user
798 BINGRP=$group
799 LIBOWN=$user
800 LIBGRP=$group
801 MANOWN=$user
802 MANGRP=$group" > ${wrkdir}/Makefile.inc
803 fi
804
805 # make sure we're using a case-sensitive file system
806 if [ $ignorecasecheck = "no" ]; then
807 case "$opsys" in
808 Interix)
809         echo_msg "Testing file system case sensitivity"
810         for fs in "$prefix"; do
811                 testdir="pkgsrc-REQUIRES-case-SENSITIVE-filesystem"
812                 testdir_mangled="PKGSRC-requires-CASE-sensitive-FILESYSTEM"
813                 mkdir_p "$fs/$testdir" || die "can't verify filesystem ($fs) case-sensitivity"
814                 if [ -d "$fs/$testdir_mangled" ]; then
815                         $rmdirprog "$fs/$testdir"
816                         die "\"$fs\" needs to be on a case-sensitive filesystem (see README.$opsys)"
817                 fi
818                 $rmdirprog "$fs/$testdir"
819         done
820         ;;
821 esac
822 fi
823
824 # export the proper environment
825 PATH=$prefix/bin:$prefix/sbin:${PATH}; export PATH
826 if [ -d /usr/ccs/bin -a -x /usr/ccs/bin/make ]; then
827         PATH=${PATH}:/usr/ccs/bin; export PATH
828 fi
829 PKG_DBDIR=$pkgdbdir; export PKG_DBDIR
830 LOCALBASE=$prefix; export LOCALBASE
831 VARBASE=$varbase; export VARBASE
832
833 # set up an example mk.conf file
834 TARGET_MKCONF=${wrkdir}/mk.conf.example
835 echo_msg "Creating default mk.conf in ${wrkdir}"
836 echo "# Example ${sysconfdir}/mk.conf file produced by bootstrap-pkgsrc" > ${TARGET_MKCONF}
837 echo "# `date`" >> ${TARGET_MKCONF}
838 echo "" >> ${TARGET_MKCONF}
839 echo ".ifdef BSD_PKG_MK # begin pkgsrc settings" >> ${TARGET_MKCONF}
840 echo "" >> ${TARGET_MKCONF}
841
842 # IRIX64 needs to be set to IRIX, for example
843 if [ "$set_opsys" = "yes" ]; then
844         echo "OPSYS=                    $opsys" >> ${TARGET_MKCONF}
845 fi
846
847 if [ ! -z "$abi" ]; then
848         echo "ABI=                      $abi" >> ${TARGET_MKCONF}
849 fi
850 if [ "$compiler" != "" ]; then
851         echo "PKGSRC_COMPILER=  $compiler" >> ${TARGET_MKCONF}
852 fi
853 case "$compiler" in
854 sunpro)
855         echo "CC=                       cc"        >> ${TARGET_MKCONF}
856         echo "CXX=                      CC"        >> ${TARGET_MKCONF}
857         echo "CPP=                      \${CC} -E" >> ${TARGET_MKCONF}
858         ;;
859 esac
860 if [ -n "$SUNWSPROBASE" ]; then
861         echo "SUNWSPROBASE=             $SUNWSPROBASE" >> ${TARGET_MKCONF}
862 fi
863 echo "" >> ${TARGET_MKCONF}
864
865 # enable unprivileged builds if not root
866 if [ "$unprivileged" = "yes" ]; then
867         echo "UNPRIVILEGED=             yes" >> ${TARGET_MKCONF}
868 fi
869
870 # save environment in example mk.conf
871 echo "PKG_DBDIR=                $pkgdbdir" >> ${TARGET_MKCONF}
872 echo "LOCALBASE=                $prefix" >> ${TARGET_MKCONF}
873 echo "VARBASE=          $varbase" >> ${TARGET_MKCONF}
874 if [ "${sysconfdir}" != "${prefix}/etc" ]; then
875         echo "PKG_SYSCONFBASE=  $sysconfdir" >> ${TARGET_MKCONF}
876 fi
877 echo "PKG_TOOLS_BIN=            $prefix/sbin" >> ${TARGET_MKCONF}
878 echo "PKGMANDIR=                $pkgmandir" >> ${TARGET_MKCONF}
879 echo "" >> ${TARGET_MKCONF}
880
881 BOOTSTRAP_MKCONF=${wrkdir}/mk.conf
882 cp ${TARGET_MKCONF} ${BOOTSTRAP_MKCONF}
883
884 # sbin is used by pkg_install, share/mk by bootstrap-mk-files
885 mkdir_p $wrkdir/sbin $wrkdir/share/mk
886 mkdir_p_early ${wrkdir}
887
888 if [ "$need_bsd_install" = "yes" ]; then
889         BSTRAP_ENV="INSTALL='$prefix/bin/install-sh -c' $BSTRAP_ENV"
890         echo "TOOLS_PLATFORM.install?=  $prefix/bin/install-sh" >> ${TARGET_MKCONF}
891         echo "TOOLS_PLATFORM.install?=  $wrkdir/bin/install-sh" >> ${BOOTSTRAP_MKCONF}
892 fi
893
894 if [ "$need_fixed_strip" = "yes" ] ; then
895         echo_msg "Installing fixed strip script"
896         run_cmd "$install_sh -c -o $user -g $group -m 755 $pkgsrcdir/pkgtools/bootstrap-extras/files/strip-sh $wrkdir/bin/strip"
897         echo "TOOLS_PLATFORM.strip?=            $prefix/bin/strip" >> ${TARGET_MKCONF}
898         echo "TOOLS_PLATFORM.strip?=            $wrkdir/bin/strip" >> ${BOOTSTRAP_MKCONF}
899         need_extras=yes
900 fi
901
902 if [ "$need_mkdir" = "yes" -a -z "$MKDIR" ]; then
903         echo_msg "Installing fixed mkdir script \"mkdir-sh\""
904         run_cmd "$install_sh -c -o $user -g $group -m 755 $pkgsrcdir/pkgtools/bootstrap-extras/files/mkdir-sh $wrkdir/bin/mkdir-sh"
905         echo "TOOLS_PLATFORM.mkdir?=            $prefix/bin/mkdir-sh -p" >> ${TARGET_MKCONF}
906         echo "TOOLS_PLATFORM.mkdir?=            $wrkdir/bin/mkdir-sh -p" >> ${BOOTSTRAP_MKCONF}
907         need_extras=yes
908 fi
909
910 if [ "$need_xargs" = "yes" ]; then
911         echo_msg "Installing fixed xargs script"
912         run_cmd "$install_sh -c -o $user -g $group -m 755 $pkgsrcdir/pkgtools/bootstrap-extras/files/xargs-sh $wrkdir/bin/xargs"
913         echo "TOOLS_PLATFORM.xargs?=            $prefix/bin/xargs" >> ${TARGET_MKCONF}
914         echo "TOOLS_PLATFORM.xargs?=            $wrkdir/bin/xargs" >> ${BOOTSTRAP_MKCONF}
915         need_extras=yes
916 fi
917
918 echo_msg "Bootstrapping mk-files"
919 run_cmd "(cd ${pkgsrcdir}/pkgtools/bootstrap-mk-files/files && env CP=${cpprog} \
920  OPSYS=${opsys} MK_DST=${wrkdir}/share/mk ROOT_GROUP=${root_group} \
921 ROOT_USER=${root_user} SED=${sedprog} SYSCONFDIR=${sysconfdir} \
922 $shprog ./bootstrap.sh)"
923
924 bootstrap_bmake() {
925         echo_msg "Bootstrapping bmake"
926         copy_src $pkgsrcdir/devel/bmake/files bmake
927         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)"
928         run_cmd "$install_sh -c -o $user -g $group -m 755 $wrkdir/bmake/$opsys/bmake $wrkdir/bin/bmake"
929 }
930 bootstrap_bmake
931
932 bmake="$wrkdir/bin/bmake $make_quiet_flags"
933
934 # build libnbcompat
935 echo_msg "Building libnbcompat"
936 copy_src $pkgsrcdir/pkgtools/libnbcompat/files libnbcompat
937 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)"
938
939 # bootstrap ksh if necessary
940 case "$need_ksh" in
941 yes)    echo_msg "Bootstrapping ksh"
942         copy_src $pkgsrcdir/shells/pdksh/files ksh
943         test -n "$CC" || CC=gcc # default to gcc if no compiler is specified
944         run_cmd "(cd $wrkdir/ksh && env $BSTRAP_ENV $shprog ./configure $configure_quiet_flags --prefix=$prefix --mandir=$mandir --sysconfdir=$sysconfdir && $bmake)"
945         run_cmd "$install_sh -c -o $user -g $group -m 755 $wrkdir/ksh/ksh $wrkdir/bin/pdksh"
946         echo "TOOLS_PLATFORM.sh?=               $prefix/bin/pdksh" >> ${TARGET_MKCONF}
947         echo "TOOLS_PLATFORM.sh?=               $wrkdir/bin/pdksh" >> ${BOOTSTRAP_MKCONF}
948         echo "TOOLS_PLATFORM.ksh?=              $prefix/bin/pdksh" >> ${TARGET_MKCONF}
949         echo "TOOLS_PLATFORM.ksh?=              $wrkdir/bin/pdksh" >> ${BOOTSTRAP_MKCONF}
950 # Now rebootstrap bmake for ksh
951         echo_msg "Rebootstrapping bmake for ksh"
952         bmakexargs="$bmakexargs --with-defshell=$wrkdir/bin/pdksh"
953         bootstrap_bmake
954         ;;
955 esac
956
957 # bootstrap awk if necessary
958 case "$need_awk" in
959 yes)    echo_msg "Bootstrapping awk"
960         copy_src $pkgsrcdir/lang/nawk/files awk
961         test -n "$CC" || CC=gcc # default to gcc if no compiler is specified
962         run_cmd "(cd $wrkdir/awk && $bmake -f Makefile CC=\"${CC}\" CFLAGS=\"${CFLAGS}\")"
963         run_cmd "$install_sh -c -o $user -g $group -m 755 $wrkdir/awk/a.out $wrkdir/bin/nawk"
964         echo "TOOLS_PLATFORM.awk?=              $prefix/bin/nawk" >> ${TARGET_MKCONF}
965         echo "TOOLS_PLATFORM.awk?=              $wrkdir/bin/nawk" >> ${BOOTSTRAP_MKCONF}
966         ;;
967 esac
968
969 # bootstrap sed if necessary
970 case "$need_sed" in
971 yes)    echo_msg "Bootstrapping sed"
972         copy_src $pkgsrcdir/textproc/nbsed/files sed
973         run_cmd "(cd $wrkdir/sed; env $BSTRAP_ENV CPPFLAGS='$CPPFLAGS -I../libnbcompat' LDFLAGS='$LDFLAGS -L../libnbcompat' LIBS='$LIBS -lnbcompat' $shprog ./configure $configure_quiet_flags -C --prefix=$prefix --mandir=$mandir --sysconfdir=$sysconfdir --program-transform-name='s,sed,nbsed,' && $bmake)"
974         run_cmd "$install_sh -c -o $user -g $group -m 755 $wrkdir/sed/sed $wrkdir/bin/sed"
975         echo "TOOLS_PLATFORM.sed?=              $prefix/bin/nbsed" >> ${TARGET_MKCONF}
976         echo "TOOLS_PLATFORM.sed?=              $wrkdir/bin/sed" >> ${BOOTSTRAP_MKCONF}
977         ;;
978 esac
979
980 # bootstrap pkg_install
981 echo_msg "Bootstrapping pkgtools"
982 copy_src $pkgsrcdir/pkgtools/pkg_install/files pkg_install
983 run_cmd "(cd $wrkdir/pkg_install; env $BSTRAP_ENV \
984 CPPFLAGS='$CPPFLAGS -I../libnbcompat -I../../libnbcompat' \
985 LDFLAGS='$LDFLAGS -L../libnbcompat -L../../libnbcompat' \
986 LIBS='$LIBS -lnbcompat' $shprog ./configure $configure_quiet_flags -C \
987 --enable-bootstrap --prefix=$prefix --sysconfdir=$sysconfdir \
988 --with-pkgdbdir=$pkgdbdir --mandir=$mandir $pkg_install_args && $bmake)"
989 run_cmd "$install_sh -c -o $user -g $group -m 755 $wrkdir/pkg_install/admin/pkg_admin $wrkdir/sbin/pkg_admin"
990 run_cmd "$install_sh -c -o $user -g $group -m 755 $wrkdir/pkg_install/create/pkg_create $wrkdir/sbin/pkg_create"
991 run_cmd "$install_sh -c -o $user -g $group -m 755 $wrkdir/pkg_install/info/pkg_info $wrkdir/sbin/pkg_info"
992 echo "PKG_ADMIN_CMD?=                   $wrkdir/sbin/pkg_admin" >> ${BOOTSTRAP_MKCONF}
993 echo "PKG_CREATE_CMD?=                  $wrkdir/sbin/pkg_create" >> ${BOOTSTRAP_MKCONF}
994 echo "PKG_INFO_CMD?=                    $wrkdir/sbin/pkg_info" >> ${BOOTSTRAP_MKCONF}
995
996 if [ -n "${fetch_cmd}" ]; then
997         echo "FETCH_CMD=                $fetch_cmd" >> ${TARGET_MKCONF}
998 fi
999
1000 MAKECONF=$wrkdir/mk.conf
1001 export MAKECONF
1002
1003 if [ "$bootstrap_sh_set" = "set" ]; then
1004         echo "TOOLS_PLATFORM.sh?=               ${bootstrap_sh}" >> ${TARGET_MKCONF}
1005         echo "TOOLS_PLATFORM.sh?=               ${bootstrap_sh}" >> ${BOOTSTRAP_MKCONF}
1006 fi
1007
1008 # preserve compiler and tool environment variables settings
1009 if test -n "$CP"; then
1010         echo "TOOLS_PLATFORM.cp?=               $CP" >> ${TARGET_MKCONF}
1011         echo "TOOLS_PLATFORM.cp?=               $CP" >> ${BOOTSTRAP_MKCONF}
1012 fi
1013 if test -n "$GREP"; then
1014         echo "TOOLS_PLATFORM.grep?=             $GREP" >> ${TARGET_MKCONF}
1015         echo "TOOLS_PLATFORM.grep?=             $GREP" >> ${BOOTSTRAP_MKCONF}
1016 fi
1017 if test -n "$ID"; then
1018         echo "TOOLS_PLATFORM.id?=               $ID" >> ${TARGET_MKCONF}
1019         echo "TOOLS_PLATFORM.id?=               $ID" >> ${BOOTSTRAP_MKCONF}
1020 fi
1021 if test -n "$MKDIR"; then
1022         echo "TOOLS_PLATFORM.mkdir?=            $MKDIR" >> ${TARGET_MKCONF}
1023         echo "TOOLS_PLATFORM.mkdir?=            $MKDIR" >> ${BOOTSTRAP_MKCONF}
1024 fi
1025 if test -n "$TEST"; then
1026         echo "TOOLS_PLATFORM.test?=             $TEST" >> ${TARGET_MKCONF}
1027         echo "TOOLS_PLATFORM.test?=             $TEST" >> ${BOOTSTRAP_MKCONF}
1028 fi
1029 if test -n "$TOUCH"; then
1030         echo "TOOLS_PLATFORM.touch?=            $TOUCH" >> ${TARGET_MKCONF}
1031         echo "TOOLS_PLATFORM.touch?=            $TOUCH" >> ${BOOTSTRAP_MKCONF}
1032 fi
1033 if test -n "$XARGS"; then
1034         echo "TOOLS_PLATFORM.xargs?=            $XARGS" >> ${TARGET_MKCONF}
1035         echo "TOOLS_PLATFORM.xargs?=            $XARGS" >> ${BOOTSTRAP_MKCONF}
1036 fi
1037 if test -n "$CFLAGS"; then
1038         echo "CFLAGS+=          $CFLAGS" >> ${TARGET_MKCONF}
1039         echo "DBG=                      # prevent DBG from adding default optimizer flags" >> ${TARGET_MKCONF}
1040         echo "DBG=                      # prevent DBG from adding default optimizer flags" >> ${BOOTSTRAP_MKCONF}
1041 fi
1042 if test -n "$CPPFLAGS"; then
1043         echo "CPPFLAGS+=                $CPPFLAGS" >> ${TARGET_MKCONF}
1044 fi
1045 if test -n "$LDFLAGS"; then
1046         echo "LDFLAGS+=         $LDFLAGS" >> ${TARGET_MKCONF}
1047 fi
1048 if test -n "$LIBS"; then
1049         echo "LIBS+=            $LIBS" >> ${TARGET_MKCONF}
1050 fi
1051
1052 # opsys specific fiddling
1053 opsys_finish
1054
1055 echo "WRKOBJDIR=                ${wrkdir}/wrk" >> ${BOOTSTRAP_MKCONF}
1056
1057 echo "" >> ${TARGET_MKCONF}
1058 echo "" >> ${BOOTSTRAP_MKCONF}
1059 if test -n "${mk_fragment}"; then
1060         cat "${mk_fragment}" >> ${TARGET_MKCONF}
1061         echo "" >> ${TARGET_MKCONF}
1062 fi
1063 echo ".endif                    # end pkgsrc settings" >> ${TARGET_MKCONF}
1064 echo ".endif                    # end pkgsrc settings" >> ${BOOTSTRAP_MKCONF}
1065
1066 # register packages
1067 # usage: register_package <packagedirectory> [additional arguments]
1068 build_package() {
1069         run_cmd "(cd $pkgsrcdir/$1 && $bmake -DPKG_PRESERVE MAKECONF=${BOOTSTRAP_MKCONF} install)"
1070 }
1071
1072 #
1073 # Please make sure that the following packages and
1074 # only the following packages set BOOTSTRAP_PKG=yes.
1075 #
1076 echo_msg "Installing packages"
1077 build_package "pkgtools/bootstrap-mk-files"
1078 case "$need_bsd_install" in
1079 yes)    build_package "sysutils/install-sh";;
1080 esac
1081 case "$need_ksh" in
1082 yes)    build_package "shells/pdksh";;
1083 esac
1084 build_package "devel/bmake"
1085 case "$need_awk" in
1086 yes)    build_package "lang/nawk";;
1087 esac
1088 case "$need_sed" in
1089 yes)    build_package "textproc/nbsed";;
1090 esac
1091 case "$need_extras" in
1092 yes)    build_package "pkgtools/bootstrap-extras";;
1093 esac
1094 build_package "pkgtools/pkg_install"
1095
1096 etc_mk_conf="$sysconfdir/mk.conf"
1097
1098 # Install the example mk.conf so that it is used, but only if it doesn't
1099 # exist yet. This can happen with non-default sysconfdir settings.
1100 mkdir_p "$sysconfdir"
1101 if [ ! -f "$etc_mk_conf" ]; then
1102         cp "$TARGET_MKCONF" "$etc_mk_conf"
1103         TARGET_MKCONF="$etc_mk_conf"
1104 fi
1105
1106 hline="==========================================================================="
1107 echo ""
1108 echo "$hline"
1109 echo ""
1110 echo "Please remember to add $prefix/bin to your PATH environment variable"
1111 echo "and $mandir to your MANPATH environment variable, if necessary."
1112 echo ""
1113 echo "An example mk.conf file with the settings you provided to \"bootstrap\""
1114 echo "has been created for you. It can be found in:"
1115 echo ""
1116 echo "      ${TARGET_MKCONF}"
1117 echo ""
1118 if [ "$TARGET_MKCONF" != "$etc_mk_conf" ]; then
1119         echo "Please copy it to $etc_mk_conf to use it."
1120         echo ""
1121 fi
1122 echo "You can find extensive documentation of the NetBSD Packages Collection"
1123 echo "in $pkgsrcdir/doc/pkgsrc.txt."
1124 echo ""
1125 echo "Hopefully everything is now complete."
1126 echo "Thank you"
1127 echo ""
1128 echo "$hline"
1129 echo ""
1130
1131 [ ! -z "${binary_kit}" ] && mkbinarykit_tar
1132 [ ! -z "${binary_gzip_kit}" ] && mkbinarykit_tgz
1133 [ ! -z "${binary_macpkg}" ] && mkbinarykit_macpkg
1134
1135 echo_msg "bootstrap started: $build_start"
1136 echo_msg "bootstrap ended:   `date`"
1137
1138 exit 0