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