Import KDE4.3.1 from wip
[pkgsrc.git] / bootstrap / bootstrap
1 #! /bin/sh
2
3 # $NetBSD: bootstrap,v 1.89 2004/03/11 01:08:14 tv 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 BOOTSTRAP_VERSION=20040214
39
40 # set defaults for system locations
41 prefix=/usr/pkg
42 pkgdbdir=/var/db/pkg
43 pkgsrcdir=/usr/pkgsrc
44
45 ignorecasecheck=no
46 ignoreusercheck=no
47
48 usage="Usage: $0 "'
49     [ --prefix=<prefix> ]
50     [ --pkgdbdir=<pkgdbdir> ]
51     [ --pkgsrcdir=<pkgsrcdir> ]
52     [ --ignore-case-check ]
53     [ --ignore-user-check ]
54     [ --help ]'
55
56 # this replicates some of the logic in bsd.prefs.mk. until
57 # bootstrap-pkgsrc is merged into pkgsrc, we need to determine the
58 # right value for OPSYS and MACHINE_ARCH.
59
60 # strip / for BSD/OS
61 opsys=`uname -s | tr -d /`
62
63 die()
64 {
65         echo >&2 "$@"
66         exit 1
67 }
68
69 echo_msg()
70 {
71         echo "===> $@"
72 }
73
74 get_abi()
75 {
76         abi_opsys=$@
77         case "$abi_opsys" in
78         IRIX)
79                 abi=`sed -e 's/.*\(abi=\)\([on]*[36][24]\).*/\2/' /etc/compiler.defaults`
80                 isa=`sed -e 's/.*\(isa=mips\)\([1234]\).*/\2/' /etc/compiler.defaults`
81                 case "$abi" in
82                 o32)
83                         imakeopts="-DBuildO32 -DSgiISAo32=$isa"
84                         abi=""
85                         ;;
86                 n32)    imakeopts="-DBuildN32 -DSgiISA32=$isa"
87                         abi="32"
88                         ;;
89                 64 | n64)
90                         imakeopts="-DBuild64bit -DSgiISA64=$isa"
91                         abi="64"
92                         ;;
93                 esac
94
95
96                 ;;
97         esac
98 }
99
100 check_prog()
101 {
102         _var="$1"; _name="$2"
103
104         eval _tmp=\"\$$_var\"
105         if [ "x$_tmp" != "x" ]; then
106                 # Variable is already set (by the user, for example)
107                 return 0
108         fi
109
110         for _d in `echo $PATH | tr ':' ' '`; do
111                 if [ -x "$_d/$_name" ]; then
112                         # Program found
113                         eval $_var=\""$_d/$_name"\"
114                         return 1
115                 fi
116         done
117
118         die "$_name not found in path."
119 }
120
121 opsys_finish()
122 {
123         case "$opsys" in
124         IRIX)
125                 # setting X11BASE to /usr breaks buildlink
126                 if [ ! -e /usr/X11R6 ]; then
127                         ln -sf /usr /usr/X11R6
128                 fi
129                 if [ ! -z $imakeopts ]; then
130                         echo "IMAKEOPTS+=$imakeopts" >> mk.conf.example
131                 fi
132                 patch -d / --forward --quiet -E -p0 \
133                         < files/irix.patch 2>/dev/null || true
134                 ;;
135         esac
136 }
137
138 is_root()
139 {
140         if [ `$idprog -u` != 0 ]; then
141                 return 0
142         fi
143         return 1
144 }
145
146 # run a command, abort if it fails
147 run_cmd()
148 {
149         echo_msg "running: $@"
150         eval "$@"
151         ret=$?
152         if [ $ret -ne 0 ]; then
153                 echo_msg "exited with status $ret"
154                 die "aborted."
155         fi
156 }
157
158 build_start=`date`
159 echo_msg "bootstrap command: $0 $@"
160 echo_msg "bootstrap started: $build_start"
161
162 overpath=""
163 case "$opsys" in
164 Darwin)
165         root_group=wheel
166         need_pax=yes
167         need_mtree=no
168         need_bsd_install=no
169         need_sed=no
170         set_opsys=no
171         check_prog mtreeprog mtree
172         machine_arch=`uname -p`
173         ;;
174 FreeBSD)
175         root_group=wheel
176         need_pax=yes
177         need_mtree=yes
178         need_bsd_install=no
179         need_sed=no
180         set_opsys=no
181         machine_arch=`uname -p`
182         ;;
183 HP-UX)
184         root_group=root
185         need_pax=yes
186         need_mtree=yes
187         need_bsd_install=yes
188         need_sed=yes
189         set_opsys=no
190         ;;
191 IRIX*)
192         if [ -d "/usr/freeware/bin" ]; then
193                 overpath="/usr/freeware/bin:$overpath"
194         fi
195         if [ -d "/usr/bsd/bin" ]; then
196                 overpath="/usr/bsd/bin:$overpath"
197         fi
198         root_group=sys
199         need_mtree=yes
200         need_bsd_install=yes
201         need_pax=yes
202         configargs="--with-machine_arch=mipseb"
203         get_abi "IRIX"
204         opsys=IRIX
205         need_sed=yes
206         set_opsys=yes
207         ;;
208 Linux)
209         if [ -f /etc/debian_version ]; then
210                 DEBIAN=yes
211         fi
212         root_group=root
213         need_pax=yes
214         need_mtree=yes
215         need_bsd_install=no
216         need_sed=no
217         set_opsys=no
218         machine_arch=`uname -m | sed -e 's/i.86/i386/'`
219         ;;
220 NetBSD)
221         root_group=wheel
222         need_pax=no
223         need_mtree=no
224         need_bsd_install=no
225         need_sed=no
226         set_opsys=no
227         check_prog paxprog pax
228         check_prog tarprog tar
229         check_prog mtreeprog mtree
230         machine_arch=`uname -p`
231         ;;
232 OpenBSD)
233         root_group=wheel
234         need_pax=yes
235         need_mtree=no
236         need_bsd_install=no
237         need_sed=no
238         set_opsys=no
239         check_prog mtreeprog mtree
240         machine_arch=`uname -p`
241         ;;
242 SunOS)
243         if [ -d "/usr/xpg4/bin" ]; then
244                 overpath="/usr/xpg4/bin:$overpath"
245         fi
246         root_group=root
247         need_pax=yes
248         need_mtree=yes
249         need_bsd_install=no
250         need_sed=yes
251         set_opsys=no
252         whoamiprog=/usr/ucb/whoami
253         machine_arch=`uname -p | sed -e 's/i86pc/i386/'`
254         ;;
255 AIX)
256         root_group=system
257         need_pax=yes
258         need_mtree=yes
259         need_bsd_install=yes
260         need_sed=yes
261         need_fixed_strip=yes
262         set_opsys=no
263         ;;
264 Interix)
265         ignoreusercheck=yes
266         root_group=+Administrators
267         need_pax=no
268         need_mtree=yes
269         need_bsd_install=yes
270         need_sed=yes
271         set_opsys=no
272         groupsprog="id -Gn"
273         CC="gcc -D_ALL_SOURCE"; export CC
274         ;;
275 *)
276         echo "This platform ($opsys) is untried - good luck, and thanks for using pkgsrc"
277         root_group=wheel
278         need_pax=yes
279         need_mtree=yes
280         need_bsd_install=yes
281         need_sed=yes
282         set_opsys=no
283         ;;
284 esac
285
286 # export OPSYS and MACHINE_ARCH for pkg_install. we only set
287 # MACHINE_ARCH on platforms where we override bmake's value.
288 OPSYS=${opsys}
289 export OPSYS
290 if [ "${machine_arch}" != "" ]; then
291         MACHINE_ARCH=${machine_arch}
292         export MACHINE_ARCH
293 fi
294
295 if [ "x${PRESERVE_PATH}" != "xyes" ]; then
296         PATH="$overpath:$PATH"
297 fi
298
299 check_prog awkprog awk
300 check_prog cpprog cp
301 check_prog idprog id
302 check_prog groupsprog groups
303 check_prog lsprog ls
304 check_prog mkdirprog mkdir
305 check_prog rmdirprog rmdir
306 check_prog sedprog sed
307 check_prog shprog sh
308 check_prog whoamiprog whoami
309
310 while [ $# -gt 0 ]; do
311         case $1 in
312         --prefix=*)     prefix=`echo $1 | $sedprog -e 's|--prefix=||'` ;;
313         --pkgdbdir=*)   pkgdbdir=`echo $1 | $sedprog -e 's|--pkgdbdir=||'` ;;
314         --pkgsrcdir=*)  pkgsrcdir=`echo $1 | $sedprog -e 's|--pkgsrcdir=||'` ;;
315         --ignore-case-check) ignorecasecheck=yes ;;
316         --ignore-user-check) ignoreusercheck=yes ;;
317         --help)         echo "$usage"; exit ;;
318         -h)             echo "$usage"; exit ;;
319         --*)            echo "$usage"; exit 1 ;;
320         esac
321         shift
322 done
323
324 is_root
325 if [ $? = 1 ]; then
326         user=root
327         group=$root_group
328 else
329         if [ $ignoreusercheck = "no" ]; then
330                 die "You must be root to install bootstrap-pkgsrc."
331         fi
332
333         user=`$whoamiprog`
334         group=`$groupsprog | $awkprog '{print $1}'`
335         echo_msg "building as unprivileged user $user/$group"
336
337         # force bmake install target to use $user and $group
338         echo "BINOWN=$user
339 BINGRP=$group
340 LIBOWN=$user
341 LIBGRP=$group
342 MANOWN=$user
343 MANGRP=$group" > Makefile.inc
344 fi
345
346 # make sure we're using a case-sensitive file system on Darwin
347 if [ $ignorecasecheck = "no" ]; then
348 case "$opsys" in
349 Darwin)
350         echo_msg "Testing file system case sensitivity"
351         for fs in "$prefix" "$pkgsrcdir"; do
352                 testdir="pkgsrc-REQUIRES-case-SENSITIVE-filesystem"
353                 testdir_mangled="PKGSRC-requires-CASE-sensitive-FILESYSTEM"
354                 $mkdirprog -p "$fs/$testdir" || die "can't verify filesystem ($fs) case-sensitivity"
355                 if [ -d "$fs/$testdir_mangled" ]; then
356                         $rmdirprog "$fs/$testdir"
357                         die "\"$fs\" needs to be on a case-sensitive filesystem (see README.Darwin)"
358                 fi
359                 $rmdirprog "$fs/$testdir"
360         done
361         ;;
362 esac
363 fi
364
365 # export the proper environment
366 PATH=$prefix/bin:$prefix/sbin:${PATH}; export PATH
367 if [ -d /usr/ccs/bin -a -x /usr/ccs/bin/make ]; then
368         PATH=/usr/ccs/bin:$PATH; export PATH
369 fi
370 PKG_DBDIR=$pkgdbdir; export PKG_DBDIR
371 LOCALBASE=$prefix; export LOCALBASE
372
373 # set up an example mk.conf file
374 echo_msg "Creating sample mk.conf"
375 echo "# Example /etc/mk.conf file produced by bootstrap-pkgsrc" > mk.conf.example
376 echo "# `date`" >> mk.conf.example
377 echo "" >> mk.conf.example
378 echo ".ifdef BSD_PKG_MK # begin pkgsrc settings" >> mk.conf.example
379 echo "" >> mk.conf.example
380
381 # IRIX64 needs to be set to IRIX, for example
382 if [ "$set_opsys" = "yes" ]; then
383         echo "OPSYS=$opsys" >> mk.conf.example
384 fi
385
386 if [ ! -z "$abi" ]; then
387         echo "ABI=$abi" >> mk.conf.example
388 fi
389
390 # save environment in example mk.conf
391 echo "PKG_DBDIR=$pkgdbdir" >> mk.conf.example
392 echo "LOCALBASE=$prefix" >> mk.conf.example
393
394 # create directories
395 run_cmd "$mkdirprog -p $prefix $pkgdbdir $prefix/sbin"
396 run_cmd "$mkdirprog -p $prefix/man/man1 $prefix/man/cat1"
397 run_cmd "$mkdirprog -p $prefix/man/man8 $prefix/man/cat8"
398
399 # bootstrap make and *.mk files
400 run_cmd "$mkdirprog -p $prefix/share/mk $prefix/lib"
401 (cd bmake/mk;
402         if [ -f ../../mods/mk/$opsys.sys.mk ]; then
403                 run_cmd "$cpprog ../../mods/mk/$opsys.sys.mk $prefix/share/mk/sys.mk"
404         else
405                 run_cmd "$cpprog ../../mods/mk/generic.sys.mk $prefix/share/mk/sys.mk"
406         fi
407         run_cmd "$cpprog bsd.*.mk $prefix/share/mk")
408
409 if [ -f mods/mk/$opsys.bsd.lib.mk ] ;  then
410         run_cmd "$cpprog bmake/mk/bsd.lib.mk bmake/mk/bsd.lib.mk.orig"
411         run_cmd "$cpprog mods/mk/$opsys.bsd.lib.mk bmake/mk/bsd.lib.mk"
412 fi
413
414 run_cmd "$cpprog bmake/mk/bsd.man.mk bmake/mk/bsd.man.mk.orig"
415 if [ -f mods/mk/$opsys.bsd.man.mk ] ;  then
416         run_cmd "$cpprog mods/mk/$opsys.bsd.man.mk bmake/mk/bsd.man.mk"
417         run_cmd "$cpprog mods/mk/$opsys.bsd.man.mk $prefix/share/mk/bsd.man.mk"
418 fi
419
420 if [ -f mods/mk/$opsys.bsd.prog.mk ] ; then
421         run_cmd "$cpprog mods/mk/$opsys.bsd.own.mk $prefix/share/mk/bsd.own.mk"
422 else
423         run_cmd "$sedprog -e 's|@ROOT_GROUP@|'$root_group'|g' mods/mk/bsd.own.mk.in > $prefix/share/mk/bsd.own.mk"
424 fi
425
426 if [ -f mods/mk/$opsys.bsd.sys.mk ]; then
427         run_cmd "$cpprog mods/mk/$opsys.bsd.sys.mk $prefix/share/mk/bsd.sys.mk"
428 fi      
429         
430 case "$opsys" in
431 NetBSD) run_cmd "$cpprog mods/bmake/Makefile.in bmake/Makefile.in"
432         ;;
433 esac
434
435 if [ "$need_bsd_install" = "yes" ]; then
436         echo_msg "Installing BSD compatible install script"
437         run_cmd "(cd files; $shprog ./install-sh -c -o $user -g $group -m 755 install-sh $prefix/bin/install-sh)"
438         BSTRAP_ENV="INSTALL='$prefix/bin/install-sh -c' $BSTRAP_ENV"
439 fi
440
441 if [ "$need_fixed_strip" = "yes" ] ; then
442         echo_msg "Installing fixed strip script"
443         run_cmd "(cd files; $shprog ./install-sh -c -o $user -g $group -m 755 strip-sh $prefix/bin/strip)"
444 fi
445
446 echo_msg "Installing bmake"
447 run_cmd "(cd bmake; $shprog ./configure --prefix=$prefix --with-default-sys-path=$prefix/share/mk $configargs && make -f makefile.boot bootstrap && env BINDIR=$prefix/bin MANDIR=$prefix/man $BSTRAP_ENV ./bmake -f Makefile install)"
448
449 # build libnbcompat
450 echo_msg "Building libnbcompat"
451 run_cmd "(cd libnbcompat; $shprog ./configure -C --prefix=$prefix && $prefix/bin/bmake)"
452
453 # bootstrap tnftp
454 case "$DEBIAN" in
455 yes)
456         LIBS="-lncurses"
457         ;;
458 esac
459 echo_msg "Installing tnftp"
460 run_cmd "(cd tnftp; env $BSTRAP_ENV $shprog ./configure --prefix=$prefix && make && (cd src && make install))"
461 pkg_install_args="$pkg_install_args --with-ftp=$prefix/bin/ftp"
462
463 FETCH_CMD=$prefix/bin/ftp
464 export FETCH_CMD
465 echo "FETCH_CMD=$prefix/bin/ftp" >> mk.conf.example
466
467 # bootstrap digest
468 echo_msg "Installing digest"
469 run_cmd "(cd digest; env $BSTRAP_ENV $shprog ./configure -C --prefix=$prefix && make && make install)"
470
471 # create the digest package's meta files
472 digestversion=digest-`$awkprog -F "'" '/^PACKAGE_VERSION=/ {print $2}' digest/configure`
473 $lsprog -ld $prefix/bin/digest | $awkprog '{ print $5 }' > digest/.size
474 env DIGESTPROG=$prefix/bin/digest PKG_DIGEST=md5 PKG_DBDIR=$pkgdbdir \
475         $shprog ./pkg.sh create -d digest/DESCR -O \
476         -c '-Message digest wrapper utility' -l -p $prefix \
477         -f digest/PLIST -s digest/.size -S digest/.size $digestversion
478
479 # we often need NetBSD's pax as well, nowadays, to make binary packages
480 case "$need_pax" in
481 yes)    echo_msg "Installing pax"
482         run_cmd "(cd pax; env $BSTRAP_ENV CPPFLAGS='-I../libnbcompat' LDFLAGS='-L../libnbcompat' LIBS='-lnbcompat' $shprog ./configure -C --prefix=$prefix && $prefix/bin/bmake && $prefix/bin/bmake install)"
483         echo "PAX=$prefix/bin/pax" >> mk.conf.example
484         pkg_install_args="$pkg_install_args --with-pax=$prefix/bin/pax --with-tar=$prefix/bin/tar"
485         ;;
486 *)
487         pkg_install_args="$pkg_install_args --with-pax=$paxprog --with-tar=$tarprog"
488         ;;
489 esac
490
491 # bootstrap mtree if necessary
492 case "$need_mtree" in
493 yes)    echo_msg "Installing mtree"
494         run_cmd "(cd mtree; env $BSTRAP_ENV CPPFLAGS='-I../libnbcompat' LDFLAGS='-L../libnbcompat' LIBS='-lnbcompat' $shprog ./configure -C --prefix=$prefix && $prefix/bin/bmake && $prefix/bin/bmake install)"
495         pkg_install_args="$pkg_install_args --with-mtree=$prefix/sbin/mtree"
496         ;;
497 *)      pkg_install_args="$pkg_install_args --with-mtree=$mtreeprog"
498         ;;
499 esac
500
501 # bootstrap sed if necessary
502 case "$need_sed" in
503 yes)    echo_msg "Installing sed"
504         run_cmd "(cd sed; env $BSTRAP_ENV $shprog ./configure -C --prefix=$prefix && make && make install)"
505         echo "SED=$prefix/bin/nbsed" >> mk.conf.example
506         ;;
507 esac
508
509 # bootstrap pkg_install
510 echo_msg "Installing pkgtools"
511 pkg_install_mandir="$prefix/man"
512 if [ "$prefix" = "/usr" ]; then
513         pkg_install_mandir="$prefix/share/man"
514 fi
515 run_cmd "(cd pkg_install; env $BSTRAP_ENV CPPFLAGS='-I../libnbcompat -I../../libnbcompat' LDFLAGS='-L../libnbcompat -L../../libnbcompat' LIBS='-lnbcompat' $shprog ./configure -C --prefix=$prefix --with-pkgdbdir=$pkgdbdir --mandir=$pkg_install_mandir $pkg_install_args && $prefix/bin/bmake && $prefix/bin/bmake install)"
516
517 # all's ready, install the man page
518 echo_msg "Installing packages(7) man page"
519 (cd files; run_cmd "$shprog ./install-sh -c -m 444 packages.cat7 $prefix/man/cat7/packages.0")
520
521 # opsys specific fiddling
522 opsys_finish
523
524 echo "" >> mk.conf.example
525 echo ".endif                    # end pkgsrc settings" >> mk.conf.example
526
527 echo "Please remember to add $prefix/bin to your PATH environment variable."
528 echo "If necessary, please remember to add $prefix/man to your MANPATH environment variable."
529 echo "Please remember to set FETCH_CMD in /etc/mk.conf to $prefix/bin/ftp"
530 echo ""
531 echo "An example mk.conf file has been created for you in mk.conf.example"
532 echo "with the settings you provided to bootstrap pkgsrc."
533 echo ""
534 echo "You can find extensive documentation of the NetBSD Packages Collection"
535 echo "in $pkgsrcdir/Packages.txt and packages(7)."
536 echo ""
537 echo "Hopefully everything is now complete."
538 echo "Thank you"
539
540 echo_msg "bootstrap started: $build_start"
541 echo_msg "bootstrap ended:   `date`"
542
543 exit 0