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