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