]> gitweb.factorcode.org Git - factor.git/blob - build-support/factor.sh
remove BSD,solaris,etc. misc platform support code
[factor.git] / build-support / factor.sh
1 #!/usr/bin/env bash
2
3 # Programs returning != 0 will not cause script to exit
4 set +e
5
6 # Case insensitive string comparison
7 shopt -s nocaseglob
8 #shopt -s nocasematch
9
10 ECHO=echo
11 OS=
12 ARCH=
13 WORD=
14 NO_UI=${NO_UI-}
15 GIT_PROTOCOL=${GIT_PROTOCOL:="git"}
16 GIT_URL=${GIT_URL:=$GIT_PROTOCOL"://factorcode.org/git/factor.git"}
17 SCRIPT_ARGS="$*"
18
19 test_program_installed() {
20     if ! [[ -n `type -p $1` ]] ; then
21         return 0;
22     fi
23     return 1;
24 }
25
26 exit_script() {
27     if [[ $FIND_MAKE_TARGET = true ]] ; then
28         # Must be echo not $ECHO
29         echo $MAKE_TARGET;
30     fi
31     exit $1
32 }
33
34 ensure_program_installed() {
35     installed=0;
36     for i in $* ;
37     do
38         $ECHO -n "Checking for $i..."
39         test_program_installed $i
40         if [[ $? -eq 0 ]]; then
41             $ECHO -n "not "
42         else
43             installed=$(( $installed + 1 ))
44         fi
45         $ECHO "found!"
46     done
47     if [[ $installed -eq 0 ]] ; then
48         $ECHO -n "Install "
49         if [[ $# -eq 1 ]] ; then
50             $ECHO -n $1
51         else
52             $ECHO -n "any of [ $* ]"
53         fi
54         $ECHO " and try again."
55         exit_script 1;
56     fi
57 }
58
59 check_ret() {
60     RET=$?
61     if [[ $RET -ne 0 ]] ; then
62        $ECHO $1 failed
63        exit_script 2
64     fi
65 }
66
67 set_downloader() {
68     test_program_installed wget curl
69     if [[ $? -ne 0 ]] ; then
70         DOWNLOADER=wget
71     else
72         DOWNLOADER="curl -f -O"
73     fi
74 }
75
76 set_md5sum() {
77     test_program_installed md5sum
78     if [[ $? -ne 0 ]] ; then
79         MD5SUM=md5sum
80     else
81         MD5SUM="md5 -r"
82     fi
83 }
84
85 set_gcc() {
86     case $OS in
87         macosx)
88             if xcodebuild -version | grep 'Xcode 4' >/dev/null; then
89                 CC=clang
90             else
91                 CC=gcc
92             fi
93         ;;
94         *) CC=gcc;;
95     esac
96 }
97
98 set_make() {
99     MAKE='make'
100 }
101
102 check_installed_programs() {
103     ensure_program_installed chmod
104     ensure_program_installed uname
105     ensure_program_installed git
106     ensure_program_installed wget curl
107     ensure_program_installed gcc
108     ensure_program_installed g++ cl
109     ensure_program_installed make gmake
110     ensure_program_installed md5sum md5
111     ensure_program_installed cut
112 }
113
114 check_library_exists() {
115     GCC_TEST=factor-library-test.c
116     GCC_OUT=factor-library-test.out
117     $ECHO -n "Checking for library $1..."
118     $ECHO "int main(){return 0;}" > $GCC_TEST
119     $CC $GCC_TEST -o $GCC_OUT -l $1 2>&-
120     if [[ $? -ne 0 ]] ; then
121         $ECHO "not found!"
122         $ECHO "***Factor will compile NO_UI=1"
123         NO_UI=1
124     else
125         $ECHO "found."
126     fi
127     $DELETE -f $GCC_TEST
128     check_ret $DELETE
129     $DELETE -f $GCC_OUT
130     check_ret $DELETE
131 }
132
133 check_X11_libraries() {
134     if [ -z "$NO_UI" ]; then
135         check_library_exists GL
136         check_library_exists X11
137         check_library_exists pango-1.0
138     fi
139 }
140
141 check_gtk_libraries() {
142     if [ -z "$NO_UI" ]; then
143         check_library_exists gobject-2.0
144         check_library_exists gtk-x11-2.0
145         check_library_exists gdk-x11-2.0
146         check_library_exists gdk_pixbuf-2.0
147         check_library_exists gtkglext-x11-1.0
148         check_library_exists atk-1.0
149         check_library_exists gio-2.0
150         check_library_exists gdkglext-x11-1.0
151         check_library_exists pango-1.0
152     fi
153 }
154
155
156 check_libraries() {
157     case $OS in
158             linux) check_X11_libraries
159                    check_gtk_libraries;;
160             unix) check_gtk_libraries;;
161     esac
162 }
163
164 check_factor_exists() {
165     if [[ -d "factor" ]] ; then
166         $ECHO "A directory called 'factor' already exists."
167         $ECHO "Rename or delete it and try again."
168         exit_script 4
169     fi
170 }
171
172 find_os() {
173     if [[ -n $OS ]] ; then return; fi
174     $ECHO "Finding OS..."
175     uname_s=`uname -s`
176     check_ret uname
177     case $uname_s in
178         CYGWIN_NT-5.2-WOW64) OS=windows;;
179         *CYGWIN_NT*) OS=windows;;
180         *CYGWIN*) OS=windows;;
181         MINGW32*) OS=windows;;
182         *darwin*) OS=macosx;;
183         *Darwin*) OS=macosx;;
184         *linux*) OS=linux;;
185         *Linux*) OS=linux;;
186         *BSD*)
187             echo "Here's a nickel, kid:"
188             echo "                                  ..;;iiiittttjjttttii;;........                                    "
189             echo "                              ..ttLLGGGGffttjjjjttttttjjjjtt;;....                                  "
190             echo "                          ..iiLLLLjjffffiiiiffttLLjjiiiiiiiiffjjii....                              "
191             echo "                        ;;LLttii;;iiiittttiiiittttffLLGGiiii;;iiLLLL;;..                            "
192             echo "                    ..iiGGii..ii;;iiiittiittiiiiiittffffGGtt;;;;;;ffDDii..                          "
193             echo "                    iiDD;;;;..;;ttttiittiittttiiiittttffDDEE;;,,;;;;ffKKtt..                        "
194             echo "                  iiKKii..,,..iiii;;ii;;iittttttttttjjjjffGGLL::....;;LLWWtt..                      "
195             echo "                ..DDff..;;ii;;ii::,,;;;;iiiittttttjjffttjjLLDDtt..iiii;;KKWWii..                    "
196             echo "                LLLL;;;;ii;;;;;;,,;;;;;;ii;;ttttffffttttttLLGGKK..iiiiiiiiWWDD;;                    "
197             echo "              ;;DD;;;;ii;;;;;;;;,,;;;;iiiiiiiijjffffffttttLLGGWWii..iiiiiiGG##jj..                  "
198             echo "              LLtt::ii;;....ii::;;,,;;;;iiiittttLLLLLLttttttGGKKGG;;;;iiiiii##KK;;                  "
199             echo "            ..LL..iiii;;::::;;;;,,,,;;;;ii;;ttiiDDLLtttt;;iittLL##ii;;iiii;;WW##tt..                "
200             echo "            ii;;;;iiff  ..;;..ii..;;;;;;ii;;iiiiDDLLiiii;;ttLLEE##tt;;;;ii;;DD##GG..                "
201             echo "            tt..iiGG::....ii..;;;;;;;;;;;;;;iiiiDDLLiiiiiiffGGWWKKiiii;;ii..tt##KK;;                "
202             echo "            ff;;GGtt..,,  tt..;;;;,,;;iiii;;iittGGLLttffLLGGKKDDjjLLii;;;;,,ii##WWii                "
203             echo "            GGttii..;;..  ff::;;..;;iiiitt..iiiittGGffLLEEffEE;;;;EEtt;;::..;;WW##tt                "
204             echo "            GGjjii..ii..,,ff..::;;iiiiiitt..;;iiffttttffGGWWKKiiiiLLDD..;;....WW##tt                "
205             echo "            GGff,,..ii..iiii,,..::iiiiii;;..;;jjjjiiiiffGG##DDttiiffKK;;..  ..WW##jj                "
206             echo "            GGff..;;;;..LL..;;;;..;;iiii..;;ttLLiiiiiiffGGKKEE..jjLLKKjj..  ..WW##tt                "
207             echo "            LLff..ii..iiGG..::......;;;;;;;;LL;;iittttttLLKKLLffKKLLKKff..  ;;####ii                "
208             echo "            LLjj..;;  LLii;;..;;......;;;;iitt;;ttLLttiiGGGGGGffDDKKii::..  tt##WW;;                "
209             echo "            jjtt....;;GGttjj  ......;;ii;;iitt;;ttffttttLLDDGGttDDKK;;..    LL##KK..                "
210             echo "            ;;ii;;..LLtt;;;;  ....;;ii....iiGG;;iijjffLLGGKKjjGGKKGG....    KK##LL                  "
211             echo "            ..ffiittttiiii;;  ....;;ii,,;;ttLLttiittjjLLGGttjjGGKKtt      iiWW##ii                  "
212             echo "              ttLLii;;iiii;;  ....,,;;  ..;;ttDDiiiiffffffffGGGGGG..      GG##KK..                  "
213             echo "              ..LLjjii;;;;....::;;,,;;;;..;;ttDDttttjjttffLLLLDDff      ;;WW##tt                    "
214             echo "                iiGGii,,....,,iiiiii,,ii  ::ttEEttffDDttLLttLLKKii      GG##KK..                    "
215             echo "                  LLtt......,,;;;;;;iiiiii;;;;KKffLLKK;;;;tttttt      tt##WWii                      "
216             echo "                  ..ii;;;;..;;iiii;;tt,,jj  iiGGffLLKK....;;....    ;;WW##ff..                      "
217             echo "                    ..;;..::;;iiiiii;;..jj;;..GGttffEE........    iiKK##LL..                        "
218             echo "                      ..,,..;;;;;;;;..;;iitt,,ttttjjLL          iiKK##LL..                          "
219             echo "                        ............  ,,;;;;..;;;;LLii      ..ttKKKKtt..                            "
220             echo "                            ..::........;;......;;;;;;  ..iiDDWWLL,,                                "
221             echo "                              ....;;................iittGGDDff;;                                    "
222             echo "                                    ....;;;;;;;;;;iiiiii::..                                        "
223             echo "Get yourself a real OS."
224             echo "http://www.microsoft.com/"
225             exit_script 1
226     esac
227 }
228
229 find_architecture() {
230     if [[ -n $ARCH ]] ; then return; fi
231     $ECHO "Finding ARCH..."
232     uname_m=`uname -m`
233     check_ret uname
234     case $uname_m in
235        i386) ARCH=x86;;
236        i686) ARCH=x86;;
237        i86pc) ARCH=x86;;
238        amd64) ARCH=x86;;
239        ppc64) ARCH=ppc;;
240        *86) ARCH=x86;;
241        *86_64) ARCH=x86;;
242        "Power Macintosh") ARCH=ppc;;
243     esac
244 }
245
246 write_test_program() {
247     #! Must be 'echo'
248     echo "#include <stdio.h>" > $C_WORD.c
249     echo "int main(){printf(\"%ld\", (long)(8*sizeof(void*))); return 0; }" >> $C_WORD.c
250 }
251
252 c_find_word_size() {
253     $ECHO "Finding WORD..."
254     C_WORD=factor-word-size
255     write_test_program
256     gcc -o $C_WORD $C_WORD.c
257     WORD=$(./$C_WORD)
258     check_ret $C_WORD
259     $DELETE -f $C_WORD*
260 }
261
262 intel_macosx_word_size() {
263     ensure_program_installed sysctl
264     $ECHO -n "Testing if your Intel Mac supports 64bit binaries..."
265     sysctl machdep.cpu.extfeatures | grep EM64T >/dev/null
266     if [[ $? -eq 0 ]] ; then
267         WORD=64
268         $ECHO "yes!"
269     else
270         WORD=32
271         $ECHO "no."
272     fi
273 }
274
275 find_word_size() {
276     if [[ -n $WORD ]] ; then return; fi
277     if [[ $OS == macosx && $ARCH == x86 ]] ; then
278         intel_macosx_word_size
279     else
280         c_find_word_size
281     fi
282 }
283
284 set_factor_binary() {
285     case $OS in
286         windows) FACTOR_BINARY=factor.com;;
287         *) FACTOR_BINARY=factor;;
288     esac
289 }
290
291 set_factor_library() {
292     case $OS in
293         windows) FACTOR_LIBRARY=factor.dll;;
294         macosx) FACTOR_LIBRARY=libfactor.dylib;;
295         *) FACTOR_LIBRARY=libfactor.a;;
296     esac
297 }
298
299 set_factor_image() {
300     FACTOR_IMAGE=factor.image
301     FACTOR_IMAGE_FRESH=factor.image.fresh
302 }
303
304 echo_build_info() {
305     $ECHO OS=$OS
306     $ECHO ARCH=$ARCH
307     $ECHO WORD=$WORD
308     $ECHO FACTOR_BINARY=$FACTOR_BINARY
309     $ECHO FACTOR_LIBRARY=$FACTOR_LIBRARY
310     $ECHO FACTOR_IMAGE=$FACTOR_IMAGE
311     $ECHO MAKE_TARGET=$MAKE_TARGET
312     $ECHO BOOT_IMAGE=$BOOT_IMAGE
313     $ECHO MAKE_IMAGE_TARGET=$MAKE_IMAGE_TARGET
314     $ECHO GIT_PROTOCOL=$GIT_PROTOCOL
315     $ECHO GIT_URL=$GIT_URL
316     $ECHO DOWNLOADER=$DOWNLOADER
317     $ECHO CC=$CC
318     $ECHO MAKE=$MAKE
319     $ECHO COPY=$COPY
320     $ECHO DELETE=$DELETE
321 }
322
323 check_os_arch_word() {
324     if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
325         $ECHO "OS: $OS"
326         $ECHO "ARCH: $ARCH"
327         $ECHO "WORD: $WORD"
328         $ECHO "OS, ARCH, or WORD is empty.  Please report this."
329
330         $ECHO $MAKE_TARGET
331         exit_script 5
332     fi
333 }
334
335 set_build_info() {
336     check_os_arch_word
337     if [[ $OS == macosx && $ARCH == ppc ]] ; then
338         MAKE_IMAGE_TARGET=macosx-ppc
339         MAKE_TARGET=macosx-ppc
340     elif [[ $OS == linux && $ARCH == ppc ]] ; then
341         MAKE_IMAGE_TARGET=linux-ppc.32
342         MAKE_TARGET=linux-ppc-32
343     elif [[ $OS == windows && $ARCH == x86 && $WORD == 64 ]] ; then
344         MAKE_IMAGE_TARGET=windows-x86.64
345         MAKE_TARGET=windows-x86-64
346     elif [[ $OS == windows && $ARCH == x86 && $WORD == 32 ]] ; then
347         MAKE_IMAGE_TARGET=windows-x86.32
348         MAKE_TARGET=windows-x86-32
349     elif [[ $ARCH == x86 && $WORD == 64 ]] ; then
350         MAKE_IMAGE_TARGET=unix-x86.64
351         MAKE_TARGET=$OS-x86-64
352     elif [[ $ARCH == x86 && $WORD == 32 ]] ; then
353         MAKE_IMAGE_TARGET=unix-x86.32
354         MAKE_TARGET=$OS-x86-32
355     else
356         MAKE_IMAGE_TARGET=$ARCH.$WORD
357         MAKE_TARGET=$OS-$ARCH-$WORD
358     fi
359     BOOT_IMAGE=boot.$MAKE_IMAGE_TARGET.image
360 }
361
362 parse_build_info() {
363     ensure_program_installed cut
364     $ECHO "Parsing make target from command line: $1"
365     OS=`echo $1 | cut -d '-' -f 1`
366     ARCH=`echo $1 | cut -d '-' -f 2`
367     WORD=`echo $1 | cut -d '-' -f 3`
368     
369     if [[ $OS == linux && $ARCH == ppc ]] ; then WORD=32; fi
370     if [[ $OS == linux && $ARCH == arm ]] ; then WORD=32; fi
371     if [[ $OS == macosx && $ARCH == ppc ]] ; then WORD=32; fi
372     
373     $ECHO "OS=$OS"
374     $ECHO "ARCH=$ARCH"
375     $ECHO "WORD=$WORD"
376 }
377
378 find_build_info() {
379     find_os
380     find_architecture
381     find_word_size
382     set_factor_binary
383     set_factor_library
384     set_factor_image
385     set_build_info
386     set_downloader
387     set_gcc
388     set_make
389     echo_build_info
390 }
391
392 invoke_git() {
393     git $*
394     check_ret git
395 }
396
397 git_clone() {
398     $ECHO "Downloading the git repository from factorcode.org..."
399     invoke_git clone $GIT_URL
400 }
401
402 update_script_name() {
403     $ECHO `dirname $0`/_update.sh
404 }
405
406 update_script() {
407     update_script=`update_script_name`
408     bash_path=`which bash`
409     $ECHO "#!$bash_path" >"$update_script"
410     $ECHO "git pull \"$GIT_URL\" master" >>"$update_script"
411     $ECHO "if [[ \$? -eq 0 ]]; then exec \"$0\" $SCRIPT_ARGS; else echo \"git pull failed\"; exit 2; fi" \
412         >>"$update_script"
413     $ECHO "exit 0" >>"$update_script"
414
415     chmod 755 "$update_script"
416     exec "$update_script"
417 }
418
419 update_script_changed() {
420     invoke_git diff --stat `invoke_git merge-base HEAD FETCH_HEAD` FETCH_HEAD | grep 'build-support.factor\.sh' >/dev/null 
421 }
422
423 git_fetch_factorcode() {
424     $ECHO "Fetching the git repository from factorcode.org..."
425
426     rm -f `update_script_name`
427     invoke_git fetch "$GIT_URL" master
428
429     if update_script_changed; then
430         $ECHO "Updating and restarting the factor.sh script..."
431         update_script
432     else
433         $ECHO "Updating the working tree..."
434         invoke_git pull "$GIT_URL" master
435     fi
436 }
437
438 cd_factor() {
439     cd factor
440     check_ret cd
441 }
442
443 set_copy() {
444     case $OS in
445         windows) COPY=cp;;
446         *) COPY=cp;;
447     esac
448 }
449
450 set_delete() {
451     case $OS in
452         windows) DELETE=rm;;
453         *) DELETE=rm;;
454     esac
455 }
456
457 backup_factor() {
458     $ECHO "Backing up factor..."
459     $COPY $FACTOR_BINARY $FACTOR_BINARY.bak
460     $COPY $FACTOR_LIBRARY $FACTOR_LIBRARY.bak
461     $COPY $BOOT_IMAGE $BOOT_IMAGE.bak
462     $COPY $FACTOR_IMAGE $FACTOR_IMAGE.bak
463     $ECHO "Done with backup."
464 }
465
466 check_makefile_exists() {
467     if [[ ! -e "GNUmakefile" ]] ; then
468         $ECHO ""
469         $ECHO "***GNUmakefile not found***"
470         $ECHO "You are likely in the wrong directory."
471         $ECHO "Run this script from your factor directory:"
472         $ECHO "     ./build-support/factor.sh"
473         exit_script 6
474     fi
475 }
476
477 invoke_make() {
478     check_makefile_exists
479     $MAKE $MAKE_OPTS $*
480     check_ret $MAKE
481 }
482
483 make_clean() {
484     invoke_make clean
485 }
486
487 make_factor() {
488     invoke_make NO_UI=$NO_UI $MAKE_TARGET -j5
489 }
490
491 make_clean_factor() {
492     make_clean
493     make_factor
494 }
495
496 update_boot_images() {
497     $ECHO "Deleting old images..."
498     $DELETE checksums.txt* > /dev/null 2>&1
499     # delete boot images with one or two characters after the dot
500     $DELETE $BOOT_IMAGE.{?,??} > /dev/null 2>&1
501     $DELETE temp/staging.*.image > /dev/null 2>&1
502     if [[ -f $BOOT_IMAGE ]] ; then
503         get_url http://downloads.factorcode.org/images/latest/checksums.txt
504         factorcode_md5=`cat checksums.txt|grep $BOOT_IMAGE|cut -f2 -d' '`;
505         set_md5sum
506         disk_md5=`$MD5SUM $BOOT_IMAGE|cut -f1 -d' '` ;;
507         $ECHO "Factorcode md5: $factorcode_md5";
508         $ECHO "Disk md5: $disk_md5";
509         if [[ "$factorcode_md5" == "$disk_md5" ]] ; then
510             $ECHO "Your disk boot image matches the one on factorcode.org."
511         else
512             $DELETE $BOOT_IMAGE > /dev/null 2>&1
513             get_boot_image;
514         fi
515     else
516         get_boot_image
517     fi
518 }
519
520 get_boot_image() {
521     $ECHO "Downloading boot image $BOOT_IMAGE."
522     get_url http://downloads.factorcode.org/images/latest/$BOOT_IMAGE
523 }
524
525 get_url() {
526     if [[ -z $DOWNLOADER ]] ; then
527         set_downloader;
528     fi
529     $ECHO $DOWNLOADER $1 ;
530     $DOWNLOADER $1
531     check_ret $DOWNLOADER
532 }
533
534 get_config_info() {
535     find_build_info
536     check_installed_programs
537     check_libraries
538 }
539
540 copy_fresh_image() {
541     $ECHO "Copying $FACTOR_IMAGE to $FACTOR_IMAGE_FRESH..."
542     $COPY $FACTOR_IMAGE $FACTOR_IMAGE_FRESH
543 }
544
545 bootstrap() {
546     ./$FACTOR_BINARY -i=$BOOT_IMAGE
547     copy_fresh_image
548 }
549
550 install() {
551     check_factor_exists
552     get_config_info
553     git_clone
554     cd_factor
555     make_factor
556     get_boot_image
557     bootstrap
558 }
559
560 update() {
561     get_config_info
562     git_fetch_factorcode
563     backup_factor
564     make_clean_factor
565 }
566
567 update_bootstrap() {
568     update_boot_images
569     bootstrap
570 }
571
572 net_bootstrap_no_pull() {
573     get_config_info
574     make_clean_factor
575     update_bootstrap
576 }
577
578 refresh_image() {
579     ./$FACTOR_BINARY -script -e="USING: vocabs.loader vocabs.refresh system memory ; refresh-all save 0 exit"
580     check_ret factor
581 }
582
583 make_boot_image() {
584     ./$FACTOR_BINARY -script -e="\"$MAKE_IMAGE_TARGET\" USING: system bootstrap.image memory ; make-image save 0 exit"
585     check_ret factor
586 }
587
588 install_deps_linux() {
589     sudo apt-get --yes install libc6-dev libpango1.0-dev libx11-dev xorg-dev libgtk2.0-dev gtk2-engines-pixbuf libgtkglext1-dev wget git-core git-doc rlwrap gcc make
590     check_ret sudo
591 }
592
593 install_deps_macosx() {
594     test_program_installed git
595     if [[ $? -ne 1 ]] ; then
596         ensure_program_installed yes
597         $ECHO "git not found."
598         $ECHO "This script requires either git-core or port."
599         $ECHO "If it fails, install git-core or port and try again."
600         ensure_program_installed port
601         $ECHO "Installing git-core with port...this will take awhile."
602         yes | sudo port install git-core
603     fi
604 }
605
606 usage() {
607     $ECHO "usage: $0 install|deps-linux|deps-macosx|self-update|quick-update|update|bootstrap|net-bootstrap|make-target|report [optional-target]"
608     $ECHO "If you are behind a firewall, invoke as:"
609     $ECHO "env GIT_PROTOCOL=http $0 <command>"
610     $ECHO ""
611     $ECHO "Example for overriding the default target:"
612     $ECHO "    $0 update macosx-x86-32"
613 }
614
615 MAKE_TARGET=unknown
616
617 # -n is nonzero length, -z is zero length
618 if [[ -n "$2" ]] ; then
619     parse_build_info $2
620 fi
621
622 set_copy
623 set_delete
624
625 case "$1" in
626     install) install ;;
627     deps-linux) install_deps_linux ;;
628     deps-macosx) install_deps_macosx ;;
629     self-update) update; make_boot_image; bootstrap;;
630     quick-update) update; refresh_image ;;
631     update) update; update_bootstrap ;;
632     bootstrap) get_config_info; bootstrap ;;
633     net-bootstrap) net_bootstrap_no_pull ;;
634     make-target) FIND_MAKE_TARGET=true; ECHO=false; find_build_info; exit_script ;;
635     report) find_build_info ;;
636     *) usage ;;
637 esac