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"}
19 test_program_installed() {
20 if ! [[ -n `type -p $1` ]] ; then
27 if [[ $FIND_MAKE_TARGET = true ]] ; then
28 # Must be echo not $ECHO
34 ensure_program_installed() {
38 $ECHO -n "Checking for $i..."
39 test_program_installed $i
40 if [[ $? -eq 0 ]]; then
43 installed=$(( $installed + 1 ))
47 if [[ $installed -eq 0 ]] ; then
49 if [[ $# -eq 1 ]] ; then
52 $ECHO -n "any of [ $* ]"
54 $ECHO " and try again."
55 if [[ $OS == macosx ]] ; then
56 $ECHO "If you have Xcode 4.3 or higher installed, you must install the"
57 $ECHO "Command Line Tools from Xcode Preferences > Downloads in order"
58 $ECHO "to build Factor."
66 if [[ $RET -ne 0 ]] ; then
73 test_program_installed wget curl
74 if [[ $? -ne 0 ]] ; then
77 DOWNLOADER="curl -f -O"
82 test_program_installed md5sum
83 if [[ $? -ne 0 ]] ; then
93 xcode_major=`xcodebuild -version | sed -E -ne 's/^Xcode ([0-9]+).*$/\1/p'`
94 if [[ $xcode_major -ge 4 ]]; then
95 [ -z "$CC" ] && CC=clang
96 [ -z "$CXX" ] && CXX=clang++
98 [ -z "$CC" ] && CC=gcc
99 [ -z "$CXX" ] && CXX=g++
103 [ -z "$CC" ] && CC=gcc
104 [ -z "$CXX" ] && CXX=g++
114 BRANCH=`git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,'`
115 if [ "$BRANCH" != "master" ] ; then
116 $ECHO "git branch is $BRANCH, not master"
121 check_installed_programs() {
122 ensure_program_installed chmod
123 ensure_program_installed uname
124 ensure_program_installed git
125 ensure_program_installed wget curl
126 ensure_program_installed clang gcc
127 ensure_program_installed clang++ g++ cl
128 ensure_program_installed make gmake
129 ensure_program_installed md5sum md5
130 ensure_program_installed cut
133 check_library_exists() {
134 GCC_TEST=factor-library-test.c
135 GCC_OUT=factor-library-test.out
136 $ECHO -n "Checking for library $1..."
137 $ECHO "int main(){return 0;}" > $GCC_TEST
138 $CC $GCC_TEST -o $GCC_OUT -l $1 2>&-
139 if [[ $? -ne 0 ]] ; then
141 $ECHO "***Factor will compile NO_UI=1"
152 check_X11_libraries() {
153 if [ -z "$NO_UI" ]; then
154 check_library_exists GL
155 check_library_exists X11
156 check_library_exists pango-1.0
160 check_gtk_libraries() {
161 if [ -z "$NO_UI" ]; then
162 check_library_exists gobject-2.0
163 check_library_exists gtk-x11-2.0
164 check_library_exists gdk-x11-2.0
165 check_library_exists gdk_pixbuf-2.0
166 check_library_exists gtkglext-x11-1.0
167 check_library_exists atk-1.0
168 check_library_exists gio-2.0
169 check_library_exists gdkglext-x11-1.0
170 check_library_exists pango-1.0
177 linux) check_X11_libraries
178 check_gtk_libraries;;
179 unix) check_gtk_libraries;;
183 check_factor_exists() {
184 if [[ -d "factor" ]] ; then
185 $ECHO "A directory called 'factor' already exists."
186 $ECHO "Rename or delete it and try again."
192 if [[ -n $OS ]] ; then return; fi
193 $ECHO "Finding OS..."
197 CYGWIN_NT-5.2-WOW64) OS=windows;;
198 *CYGWIN_NT*) OS=windows;;
199 *CYGWIN*) OS=windows;;
200 MINGW32*) OS=windows;;
201 *darwin*) OS=macosx;;
202 *Darwin*) OS=macosx;;
208 find_architecture() {
209 if [[ -n $ARCH ]] ; then return; fi
210 $ECHO "Finding ARCH..."
221 "Power Macintosh") ARCH=ppc;;
225 write_test_program() {
227 echo "#include <stdio.h>" > $C_WORD.c
228 echo "int main(){printf(\"%ld\", (long)(8*sizeof(void*))); return 0; }" >> $C_WORD.c
232 $ECHO "Finding WORD..."
233 C_WORD=factor-word-size
235 $CC -o $C_WORD $C_WORD.c
241 intel_macosx_word_size() {
242 ensure_program_installed sysctl
243 $ECHO -n "Testing if your Intel Mac supports 64bit binaries..."
244 sysctl machdep.cpu.extfeatures | grep EM64T >/dev/null
245 if [[ $? -eq 0 ]] ; then
255 if [[ -n $WORD ]] ; then return; fi
256 if [[ $OS == macosx && $ARCH == x86 ]] ; then
257 intel_macosx_word_size
263 set_factor_binary() {
265 windows) FACTOR_BINARY=factor.com;;
266 *) FACTOR_BINARY=factor;;
270 set_factor_library() {
272 windows) FACTOR_LIBRARY=factor.dll;;
273 macosx) FACTOR_LIBRARY=libfactor.dylib;;
274 *) FACTOR_LIBRARY=libfactor.a;;
279 FACTOR_IMAGE=factor.image
280 FACTOR_IMAGE_FRESH=factor.image.fresh
287 $ECHO FACTOR_BINARY=$FACTOR_BINARY
288 $ECHO FACTOR_LIBRARY=$FACTOR_LIBRARY
289 $ECHO FACTOR_IMAGE=$FACTOR_IMAGE
290 $ECHO MAKE_TARGET=$MAKE_TARGET
291 $ECHO BOOT_IMAGE=$BOOT_IMAGE
292 $ECHO MAKE_IMAGE_TARGET=$MAKE_IMAGE_TARGET
293 $ECHO GIT_PROTOCOL=$GIT_PROTOCOL
294 $ECHO GIT_URL=$GIT_URL
295 $ECHO DOWNLOADER=$DOWNLOADER
303 check_os_arch_word() {
304 if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
308 $ECHO "OS, ARCH, or WORD is empty. Please report this."
317 if [[ $OS == linux && $ARCH == ppc ]] ; then
318 MAKE_IMAGE_TARGET=linux-ppc.32
319 MAKE_TARGET=linux-ppc-32
320 elif [[ $OS == windows && $ARCH == x86 && $WORD == 64 ]] ; then
321 MAKE_IMAGE_TARGET=windows-x86.64
322 MAKE_TARGET=windows-x86-64
323 elif [[ $OS == windows && $ARCH == x86 && $WORD == 32 ]] ; then
324 MAKE_IMAGE_TARGET=windows-x86.32
325 MAKE_TARGET=windows-x86-32
326 elif [[ $ARCH == x86 && $WORD == 64 ]] ; then
327 MAKE_IMAGE_TARGET=unix-x86.64
328 MAKE_TARGET=$OS-x86-64
329 elif [[ $ARCH == x86 && $WORD == 32 ]] ; then
330 MAKE_IMAGE_TARGET=unix-x86.32
331 MAKE_TARGET=$OS-x86-32
333 MAKE_IMAGE_TARGET=$ARCH.$WORD
334 MAKE_TARGET=$OS-$ARCH-$WORD
336 BOOT_IMAGE=boot.$MAKE_IMAGE_TARGET.image
340 ensure_program_installed cut
341 $ECHO "Parsing make target from command line: $1"
342 OS=`echo $1 | cut -d '-' -f 1`
343 ARCH=`echo $1 | cut -d '-' -f 2`
344 WORD=`echo $1 | cut -d '-' -f 3`
346 if [[ $OS == linux && $ARCH == ppc ]] ; then WORD=32; fi
347 if [[ $OS == linux && $ARCH == arm ]] ; then WORD=32; fi
348 if [[ $OS == macosx && $ARCH == ppc ]] ; then WORD=32; fi
375 $ECHO "Downloading the git repository from factorcode.org..."
376 invoke_git clone $GIT_URL
379 update_script_name() {
380 $ECHO `dirname $0`/_update.sh
384 update_script=`update_script_name`
385 bash_path=`which bash`
386 $ECHO "#!$bash_path" >"$update_script"
387 $ECHO "git pull \"$GIT_URL\" master" >>"$update_script"
388 $ECHO "if [[ \$? -eq 0 ]]; then exec \"$0\" $SCRIPT_ARGS; else echo \"git pull failed\"; exit 2; fi" \
390 $ECHO "exit 0" >>"$update_script"
392 chmod 755 "$update_script"
393 exec "$update_script"
396 update_script_changed() {
397 invoke_git diff --stat `invoke_git merge-base HEAD FETCH_HEAD` FETCH_HEAD | grep 'build-support.factor\.sh' >/dev/null
400 git_fetch_factorcode() {
401 $ECHO "Fetching the git repository from factorcode.org..."
403 rm -f `update_script_name`
404 invoke_git fetch "$GIT_URL" master
406 if update_script_changed; then
407 $ECHO "Updating and restarting the factor.sh script..."
410 $ECHO "Updating the working tree..."
411 invoke_git pull "$GIT_URL" master
435 $ECHO "Backing up factor..."
436 $COPY $FACTOR_BINARY $FACTOR_BINARY.bak
437 $COPY $FACTOR_LIBRARY $FACTOR_LIBRARY.bak
438 $COPY $BOOT_IMAGE $BOOT_IMAGE.bak
439 $COPY $FACTOR_IMAGE $FACTOR_IMAGE.bak
440 $ECHO "Done with backup."
443 check_makefile_exists() {
444 if [[ ! -e "GNUmakefile" ]] ; then
446 $ECHO "***GNUmakefile not found***"
447 $ECHO "You are likely in the wrong directory."
448 $ECHO "Run this script from your factor directory:"
449 $ECHO " ./build-support/factor.sh"
455 check_makefile_exists
465 invoke_make NO_UI=$NO_UI $MAKE_TARGET -j5
468 make_clean_factor() {
473 update_boot_images() {
474 $ECHO "Deleting old images..."
475 $DELETE checksums.txt* > /dev/null 2>&1
476 # delete boot images with one or two characters after the dot
477 $DELETE $BOOT_IMAGE.{?,??} > /dev/null 2>&1
478 $DELETE temp/staging.*.image > /dev/null 2>&1
479 if [[ -f $BOOT_IMAGE ]] ; then
480 get_url http://downloads.factorcode.org/images/latest/checksums.txt
481 factorcode_md5=`cat checksums.txt|grep $BOOT_IMAGE|cut -f2 -d' '`
483 disk_md5=`$MD5SUM $BOOT_IMAGE|cut -f1 -d' '`
484 $ECHO "Factorcode md5: $factorcode_md5";
485 $ECHO "Disk md5: $disk_md5";
486 if [[ "$factorcode_md5" == "$disk_md5" ]] ; then
487 $ECHO "Your disk boot image matches the one on factorcode.org."
489 $DELETE $BOOT_IMAGE > /dev/null 2>&1
498 $ECHO "Downloading boot image $BOOT_IMAGE."
499 get_url http://downloads.factorcode.org/images/latest/$BOOT_IMAGE
503 if [[ -z $DOWNLOADER ]] ; then
506 $ECHO $DOWNLOADER $1 ;
508 check_ret $DOWNLOADER
513 check_installed_programs
518 $ECHO "Copying $FACTOR_IMAGE to $FACTOR_IMAGE_FRESH..."
519 $COPY $FACTOR_IMAGE $FACTOR_IMAGE_FRESH
523 ./$FACTOR_BINARY -i=$BOOT_IMAGE
545 download_and_bootstrap() {
550 net_bootstrap_no_pull() {
553 download_and_bootstrap
557 ./$FACTOR_BINARY -script -e="USING: vocabs.loader vocabs.refresh system memory ; refresh-all save 0 exit"
562 ./$FACTOR_BINARY -script -e="\"$MAKE_IMAGE_TARGET\" USING: system bootstrap.image memory ; make-image save 0 exit"
566 install_deps_apt_get() {
567 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
571 install_deps_pacman() {
572 sudo pacman --noconfirm -S gcc clang make rlwrap git wget pango glibc gtk2 gtk3 gtkglext gtk-engines gdk-pixbuf2 libx11 screen tmux
576 install_deps_macosx() {
577 test_program_installed git
578 if [[ $? -ne 1 ]] ; then
579 ensure_program_installed yes
580 $ECHO "git not found."
581 $ECHO "This script requires either git-core or port."
582 $ECHO "If it fails, install git-core or port and try again."
583 ensure_program_installed port
584 $ECHO "Installing git-core with port...this will take awhile."
585 yes | sudo port install git-core
590 $ECHO "usage: $0 command [optional-target]"
591 $ECHO " install - git clone, compile, bootstrap"
592 $ECHO " deps-apt-get - install required packages for Factor on Linux using apt-get"
593 $ECHO " deps-pacman - install required packages for Factor on Linux using pacman"
594 $ECHO " deps-macosx - install git on MacOSX using port"
595 $ECHO " self-update - git pull, make local boot image, bootstrap"
596 $ECHO " quick-update - git pull, refresh-all, save"
597 $ECHO " update - git pull, download a boot image, recompile, bootstrap"
598 $ECHO " bootstrap - bootstrap with an existing boot image"
599 $ECHO " net-bootstrap - download a boot image, bootstrap"
600 $ECHO " make-target - find and print the os-arch-cpu string"
601 $ECHO " report - print the build variables"
603 $ECHO "If you are behind a firewall, invoke as:"
604 $ECHO "env GIT_PROTOCOL=http $0 <command>"
606 $ECHO "Example for overriding the default target:"
607 $ECHO " $0 update macosx-x86-32"
612 # -n is nonzero length, -z is zero length
613 if [[ -n "$2" ]] ; then
622 deps-apt-get) install_deps_apt_get ;;
623 deps-pacman) install_deps_pacman ;;
624 deps-macosx) install_deps_macosx ;;
625 self-update) update; make_boot_image; bootstrap;;
626 quick-update) update; refresh_image ;;
627 update) update; download_and_bootstrap ;;
628 bootstrap) get_config_info; bootstrap ;;
629 net-bootstrap) net_bootstrap_no_pull ;;
630 make-target) FIND_MAKE_TARGET=true; ECHO=false; find_build_info; exit_script ;;
631 report) find_build_info ;;