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"://factorcode.org/git/factor.git"}
19 test_program_installed() {
20 if ! [[ -n `type -p $1` ]] ; then
27 test_programs_installed() {
29 $ECHO -n "Checking for all($*)..."
32 test_program_installed $i
33 if [[ $? -eq 1 ]]; then
34 installed=$(( $installed + 1 ))
37 if [[ $installed -eq $# ]] ; then
41 $ECHO "all not found."
47 if [[ $FIND_MAKE_TARGET = true ]] ; then
48 # Must be echo not $ECHO
54 ensure_program_installed() {
56 $ECHO -n "Checking for any($*)..."
59 test_program_installed $i
60 if [[ $? -eq 1 ]]; then
62 installed=$(( $installed + 1 ))
68 if [[ $# -eq 1 ]] ; then
71 $ECHO -n "any of [ $* ]"
73 $ECHO " and try again."
74 if [[ $OS == macosx ]] ; then
75 $ECHO "If you have Xcode 4.3 or higher installed, you must install the"
76 $ECHO "Command Line Tools from Xcode Preferences > Downloads in order"
77 $ECHO "to build Factor."
84 if [[ $RET -ne 0 ]] ; then
91 test_program_installed wget
92 if [[ $? -ne 0 ]] ; then
96 test_program_installed curl
97 if [[ $? -ne 0 ]] ; then
98 DOWNLOADER="curl -f -O"
101 $ECHO "error: wget or curl required"
106 test_program_installed md5sum
107 if [[ $? -ne 0 ]] ; then
115 RE_SEMVER="^([0-9]*)\.([0-9]*)\.([0-9]*)-?(.*)?$" # 3.3.3-5
116 CLANG_RE_OLD="^([0-9]*)\.([0-9]*)-?(.*)?$" # 3.3-5
117 if [[ $1 =~ $RE_SEMVER ]] ; then
118 export "$2=${BASH_REMATCH[1]}"
119 export "$3=${BASH_REMATCH[2]}"
120 export "$4=${BASH_REMATCH[3]}"
121 export "$5=${BASH_REMATCH[4]}"
122 elif [[ $1 =~ $CLANG_RE_OLD ]] ; then
123 export "$2=${BASH_REMATCH[1]}"
124 export "$3=${BASH_REMATCH[2]}"
126 export "$5=${BASH_REMATCH[3]}"
128 echo "unsupported version number, please report a bug: $1"
135 GCC_VERSION=`gcc -dumpversion`
136 local GCC_MAJOR local GCC_MINOR local GCC_PATCH local GCC_SPECIAL
137 semver_into $GCC_VERSION GCC_MAJOR GCC_MINOR GCC_PATCH GCC_SPECIAL
139 if [[ $GCC_MAJOR -lt 4
140 || ( $GCC_MAJOR -eq 4 && $GCC_MINOR -lt 7 )
141 || ( $GCC_MAJOR -eq 4 && $GCC_MINOR -eq 7 && $GCC_PATCH -lt 3 )
142 || ( $GCC_MAJOR -eq 4 && $GCC_MINOR -eq 8 && $GCC_PATCH -eq 0 )
144 echo "gcc version required >= 4.7.3, != 4.8.0, >= 4.8.1, got $GCC_VERSION"
151 CLANG_VERSION=`clang --version | head -n1`
152 CLANG_VERSION_RE='^[a-zA-Z0-9 ]* version (.*)$' # 3.3-5
153 if [[ $CLANG_VERSION =~ $CLANG_VERSION_RE ]] ; then
154 export "CLANG_VERSION=${BASH_REMATCH[1]}"
155 local CLANG_MAJOR local CLANG_MINOR local CLANG_PATCH local CLANG_SPECIAL
156 semver_into "$CLANG_VERSION" CLANG_MAJOR CLANG_MINOR CLANG_PATCH CLANG_SPECIAL
157 if [[ $CLANG_MAJOR -lt 3
158 || ( $CLANG_MAJOR -eq 3 && $CLANG_MINOR -le 1 )
160 echo "clang version required >= 3.1, got $CLANG_VERSION"
170 test_programs_installed clang clang++
171 if [[ $? -ne 0 ]] && clang_version_ok ; then
172 [ -z "$CC" ] && CC=clang
173 [ -z "$CXX" ] && CXX=clang++
177 test_programs_installed gcc g++
178 if [[ $? -ne 0 ]] && gcc_version_ok ; then
179 [ -z "$CC" ] && CC=gcc
180 [ -z "$CXX" ] && CXX=g++
184 $ECHO "error: high enough version of either (clang/clang++) or (gcc/g++) required!"
192 check_installed_programs() {
193 ensure_program_installed chmod
194 ensure_program_installed uname
195 ensure_program_installed git
196 ensure_program_installed wget curl
197 ensure_program_installed clang gcc
198 ensure_program_installed clang++ g++ cl
199 ensure_program_installed make gmake
200 ensure_program_installed md5sum md5
201 ensure_program_installed cut
204 check_library_exists() {
205 GCC_TEST=factor-library-test.c
206 GCC_OUT=factor-library-test.out
207 $ECHO -n "Checking for library $1..."
208 $ECHO "int main(){return 0;}" > $GCC_TEST
209 $CC $GCC_TEST -o $GCC_OUT -l $1 2>&-
210 if [[ $? -ne 0 ]] ; then
221 check_X11_libraries() {
222 check_library_exists GL
223 check_library_exists X11
224 check_library_exists pango-1.0
227 check_gtk_libraries() {
228 check_library_exists gobject-2.0
229 check_library_exists gtk-x11-2.0
230 check_library_exists gdk-x11-2.0
231 check_library_exists gdk_pixbuf-2.0
232 check_library_exists gtkglext-x11-1.0
233 check_library_exists atk-1.0
234 check_library_exists gio-2.0
235 check_library_exists gdkglext-x11-1.0
236 check_library_exists pango-1.0
242 linux) check_X11_libraries
243 check_gtk_libraries;;
244 unix) check_gtk_libraries;;
248 check_factor_exists() {
249 if [[ -d "factor" ]] ; then
250 $ECHO "A directory called 'factor' already exists."
251 $ECHO "Rename or delete it and try again."
257 if [[ -n $OS ]] ; then return; fi
258 $ECHO "Finding OS..."
262 CYGWIN_NT-5.2-WOW64) OS=windows;;
263 *CYGWIN_NT*) OS=windows;;
264 *CYGWIN*) OS=windows;;
265 MINGW32*) OS=windows;;
266 *darwin*) OS=macosx;;
267 *Darwin*) OS=macosx;;
273 find_architecture() {
274 if [[ -n $ARCH ]] ; then return; fi
275 $ECHO "Finding ARCH..."
286 "Power Macintosh") ARCH=ppc;;
291 $ECHO "Finding num cores..."
296 CYGWIN_NT-5.2-WOW64 | *CYGWIN_NT* | *CYGWIN* | MINGW32*) NUM_CORES=$NUMBER_OF_PROCESSORS;;
297 *darwin* | *Darwin* | *linux* | *Linux*) NUM_CORES=$(getconf _NPROCESSORS_ONLN);;
301 write_test_program() {
303 echo "#include <stdio.h>" > $C_WORD.c
304 echo "int main(){printf(\"%ld\", (long)(8*sizeof(void*))); return 0; }" >> $C_WORD.c
308 $ECHO "Finding WORD..."
309 C_WORD=factor-word-size
311 $CC -o $C_WORD $C_WORD.c
317 intel_macosx_word_size() {
318 ensure_program_installed sysctl
319 $ECHO -n "Testing if your Intel Mac supports 64bit binaries..."
320 sysctl machdep.cpu.extfeatures | grep EM64T >/dev/null
321 if [[ $? -eq 0 ]] ; then
331 if [[ -n $WORD ]] ; then return; fi
332 if [[ $OS == macosx && $ARCH == x86 ]] ; then
333 intel_macosx_word_size
339 set_factor_binary() {
341 windows) FACTOR_BINARY=factor.com;;
342 *) FACTOR_BINARY=factor;;
346 set_factor_library() {
348 windows) FACTOR_LIBRARY=factor.dll;;
349 macosx) FACTOR_LIBRARY=libfactor.dylib;;
350 *) FACTOR_LIBRARY=libfactor.a;;
355 FACTOR_IMAGE=factor.image
356 FACTOR_IMAGE_FRESH=factor.image.fresh
362 $ECHO NUM_CORES=$NUM_CORES
365 $ECHO CURRENT_BRANCH=$CURRENT_BRANCH
366 $ECHO FACTOR_BINARY=$FACTOR_BINARY
367 $ECHO FACTOR_LIBRARY=$FACTOR_LIBRARY
368 $ECHO FACTOR_IMAGE=$FACTOR_IMAGE
369 $ECHO MAKE_TARGET=$MAKE_TARGET
370 $ECHO BOOT_IMAGE=$BOOT_IMAGE
371 $ECHO MAKE_IMAGE_TARGET=$MAKE_IMAGE_TARGET
372 $ECHO GIT_PROTOCOL=$GIT_PROTOCOL
373 $ECHO GIT_URL=$GIT_URL
374 $ECHO DOWNLOADER=$DOWNLOADER
382 check_os_arch_word() {
383 if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
387 $ECHO "OS, ARCH, or WORD is empty. Please report this."
396 if [[ $OS == linux && $ARCH == ppc ]] ; then
397 MAKE_IMAGE_TARGET=linux-ppc.32
398 MAKE_TARGET=linux-ppc-32
399 elif [[ $OS == windows && $ARCH == x86 && $WORD == 64 ]] ; then
400 MAKE_IMAGE_TARGET=windows-x86.64
401 MAKE_TARGET=windows-x86-64
402 elif [[ $OS == windows && $ARCH == x86 && $WORD == 32 ]] ; then
403 MAKE_IMAGE_TARGET=windows-x86.32
404 MAKE_TARGET=windows-x86-32
405 elif [[ $ARCH == x86 && $WORD == 64 ]] ; then
406 MAKE_IMAGE_TARGET=unix-x86.64
407 MAKE_TARGET=$OS-x86-64
408 elif [[ $ARCH == x86 && $WORD == 32 ]] ; then
409 MAKE_IMAGE_TARGET=unix-x86.32
410 MAKE_TARGET=$OS-x86-32
412 MAKE_IMAGE_TARGET=$ARCH.$WORD
413 MAKE_TARGET=$OS-$ARCH-$WORD
415 BOOT_IMAGE=boot.$MAKE_IMAGE_TARGET.image
419 ensure_program_installed cut
420 $ECHO "Parsing make target from command line: $1"
421 OS=`echo $1 | cut -d '-' -f 1`
422 ARCH=`echo $1 | cut -d '-' -f 2`
423 WORD=`echo $1 | cut -d '-' -f 3`
425 if [[ $OS == linux && $ARCH == ppc ]] ; then WORD=32; fi
426 if [[ $OS == linux && $ARCH == arm ]] ; then WORD=32; fi
427 if [[ $OS == macosx && $ARCH == ppc ]] ; then WORD=32; fi
456 $ECHO "Downloading the git repository from factorcode.org..."
457 invoke_git clone $GIT_URL
460 update_script_name() {
461 $ECHO `dirname $0`/_update.sh
465 update_script=`update_script_name`
466 bash_path=`which bash`
467 $ECHO "#!$bash_path" >"$update_script"
468 $ECHO "git pull \"$GIT_URL\" master" >>"$update_script"
469 $ECHO "if [[ \$? -eq 0 ]]; then exec \"$0\" $SCRIPT_ARGS; else echo \"git pull failed\"; exit 2; fi" \
471 $ECHO "exit 0" >>"$update_script"
473 chmod 755 "$update_script"
474 exec "$update_script"
477 update_script_changed() {
478 invoke_git diff --stat `invoke_git merge-base HEAD FETCH_HEAD` FETCH_HEAD | grep 'build\.sh' >/dev/null
481 git_fetch_factorcode() {
482 $ECHO "Fetching the git repository from factorcode.org..."
484 rm -f `update_script_name`
485 invoke_git fetch "$GIT_URL" master
487 if update_script_changed; then
488 $ECHO "Updating and restarting the build.sh script..."
491 $ECHO "Updating the working tree..."
492 invoke_git pull "$GIT_URL" master
516 $ECHO "Backing up factor..."
517 $COPY $FACTOR_BINARY $FACTOR_BINARY.bak
518 $COPY $FACTOR_LIBRARY $FACTOR_LIBRARY.bak
519 $COPY $BOOT_IMAGE $BOOT_IMAGE.bak
520 $COPY $FACTOR_IMAGE $FACTOR_IMAGE.bak
521 $ECHO "Done with backup."
524 check_makefile_exists() {
525 if [[ ! -e "GNUmakefile" ]] ; then
527 $ECHO "***GNUmakefile not found***"
528 $ECHO "You are likely in the wrong directory."
529 $ECHO "Run this script from your factor directory:"
536 check_makefile_exists
546 $ECHO "Building factor with $NUM_CORES cores"
547 invoke_make CC=$CC CXX=$CXX $MAKE_TARGET -j$NUM_CORES
550 make_clean_factor() {
555 current_git_branch() {
556 git rev-parse --abbrev-ref HEAD
560 if [ -z ${TRAVIS_BRANCH} ]; then
561 CURRENT_BRANCH=$(current_git_branch)
563 CURRENT_BRANCH=${TRAVIS_BRANCH}
568 branch=$(current_git_branch)
569 echo "http://downloads.factorcode.org/images/$branch/checksums.txt"
572 update_boot_images() {
573 $ECHO "Deleting old images..."
574 $DELETE checksums.txt* > /dev/null 2>&1
575 # delete boot images with one or two characters after the dot
576 $DELETE $BOOT_IMAGE.{?,??} > /dev/null 2>&1
577 $DELETE temp/staging.*.image > /dev/null 2>&1
578 if [[ -f $BOOT_IMAGE ]] ; then
579 get_url $(checksum_url)
580 factorcode_md5=`cat checksums.txt|grep $BOOT_IMAGE|cut -f2 -d' '`
582 disk_md5=`$MD5SUM $BOOT_IMAGE|cut -f1 -d' '`
583 $ECHO "Factorcode md5: $factorcode_md5";
584 $ECHO "Disk md5: $disk_md5";
585 if [[ "$factorcode_md5" == "$disk_md5" ]] ; then
586 $ECHO "Your disk boot image matches the one on factorcode.org."
588 $DELETE $BOOT_IMAGE > /dev/null 2>&1
597 branch=$(current_git_branch)
598 echo "http://downloads.factorcode.org/images/$branch/$BOOT_IMAGE"
602 $ECHO "Downloading boot image $BOOT_IMAGE."
603 get_url $(boot_image_url)
607 if [[ -z $DOWNLOADER ]] ; then
610 $ECHO $DOWNLOADER $1 ;
612 check_ret $DOWNLOADER
617 check_installed_programs
622 $ECHO "Copying $FACTOR_IMAGE to $FACTOR_IMAGE_FRESH..."
623 $COPY $FACTOR_IMAGE $FACTOR_IMAGE_FRESH
627 ./$FACTOR_BINARY -i=$BOOT_IMAGE
648 download_and_bootstrap() {
653 net_bootstrap_no_pull() {
656 download_and_bootstrap
660 ./$FACTOR_BINARY -script -e="USING: vocabs.loader vocabs.refresh system memory ; refresh-all save 0 exit"
665 ./$FACTOR_BINARY -script -e="\"$MAKE_IMAGE_TARGET\" USING: system bootstrap.image memory ; make-image save 0 exit"
669 install_deps_apt_get() {
670 sudo apt-get --yes install libc6-dev libpango1.0-dev libx11-dev xorg-dev libgtk2.0-dev gtk2-engines-pixbuf libgtkglext1-dev wget git git-doc rlwrap clang gcc make screen tmux libssl-dev g++
674 install_deps_pacman() {
675 sudo pacman --noconfirm -S gcc clang make rlwrap git wget pango glibc gtk2 gtk3 gtkglext gtk-engines gdk-pixbuf2 libx11 screen tmux
680 sudo dnf --assumeyes install gcc gcc-c++ glibc-devel binutils libX11-devel pango-devel gtk3-devel gdk-pixbuf2-devel gtkglext-devel tmux rlwrap wget
685 install_deps_macosx() {
686 test_program_installed git
687 if [[ $? -ne 1 ]] ; then
688 ensure_program_installed yes
689 $ECHO "git not found."
690 $ECHO "This script requires either git-core or port."
691 $ECHO "If it fails, install git-core or port and try again."
692 ensure_program_installed port
693 $ECHO "Installing git-core with port...this will take awhile."
694 yes | sudo port install git-core
699 $ECHO "usage: $0 command [optional-target]"
700 $ECHO " install - git clone, compile, bootstrap"
701 $ECHO " deps-apt-get - install required packages for Factor on Linux using apt-get"
702 $ECHO " deps-pacman - install required packages for Factor on Linux using pacman"
703 $ECHO " deps-dnf - install required packages for Factor on Linux using dnf"
704 $ECHO " deps-macosx - install git on MacOSX using port"
705 $ECHO " self-update - git pull, recompile, make local boot image, bootstrap"
706 $ECHO " quick-update - git pull, refresh-all, save"
707 $ECHO " update|latest - git pull, recompile, download a boot image, bootstrap"
708 $ECHO " bootstrap - bootstrap with existing boot image"
709 $ECHO " net-bootstrap - recompile, download a boot image, bootstrap"
710 $ECHO " make-target - find and print the os-arch-cpu string"
711 $ECHO " report - print the build variables"
713 $ECHO "If you are behind a firewall, invoke as:"
714 $ECHO "env GIT_PROTOCOL=http $0 <command>"
716 $ECHO "Example for overriding the default target:"
717 $ECHO " $0 update macosx-x86-32"
722 # -n is nonzero length, -z is zero length
723 if [[ -n "$2" ]] ; then
732 deps-apt-get) install_deps_apt_get ;;
733 deps-pacman) install_deps_pacman ;;
734 deps-macosx) install_deps_macosx ;;
735 deps-dnf) install_deps_dnf ;;
736 self-update) update; make_boot_image; bootstrap;;
737 quick-update) update; refresh_image ;;
738 update) update; download_and_bootstrap ;;
739 latest) update; download_and_bootstrap ;;
740 bootstrap) get_config_info; bootstrap ;;
741 net-bootstrap) net_bootstrap_no_pull ;;
742 make-target) FIND_MAKE_TARGET=true; ECHO=false; find_build_info; exit_script ;;
743 report) find_build_info ;;
744 full-report) find_build_info; check_installed_programs; check_libraries ;;