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