3 # Programs returning != 0 will not cause script to exit
6 # Case insensitive string comparison
14 GIT_PROTOCOL=${GIT_PROTOCOL:="https"}
15 GIT_URL=${GIT_URL:=$GIT_PROTOCOL"://github.com/factor/factor.git"}
18 test_program_installed() {
19 command -v "$1" >/dev/null 2>&1
23 test_programs_installed() {
25 $ECHO -n "Checking for all($*)..."
28 if test_program_installed "$cmd"; then
32 if [[ $installed -eq $# ]] ; then
40 if [[ $FIND_MAKE_TARGET = true ]] ; then
47 ensure_program_installed() {
49 $ECHO -n "Checking for any($*)..."
52 if test_program_installed "$cmd"; then
60 if [[ $# -eq 1 ]] ; then
63 $ECHO -n "any of [ $* ]"
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."
75 # Can't execute any commands before saving $?
76 # $1 is the name of the command we are checking
78 if [[ $RET -ne 0 ]] ; then
85 if test_program_installed wget; then
86 DOWNLOADER="wget -nd --prefer-family=IPv4"
90 if test_program_installed curl; then
91 DOWNLOADER="curl -L -f -O"
95 $ECHO "error: wget or curl required"
100 if test_program_installed md5sum; then
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
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++
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++
126 if test_programs_installed clang clang++ ; then
127 [ -z "$CC" ] && CC=clang
128 [ -z "$CXX" ] && CXX=clang++
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++
139 $ECHO "error: high enough version of either (clang/clang++) or (gcc/g++) required!"
145 freebsd) MAKE="gmake" ;;
148 if [[ $MAKE = "gmake" ]] ; then
149 ensure_program_installed gmake
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
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
181 check_X11_libraries() {
182 check_library_exists GL
183 check_library_exists X11
184 check_library_exists pango-1.0
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
202 linux) check_X11_libraries
203 check_gtk_libraries ;;
204 unix) check_gtk_libraries ;;
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."
217 if [[ -n $OS ]] ; then return; fi
218 $ECHO "Finding OS..."
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 ;;
233 FreeBSD) OS=freebsd ;;
238 find_architecture() {
239 if [[ -n $ARCH ]] ; then return; fi
240 $ECHO "Finding ARCH..."
251 aarch64) ARCH=arm64 ;;
253 iPhone5*[3-9]) ARCH=arm64 ;;
254 iPhone[6-9]*) ARCH=arm64 ;;
255 iPhone[1-9][0-9]*) ARCH=arm64 ;;
256 iPad[4-9]*) ARCH=arm64 ;;
257 iPad[1-9][0-9]*) ARCH=arm64 ;;
258 AppleTV[5-9]*) ARCH=arm64 ;;
259 AppleTV[1-9][0-9]*) ARCH=arm64 ;;
260 "Power Macintosh") ARCH=ppc ;;
265 $ECHO "Finding NUM_CORES..."
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) ;;
277 if [[ -n $WORD ]] ; then return; fi
278 $ECHO "Finding WORD..."
279 WORD=$(getconf LONG_BIT || find_word_size_cpp || find_word_size_c)
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
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 -
299 echo "Word size should be 32/64, got '$WORD_OUT'"
306 set_factor_binary() {
308 windows) FACTOR_BINARY=factor.com ;;
309 *) FACTOR_BINARY=factor ;;
313 set_factor_library() {
315 windows) FACTOR_LIBRARY=factor.dll ;;
316 macosx) FACTOR_LIBRARY=libfactor.dylib ;;
317 *) FACTOR_LIBRARY=libfactor.a ;;
322 FACTOR_IMAGE=factor.image
323 FACTOR_IMAGE_FRESH=factor.image.fresh
329 $ECHO "NUM_CORES=$NUM_CORES"
332 $ECHO "REPRODUCIBLE=$REPRODUCIBLE"
333 $ECHO "CURRENT_BRANCH=$CURRENT_BRANCH"
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"
349 $ECHO "DELETE=$DELETE"
352 check_os_arch_word() {
353 if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
357 $ECHO "OS, ARCH, or WORD is empty. Please report this."
366 if [[ $OS == linux && $ARCH == ppc ]] ; then
367 MAKE_IMAGE_TARGET=linux-ppc.32
368 MAKE_TARGET=linux-ppc-32
369 elif [[ $OS == linux && $ARCH == arm64 ]] ; then
370 MAKE_IMAGE_TARGET=unix-arm.64
371 MAKE_TARGET=linux-arm-64
372 elif [[ $OS == macosx && $ARCH == arm64 ]] ; then
373 MAKE_IMAGE_TARGET=unix-arm.64
374 MAKE_TARGET=macosx-arm64
375 elif [[ $OS == windows && $ARCH == x86 && $WORD == 64 ]] ; then
376 MAKE_IMAGE_TARGET=windows-x86.64
377 MAKE_TARGET=windows-x86-64
378 elif [[ $OS == windows && $ARCH == x86 && $WORD == 32 ]] ; then
379 MAKE_IMAGE_TARGET=windows-x86.32
380 MAKE_TARGET=windows-x86-32
381 elif [[ $ARCH == x86 && $WORD == 64 ]] ; then
382 MAKE_IMAGE_TARGET=unix-x86.64
383 MAKE_TARGET=$OS-x86-64
384 elif [[ $ARCH == x86 && $WORD == 32 ]] ; then
385 MAKE_IMAGE_TARGET=unix-x86.32
386 MAKE_TARGET=$OS-x86-32
388 MAKE_IMAGE_TARGET=$ARCH.$WORD
389 MAKE_TARGET=$OS-$ARCH-$WORD
391 BOOT_IMAGE=boot.$MAKE_IMAGE_TARGET.image
395 ensure_program_installed cut
396 $ECHO "Parsing make target from command line: $1"
397 OS=$(echo "$1" | cut -d '-' -f 1)
398 ARCH=$(echo "$1" | cut -d '-' -f 2)
399 WORD=$(echo "$1" | cut -d '-' -f 3)
401 if [[ $OS == linux && $ARCH == ppc ]] ; then WORD=32; fi
402 if [[ $OS == linux && $ARCH == arm ]] ; then WORD=32; fi
403 if [[ $OS == macosx && $ARCH == ppc ]] ; then WORD=32; fi
433 $ECHO "Downloading the git repository from github.com..."
434 invoke_git clone "$GIT_URL"
437 update_script_name() {
438 $ECHO "$(dirname "$0")/_update.sh"
442 local -r update_script=$(update_script_name)
443 local -r bash_path=$(which bash)
444 $ECHO "updating from ${CURRENT_BRANCH}"
445 $ECHO "#!$bash_path" >"$update_script"
446 $ECHO "git pull ${GIT_URL} ${CURRENT_BRANCH} >>$update_script"
447 $ECHO "if [[ \$? -eq 0 ]]; then exec \"$0\" $SCRIPT_ARGS; else echo \"git pull failed\"; exit 2; fi" \
449 $ECHO "exit 0" >>"$update_script"
451 chmod 755 "$update_script"
452 exec "$update_script"
455 update_script_changed() {
456 invoke_git diff --stat "$(invoke_git merge-base HEAD FETCH_HEAD)" FETCH_HEAD | grep 'build\.sh' >/dev/null
460 $ECHO "Fetching the git repository from github.com..."
463 rm -f "$(update_script_name)"
464 $ECHO git fetch "$GIT_URL" "${CURRENT_BRANCH}"
465 invoke_git fetch "$GIT_URL" "${CURRENT_BRANCH}"
467 if update_script_changed; then
468 $ECHO "Updating and restarting the build.sh script..."
471 $ECHO "Updating the working tree..."
472 invoke_git pull "$GIT_URL" "${CURRENT_BRANCH}"
477 cd "factor" || exit 12
483 windows) COPY="cp" ;;
490 windows) DELETE="rm" ;;
496 $ECHO "Backing up factor..."
497 $COPY $FACTOR_BINARY $FACTOR_BINARY.bak
498 $COPY $FACTOR_LIBRARY $FACTOR_LIBRARY.bak
499 $COPY "$BOOT_IMAGE" "$BOOT_IMAGE.bak"
500 $COPY $FACTOR_IMAGE $FACTOR_IMAGE.bak
501 $ECHO "Done with backup."
504 check_makefile_exists() {
505 if [[ ! -e "GNUmakefile" ]] ; then
507 $ECHO "***GNUmakefile not found***"
508 $ECHO "You are likely in the wrong directory."
509 $ECHO "Run this script from your factor directory:"
516 check_makefile_exists
517 if [ -n "$MAKE_OPTS" ]; then
518 "$MAKE" "$MAKE_OPTS" "$@"
530 $ECHO "Building factor with $NUM_CORES cores"
531 invoke_make "CC=$CC" "CXX=$CXX" "$MAKE_TARGET" "-j$NUM_CORES"
534 make_clean_factor() {
539 current_git_branch() {
540 # git rev-parse --abbrev-ref HEAD # outputs HEAD for detached head
541 # outputs nothing for detached HEAD, which is fine for ``git fetch``
542 git describe --all --exact-match 2>/dev/null | sed 's=.*/=='
546 if [[ $DOWNLOADER_NAME == 'wget' ]]; then
547 if wget -S --spider --prefer-family=IPv4 "$1" 2>&1 | grep -q 'HTTP/1.1 200 OK'; then
552 elif [[ $DOWNLOADER_NAME == 'curl' ]]; then
554 code=$(curl -4 -sL -w "%{http_code}\\n" "$1" -o /dev/null)
555 if [[ $code -eq 200 ]]; then return 0; else return 1; fi
557 $ECHO "error: wget or curl required in check_url"
562 # If we are on a branch, first try to get a boot image for that branch.
563 # Otherwise, just use `master`
564 set_boot_image_vars() {
566 local url="https://downloads.factorcode.org/images/${CURRENT_BRANCH}/checksums.txt"
567 $ECHO "Getting checksum from ${url}"
569 $ECHO "got checksum!"
570 if [[ $? -eq 0 ]]; then
572 BOOT_IMAGE_URL="https://downloads.factorcode.org/images/${CURRENT_BRANCH}/${BOOT_IMAGE}"
574 $ECHO "boot image for branch \`${CURRENT_BRANCH}\` is not on server, trying master instead"
575 $ECHO " tried nonexistent url: ${url}"
576 CHECKSUM_URL="https://downloads.factorcode.org/images/master/checksums.txt"
577 BOOT_IMAGE_URL="https://downloads.factorcode.org/images/master/${BOOT_IMAGE}"
581 set_current_branch() {
582 if [ -n "${CI_BRANCH}" ]; then
583 CURRENT_BRANCH="${CI_BRANCH}"
585 CURRENT_BRANCH=$(current_git_branch)
589 update_boot_image() {
591 $ECHO "Deleting old images..."
592 $DELETE checksums.txt* > /dev/null 2>&1
593 # delete boot images with one or two characters after the dot
594 $DELETE "$BOOT_IMAGE".{?,??} > /dev/null 2>&1
595 $DELETE temp/staging.*.image > /dev/null 2>&1
596 if [[ -f $BOOT_IMAGE ]] ; then
597 get_url "$CHECKSUM_URL"
599 factorcode_md5=$(grep "$BOOT_IMAGE" checksums.txt | cut -f2 -d' ')
602 disk_md5=$($MD5SUM "$BOOT_IMAGE" | cut -f1 -d' ')
603 $ECHO "Factorcode md5: $factorcode_md5";
604 $ECHO "Disk md5: $disk_md5";
605 if [[ "$factorcode_md5" == "$disk_md5" ]] ; then
606 $ECHO "Your disk boot image matches the one on downloads.factorcode.org."
608 $DELETE "$BOOT_IMAGE" > /dev/null 2>&1
617 $ECHO "Downloading boot image $BOOT_IMAGE."
618 get_url "${BOOT_IMAGE_URL}"
622 if [[ -z $DOWNLOADER ]] ; then
625 $ECHO "$DOWNLOADER" "$1" ;
627 check_ret "$DOWNLOADER"
632 check_installed_programs
637 $ECHO "Copying $FACTOR_IMAGE to $FACTOR_IMAGE_FRESH..."
638 $COPY $FACTOR_IMAGE $FACTOR_IMAGE_FRESH
642 ./$FACTOR_BINARY -i="$BOOT_IMAGE"
643 check_ret "./$FACTOR_BINARY bootstrap failed"
665 download_and_bootstrap() {
670 net_bootstrap_no_pull() {
673 download_and_bootstrap
677 ./$FACTOR_BINARY -e="USING: vocabs.loader vocabs.refresh system memory ; refresh-all save 0 exit"
682 ./$FACTOR_BINARY -run="bootstrap.image" "$MAKE_IMAGE_TARGET"
687 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
691 install_deps_pacman() {
692 sudo pacman --noconfirm -Syu gcc clang make rlwrap git wget pango glibc gtk2 gtk3 gtkglext gtk-engines gdk-pixbuf2 libx11 screen tmux
697 sudo dnf --assumeyes install gcc gcc-c++ glibc-devel binutils libX11-devel pango-devel gtk3-devel gdk-pixbuf2-devel gtkglext-devel tmux rlwrap wget
702 sudo pkg install --yes bash git gmake gcc rlwrap ripgrep curl gmake x11-toolkits/gtk30 x11-toolkits/gtkglext pango cairo vim
706 install_deps_macosx() {
707 if test_program_installed git; then
708 ensure_program_installed yes
709 $ECHO "git not found."
710 $ECHO "This script requires either git-core or port."
711 $ECHO "If it fails, install git-core or port and try again."
712 ensure_program_installed port
713 $ECHO "Installing git-core with port...this will take awhile."
714 yes | sudo port install git-core
719 $ECHO "usage: $0 command [optional-target]"
720 $ECHO " install - git clone, compile, bootstrap"
721 $ECHO " deps-apt - install required packages for Factor on Linux using apt"
722 $ECHO " deps-pacman - install required packages for Factor on Linux using pacman"
723 $ECHO " deps-dnf - install required packages for Factor on Linux using dnf"
724 $ECHO " deps-pkg - install required packages for Factor on FreeBSD using pkg"
725 $ECHO " deps-macosx - install git on MacOSX using port"
726 $ECHO " self-bootstrap - make local boot image, bootstrap"
727 $ECHO " self-update - git pull, recompile, make local boot image, bootstrap"
728 $ECHO " quick-update - git pull, refresh-all, save"
729 $ECHO " update|latest - git pull, recompile, download a boot image, bootstrap"
730 $ECHO " compile - compile the binary"
731 $ECHO " recompile - recompile the binary"
732 $ECHO " bootstrap - bootstrap with existing boot image"
733 $ECHO " net-bootstrap - recompile, download a boot image, bootstrap"
734 $ECHO " make-target - find and print the os-arch-cpu string"
735 $ECHO " report|info - print the build variables"
736 $ECHO " full-report - print the build variables, check programs and libraries"
737 $ECHO " update-boot-image - get the boot image for the current branch"
739 $ECHO "If you are behind a firewall, invoke as:"
740 $ECHO "env GIT_PROTOCOL=http $0 <command>"
742 $ECHO "Example for overriding the default target:"
743 $ECHO " $0 update macosx-x86-32"
748 # -n is nonzero length, -z is zero length
749 if [[ -n "$2" ]] ; then
750 parse_build_info "$2"
753 if [ "$#" -gt 3 ]; then
755 $ECHO "error: too many arguments"
765 deps-apt) install_deps_apt ;;
766 deps-pacman) install_deps_pacman ;;
767 deps-macosx) install_deps_macosx ;;
768 deps-dnf) install_deps_dnf ;;
769 deps-pkg) install_deps_pkg ;;
770 self-bootstrap) get_config_info; make_boot_image; bootstrap ;;
771 self-update) update; make_boot_image; bootstrap ;;
772 quick-update) update; refresh_image ;;
773 update|latest) update; download_and_bootstrap ;;
774 compile) find_build_info; make_factor ;;
775 recompile) find_build_info; make_clean; make_factor ;;
776 bootstrap) get_config_info; bootstrap ;;
777 net-bootstrap) net_bootstrap_no_pull ;;
778 make-target) FIND_MAKE_TARGET=true; ECHO=false; find_build_info; exit_script 0;;
779 report|info) find_build_info ;;
780 full-report) find_build_info; check_installed_programs; check_libraries ;;
781 update-boot-image) find_build_info; check_installed_programs; update_boot_image ;;