3 # Programs returning != 0 will not cause script to exit
6 # Case insensitive string comparison
14 GIT_PROTOCOL=${GIT_PROTOCOL:="git"}
15 GIT_URL=${GIT_URL:=$GIT_PROTOCOL"://github.com/factor/factor.git"}
18 REQUIRE_CLANG_VERSION=3.1
21 test_program_installed() {
22 if ! [[ -n $(type -p $1) ]] ; then
29 test_programs_installed() {
31 $ECHO -n "Checking for all($*)..."
34 test_program_installed $i
35 if [[ $? -eq 1 ]]; then
36 installed=$(( $installed + 1 ))
39 if [[ $installed -eq $# ]] ; then
43 $ECHO "all not found."
49 if [[ $FIND_MAKE_TARGET = true ]] ; then
50 # Must be echo not $ECHO
56 ensure_program_installed() {
58 $ECHO -n "Checking for any($*)..."
61 test_program_installed $i
62 if [[ $? -eq 1 ]]; then
64 installed=$(( $installed + 1 ))
70 if [[ $# -eq 1 ]] ; then
73 $ECHO -n "any of [ $* ]"
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."
86 if [[ $RET -ne 0 ]] ; then
93 test_program_installed wget
94 if [[ $? -ne 0 ]] ; then
99 test_program_installed curl
100 if [[ $? -ne 0 ]] ; then
101 DOWNLOADER="curl -L -f -O"
105 $ECHO "error: wget or curl required"
110 test_program_installed md5sum
111 if [[ $? -ne 0 ]] ; then
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]}"
130 export "$5=${BASH_REMATCH[3]}"
132 echo "unsupported version number, please report a bug: $1"
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 )
147 echo "clang version required >= $REQUIRE_CLANG_VERSION, got $CLANG_VERSION"
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
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++
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++
177 test_programs_installed clang clang++
178 if [[ $? -ne 0 ]] && clang_version_ok ; then
179 [ -z "$CC" ] && CC=clang
180 [ -z "$CXX" ] && CXX=clang++
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++
192 $ECHO "error: high enough version of either (clang/clang++) or (gcc/g++) required!"
198 freebsd) MAKE=gmake ;;
201 if [[ $MAKE = 'gmake' ]] ; then
202 ensure_program_installed gmake
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
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
235 check_X11_libraries() {
236 check_library_exists GL
237 check_library_exists X11
238 check_library_exists pango-1.0
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
256 linux) check_X11_libraries
257 check_gtk_libraries;;
258 unix) check_gtk_libraries;;
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."
271 if [[ -n $OS ]] ; then return; fi
272 $ECHO "Finding OS..."
273 local uname_s=$(uname -s)
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;;
286 FreeBSD) OS=freebsd;;
291 find_architecture() {
292 if [[ -n $ARCH ]] ; then return; fi
293 $ECHO "Finding ARCH..."
304 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;;
318 $ECHO "Finding NUM_CORES..."
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);;
330 if [[ -n $WORD ]] ; then return; fi
331 $ECHO "Finding WORD..."
332 WORD=$(getconf LONG_BIT || find_word_size_cpp || find_word_size_c)
335 find_word_size_cpp() {
336 SIXTY_FOUR='defined(__aarch64__) || defined(__x86_64__) || defined(_M_AMD64) || defined(__PPC64__) || defined(__64BIT__)'
337 THIRTY_TWO='defined(i386) || defined(__i386) || defined(__i386__) || defined(_MIX86)'
338 $CC -E -xc <(echo -e "#if ${SIXTY_FOUR}\n64\n#elif ${THIRTY_TWO}\n32\n#endif") | tail -1
342 C_WORD="factor-word-size"
343 TEST_PROGRAM="int main(){ return (long)(8*sizeof(void*)); }"
344 echo $TEST_PROGRAM | $CC -o $C_WORD -xc -
352 echo "Word size should be 32/64, got '$WORD_OUT'"
359 set_factor_binary() {
361 windows) FACTOR_BINARY=factor.com;;
362 *) FACTOR_BINARY=factor;;
366 set_factor_library() {
368 windows) FACTOR_LIBRARY=factor.dll;;
369 macosx) FACTOR_LIBRARY=libfactor.dylib;;
370 *) FACTOR_LIBRARY=libfactor.a;;
375 FACTOR_IMAGE=factor.image
376 FACTOR_IMAGE_FRESH=factor.image.fresh
382 $ECHO NUM_CORES=$NUM_CORES
385 $ECHO REPRODUCIBLE=$REPRODUCIBLE
386 $ECHO CURRENT_BRANCH=$CURRENT_BRANCH
387 $ECHO FACTOR_BINARY=$FACTOR_BINARY
388 $ECHO FACTOR_LIBRARY=$FACTOR_LIBRARY
389 $ECHO FACTOR_IMAGE=$FACTOR_IMAGE
390 $ECHO MAKE_TARGET=$MAKE_TARGET
391 $ECHO BOOT_IMAGE=$BOOT_IMAGE
392 $ECHO MAKE_IMAGE_TARGET=$MAKE_IMAGE_TARGET
393 $ECHO GIT_PROTOCOL=$GIT_PROTOCOL
394 $ECHO GIT_URL=$GIT_URL
395 $ECHO DOWNLOADER=$DOWNLOADER
403 check_os_arch_word() {
404 if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
408 $ECHO "OS, ARCH, or WORD is empty. Please report this."
417 if [[ $OS == linux && $ARCH == ppc ]] ; then
418 MAKE_IMAGE_TARGET=linux-ppc.32
419 MAKE_TARGET=linux-ppc-32
420 elif [[ $OS == linux && $ARCH == arm64 ]] ; then
421 MAKE_IMAGE_TARGET=unix-arm.64
422 MAKE_TARGET=linux-arm-64
423 elif [[ $OS == macosx && $ARCH == arm64 ]] ; then
424 MAKE_IMAGE_TARGET=unix-arm.64
425 MAKE_TARGET=macosx-arm64
426 elif [[ $OS == windows && $ARCH == x86 && $WORD == 64 ]] ; then
427 MAKE_IMAGE_TARGET=windows-x86.64
428 MAKE_TARGET=windows-x86-64
429 elif [[ $OS == windows && $ARCH == x86 && $WORD == 32 ]] ; then
430 MAKE_IMAGE_TARGET=windows-x86.32
431 MAKE_TARGET=windows-x86-32
432 elif [[ $ARCH == x86 && $WORD == 64 ]] ; then
433 MAKE_IMAGE_TARGET=unix-x86.64
434 MAKE_TARGET=$OS-x86-64
435 elif [[ $ARCH == x86 && $WORD == 32 ]] ; then
436 MAKE_IMAGE_TARGET=unix-x86.32
437 MAKE_TARGET=$OS-x86-32
439 MAKE_IMAGE_TARGET=$ARCH.$WORD
440 MAKE_TARGET=$OS-$ARCH-$WORD
442 BOOT_IMAGE=boot.$MAKE_IMAGE_TARGET.image
446 ensure_program_installed cut
447 $ECHO "Parsing make target from command line: $1"
448 OS=$(echo $1 | cut -d '-' -f 1)
449 ARCH=$(echo $1 | cut -d '-' -f 2)
450 WORD=$(echo $1 | cut -d '-' -f 3)
452 if [[ $OS == linux && $ARCH == ppc ]] ; then WORD=32; fi
453 if [[ $OS == linux && $ARCH == arm ]] ; then WORD=32; fi
454 if [[ $OS == macosx && $ARCH == ppc ]] ; then WORD=32; fi
483 $ECHO "Downloading the git repository from github.com..."
484 invoke_git clone $GIT_URL
487 update_script_name() {
488 $ECHO "$(dirname $0)/_update.sh"
492 local -r update_script=$(update_script_name)
493 local -r bash_path=$(which bash)
494 $ECHO "updating from ${CURRENT_BRANCH}"
495 $ECHO "#!$bash_path" >"$update_script"
496 $ECHO "git pull \"$GIT_URL\" ${CURRENT_BRANCH}" >>"$update_script"
497 $ECHO "if [[ \$? -eq 0 ]]; then exec \"$0\" $SCRIPT_ARGS; else echo \"git pull failed\"; exit 2; fi" \
499 $ECHO "exit 0" >>"$update_script"
501 chmod 755 "$update_script"
502 exec "$update_script"
505 update_script_changed() {
506 invoke_git diff --stat "$(invoke_git merge-base HEAD FETCH_HEAD)" FETCH_HEAD | grep 'build\.sh' >/dev/null
510 $ECHO "Fetching the git repository from github.com..."
513 rm -f "$(update_script_name)"
514 $ECHO git fetch "$GIT_URL" "${CURRENT_BRANCH}"
515 invoke_git fetch "$GIT_URL" "${CURRENT_BRANCH}"
517 if update_script_changed; then
518 $ECHO "Updating and restarting the build.sh script..."
521 $ECHO "Updating the working tree..."
522 invoke_git pull "$GIT_URL" "${CURRENT_BRANCH}"
546 $ECHO "Backing up factor..."
547 $COPY $FACTOR_BINARY $FACTOR_BINARY.bak
548 $COPY $FACTOR_LIBRARY $FACTOR_LIBRARY.bak
549 $COPY $BOOT_IMAGE $BOOT_IMAGE.bak
550 $COPY $FACTOR_IMAGE $FACTOR_IMAGE.bak
551 $ECHO "Done with backup."
554 check_makefile_exists() {
555 if [[ ! -e "GNUmakefile" ]] ; then
557 $ECHO "***GNUmakefile not found***"
558 $ECHO "You are likely in the wrong directory."
559 $ECHO "Run this script from your factor directory:"
566 check_makefile_exists
567 $MAKE $MAKE_OPTS "$@"
576 $ECHO "Building factor with $NUM_CORES cores"
577 invoke_make CC=$CC CXX=$CXX $MAKE_TARGET -j$NUM_CORES
580 make_clean_factor() {
585 current_git_branch() {
586 # git rev-parse --abbrev-ref HEAD # outputs HEAD for detached head
587 # outputs nothing for detached HEAD, which is fine for ``git fetch``
588 git describe --all --exact-match 2>/dev/null | sed 's=.*/=='
592 if [[ $DOWNLOADER_NAME == 'wget' ]]; then
593 if [[ $(wget -S --spider $1 2>&1 | grep 'HTTP/1.1 200 OK') ]]; then
598 elif [[ $DOWNLOADER_NAME == 'curl' ]]; then
599 local code=$(curl -sL -w "%{http_code}\\n" "$1" -o /dev/null)
600 if [[ $code -eq 200 ]]; then return 0; else return 1; fi
602 $ECHO "error: wget or curl required in check_url"
607 # If we are on a branch, first try to get a boot image for that branch.
608 # Otherwise, just use `master`
609 set_boot_image_vars() {
611 local url="http://downloads.factorcode.org/images/${CURRENT_BRANCH}/checksums.txt"
613 if [[ $? -eq 0 ]]; then
615 BOOT_IMAGE_URL="http://downloads.factorcode.org/images/${CURRENT_BRANCH}/${BOOT_IMAGE}"
617 $ECHO "boot image for branch \`${CURRENT_BRANCH}\` is not on server, trying master instead"
618 $ECHO " tried nonexistent url: ${url}"
619 CHECKSUM_URL="http://downloads.factorcode.org/images/master/checksums.txt"
620 BOOT_IMAGE_URL="http://downloads.factorcode.org/images/master/${BOOT_IMAGE}"
624 set_current_branch() {
625 if [ -n "${CI_BRANCH}" ]; then
626 CURRENT_BRANCH="${CI_BRANCH}"
628 CURRENT_BRANCH=$(current_git_branch)
632 update_boot_image() {
634 $ECHO "Deleting old images..."
635 $DELETE checksums.txt* > /dev/null 2>&1
636 # delete boot images with one or two characters after the dot
637 $DELETE $BOOT_IMAGE.{?,??} > /dev/null 2>&1
638 $DELETE temp/staging.*.image > /dev/null 2>&1
639 if [[ -f $BOOT_IMAGE ]] ; then
640 get_url $CHECKSUM_URL
641 local factorcode_md5=$(cat checksums.txt | grep $BOOT_IMAGE | cut -f2 -d' ')
643 local disk_md5=$($MD5SUM $BOOT_IMAGE | cut -f1 -d' ')
644 $ECHO "Factorcode md5: $factorcode_md5";
645 $ECHO "Disk md5: $disk_md5";
646 if [[ "$factorcode_md5" == "$disk_md5" ]] ; then
647 $ECHO "Your disk boot image matches the one on downloads.factorcode.org."
649 $DELETE $BOOT_IMAGE > /dev/null 2>&1
658 $ECHO "Downloading boot image $BOOT_IMAGE."
659 get_url "${BOOT_IMAGE_URL}"
663 if [[ -z $DOWNLOADER ]] ; then
666 $ECHO $DOWNLOADER $1 ;
668 check_ret $DOWNLOADER
673 check_installed_programs
678 $ECHO "Copying $FACTOR_IMAGE to $FACTOR_IMAGE_FRESH..."
679 $COPY $FACTOR_IMAGE $FACTOR_IMAGE_FRESH
683 ./$FACTOR_BINARY -i=$BOOT_IMAGE
705 download_and_bootstrap() {
710 net_bootstrap_no_pull() {
713 download_and_bootstrap
717 ./$FACTOR_BINARY -e="USING: vocabs.loader vocabs.refresh system memory ; refresh-all save 0 exit"
722 ./$FACTOR_BINARY -run="bootstrap.image" "$MAKE_IMAGE_TARGET"
727 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
731 install_deps_pacman() {
732 sudo pacman --noconfirm -Syu gcc clang make rlwrap git wget pango glibc gtk2 gtk3 gtkglext gtk-engines gdk-pixbuf2 libx11 screen tmux
737 sudo dnf --assumeyes install gcc gcc-c++ glibc-devel binutils libX11-devel pango-devel gtk3-devel gdk-pixbuf2-devel gtkglext-devel tmux rlwrap wget
742 sudo pkg install --yes git gmake gcc rlwrap ripgrep curl gmake x11-toolkits/gtk30 x11-toolkits/gtkglext pango cairo vim
746 install_deps_macosx() {
747 test_program_installed git
748 if [[ $? -ne 1 ]] ; then
749 ensure_program_installed yes
750 $ECHO "git not found."
751 $ECHO "This script requires either git-core or port."
752 $ECHO "If it fails, install git-core or port and try again."
753 ensure_program_installed port
754 $ECHO "Installing git-core with port...this will take awhile."
755 yes | sudo port install git-core
760 $ECHO "usage: $0 command [optional-target]"
761 $ECHO " install - git clone, compile, bootstrap"
762 $ECHO " deps-apt - install required packages for Factor on Linux using apt"
763 $ECHO " deps-pacman - install required packages for Factor on Linux using pacman"
764 $ECHO " deps-dnf - install required packages for Factor on Linux using dnf"
765 $ECHO " deps-pkg - install required packages for Factor on FreeBSD using pkg"
766 $ECHO " deps-macosx - install git on MacOSX using port"
767 $ECHO " self-update - git pull, recompile, make local boot image, bootstrap"
768 $ECHO " quick-update - git pull, refresh-all, save"
769 $ECHO " update|latest - git pull, recompile, download a boot image, bootstrap"
770 $ECHO " compile - compile the binary"
771 $ECHO " recompile - recompile the binary"
772 $ECHO " bootstrap - bootstrap with existing boot image"
773 $ECHO " net-bootstrap - recompile, download a boot image, bootstrap"
774 $ECHO " make-target - find and print the os-arch-cpu string"
775 $ECHO " report|info - print the build variables"
776 $ECHO " update-boot-image - get the boot image for the current branch"
778 $ECHO "If you are behind a firewall, invoke as:"
779 $ECHO "env GIT_PROTOCOL=http $0 <command>"
781 $ECHO "Example for overriding the default target:"
782 $ECHO " $0 update macosx-x86-32"
787 # -n is nonzero length, -z is zero length
788 if [[ -n "$2" ]] ; then
792 if [ "$#" -gt 3 ]; then
794 $ECHO "error: too many arguments"
804 deps-apt) install_deps_apt ;;
805 deps-pacman) install_deps_pacman ;;
806 deps-macosx) install_deps_macosx ;;
807 deps-dnf) install_deps_dnf ;;
808 deps-pkg) install_deps_pkg ;;
809 self-bootstrap) get_config_info; make_boot_image; bootstrap;;
810 self-update) update; make_boot_image; bootstrap;;
811 quick-update) update; refresh_image ;;
812 update|latest) update; download_and_bootstrap ;;
813 compile) find_build_info; make_factor ;;
814 recompile) find_build_info; make_clean; make_factor ;;
815 bootstrap) get_config_info; bootstrap ;;
816 net-bootstrap) net_bootstrap_no_pull ;;
817 make-target) FIND_MAKE_TARGET=true; ECHO=false; find_build_info; exit_script ;;
818 report|info) find_build_info ;;
819 full-report) find_build_info; check_installed_programs; check_libraries ;;
820 update-boot-image) find_build_info; check_installed_programs; update_boot_image;;