3 # Programs returning != 0 will not cause script to exit
6 # Case insensitive string comparison
15 GIT_PROTOCOL=${GIT_PROTOCOL:="git"}
16 GIT_URL=${GIT_URL:=$GIT_PROTOCOL"://factorcode.org/git/factor.git"}
20 test_program_installed() {
21 if ! [[ -n `type -p $1` ]] ; then
28 test_programs_installed() {
30 $ECHO -n "Checking for all($*)..."
33 test_program_installed $i
34 if [[ $? -eq 1 ]]; then
35 installed=$(( $installed + 1 ))
38 if [[ $installed -eq $# ]] ; then
42 $ECHO "all not found."
48 if [[ $FIND_MAKE_TARGET = true ]] ; then
49 # Must be echo not $ECHO
55 ensure_program_installed() {
57 $ECHO -n "Checking for any($*)..."
60 test_program_installed $i
61 if [[ $? -eq 1 ]]; then
63 installed=$(( $installed + 1 ))
69 if [[ $# -eq 1 ]] ; then
72 $ECHO -n "any of [ $* ]"
74 $ECHO " and try again."
75 if [[ $OS == macosx ]] ; then
76 $ECHO "If you have Xcode 4.3 or higher installed, you must install the"
77 $ECHO "Command Line Tools from Xcode Preferences > Downloads in order"
78 $ECHO "to build Factor."
85 if [[ $RET -ne 0 ]] ; then
92 test_program_installed wget
93 if [[ $? -ne 0 ]] ; then
97 test_program_installed curl
98 if [[ $? -ne 0 ]] ; then
99 DOWNLOADER="curl -f -O"
102 $ECHO "error: wget or curl required"
107 test_program_installed md5sum
108 if [[ $? -ne 0 ]] ; then
116 RE_SEMVER="^([0-9]*)\.([0-9]*)\.([0-9]*)-?(.*)?$" # 3.3.3-5
117 CLANG_RE_OLD="^([0-9]*)\.([0-9]*)-?(.*)?$" # 3.3-5
118 if [[ $1 =~ $RE_SEMVER ]] ; then
119 export "$2=${BASH_REMATCH[1]}"
120 export "$3=${BASH_REMATCH[2]}"
121 export "$4=${BASH_REMATCH[3]}"
122 export "$5=${BASH_REMATCH[4]}"
123 elif [[ $1 =~ $CLANG_RE_OLD ]] ; then
124 export "$2=${BASH_REMATCH[1]}"
125 export "$3=${BASH_REMATCH[2]}"
127 export "$5=${BASH_REMATCH[3]}"
129 echo "unsupported version number, please report a bug: $1"
136 GCC_VERSION=`gcc -dumpversion`
137 local GCC_MAJOR local GCC_MINOR local GCC_PATCH local GCC_SPECIAL
138 semver_into $GCC_VERSION GCC_MAJOR GCC_MINOR GCC_PATCH GCC_SPECIAL
140 if [[ $GCC_MAJOR -lt 4
141 || ( $GCC_MAJOR -eq 4 && $GCC_MINOR -lt 7 )
142 || ( $GCC_MAJOR -eq 4 && $GCC_MINOR -eq 7 && $GCC_PATCH -lt 3 )
143 || ( $GCC_MAJOR -eq 4 && $GCC_MINOR -eq 8 && $GCC_PATCH -eq 0 )
145 echo "gcc version required >= 4.7.3, != 4.8.0, >= 4.8.1, got $GCC_VERSION"
152 CLANG_VERSION=`clang --version | head -n1`
153 CLANG_VERSION_RE='^[a-zA-Z0-9 ]* version (.*)$' # 3.3-5
154 if [[ $CLANG_VERSION =~ $CLANG_VERSION_RE ]] ; then
155 export "CLANG_VERSION=${BASH_REMATCH[1]}"
156 local CLANG_MAJOR local CLANG_MINOR local CLANG_PATCH local CLANG_SPECIAL
157 semver_into "$CLANG_VERSION" CLANG_MAJOR CLANG_MINOR CLANG_PATCH CLANG_SPECIAL
158 if [[ $CLANG_MAJOR -lt 3
159 || ( $CLANG_MAJOR -eq 3 && $CLANG_MINOR -le 1 )
161 echo "clang version required >= 3.1, got $CLANG_VERSION"
171 test_programs_installed clang clang++
172 if [[ $? -ne 0 ]] && clang_version_ok ; then
173 [ -z "$CC" ] && CC=clang
174 [ -z "$CXX" ] && CXX=clang++
178 test_programs_installed gcc g++
179 if [[ $? -ne 0 ]] && gcc_version_ok ; then
180 [ -z "$CC" ] && CC=gcc
181 [ -z "$CXX" ] && CXX=g++
185 $ECHO "error: high enough version of either (clang/clang++) or (gcc/g++) required!"
194 BRANCH=`git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,'`
195 if [ "$BRANCH" != "master" ] ; then
196 $ECHO "git branch is $BRANCH, not master"
201 check_installed_programs() {
202 ensure_program_installed chmod
203 ensure_program_installed uname
204 ensure_program_installed git
205 ensure_program_installed wget curl
206 ensure_program_installed clang gcc
207 ensure_program_installed clang++ g++ cl
208 ensure_program_installed make gmake
209 ensure_program_installed md5sum md5
210 ensure_program_installed cut
213 check_library_exists() {
214 GCC_TEST=factor-library-test.c
215 GCC_OUT=factor-library-test.out
216 $ECHO -n "Checking for library $1..."
217 $ECHO "int main(){return 0;}" > $GCC_TEST
218 $CC $GCC_TEST -o $GCC_OUT -l $1 2>&-
219 if [[ $? -ne 0 ]] ; then
221 $ECHO "***Factor will compile NO_UI=1"
232 check_X11_libraries() {
233 if [ -z "$NO_UI" ]; then
234 check_library_exists GL
235 check_library_exists X11
236 check_library_exists pango-1.0
240 check_gtk_libraries() {
241 if [ -z "$NO_UI" ]; then
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
257 linux) check_X11_libraries
258 check_gtk_libraries;;
259 unix) check_gtk_libraries;;
263 check_factor_exists() {
264 if [[ -d "factor" ]] ; then
265 $ECHO "A directory called 'factor' already exists."
266 $ECHO "Rename or delete it and try again."
272 if [[ -n $OS ]] ; then return; fi
273 $ECHO "Finding OS..."
277 CYGWIN_NT-5.2-WOW64) OS=windows;;
278 *CYGWIN_NT*) OS=windows;;
279 *CYGWIN*) OS=windows;;
280 MINGW32*) OS=windows;;
281 *darwin*) OS=macosx;;
282 *Darwin*) OS=macosx;;
288 find_architecture() {
289 if [[ -n $ARCH ]] ; then return; fi
290 $ECHO "Finding ARCH..."
301 "Power Macintosh") ARCH=ppc;;
305 write_test_program() {
307 echo "#include <stdio.h>" > $C_WORD.c
308 echo "int main(){printf(\"%ld\", (long)(8*sizeof(void*))); return 0; }" >> $C_WORD.c
312 $ECHO "Finding WORD..."
313 C_WORD=factor-word-size
315 $CC -o $C_WORD $C_WORD.c
321 intel_macosx_word_size() {
322 ensure_program_installed sysctl
323 $ECHO -n "Testing if your Intel Mac supports 64bit binaries..."
324 sysctl machdep.cpu.extfeatures | grep EM64T >/dev/null
325 if [[ $? -eq 0 ]] ; then
335 if [[ -n $WORD ]] ; then return; fi
336 if [[ $OS == macosx && $ARCH == x86 ]] ; then
337 intel_macosx_word_size
343 set_factor_binary() {
345 windows) FACTOR_BINARY=factor.com;;
346 *) FACTOR_BINARY=factor;;
350 set_factor_library() {
352 windows) FACTOR_LIBRARY=factor.dll;;
353 macosx) FACTOR_LIBRARY=libfactor.dylib;;
354 *) FACTOR_LIBRARY=libfactor.a;;
359 FACTOR_IMAGE=factor.image
360 FACTOR_IMAGE_FRESH=factor.image.fresh
368 $ECHO FACTOR_BINARY=$FACTOR_BINARY
369 $ECHO FACTOR_LIBRARY=$FACTOR_LIBRARY
370 $ECHO FACTOR_IMAGE=$FACTOR_IMAGE
371 $ECHO MAKE_TARGET=$MAKE_TARGET
372 $ECHO BOOT_IMAGE=$BOOT_IMAGE
373 $ECHO MAKE_IMAGE_TARGET=$MAKE_IMAGE_TARGET
374 $ECHO GIT_PROTOCOL=$GIT_PROTOCOL
375 $ECHO GIT_URL=$GIT_URL
376 $ECHO DOWNLOADER=$DOWNLOADER
384 check_os_arch_word() {
385 if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
389 $ECHO "OS, ARCH, or WORD is empty. Please report this."
398 if [[ $OS == linux && $ARCH == ppc ]] ; then
399 MAKE_IMAGE_TARGET=linux-ppc.32
400 MAKE_TARGET=linux-ppc-32
401 elif [[ $OS == windows && $ARCH == x86 && $WORD == 64 ]] ; then
402 MAKE_IMAGE_TARGET=windows-x86.64
403 MAKE_TARGET=windows-x86-64
404 elif [[ $OS == windows && $ARCH == x86 && $WORD == 32 ]] ; then
405 MAKE_IMAGE_TARGET=windows-x86.32
406 MAKE_TARGET=windows-x86-32
407 elif [[ $ARCH == x86 && $WORD == 64 ]] ; then
408 MAKE_IMAGE_TARGET=unix-x86.64
409 MAKE_TARGET=$OS-x86-64
410 elif [[ $ARCH == x86 && $WORD == 32 ]] ; then
411 MAKE_IMAGE_TARGET=unix-x86.32
412 MAKE_TARGET=$OS-x86-32
414 MAKE_IMAGE_TARGET=$ARCH.$WORD
415 MAKE_TARGET=$OS-$ARCH-$WORD
417 BOOT_IMAGE=boot.$MAKE_IMAGE_TARGET.image
421 ensure_program_installed cut
422 $ECHO "Parsing make target from command line: $1"
423 OS=`echo $1 | cut -d '-' -f 1`
424 ARCH=`echo $1 | cut -d '-' -f 2`
425 WORD=`echo $1 | cut -d '-' -f 3`
427 if [[ $OS == linux && $ARCH == ppc ]] ; then WORD=32; fi
428 if [[ $OS == linux && $ARCH == arm ]] ; then WORD=32; fi
429 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 invoke_make CC=$CC CXX=$CXX NO_UI=$NO_UI $MAKE_TARGET -j5
549 make_clean_factor() {
554 update_boot_images() {
555 $ECHO "Deleting old images..."
556 $DELETE checksums.txt* > /dev/null 2>&1
557 # delete boot images with one or two characters after the dot
558 $DELETE $BOOT_IMAGE.{?,??} > /dev/null 2>&1
559 $DELETE temp/staging.*.image > /dev/null 2>&1
560 if [[ -f $BOOT_IMAGE ]] ; then
561 get_url http://downloads.factorcode.org/images/latest/checksums.txt
562 factorcode_md5=`cat checksums.txt|grep $BOOT_IMAGE|cut -f2 -d' '`
564 disk_md5=`$MD5SUM $BOOT_IMAGE|cut -f1 -d' '`
565 $ECHO "Factorcode md5: $factorcode_md5";
566 $ECHO "Disk md5: $disk_md5";
567 if [[ "$factorcode_md5" == "$disk_md5" ]] ; then
568 $ECHO "Your disk boot image matches the one on factorcode.org."
570 $DELETE $BOOT_IMAGE > /dev/null 2>&1
579 $ECHO "Downloading boot image $BOOT_IMAGE."
580 get_url http://downloads.factorcode.org/images/latest/$BOOT_IMAGE
584 if [[ -z $DOWNLOADER ]] ; then
587 $ECHO $DOWNLOADER $1 ;
589 check_ret $DOWNLOADER
594 check_installed_programs
599 $ECHO "Copying $FACTOR_IMAGE to $FACTOR_IMAGE_FRESH..."
600 $COPY $FACTOR_IMAGE $FACTOR_IMAGE_FRESH
604 ./$FACTOR_BINARY -i=$BOOT_IMAGE
626 download_and_bootstrap() {
631 net_bootstrap_no_pull() {
634 download_and_bootstrap
638 ./$FACTOR_BINARY -script -e="USING: vocabs.loader vocabs.refresh system memory ; refresh-all save 0 exit"
643 ./$FACTOR_BINARY -script -e="\"$MAKE_IMAGE_TARGET\" USING: system bootstrap.image memory ; make-image save 0 exit"
647 install_deps_apt_get() {
648 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++
652 install_deps_pacman() {
653 sudo pacman --noconfirm -S gcc clang make rlwrap git wget pango glibc gtk2 gtk3 gtkglext gtk-engines gdk-pixbuf2 libx11 screen tmux
658 sudo dnf --assumeyes install gcc gcc-c++ glibc-devel binutils libX11-devel pango-devel gtk3-devel gdk-pixbuf2-devel gtkglext-devel tmux rlwrap wget
663 install_deps_macosx() {
664 test_program_installed git
665 if [[ $? -ne 1 ]] ; then
666 ensure_program_installed yes
667 $ECHO "git not found."
668 $ECHO "This script requires either git-core or port."
669 $ECHO "If it fails, install git-core or port and try again."
670 ensure_program_installed port
671 $ECHO "Installing git-core with port...this will take awhile."
672 yes | sudo port install git-core
677 $ECHO "usage: $0 command [optional-target]"
678 $ECHO " install - git clone, compile, bootstrap"
679 $ECHO " deps-apt-get - install required packages for Factor on Linux using apt-get"
680 $ECHO " deps-pacman - install required packages for Factor on Linux using pacman"
681 $ECHO " deps-dnf - install required packages for Factor on Linux using dnf"
682 $ECHO " deps-macosx - install git on MacOSX using port"
683 $ECHO " self-update - git pull, recompile, make local boot image, bootstrap"
684 $ECHO " quick-update - git pull, refresh-all, save"
685 $ECHO " update|latest - git pull, recompile, download a boot image, bootstrap"
686 $ECHO " bootstrap - bootstrap with existing boot image"
687 $ECHO " net-bootstrap - recompile, download a boot image, bootstrap"
688 $ECHO " make-target - find and print the os-arch-cpu string"
689 $ECHO " report - print the build variables"
691 $ECHO "If you are behind a firewall, invoke as:"
692 $ECHO "env GIT_PROTOCOL=http $0 <command>"
694 $ECHO "Example for overriding the default target:"
695 $ECHO " $0 update macosx-x86-32"
700 # -n is nonzero length, -z is zero length
701 if [[ -n "$2" ]] ; then
710 deps-apt-get) install_deps_apt_get ;;
711 deps-pacman) install_deps_pacman ;;
712 deps-macosx) install_deps_macosx ;;
713 deps-dnf) install_deps_dnf ;;
714 self-update) update; make_boot_image; bootstrap;;
715 quick-update) update; refresh_image ;;
716 update) update; download_and_bootstrap ;;
717 latest) update; download_and_bootstrap ;;
718 bootstrap) get_config_info; bootstrap ;;
719 net-bootstrap) net_bootstrap_no_pull ;;
720 make-target) FIND_MAKE_TARGET=true; ECHO=false; find_build_info; exit_script ;;
721 report) find_build_info ;;
722 full-report) find_build_info; check_installed_programs; check_libraries ;;