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