]> gitweb.factorcode.org Git - factor.git/blob - build.sh
sequences: length-operator-last is not good, remove it
[factor.git] / build.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 GIT_PROTOCOL=${GIT_PROTOCOL:="https"}
15 GIT_URL=${GIT_URL:=$GIT_PROTOCOL"://github.com/factor/factor.git"}
16 SCRIPT_ARGS="$*"
17
18 REQUIRE_CLANG_VERSION=3.1
19
20 # return 1 on found
21 test_program_installed() {
22     if ! [[ -n $(type -p $1) ]] ; then
23         return 0;
24     fi
25     return 1;
26 }
27
28 # return 1 on found
29 test_programs_installed() {
30     local installed=0;
31     $ECHO -n "Checking for all($*)..."
32     for i in "$@" ;
33     do
34         test_program_installed $i
35         if [[ $? -eq 1 ]]; then
36             installed=$(( $installed + 1 ))
37         fi
38     done
39     if [[ $installed -eq $# ]] ; then
40         $ECHO "found!"
41         return 1
42     else
43         $ECHO "all not found."
44         return 0
45     fi
46 }
47
48 exit_script() {
49     if [[ $FIND_MAKE_TARGET = true ]] ; then
50         # Must be echo not $ECHO
51         echo $MAKE_TARGET;
52     fi
53     exit $1
54 }
55
56 ensure_program_installed() {
57     local installed=0;
58     $ECHO -n "Checking for any($*)..."
59     for i in "$@" ;
60     do
61         test_program_installed $i
62         if [[ $? -eq 1 ]]; then
63             $ECHO "found $i!"
64             installed=$(( $installed + 1 ))
65             return
66         fi
67     done
68     $ECHO "none found."
69     $ECHO -n "Install "
70     if [[ $# -eq 1 ]] ; then
71         $ECHO -n $1
72     else
73         $ECHO -n "any of [ $* ]"
74     fi
75     $ECHO " and try again."
76     if [[ $OS == macosx ]] ; then
77         $ECHO "If you have Xcode 4.3 or higher installed, you must install the"
78         $ECHO "Command Line Tools from Xcode Preferences > Downloads in order"
79         $ECHO "to build Factor."
80     fi
81     exit_script 1;
82 }
83
84 check_ret() {
85     # Can't execute any commands before saving $?
86     # $1 is the name of the command we are checking
87     RET=$?
88     if [[ $RET -ne 0 ]] ; then
89        $ECHO $1 failed
90        exit_script 2
91     fi
92 }
93
94 set_downloader() {
95     test_program_installed wget
96     if [[ $? -ne 0 ]] ; then
97         DOWNLOADER="wget -nd"
98         DOWNLOADER_NAME=wget
99         return
100     fi
101     test_program_installed curl
102     if [[ $? -ne 0 ]] ; then
103         DOWNLOADER="curl -L -f -O"
104         DOWNLOADER_NAME=curl
105         return
106     fi
107     $ECHO "error: wget or curl required"
108     exit_script 11
109 }
110
111 set_md5sum() {
112     test_program_installed md5sum
113     if [[ $? -ne 0 ]] ; then
114         MD5SUM=md5sum
115     else
116         MD5SUM="md5 -r"
117     fi
118 }
119
120 semver_into() {
121     RE_SEMVER="^([0-9]*)\.([0-9]*)\.([0-9]*)-?(.*)?$" # 3.3.3-5
122     CLANG_RE_OLD="^([0-9]*)\.([0-9]*)-?(.*)?$" # 3.3-5
123     if [[ $1 =~ $RE_SEMVER ]] ; then
124         export "$2=${BASH_REMATCH[1]}"
125         export "$3=${BASH_REMATCH[2]}"
126         export "$4=${BASH_REMATCH[3]}"
127         export "$5=${BASH_REMATCH[4]}"
128     elif [[ $1 =~ $CLANG_RE_OLD ]] ; then
129         export "$2=${BASH_REMATCH[1]}"
130         export "$3=${BASH_REMATCH[2]}"
131         export "$4=0"
132         export "$5=${BASH_REMATCH[3]}"
133     else
134         echo "unsupported version number, please report a bug: $1"
135         exit 123
136     fi
137 }
138
139 clang_version_ok() {
140     CLANG_VERSION=$(clang --version | head -n1)
141     CLANG_VERSION_RE='^[a-zA-Z0-9 ]* version (.*)$' # 3.3-5
142     if [[ $CLANG_VERSION =~ $CLANG_VERSION_RE ]] ; then
143         export "CLANG_VERSION=${BASH_REMATCH[1]}"
144         local CLANG_MAJOR CLANG_MINOR CLANG_PATCH CLANG_SPECIAL
145         semver_into "$CLANG_VERSION" CLANG_MAJOR CLANG_MINOR CLANG_PATCH CLANG_SPECIAL
146         if [[ $CLANG_MAJOR -lt 3
147             || ( $CLANG_MAJOR -eq 3 && $CLANG_MINOR -le 1 )
148             ]] ; then
149             echo "clang version required >= $REQUIRE_CLANG_VERSION, got $CLANG_VERSION"
150             return 1
151         fi
152     else
153         return 1
154     fi
155     return 0
156 }
157
158 set_cc() {
159     # on Cygwin we MUST use the MinGW "cross-compiler", therefore check these first
160     # furthermore, we prefer 64 bit over 32 bit versions if both are available
161
162     # we need this condition so we don't find a mingw32 compiler on linux
163     if [[ $OS == windows ]] ; then
164         test_programs_installed x86_64-w64-mingw32-gcc x86_64-w64-mingw32-g++
165         if [[ $? -ne 0 ]] ; then
166             [ -z "$CC" ] && CC=x86_64-w64-mingw32-gcc
167             [ -z "$CXX" ] && CXX=x86_64-w64-mingw32-g++
168             return
169         fi
170
171         test_programs_installed i686-w64-mingw32-gcc i686-w64-mingw32-g++
172         if [[ $? -ne 0 ]] ; then
173             [ -z "$CC" ] && CC=i686-w64-mingw32-gcc
174             [ -z "$CXX" ] && CXX=i686-w64-mingw32-g++
175             return
176         fi
177     fi
178
179     test_programs_installed clang clang++
180     if [[ $? -ne 0 ]] && clang_version_ok ; then
181         [ -z "$CC" ] && CC=clang
182         [ -z "$CXX" ] && CXX=clang++
183         return
184     fi
185
186     # gcc and g++ will fail to correctly build Factor on Cygwin
187     test_programs_installed gcc g++
188     if [[ $? -ne 0 ]] ; then
189         [ -z "$CC" ] && CC=gcc
190         [ -z "$CXX" ] && CXX=g++
191         return
192     fi
193
194     $ECHO "error: high enough version of either (clang/clang++) or (gcc/g++) required!"
195     exit_script 10
196 }
197
198 set_make() {
199     case $OS in
200         freebsd) MAKE=gmake ;;
201         *) MAKE=make ;;
202     esac
203     if [[ $MAKE = 'gmake' ]] ; then
204         ensure_program_installed gmake
205     fi
206 }
207
208 check_installed_programs() {
209     ensure_program_installed chmod
210     ensure_program_installed uname
211     ensure_program_installed git
212     ensure_program_installed wget curl
213     ensure_program_installed clang x86_64-w64-mingw32-gcc i686-w64-mingw32-gcc gcc
214     ensure_program_installed clang++ x86_64-w64-mingw32-g++ i686-w64-mingw32-g++ g++ cl
215     ensure_program_installed make gmake
216     ensure_program_installed md5sum md5
217     ensure_program_installed cut
218 }
219
220 check_library_exists() {
221     GCC_TEST=factor-library-test.c
222     GCC_OUT=factor-library-test.out
223     $ECHO -n "Checking for library $1..."
224     $ECHO "int main(){return 0;}" > $GCC_TEST
225     $CC $GCC_TEST -o $GCC_OUT -l $1 2>&-
226     if [[ $? -ne 0 ]] ; then
227         $ECHO "not found."
228     else
229         $ECHO "found."
230     fi
231     $DELETE -f $GCC_TEST
232     check_ret $DELETE
233     $DELETE -f $GCC_OUT
234     check_ret $DELETE
235 }
236
237 check_X11_libraries() {
238     check_library_exists GL
239     check_library_exists X11
240     check_library_exists pango-1.0
241 }
242
243 check_gtk_libraries() {
244     check_library_exists gobject-2.0
245     check_library_exists gtk-x11-2.0
246     check_library_exists gdk-x11-2.0
247     check_library_exists gdk_pixbuf-2.0
248     check_library_exists gtkglext-x11-1.0
249     check_library_exists atk-1.0
250     check_library_exists gio-2.0
251     check_library_exists gdkglext-x11-1.0
252     check_library_exists pango-1.0
253 }
254
255
256 check_libraries() {
257     case $OS in
258         linux) check_X11_libraries
259                check_gtk_libraries ;;
260         unix) check_gtk_libraries ;;
261     esac
262 }
263
264 check_factor_exists() {
265     if [[ -d "factor" ]] ; then
266         $ECHO "A directory called 'factor' already exists."
267         $ECHO "Rename or delete it and try again."
268         exit_script 4
269     fi
270 }
271
272 find_os() {
273     if [[ -n $OS ]] ; then return; fi
274     $ECHO "Finding OS..."
275     local uname_s=$(uname -s)
276     check_ret uname
277     case $uname_s in
278         CYGWIN_NT-5.2-WOW64) OS=windows ;;
279         *CYGWIN_NT*) OS=windows ;;
280         *CYGWIN*) OS=windows ;;
281         MINGW32*) OS=windows ;;
282         MINGW64*) OS=windows ;;
283         MSYS_NT*) OS=windows ;;
284         *darwin*) OS=macosx ;;
285         *Darwin*) OS=macosx ;;
286         *linux*) OS=linux ;;
287         *Linux*) OS=linux ;;
288         FreeBSD) OS=freebsd ;;
289         Haiku) OS=haiku ;;
290     esac
291 }
292
293 find_architecture() {
294     if [[ -n $ARCH ]] ; then return; fi
295     $ECHO "Finding ARCH..."
296     uname_m=$(uname -m)
297     check_ret uname
298     case $uname_m in
299        i386) ARCH=x86 ;;
300        i686) ARCH=x86 ;;
301        i86pc) ARCH=x86 ;;
302        amd64) ARCH=x86 ;;
303        ppc64) ARCH=ppc ;;
304        *86) ARCH=x86 ;;
305        *86_64) ARCH=x86 ;;
306        aarch64) ARCH=arm64 ;;
307        arm64) ARCH=arm64 ;;
308        iPhone5*[3-9]) ARCH=arm64 ;;
309        iPhone[6-9]*) ARCH=arm64 ;;
310        iPhone[1-9][0-9]*) ARCH=arm64 ;;
311        iPad[4-9]*) ARCH=arm64 ;;
312        iPad[1-9][0-9]*) ARCH=arm64 ;;
313        AppleTV[5-9]*) ARCH=arm64 ;;
314        AppleTV[1-9][0-9]*) ARCH=arm64 ;;
315        "Power Macintosh") ARCH=ppc ;;
316     esac
317 }
318
319 find_num_cores() {
320     $ECHO "Finding NUM_CORES..."
321     NUM_CORES=1
322     uname_s=$(uname -s)
323     check_ret uname
324     case $uname_s in
325         CYGWIN_NT-5.2-WOW64 | *CYGWIN_NT* | *CYGWIN* | MINGW32*) NUM_CORES=$NUMBER_OF_PROCESSORS ;;
326         *linux* | *Linux*) NUM_CORES=$(getconf _NPROCESSORS_ONLN || nproc) ;;
327         *darwin* | *Darwin* | freebsd) NUM_CORES=$(sysctl -n hw.ncpu) ;;
328     esac
329 }
330
331 find_word_size() {
332     if [[ -n $WORD ]] ; then return; fi
333     $ECHO "Finding WORD..."
334     WORD=$(getconf LONG_BIT || find_word_size_cpp || find_word_size_c)
335 }
336
337 find_word_size_cpp() {
338     SIXTY_FOUR='defined(__aarch64__) || defined(__x86_64__) || defined(_M_AMD64) || defined(__PPC64__) || defined(__64BIT__)'
339     THIRTY_TWO='defined(i386) || defined(__i386) || defined(__i386__) || defined(_MIX86)'
340     $CC -E -xc <(echo -e "#if ${SIXTY_FOUR}\n64\n#elif ${THIRTY_TWO}\n32\n#endif") | tail -1
341 }
342
343 find_word_size_c() {
344     C_WORD="factor-word-size"
345     TEST_PROGRAM="int main(){ return (long)(8*sizeof(void*)); }"
346     echo $TEST_PROGRAM | $CC -o $C_WORD -xc -
347     check_ret $CC
348     ./$C_WORD
349     WORD_OUT=$?
350     case $WORD_OUT in
351         32) ;;
352         64) ;;
353         *)
354             echo "Word size should be 32/64, got '$WORD_OUT'"
355             exit_script 15 ;;
356     esac
357     $DELETE -f $C_WORD
358     echo "$WORD_OUT"
359 }
360
361 set_factor_binary() {
362     case $OS in
363         windows) FACTOR_BINARY=factor.com ;;
364         *) FACTOR_BINARY=factor ;;
365     esac
366 }
367
368 set_factor_library() {
369     case $OS in
370         windows) FACTOR_LIBRARY=factor.dll ;;
371         macosx) FACTOR_LIBRARY=libfactor.dylib ;;
372         *) FACTOR_LIBRARY=libfactor.a ;;
373     esac
374 }
375
376 set_factor_image() {
377     FACTOR_IMAGE=factor.image
378     FACTOR_IMAGE_FRESH=factor.image.fresh
379 }
380
381 echo_build_info() {
382     $ECHO OS=$OS
383     $ECHO ARCH=$ARCH
384     $ECHO NUM_CORES=$NUM_CORES
385     $ECHO WORD=$WORD
386     $ECHO DEBUG=$DEBUG
387     $ECHO REPRODUCIBLE=$REPRODUCIBLE
388     $ECHO CURRENT_BRANCH=$CURRENT_BRANCH
389     $ECHO FACTOR_BINARY=$FACTOR_BINARY
390     $ECHO FACTOR_LIBRARY=$FACTOR_LIBRARY
391     $ECHO FACTOR_IMAGE=$FACTOR_IMAGE
392     $ECHO MAKE_TARGET=$MAKE_TARGET
393     $ECHO BOOT_IMAGE=$BOOT_IMAGE
394     $ECHO MAKE_IMAGE_TARGET=$MAKE_IMAGE_TARGET
395     $ECHO GIT_PROTOCOL=$GIT_PROTOCOL
396     $ECHO GIT_URL=$GIT_URL
397     $ECHO CHECKSUM_URL=$CHECKSUM_URL
398     $ECHO BOOT_IMAGE_URL=$BOOT_IMAGE_URL
399     $ECHO DOWNLOADER=$DOWNLOADER
400     $ECHO CC=$CC
401     $ECHO CXX=$CXX
402     $ECHO MAKE=$MAKE
403     $ECHO COPY=$COPY
404     $ECHO DELETE=$DELETE
405 }
406
407 check_os_arch_word() {
408     if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
409         $ECHO "OS: $OS"
410         $ECHO "ARCH: $ARCH"
411         $ECHO "WORD: $WORD"
412         $ECHO "OS, ARCH, or WORD is empty.  Please report this."
413
414         $ECHO $MAKE_TARGET
415         exit_script 5
416     fi
417 }
418
419 set_build_info() {
420     check_os_arch_word
421     if [[ $OS == linux && $ARCH == ppc ]] ; then
422         MAKE_IMAGE_TARGET=linux-ppc.32
423         MAKE_TARGET=linux-ppc-32
424     elif [[ $OS == linux && $ARCH == arm64 ]] ; then
425         MAKE_IMAGE_TARGET=unix-arm.64
426         MAKE_TARGET=linux-arm-64
427     elif [[ $OS == macosx && $ARCH == arm64 ]] ; then
428         MAKE_IMAGE_TARGET=unix-arm.64
429         MAKE_TARGET=macosx-arm64
430     elif [[ $OS == windows && $ARCH == x86 && $WORD == 64 ]] ; then
431         MAKE_IMAGE_TARGET=windows-x86.64
432         MAKE_TARGET=windows-x86-64
433     elif [[ $OS == windows && $ARCH == x86 && $WORD == 32 ]] ; then
434         MAKE_IMAGE_TARGET=windows-x86.32
435         MAKE_TARGET=windows-x86-32
436     elif [[ $ARCH == x86 && $WORD == 64 ]] ; then
437         MAKE_IMAGE_TARGET=unix-x86.64
438         MAKE_TARGET=$OS-x86-64
439     elif [[ $ARCH == x86 && $WORD == 32 ]] ; then
440         MAKE_IMAGE_TARGET=unix-x86.32
441         MAKE_TARGET=$OS-x86-32
442     else
443         MAKE_IMAGE_TARGET=$ARCH.$WORD
444         MAKE_TARGET=$OS-$ARCH-$WORD
445     fi
446     BOOT_IMAGE=boot.$MAKE_IMAGE_TARGET.image
447 }
448
449 parse_build_info() {
450     ensure_program_installed cut
451     $ECHO "Parsing make target from command line: $1"
452     OS=$(echo $1 | cut -d '-' -f 1)
453     ARCH=$(echo $1 | cut -d '-' -f 2)
454     WORD=$(echo $1 | cut -d '-' -f 3)
455
456     if [[ $OS == linux && $ARCH == ppc ]] ; then WORD=32; fi
457     if [[ $OS == linux && $ARCH == arm ]] ; then WORD=32; fi
458     if [[ $OS == macosx && $ARCH == ppc ]] ; then WORD=32; fi
459
460     $ECHO "OS=$OS"
461     $ECHO "ARCH=$ARCH"
462     $ECHO "WORD=$WORD"
463 }
464
465 find_build_info() {
466     find_os
467     find_architecture
468     find_num_cores
469     set_cc
470     find_word_size
471     set_current_branch
472     set_factor_binary
473     set_factor_library
474     set_factor_image
475     set_build_info
476     set_downloader
477     set_boot_image_vars
478     set_make
479     echo_build_info
480 }
481
482 invoke_git() {
483     git "$@"
484     check_ret git
485 }
486
487 git_clone() {
488     $ECHO "Downloading the git repository from github.com..."
489     invoke_git clone $GIT_URL
490 }
491
492 update_script_name() {
493     $ECHO "$(dirname $0)/_update.sh"
494 }
495
496 update_script() {
497     local -r update_script=$(update_script_name)
498     local -r bash_path=$(which bash)
499     $ECHO "updating from ${CURRENT_BRANCH}"
500     $ECHO "#!$bash_path" >"$update_script"
501     $ECHO "git pull \"$GIT_URL\" ${CURRENT_BRANCH}" >>"$update_script"
502     $ECHO "if [[ \$? -eq 0 ]]; then exec \"$0\" $SCRIPT_ARGS; else echo \"git pull failed\"; exit 2; fi" \
503         >>"$update_script"
504     $ECHO "exit 0" >>"$update_script"
505
506     chmod 755 "$update_script"
507     exec "$update_script"
508 }
509
510 update_script_changed() {
511     invoke_git diff --stat "$(invoke_git merge-base HEAD FETCH_HEAD)" FETCH_HEAD | grep 'build\.sh' >/dev/null
512 }
513
514 git_fetch() {
515     $ECHO "Fetching the git repository from github.com..."
516     set_current_branch
517
518     rm -f "$(update_script_name)"
519     $ECHO git fetch "$GIT_URL" "${CURRENT_BRANCH}"
520     invoke_git fetch "$GIT_URL" "${CURRENT_BRANCH}"
521
522     if update_script_changed; then
523         $ECHO "Updating and restarting the build.sh script..."
524         update_script
525     else
526         $ECHO "Updating the working tree..."
527         invoke_git pull "$GIT_URL" "${CURRENT_BRANCH}"
528     fi
529 }
530
531 cd_factor() {
532     cd "factor"
533     check_ret cd
534 }
535
536 set_copy() {
537     case $OS in
538         windows) COPY=cp ;;
539         *) COPY=cp ;;
540     esac
541 }
542
543 set_delete() {
544     case $OS in
545         windows) DELETE=rm ;;
546         *) DELETE=rm ;;
547     esac
548 }
549
550 backup_factor() {
551     $ECHO "Backing up factor..."
552     $COPY $FACTOR_BINARY $FACTOR_BINARY.bak
553     $COPY $FACTOR_LIBRARY $FACTOR_LIBRARY.bak
554     $COPY $BOOT_IMAGE $BOOT_IMAGE.bak
555     $COPY $FACTOR_IMAGE $FACTOR_IMAGE.bak
556     $ECHO "Done with backup."
557 }
558
559 check_makefile_exists() {
560     if [[ ! -e "GNUmakefile" ]] ; then
561         $ECHO ""
562         $ECHO "***GNUmakefile not found***"
563         $ECHO "You are likely in the wrong directory."
564         $ECHO "Run this script from your factor directory:"
565         $ECHO "     ./build.sh"
566         exit_script 6
567     fi
568 }
569
570 invoke_make() {
571     check_makefile_exists
572     $MAKE $MAKE_OPTS "$@"
573     check_ret $MAKE
574 }
575
576 make_clean() {
577     invoke_make clean
578 }
579
580 make_factor() {
581     $ECHO "Building factor with $NUM_CORES cores"
582     invoke_make CC=$CC CXX=$CXX $MAKE_TARGET -j$NUM_CORES
583 }
584
585 make_clean_factor() {
586     make_clean
587     make_factor
588 }
589
590 current_git_branch() {
591     # git rev-parse --abbrev-ref HEAD # outputs HEAD for detached head
592     # outputs nothing for detached HEAD, which is fine for ``git fetch``
593     git describe --all --exact-match 2>/dev/null | sed 's=.*/=='
594 }
595
596 check_url() {
597     if [[ $DOWNLOADER_NAME == 'wget' ]]; then
598         if [[ $(wget -S --spider $1 2>&1 | grep 'HTTP/1.1 200 OK') ]]; then
599             return 0
600         else
601             return 1
602         fi
603     elif [[ $DOWNLOADER_NAME == 'curl' ]]; then
604         local code=$(curl -sL -w "%{http_code}\\n" "$1" -o /dev/null)
605         if [[ $code -eq 200 ]]; then return 0; else return 1; fi
606     else
607         $ECHO "error: wget or curl required in check_url"
608         exit_script 12
609     fi
610 }
611
612 # If we are on a branch, first try to get a boot image for that branch.
613 # Otherwise, just use `master`
614 set_boot_image_vars() {
615     set_current_branch
616     local url="https://downloads.factorcode.org/images/${CURRENT_BRANCH}/checksums.txt"
617     check_url $url
618     if [[ $? -eq 0 ]]; then
619         CHECKSUM_URL="$url"
620         BOOT_IMAGE_URL="https://downloads.factorcode.org/images/${CURRENT_BRANCH}/${BOOT_IMAGE}"
621     else
622         $ECHO "boot image for branch \`${CURRENT_BRANCH}\` is not on server, trying master instead"
623         $ECHO "  tried nonexistent url: ${url}"
624         CHECKSUM_URL="https://downloads.factorcode.org/images/master/checksums.txt"
625         BOOT_IMAGE_URL="https://downloads.factorcode.org/images/master/${BOOT_IMAGE}"
626     fi
627 }
628
629 set_current_branch() {
630     if [ -n "${CI_BRANCH}" ]; then
631         CURRENT_BRANCH="${CI_BRANCH}"
632     else
633         CURRENT_BRANCH=$(current_git_branch)
634     fi
635 }
636
637 update_boot_image() {
638     set_boot_image_vars
639     $ECHO "Deleting old images..."
640     $DELETE checksums.txt* > /dev/null 2>&1
641     # delete boot images with one or two characters after the dot
642     $DELETE $BOOT_IMAGE.{?,??} > /dev/null 2>&1
643     $DELETE temp/staging.*.image > /dev/null 2>&1
644     if [[ -f $BOOT_IMAGE ]] ; then
645         get_url $CHECKSUM_URL
646         local factorcode_md5=$(cat checksums.txt | grep $BOOT_IMAGE | cut -f2 -d' ')
647         set_md5sum
648         local disk_md5=$($MD5SUM $BOOT_IMAGE | cut -f1 -d' ')
649         $ECHO "Factorcode md5: $factorcode_md5";
650         $ECHO "Disk md5: $disk_md5";
651         if [[ "$factorcode_md5" == "$disk_md5" ]] ; then
652             $ECHO "Your disk boot image matches the one on downloads.factorcode.org."
653         else
654             $DELETE $BOOT_IMAGE > /dev/null 2>&1
655             get_boot_image
656         fi
657     else
658         get_boot_image
659     fi
660 }
661
662 get_boot_image() {
663     $ECHO "Downloading boot image $BOOT_IMAGE."
664     get_url "${BOOT_IMAGE_URL}"
665 }
666
667 get_url() {
668     if [[ -z $DOWNLOADER ]] ; then
669         set_downloader;
670     fi
671     $ECHO $DOWNLOADER $1 ;
672     $DOWNLOADER $1
673     check_ret $DOWNLOADER
674 }
675
676 get_config_info() {
677     find_build_info
678     check_installed_programs
679     check_libraries
680 }
681
682 copy_fresh_image() {
683     $ECHO "Copying $FACTOR_IMAGE to $FACTOR_IMAGE_FRESH..."
684     $COPY $FACTOR_IMAGE $FACTOR_IMAGE_FRESH
685 }
686
687 bootstrap() {
688     ./$FACTOR_BINARY -i=$BOOT_IMAGE
689     check_ret "./$FACTOR_BINARY bootstrap failed"
690     copy_fresh_image
691 }
692
693 install() {
694     check_factor_exists
695     get_config_info
696     git_clone
697     cd_factor
698     make_factor
699     set_boot_image_vars
700     get_boot_image
701     bootstrap
702 }
703
704 update() {
705     get_config_info
706     git_fetch
707     backup_factor
708     make_clean_factor
709 }
710
711 download_and_bootstrap() {
712     update_boot_image
713     bootstrap
714 }
715
716 net_bootstrap_no_pull() {
717     get_config_info
718     make_clean_factor
719     download_and_bootstrap
720 }
721
722 refresh_image() {
723     ./$FACTOR_BINARY -e="USING: vocabs.loader vocabs.refresh system memory ; refresh-all save 0 exit"
724     check_ret factor
725 }
726
727 make_boot_image() {
728     ./$FACTOR_BINARY -run="bootstrap.image" "$MAKE_IMAGE_TARGET"
729     check_ret factor
730 }
731
732 install_deps_apt() {
733     sudo apt install --yes libc6-dev libpango1.0-dev libx11-dev xorg-dev libgtk2.0-dev gtk2-engines-pixbuf libgtkglext1-dev wget git git-doc rlwrap clang make screen tmux libssl-dev
734     check_ret sudo
735 }
736
737 install_deps_pacman() {
738     sudo pacman --noconfirm -Syu gcc clang make rlwrap git wget pango glibc gtk2 gtk3 gtkglext gtk-engines gdk-pixbuf2 libx11 screen tmux
739     check_ret sudo
740 }
741
742 install_deps_dnf() {
743     sudo dnf --assumeyes install gcc gcc-c++ glibc-devel binutils libX11-devel pango-devel gtk3-devel gdk-pixbuf2-devel gtkglext-devel tmux rlwrap wget
744     check_ret sudo
745 }
746
747 install_deps_pkg() {
748     sudo pkg install --yes bash git gmake gcc rlwrap ripgrep curl gmake x11-toolkits/gtk30 x11-toolkits/gtkglext pango cairo vim
749 }
750
751
752 install_deps_macosx() {
753     test_program_installed git
754     if [[ $? -ne 1 ]] ; then
755         ensure_program_installed yes
756         $ECHO "git not found."
757         $ECHO "This script requires either git-core or port."
758         $ECHO "If it fails, install git-core or port and try again."
759         ensure_program_installed port
760         $ECHO "Installing git-core with port...this will take awhile."
761         yes | sudo port install git-core
762     fi
763 }
764
765 usage() {
766     $ECHO "usage: $0 command [optional-target]"
767     $ECHO "  install - git clone, compile, bootstrap"
768     $ECHO "  deps-apt - install required packages for Factor on Linux using apt"
769     $ECHO "  deps-pacman - install required packages for Factor on Linux using pacman"
770     $ECHO "  deps-dnf - install required packages for Factor on Linux using dnf"
771     $ECHO "  deps-pkg - install required packages for Factor on FreeBSD using pkg"
772     $ECHO "  deps-macosx - install git on MacOSX using port"
773     $ECHO "  self-bootstrap - make local boot image, bootstrap"
774     $ECHO "  self-update - git pull, recompile, make local boot image, bootstrap"
775     $ECHO "  quick-update - git pull, refresh-all, save"
776     $ECHO "  update|latest - git pull, recompile, download a boot image, bootstrap"
777     $ECHO "  compile - compile the binary"
778     $ECHO "  recompile - recompile the binary"
779     $ECHO "  bootstrap - bootstrap with existing boot image"
780     $ECHO "  net-bootstrap - recompile, download a boot image, bootstrap"
781     $ECHO "  make-target - find and print the os-arch-cpu string"
782     $ECHO "  report|info - print the build variables"
783     $ECHO "  full-report - print the build variables, check programs and libraries"
784     $ECHO "  update-boot-image - get the boot image for the current branch"
785     $ECHO ""
786     $ECHO "If you are behind a firewall, invoke as:"
787     $ECHO "env GIT_PROTOCOL=http $0 <command>"
788     $ECHO ""
789     $ECHO "Example for overriding the default target:"
790     $ECHO "    $0 update macosx-x86-32"
791 }
792
793 MAKE_TARGET=unknown
794
795 # -n is nonzero length, -z is zero length
796 if [[ -n "$2" ]] ; then
797     parse_build_info $2
798 fi
799
800 if [ "$#" -gt 3 ]; then
801     usage
802     $ECHO "error: too many arguments"
803     exit 1
804 fi
805
806
807 set_copy
808 set_delete
809
810 case "$1" in
811     install) install ;;
812     deps-apt) install_deps_apt ;;
813     deps-pacman) install_deps_pacman ;;
814     deps-macosx) install_deps_macosx ;;
815     deps-dnf) install_deps_dnf ;;
816     deps-pkg) install_deps_pkg ;;
817     self-bootstrap) get_config_info; make_boot_image; bootstrap  ;;
818     self-update) update; make_boot_image; bootstrap  ;;
819     quick-update) update; refresh_image ;;
820     update|latest) update; download_and_bootstrap ;;
821     compile) find_build_info; make_factor ;;
822     recompile) find_build_info; make_clean; make_factor ;;
823     bootstrap) get_config_info; bootstrap ;;
824     net-bootstrap) net_bootstrap_no_pull ;;
825     make-target) FIND_MAKE_TARGET=true; ECHO=false; find_build_info; exit_script ;;
826     report|info) find_build_info ;;
827     full-report) find_build_info; check_installed_programs; check_libraries ;;
828     update-boot-image) find_build_info; check_installed_programs; update_boot_image ;;
829     *) usage ;;
830 esac
831