]> gitweb.factorcode.org Git - factor.git/blob - build-support/factor.sh
Merge branch 'master' of git://factorcode.org/git/factor
[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 -eq true ]] ; then
28         echo $MAKE_TARGET;
29     fi
30     exit $1
31 }
32
33 ensure_program_installed() {
34     installed=0;
35     for i in $* ;
36     do
37         $ECHO -n "Checking for $i..."
38         test_program_installed $i
39         if [[ $? -eq 0 ]]; then
40             echo -n "not "
41         else    
42             installed=$(( $installed + 1 ))
43         fi
44         $ECHO "found!"
45     done
46     if [[ $installed -eq 0 ]] ; then
47         $ECHO -n "Install "
48         if [[ $# -eq 1 ]] ; then
49             $ECHO -n $1
50         else
51             $ECHO -n "any of [ $* ]"
52         fi
53         $ECHO " and try again."
54         exit_script 1;
55     fi
56 }
57
58 check_ret() {
59     RET=$?
60     if [[ $RET -ne 0 ]] ; then
61        $ECHO $1 failed
62        exit_script 2
63     fi
64 }
65
66 set_downloader() {
67     test_program_installed wget curl
68     if [[ $? -ne 0 ]] ; then
69         DOWNLOADER=wget
70     else
71         DOWNLOADER="curl -O"
72     fi
73 }
74
75 set_md5sum() {
76     test_program_installed md5sum
77     if [[ $? -ne 0 ]] ; then
78         MD5SUM=md5sum
79     else
80         MD5SUM="md5 -r"
81     fi
82 }
83
84 set_gcc() {
85     case $OS in
86         openbsd) ensure_program_installed egcc; CC=egcc;;
87         *) CC=gcc;;
88     esac
89 }
90
91 set_make() {
92     case $OS in
93         netbsd) MAKE='gmake';;
94         freebsd) MAKE='gmake';;
95         openbsd) MAKE='gmake';;
96         dragonflybsd) MAKE='gmake';;
97         *) MAKE='make';;
98     esac
99     if ! [[ $MAKE -eq 'gmake' ]] ; then
100         ensure_program_installed gmake
101     fi
102 }
103
104 check_installed_programs() {
105     ensure_program_installed chmod
106     ensure_program_installed uname
107     ensure_program_installed git
108     ensure_program_installed wget curl
109     ensure_program_installed gcc
110     ensure_program_installed make gmake
111     ensure_program_installed md5sum md5
112     ensure_program_installed cut
113 }
114
115 check_library_exists() {
116     GCC_TEST=factor-library-test.c
117     GCC_OUT=factor-library-test.out
118     $ECHO -n "Checking for library $1..."
119     $ECHO "int main(){return 0;}" > $GCC_TEST
120     $CC $GCC_TEST -o $GCC_OUT -l $1
121     if [[ $? -ne 0 ]] ; then
122         $ECHO "not found!"
123         $ECHO "Warning: library $1 not found."
124         $ECHO "***Factor will compile NO_UI=1"
125         NO_UI=1
126     fi
127     $DELETE -f $GCC_TEST
128     check_ret $DELETE
129     $DELETE -f $GCC_OUT
130     check_ret $DELETE
131     $ECHO "found."
132 }
133
134 check_X11_libraries() {
135     if [ -z "$NO_UI" ]; then
136         check_library_exists GL
137         check_library_exists X11
138         check_library_exists pango-1.0
139     fi
140 }
141
142 check_libraries() {
143     case $OS in
144             linux) check_X11_libraries;;
145     esac
146 }
147
148 check_factor_exists() {
149     if [[ -d "factor" ]] ; then
150         $ECHO "A directory called 'factor' already exists."
151         $ECHO "Rename or delete it and try again."
152         exit_script 4
153     fi
154 }
155
156 find_os() {
157     if [[ -n $OS ]] ; then return; fi
158     $ECHO "Finding OS..."
159     uname_s=`uname -s`
160     check_ret uname
161     case $uname_s in
162         CYGWIN_NT-5.2-WOW64) OS=winnt;;
163         *CYGWIN_NT*) OS=winnt;;
164         *CYGWIN*) OS=winnt;;
165         MINGW32*) OS=winnt;;
166         *darwin*) OS=macosx;;
167         *Darwin*) OS=macosx;;
168         *linux*) OS=linux;;
169         *Linux*) OS=linux;;
170         *NetBSD*) OS=netbsd;;
171         *FreeBSD*) OS=freebsd;;
172         *OpenBSD*) OS=openbsd;;
173         *DragonFly*) OS=dragonflybsd;;
174         SunOS) OS=solaris;;
175     esac
176 }
177
178 find_architecture() {
179     if [[ -n $ARCH ]] ; then return; fi
180     $ECHO "Finding ARCH..."
181     uname_m=`uname -m`
182     check_ret uname
183     case $uname_m in
184        i386) ARCH=x86;;
185        i686) ARCH=x86;;
186        i86pc) ARCH=x86;;
187        amd64) ARCH=x86;;
188        ppc64) ARCH=ppc;;
189        *86) ARCH=x86;;
190        *86_64) ARCH=x86;;
191        "Power Macintosh") ARCH=ppc;;
192     esac
193 }
194
195 write_test_program() {
196     echo "#include <stdio.h>" > $C_WORD.c
197     echo "int main(){printf(\"%ld\", (long)(8*sizeof(void*))); return 0; }" >> $C_WORD.c
198 }
199
200 c_find_word_size() {
201     $ECHO "Finding WORD..."
202     C_WORD=factor-word-size
203     write_test_program
204     gcc -o $C_WORD $C_WORD.c
205     WORD=$(./$C_WORD)
206     check_ret $C_WORD
207     $DELETE -f $C_WORD*
208 }
209
210 intel_macosx_word_size() {
211     ensure_program_installed sysctl
212     $ECHO -n "Testing if your Intel Mac supports 64bit binaries..."
213     sysctl machdep.cpu.extfeatures | grep EM64T >/dev/null
214     if [[ $? -eq 0 ]] ; then
215         WORD=64
216         $ECHO "yes!"
217     else
218         WORD=32
219         $ECHO "no."
220     fi
221 }
222
223 find_word_size() {
224     if [[ -n $WORD ]] ; then return; fi
225     if [[ $OS == macosx && $ARCH == x86 ]] ; then
226         intel_macosx_word_size
227     else
228         c_find_word_size
229     fi
230 }
231
232 set_factor_binary() {
233     case $OS in
234         winnt) FACTOR_BINARY=factor.com;;
235         *) FACTOR_BINARY=factor;;
236     esac
237 }
238
239 set_factor_library() {
240     case $OS in
241         winnt) FACTOR_LIBRARY=factor.dll;;
242         macosx) FACTOR_LIBRARY=libfactor.dylib;;
243         *) FACTOR_LIBRARY=libfactor.a;;
244     esac
245 }
246
247 set_factor_image() {
248     FACTOR_IMAGE=factor.image
249 }
250
251 echo_build_info() {
252     $ECHO OS=$OS
253     $ECHO ARCH=$ARCH
254     $ECHO WORD=$WORD
255     $ECHO FACTOR_BINARY=$FACTOR_BINARY
256     $ECHO FACTOR_LIBRARY=$FACTOR_LIBRARY
257     $ECHO FACTOR_IMAGE=$FACTOR_IMAGE
258     $ECHO MAKE_TARGET=$MAKE_TARGET
259     $ECHO BOOT_IMAGE=$BOOT_IMAGE
260     $ECHO MAKE_IMAGE_TARGET=$MAKE_IMAGE_TARGET
261     $ECHO GIT_PROTOCOL=$GIT_PROTOCOL
262     $ECHO GIT_URL=$GIT_URL
263     $ECHO DOWNLOADER=$DOWNLOADER
264     $ECHO CC=$CC
265     $ECHO MAKE=$MAKE
266     $ECHO COPY=$COPY
267     $ECHO DELETE=$DELETE
268 }
269
270 check_os_arch_word() {
271     if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
272         $ECHO "OS: $OS"
273         $ECHO "ARCH: $ARCH"
274         $ECHO "WORD: $WORD"
275         $ECHO "OS, ARCH, or WORD is empty.  Please report this."
276
277         echo $MAKE_TARGET
278         exit_script 5
279     fi
280 }
281
282 set_build_info() {
283     check_os_arch_word
284     if [[ $OS == macosx && $ARCH == ppc ]] ; then
285         MAKE_IMAGE_TARGET=macosx-ppc
286         MAKE_TARGET=macosx-ppc
287     elif [[ $OS == linux && $ARCH == ppc ]] ; then
288         MAKE_IMAGE_TARGET=linux-ppc
289         MAKE_TARGET=linux-ppc
290     elif [[ $OS == winnt && $ARCH == x86 && $WORD == 64 ]] ; then
291         MAKE_IMAGE_TARGET=winnt-x86.64
292         MAKE_TARGET=winnt-x86-64
293     elif [[ $ARCH == x86 && $WORD == 64 ]] ; then
294         MAKE_IMAGE_TARGET=unix-x86.64
295         MAKE_TARGET=$OS-x86-64
296     else
297         MAKE_IMAGE_TARGET=$ARCH.$WORD
298         MAKE_TARGET=$OS-$ARCH-$WORD
299     fi
300     BOOT_IMAGE=boot.$MAKE_IMAGE_TARGET.image
301 }
302
303 parse_build_info() {
304     ensure_program_installed cut
305     $ECHO "Parsing make target from command line: $1"
306     OS=`echo $1 | cut -d '-' -f 1`
307     ARCH=`echo $1 | cut -d '-' -f 2`
308     WORD=`echo $1 | cut -d '-' -f 3`
309     
310     if [[ $OS == linux && $ARCH == ppc ]] ; then WORD=32; fi
311     if [[ $OS == linux && $ARCH == arm ]] ; then WORD=32; fi
312     if [[ $OS == macosx && $ARCH == ppc ]] ; then WORD=32; fi
313     if [[ $OS == wince && $ARCH == arm ]] ; then WORD=32; fi
314     
315     $ECHO "OS=$OS"
316     $ECHO "ARCH=$ARCH"
317     $ECHO "WORD=$WORD"
318 }
319
320 find_build_info() {
321     find_os
322     find_architecture
323     find_word_size
324     set_factor_binary
325     set_factor_library
326     set_factor_image
327     set_build_info
328     set_downloader
329     set_gcc
330     set_make
331     echo_build_info
332 }
333
334 invoke_git() {
335     git $*
336     check_ret git
337 }
338
339 git_clone() {
340     echo "Downloading the git repository from factorcode.org..."
341     invoke_git clone $GIT_URL
342 }
343
344 update_script_name() {
345     echo `dirname $0`/_update.sh
346 }
347
348 update_script() {
349     update_script=`update_script_name`
350     bash_path=`which bash`
351     echo "#!$bash_path" >"$update_script"
352     echo "git pull \"$GIT_URL\" master" >>"$update_script"
353     echo "if [[ \$? -eq 0 ]]; then exec \"$0\" $SCRIPT_ARGS; else echo \"git pull failed\"; exit 2; fi" \
354         >>"$update_script"
355     echo "exit 0" >>"$update_script"
356
357     chmod 755 "$update_script"
358     exec "$update_script"
359 }
360
361 update_script_changed() {
362     invoke_git diff --stat `invoke_git merge-base HEAD FETCH_HEAD` FETCH_HEAD | grep 'build-support.factor\.sh' >/dev/null 
363 }
364
365 git_fetch_factorcode() {
366     echo "Fetching the git repository from factorcode.org..."
367
368     rm -f `update_script_name`
369     invoke_git fetch "$GIT_URL" master
370
371     if update_script_changed; then
372         echo "Updating and restarting the factor.sh script..."
373         update_script
374     else
375         echo "Updating the working tree..."
376         invoke_git pull "$GIT_URL" master
377     fi
378 }
379
380 cd_factor() {
381     cd factor
382     check_ret cd
383 }
384
385 set_copy() {
386     case $OS in
387         winnt) COPY=cp;;
388         *) COPY=cp;;
389     esac
390 }
391
392 set_delete() {
393     case $OS in
394         winnt) DELETE=rm;;
395         *) DELETE=rm;;
396     esac
397 }
398
399 backup_factor() {
400     $ECHO "Backing up factor..."
401     $COPY $FACTOR_BINARY $FACTOR_BINARY.bak
402     $COPY $FACTOR_LIBRARY $FACTOR_LIBRARY.bak
403     $COPY $BOOT_IMAGE $BOOT_IMAGE.bak
404     $COPY $FACTOR_IMAGE $FACTOR_IMAGE.bak
405     $ECHO "Done with backup."
406 }
407
408 check_makefile_exists() {
409     if [[ ! -e "GNUmakefile" ]] ; then
410         echo ""
411         echo "***GNUmakefile not found***"
412         echo "You are likely in the wrong directory."
413         echo "Run this script from your factor directory:"
414         echo "     ./build-support/factor.sh"
415         exit_script 6
416     fi
417 }
418
419 invoke_make() {
420     check_makefile_exists
421     $MAKE $MAKE_OPTS $*
422     check_ret $MAKE
423 }
424
425 make_clean() {
426     invoke_make clean
427 }
428
429 make_factor() {
430     invoke_make NO_UI=$NO_UI $MAKE_TARGET -j5
431 }
432
433 update_boot_images() {
434     echo "Deleting old images..."
435     $DELETE checksums.txt* > /dev/null 2>&1
436     # delete boot images with one or two characters after the dot
437     $DELETE $BOOT_IMAGE.{?,??} > /dev/null 2>&1
438     $DELETE temp/staging.*.image > /dev/null 2>&1
439     if [[ -f $BOOT_IMAGE ]] ; then
440         get_url http://factorcode.org/images/latest/checksums.txt
441         factorcode_md5=`cat checksums.txt|grep $BOOT_IMAGE|cut -f2 -d' '`;
442         set_md5sum
443         case $OS in
444              netbsd) disk_md5=`md5 $BOOT_IMAGE | cut -f4 -d' '`;;
445              *) disk_md5=`$MD5SUM $BOOT_IMAGE|cut -f1 -d' '` ;;
446         esac
447         echo "Factorcode md5: $factorcode_md5";
448         echo "Disk md5: $disk_md5";
449         if [[ "$factorcode_md5" == "$disk_md5" ]] ; then
450             echo "Your disk boot image matches the one on factorcode.org."
451         else
452             $DELETE $BOOT_IMAGE > /dev/null 2>&1
453             get_boot_image;
454         fi
455     else
456         get_boot_image
457     fi
458 }
459
460 get_boot_image() {
461     echo "Downloading boot image $BOOT_IMAGE."
462     get_url http://factorcode.org/images/latest/$BOOT_IMAGE
463 }
464
465 get_url() {
466     if [[ $DOWNLOADER -eq "" ]] ; then
467         set_downloader;
468     fi
469     echo $DOWNLOADER $1 ;
470     $DOWNLOADER $1
471     check_ret $DOWNLOADER
472 }
473
474 get_config_info() {
475     find_build_info
476     check_installed_programs
477     check_libraries
478 }
479
480 bootstrap() {
481     ./$FACTOR_BINARY -i=$BOOT_IMAGE
482 }
483
484 install() {
485     check_factor_exists
486     get_config_info
487     git_clone
488     cd_factor
489     make_factor
490     get_boot_image
491     bootstrap
492 }
493
494
495 update() {
496     get_config_info
497     git_fetch_factorcode
498     backup_factor
499     make_clean
500     make_factor
501 }
502
503 update_bootstrap() {
504     update_boot_images
505     bootstrap
506 }
507
508 refresh_image() {
509     ./$FACTOR_BINARY -script -e="USING: vocabs.loader system memory ; refresh-all USE: memory save 0 exit"
510     check_ret factor
511 }
512
513 make_boot_image() {
514     ./$FACTOR_BINARY -script -e="\"$MAKE_IMAGE_TARGET\" USING: system bootstrap.image memory ; make-image save 0 exit"
515     check_ret factor
516
517 }
518
519 install_build_system_apt() {
520     sudo apt-get --yes install libc6-dev libpango1.0-dev libx11-dev xorg-dev wget git-core git-doc rlwrap gcc make
521     check_ret sudo
522 }
523
524 install_build_system_port() {
525     test_program_installed git
526     if [[ $? -ne 1 ]] ; then
527         ensure_program_installed yes
528         echo "git not found."
529         echo "This script requires either git-core or port."
530         echo "If it fails, install git-core or port and try again."
531         ensure_program_installed port
532         echo "Installing git-core with port...this will take awhile."
533         yes | sudo port install git-core
534     fi
535 }
536
537 usage() {
538     echo "usage: $0 install|install-x11|install-macosx|self-update|quick-update|update|bootstrap|dlls|net-bootstrap|make-target|report [optional-target]"
539     echo "If you are behind a firewall, invoke as:"
540     echo "env GIT_PROTOCOL=http $0 <command>"
541     echo ""
542     echo "Example for overriding the default target:"
543     echo "    $0 update macosx-x86-32"
544 }
545
546 MAKE_TARGET=unknown
547
548 # -n is nonzero length, -z is zero length
549 if [[ -n "$2" ]] ; then
550     parse_build_info $2
551 fi
552
553 set_copy
554 set_delete
555
556 case "$1" in
557     install) install ;;
558     install-x11) install_build_system_apt; install ;;
559     install-macosx) install_build_system_port; install ;;
560     self-update) update; make_boot_image; bootstrap;;
561     quick-update) update; refresh_image ;;
562     update) update; update_bootstrap ;;
563     bootstrap) get_config_info; bootstrap ;;
564     report) find_build_info ;;
565     net-bootstrap) get_config_info; update_boot_images; bootstrap ;;
566     make-target) FIND_MAKE_TARGET=true; ECHO=false; find_build_info; exit_script ;;
567     *) usage ;;
568 esac