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"
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++
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++
174 test_programs_installed clang clang++
175 if [[ $? -ne 0 ]] && clang_version_ok ; then
176 [ -z "$CC" ] && CC=clang
177 [ -z "$CXX" ] && CXX=clang++
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++
189 $ECHO "error: high enough version of either (clang/clang++) or (gcc/g++) required!"
195 freebsd) MAKE=gmake ;;
198 if [[ $MAKE = 'gmake' ]] ; then
199 ensure_program_installed gmake
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
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
232 check_X11_libraries() {
233 check_library_exists GL
234 check_library_exists X11
235 check_library_exists pango-1.0
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
253 linux) check_X11_libraries
254 check_gtk_libraries;;
255 unix) check_gtk_libraries;;
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."
268 if [[ -n $OS ]] ; then return; fi
269 $ECHO "Finding OS..."
270 local uname_s=$(uname -s)
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;;
282 FreeBSD) OS=freebsd;;
286 find_architecture() {
287 if [[ -n $ARCH ]] ; then return; fi
288 $ECHO "Finding ARCH..."
299 "Power Macintosh") ARCH=ppc;;
304 $ECHO "Finding num cores..."
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);;
315 echo_test_program() {
317 echo -e "int main(){ return (long)(8*sizeof(void*)); }"
321 $ECHO "Finding WORD..."
322 C_WORD="factor-word-size"
323 echo_test_program | $CC -o $C_WORD -xc -
331 echo "Word size should be 32/64, got $WORD"
338 intel_macosx_word_size() {
339 ensure_program_installed sysctl
340 $ECHO -n "Testing if your Intel Mac supports 64bit binaries..."
341 sysctl machdep.cpu.extfeatures | grep EM64T >/dev/null
342 if [[ $? -eq 0 ]] ; then
352 if [[ -n $WORD ]] ; then return; fi
353 if [[ $OS == macosx && $ARCH == x86 ]] ; then
354 intel_macosx_word_size
360 set_factor_binary() {
362 windows) FACTOR_BINARY=factor.com;;
363 *) FACTOR_BINARY=factor;;
367 set_factor_library() {
369 windows) FACTOR_LIBRARY=factor.dll;;
370 macosx) FACTOR_LIBRARY=libfactor.dylib;;
371 *) FACTOR_LIBRARY=libfactor.a;;
376 FACTOR_IMAGE=factor.image
377 FACTOR_IMAGE_FRESH=factor.image.fresh
383 $ECHO NUM_CORES=$NUM_CORES
386 $ECHO REPRODUCIBLE=$REPRODUCIBLE
387 $ECHO CURRENT_BRANCH=$CURRENT_BRANCH
388 $ECHO FACTOR_BINARY=$FACTOR_BINARY
389 $ECHO FACTOR_LIBRARY=$FACTOR_LIBRARY
390 $ECHO FACTOR_IMAGE=$FACTOR_IMAGE
391 $ECHO MAKE_TARGET=$MAKE_TARGET
392 $ECHO BOOT_IMAGE=$BOOT_IMAGE
393 $ECHO MAKE_IMAGE_TARGET=$MAKE_IMAGE_TARGET
394 $ECHO GIT_PROTOCOL=$GIT_PROTOCOL
395 $ECHO GIT_URL=$GIT_URL
396 $ECHO DOWNLOADER=$DOWNLOADER
404 check_os_arch_word() {
405 if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
409 $ECHO "OS, ARCH, or WORD is empty. Please report this."
418 if [[ $OS == linux && $ARCH == ppc ]] ; then
419 MAKE_IMAGE_TARGET=linux-ppc.32
420 MAKE_TARGET=linux-ppc-32
421 elif [[ $OS == windows && $ARCH == x86 && $WORD == 64 ]] ; then
422 MAKE_IMAGE_TARGET=windows-x86.64
423 MAKE_TARGET=windows-x86-64
424 elif [[ $OS == windows && $ARCH == x86 && $WORD == 32 ]] ; then
425 MAKE_IMAGE_TARGET=windows-x86.32
426 MAKE_TARGET=windows-x86-32
427 elif [[ $ARCH == x86 && $WORD == 64 ]] ; then
428 MAKE_IMAGE_TARGET=unix-x86.64
429 MAKE_TARGET=$OS-x86-64
430 elif [[ $ARCH == x86 && $WORD == 32 ]] ; then
431 MAKE_IMAGE_TARGET=unix-x86.32
432 MAKE_TARGET=$OS-x86-32
434 MAKE_IMAGE_TARGET=$ARCH.$WORD
435 MAKE_TARGET=$OS-$ARCH-$WORD
437 BOOT_IMAGE=boot.$MAKE_IMAGE_TARGET.image
441 ensure_program_installed cut
442 $ECHO "Parsing make target from command line: $1"
443 OS=$(echo $1 | cut -d '-' -f 1)
444 ARCH=$(echo $1 | cut -d '-' -f 2)
445 WORD=$(echo $1 | cut -d '-' -f 3)
447 if [[ $OS == linux && $ARCH == ppc ]] ; then WORD=32; fi
448 if [[ $OS == linux && $ARCH == arm ]] ; then WORD=32; fi
449 if [[ $OS == macosx && $ARCH == ppc ]] ; then WORD=32; fi
478 $ECHO "Downloading the git repository from github.com..."
479 invoke_git clone $GIT_URL
482 update_script_name() {
483 $ECHO "$(dirname $0)/_update.sh"
487 local -r update_script=$(update_script_name)
488 local -r bash_path=$(which bash)
489 $ECHO "#!$bash_path" >"$update_script"
490 $ECHO "git pull \"$GIT_URL\" master" >>"$update_script"
491 $ECHO "if [[ \$? -eq 0 ]]; then exec \"$0\" $SCRIPT_ARGS; else echo \"git pull failed\"; exit 2; fi" \
493 $ECHO "exit 0" >>"$update_script"
495 chmod 755 "$update_script"
496 exec "$update_script"
499 update_script_changed() {
500 invoke_git diff --stat "$(invoke_git merge-base HEAD FETCH_HEAD)" FETCH_HEAD | grep 'build\.sh' >/dev/null
504 $ECHO "Fetching the git repository from github.com..."
505 branch=$(current_git_branch)
507 rm -f "$(update_script_name)"
508 $ECHO git fetch "$GIT_URL" "$branch"
509 invoke_git fetch "$GIT_URL" "$branch"
511 if update_script_changed; then
512 $ECHO "Updating and restarting the build.sh script..."
515 $ECHO "Updating the working tree..."
516 invoke_git pull "$GIT_URL" master
540 $ECHO "Backing up factor..."
541 $COPY $FACTOR_BINARY $FACTOR_BINARY.bak
542 $COPY $FACTOR_LIBRARY $FACTOR_LIBRARY.bak
543 $COPY $BOOT_IMAGE $BOOT_IMAGE.bak
544 $COPY $FACTOR_IMAGE $FACTOR_IMAGE.bak
545 $ECHO "Done with backup."
548 check_makefile_exists() {
549 if [[ ! -e "GNUmakefile" ]] ; then
551 $ECHO "***GNUmakefile not found***"
552 $ECHO "You are likely in the wrong directory."
553 $ECHO "Run this script from your factor directory:"
560 check_makefile_exists
561 $MAKE $MAKE_OPTS "$@"
570 $ECHO "Building factor with $NUM_CORES cores"
571 invoke_make CC=$CC CXX=$CXX $MAKE_TARGET -j$NUM_CORES
574 make_clean_factor() {
579 current_git_branch() {
580 # git rev-parse --abbrev-ref HEAD # outputs HEAD for detached head
581 # outputs nothing for detached HEAD, which is fine for ``git fetch``
582 git describe --all --exact-match 2>/dev/null | sed 's=.*/=='
586 if [[ $DOWNLOADER_NAME == 'wget' ]]; then
587 if [[ $(wget -S --spider $1 2>&1 | grep 'HTTP/1.1 200 OK') ]]; then
592 elif [[ $DOWNLOADER_NAME == 'curl' ]]; then
593 local code=$(curl -sL -w "%{http_code}\\n" "$1" -o /dev/null)
594 if [[ $code -eq 200 ]]; then return 0; else return 1; fi
596 $ECHO "error: wget or curl required in check_url"
601 # If we are on a branch, first try to get a boot image for that branch.
602 # Otherwise, just use `master`
603 set_boot_image_vars() {
605 local url="http://downloads.factorcode.org/images/${CURRENT_BRANCH}/checksums.txt"
607 if [[ $? -eq 0 ]]; then
609 BOOT_IMAGE_URL="http://downloads.factorcode.org/images/${CURRENT_BRANCH}/${BOOT_IMAGE}"
611 CHECKSUM_URL="http://downloads.factorcode.org/images/master/checksums.txt"
612 BOOT_IMAGE_URL="http://downloads.factorcode.org/images/master/${BOOT_IMAGE}"
616 set_current_branch() {
617 if [ -n "${CI_BRANCH}" ]; then
618 CURRENT_BRANCH="${CI_BRANCH}"
620 CURRENT_BRANCH=$(current_git_branch)
624 update_boot_image() {
626 $ECHO "Deleting old images..."
627 $DELETE checksums.txt* > /dev/null 2>&1
628 # delete boot images with one or two characters after the dot
629 $DELETE $BOOT_IMAGE.{?,??} > /dev/null 2>&1
630 $DELETE temp/staging.*.image > /dev/null 2>&1
631 if [[ -f $BOOT_IMAGE ]] ; then
632 get_url $CHECKSUM_URL
633 local factorcode_md5=$(cat checksums.txt | grep $BOOT_IMAGE | cut -f2 -d' ')
635 local disk_md5=$($MD5SUM $BOOT_IMAGE | cut -f1 -d' ')
636 $ECHO "Factorcode md5: $factorcode_md5";
637 $ECHO "Disk md5: $disk_md5";
638 if [[ "$factorcode_md5" == "$disk_md5" ]] ; then
639 $ECHO "Your disk boot image matches the one on downloads.factorcode.org."
641 $DELETE $BOOT_IMAGE > /dev/null 2>&1
650 $ECHO "Downloading boot image $BOOT_IMAGE."
651 get_url "${BOOT_IMAGE_URL}"
655 if [[ -z $DOWNLOADER ]] ; then
658 $ECHO $DOWNLOADER $1 ;
660 check_ret $DOWNLOADER
665 check_installed_programs
670 $ECHO "Copying $FACTOR_IMAGE to $FACTOR_IMAGE_FRESH..."
671 $COPY $FACTOR_IMAGE $FACTOR_IMAGE_FRESH
675 ./$FACTOR_BINARY -i=$BOOT_IMAGE
697 download_and_bootstrap() {
702 net_bootstrap_no_pull() {
705 download_and_bootstrap
709 ./$FACTOR_BINARY -e="USING: vocabs.loader vocabs.refresh system memory ; refresh-all save 0 exit"
714 ./$FACTOR_BINARY -e="\"$MAKE_IMAGE_TARGET\" USING: system bootstrap.image memory ; make-image save 0 exit"
719 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
723 install_deps_pacman() {
724 sudo pacman --noconfirm -Syu gcc clang make rlwrap git wget pango glibc gtk2 gtk3 gtkglext gtk-engines gdk-pixbuf2 libx11 screen tmux
729 sudo dnf --assumeyes install gcc gcc-c++ glibc-devel binutils libX11-devel pango-devel gtk3-devel gdk-pixbuf2-devel gtkglext-devel tmux rlwrap wget
734 sudo pkg install --yes git gmake gcc rlwrap ripgrep curl gmake x11-toolkits/gtk30 x11-toolkits/gtkglext pango cairo vim
738 install_deps_macosx() {
739 test_program_installed git
740 if [[ $? -ne 1 ]] ; then
741 ensure_program_installed yes
742 $ECHO "git not found."
743 $ECHO "This script requires either git-core or port."
744 $ECHO "If it fails, install git-core or port and try again."
745 ensure_program_installed port
746 $ECHO "Installing git-core with port...this will take awhile."
747 yes | sudo port install git-core
752 $ECHO "usage: $0 command [optional-target]"
753 $ECHO " install - git clone, compile, bootstrap"
754 $ECHO " deps-apt - install required packages for Factor on Linux using apt"
755 $ECHO " deps-pacman - install required packages for Factor on Linux using pacman"
756 $ECHO " deps-dnf - install required packages for Factor on Linux using dnf"
757 $ECHO " deps-pkg - install required packages for Factor on FreeBSD using pkg"
758 $ECHO " deps-macosx - install git on MacOSX using port"
759 $ECHO " self-update - git pull, recompile, make local boot image, bootstrap"
760 $ECHO " quick-update - git pull, refresh-all, save"
761 $ECHO " update|latest - git pull, recompile, download a boot image, bootstrap"
762 $ECHO " compile - compile the binary"
763 $ECHO " recompile - recompile the binary"
764 $ECHO " bootstrap - bootstrap with existing boot image"
765 $ECHO " net-bootstrap - recompile, download a boot image, bootstrap"
766 $ECHO " make-target - find and print the os-arch-cpu string"
767 $ECHO " report|info - print the build variables"
768 $ECHO " update-boot-image - get the boot image for the current branch of for master"
770 $ECHO "If you are behind a firewall, invoke as:"
771 $ECHO "env GIT_PROTOCOL=http $0 <command>"
773 $ECHO "Example for overriding the default target:"
774 $ECHO " $0 update macosx-x86-32"
779 # -n is nonzero length, -z is zero length
780 if [[ -n "$2" ]] ; then
784 if [ "$#" -gt 3 ]; then
786 $ECHO "error: too many arguments"
796 deps-apt) install_deps_apt ;;
797 deps-pacman) install_deps_pacman ;;
798 deps-macosx) install_deps_macosx ;;
799 deps-dnf) install_deps_dnf ;;
800 deps-pkg) install_deps_pkg ;;
801 self-update) update; make_boot_image; bootstrap;;
802 quick-update) update; refresh_image ;;
803 update|latest) update; download_and_bootstrap ;;
804 compile) find_build_info; make_factor ;;
805 recompile) find_build_info; make_clean; make_factor ;;
806 bootstrap) get_config_info; bootstrap ;;
807 net-bootstrap) net_bootstrap_no_pull ;;
808 make-target) FIND_MAKE_TARGET=true; ECHO=false; find_build_info; exit_script ;;
809 report|info) find_build_info ;;
810 full-report) find_build_info; check_installed_programs; check_libraries ;;
811 update-boot-image) find_build_info; check_installed_programs; update_boot_image;;