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."
61 if [[ $RET -ne 0 ]] ; then
68 test_program_installed wget curl
69 if [[ $? -ne 0 ]] ; then
72 DOWNLOADER="curl -f -O"
77 test_program_installed md5sum
78 if [[ $? -ne 0 ]] ; then
87 openbsd) ensure_program_installed egcc; CC=egcc;;
94 netbsd) MAKE='gmake';;
95 freebsd) MAKE='gmake';;
96 openbsd) MAKE='gmake';;
97 dragonflybsd) MAKE='gmake';;
100 if [[ $MAKE = 'gmake' ]] ; then
101 ensure_program_installed gmake
105 check_installed_programs() {
106 ensure_program_installed chmod
107 ensure_program_installed uname
108 ensure_program_installed git
109 ensure_program_installed wget curl
110 ensure_program_installed gcc
111 ensure_program_installed g++ cl
112 ensure_program_installed make gmake
113 ensure_program_installed md5sum md5
114 ensure_program_installed cut
117 check_library_exists() {
118 GCC_TEST=factor-library-test.c
119 GCC_OUT=factor-library-test.out
120 $ECHO -n "Checking for library $1..."
121 $ECHO "int main(){return 0;}" > $GCC_TEST
122 $CC $GCC_TEST -o $GCC_OUT -l $1 2>&-
123 if [[ $? -ne 0 ]] ; then
125 $ECHO "***Factor will compile NO_UI=1"
136 check_X11_libraries() {
137 if [ -z "$NO_UI" ]; then
138 check_library_exists GL
139 check_library_exists X11
140 check_library_exists pango-1.0
144 check_gtk_libraries() {
145 if [ -z "$NO_UI" ]; then
146 check_library_exists gobject-2.0
147 check_library_exists gtk-x11-2.0
148 check_library_exists gdk-x11-2.0
149 check_library_exists gdk_pixbuf-2.0
150 check_library_exists gtkglext-x11-1.0
151 check_library_exists atk-1.0
152 check_library_exists gio-2.0
153 check_library_exists gdkglext-x11-1.0
154 check_library_exists pango-1.0
161 linux) check_X11_libraries
162 check_gtk_libraries;;
163 unix) check_gtk_libraries;;
167 check_factor_exists() {
168 if [[ -d "factor" ]] ; then
169 $ECHO "A directory called 'factor' already exists."
170 $ECHO "Rename or delete it and try again."
176 if [[ -n $OS ]] ; then return; fi
177 $ECHO "Finding OS..."
181 CYGWIN_NT-5.2-WOW64) OS=winnt;;
182 *CYGWIN_NT*) OS=winnt;;
185 *darwin*) OS=macosx;;
186 *Darwin*) OS=macosx;;
189 *NetBSD*) OS=netbsd;;
190 *FreeBSD*) OS=freebsd;;
191 *OpenBSD*) OS=openbsd;;
192 *DragonFly*) OS=dragonflybsd;;
197 find_architecture() {
198 if [[ -n $ARCH ]] ; then return; fi
199 $ECHO "Finding ARCH..."
210 "Power Macintosh") ARCH=ppc;;
214 write_test_program() {
216 echo "#include <stdio.h>" > $C_WORD.c
217 echo "int main(){printf(\"%ld\", (long)(8*sizeof(void*))); return 0; }" >> $C_WORD.c
221 $ECHO "Finding WORD..."
222 C_WORD=factor-word-size
224 gcc -o $C_WORD $C_WORD.c
230 intel_macosx_word_size() {
231 ensure_program_installed sysctl
232 $ECHO -n "Testing if your Intel Mac supports 64bit binaries..."
233 sysctl machdep.cpu.extfeatures | grep EM64T >/dev/null
234 if [[ $? -eq 0 ]] ; then
244 if [[ -n $WORD ]] ; then return; fi
245 if [[ $OS == macosx && $ARCH == x86 ]] ; then
246 intel_macosx_word_size
252 set_factor_binary() {
254 winnt) FACTOR_BINARY=factor.com;;
255 *) FACTOR_BINARY=factor;;
259 set_factor_library() {
261 winnt) FACTOR_LIBRARY=factor.dll;;
262 macosx) FACTOR_LIBRARY=libfactor.dylib;;
263 *) FACTOR_LIBRARY=libfactor.a;;
268 FACTOR_IMAGE=factor.image
269 FACTOR_IMAGE_FRESH=factor.image.fresh
276 $ECHO FACTOR_BINARY=$FACTOR_BINARY
277 $ECHO FACTOR_LIBRARY=$FACTOR_LIBRARY
278 $ECHO FACTOR_IMAGE=$FACTOR_IMAGE
279 $ECHO MAKE_TARGET=$MAKE_TARGET
280 $ECHO BOOT_IMAGE=$BOOT_IMAGE
281 $ECHO MAKE_IMAGE_TARGET=$MAKE_IMAGE_TARGET
282 $ECHO GIT_PROTOCOL=$GIT_PROTOCOL
283 $ECHO GIT_URL=$GIT_URL
284 $ECHO DOWNLOADER=$DOWNLOADER
291 check_os_arch_word() {
292 if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
296 $ECHO "OS, ARCH, or WORD is empty. Please report this."
305 if [[ $OS == macosx && $ARCH == ppc ]] ; then
306 MAKE_IMAGE_TARGET=macosx-ppc
307 MAKE_TARGET=macosx-ppc
308 elif [[ $OS == linux && $ARCH == ppc ]] ; then
309 MAKE_IMAGE_TARGET=linux-ppc.32
310 MAKE_TARGET=linux-ppc-32
311 elif [[ $OS == winnt && $ARCH == x86 && $WORD == 64 ]] ; then
312 MAKE_IMAGE_TARGET=winnt-x86.64
313 MAKE_TARGET=winnt-x86-64
314 elif [[ $OS == winnt && $ARCH == x86 && $WORD == 32 ]] ; then
315 MAKE_IMAGE_TARGET=winnt-x86.32
316 MAKE_TARGET=winnt-x86-32
317 elif [[ $ARCH == x86 && $WORD == 64 ]] ; then
318 MAKE_IMAGE_TARGET=unix-x86.64
319 MAKE_TARGET=$OS-x86-64
320 elif [[ $ARCH == x86 && $WORD == 32 ]] ; then
321 MAKE_IMAGE_TARGET=unix-x86.32
322 MAKE_TARGET=$OS-x86-32
324 MAKE_IMAGE_TARGET=$ARCH.$WORD
325 MAKE_TARGET=$OS-$ARCH-$WORD
327 BOOT_IMAGE=boot.$MAKE_IMAGE_TARGET.image
331 ensure_program_installed cut
332 $ECHO "Parsing make target from command line: $1"
333 OS=`echo $1 | cut -d '-' -f 1`
334 ARCH=`echo $1 | cut -d '-' -f 2`
335 WORD=`echo $1 | cut -d '-' -f 3`
337 if [[ $OS == linux && $ARCH == ppc ]] ; then WORD=32; fi
338 if [[ $OS == linux && $ARCH == arm ]] ; then WORD=32; fi
339 if [[ $OS == macosx && $ARCH == ppc ]] ; then WORD=32; fi
340 if [[ $OS == wince && $ARCH == arm ]] ; then WORD=32; fi
367 $ECHO "Downloading the git repository from factorcode.org..."
368 invoke_git clone $GIT_URL
371 update_script_name() {
372 $ECHO `dirname $0`/_update.sh
376 update_script=`update_script_name`
377 bash_path=`which bash`
378 $ECHO "#!$bash_path" >"$update_script"
379 $ECHO "git pull \"$GIT_URL\" master" >>"$update_script"
380 $ECHO "if [[ \$? -eq 0 ]]; then exec \"$0\" $SCRIPT_ARGS; else echo \"git pull failed\"; exit 2; fi" \
382 $ECHO "exit 0" >>"$update_script"
384 chmod 755 "$update_script"
385 exec "$update_script"
388 update_script_changed() {
389 invoke_git diff --stat `invoke_git merge-base HEAD FETCH_HEAD` FETCH_HEAD | grep 'build-support.factor\.sh' >/dev/null
392 git_fetch_factorcode() {
393 $ECHO "Fetching the git repository from factorcode.org..."
395 rm -f `update_script_name`
396 invoke_git fetch "$GIT_URL" master
398 if update_script_changed; then
399 $ECHO "Updating and restarting the factor.sh script..."
402 $ECHO "Updating the working tree..."
403 invoke_git pull "$GIT_URL" master
427 $ECHO "Backing up factor..."
428 $COPY $FACTOR_BINARY $FACTOR_BINARY.bak
429 $COPY $FACTOR_LIBRARY $FACTOR_LIBRARY.bak
430 $COPY $BOOT_IMAGE $BOOT_IMAGE.bak
431 $COPY $FACTOR_IMAGE $FACTOR_IMAGE.bak
432 $ECHO "Done with backup."
435 check_makefile_exists() {
436 if [[ ! -e "GNUmakefile" ]] ; then
438 $ECHO "***GNUmakefile not found***"
439 $ECHO "You are likely in the wrong directory."
440 $ECHO "Run this script from your factor directory:"
441 $ECHO " ./build-support/factor.sh"
447 check_makefile_exists
457 invoke_make NO_UI=$NO_UI $MAKE_TARGET -j5
460 update_boot_images() {
461 $ECHO "Deleting old images..."
462 $DELETE checksums.txt* > /dev/null 2>&1
463 # delete boot images with one or two characters after the dot
464 $DELETE $BOOT_IMAGE.{?,??} > /dev/null 2>&1
465 $DELETE temp/staging.*.image > /dev/null 2>&1
466 if [[ -f $BOOT_IMAGE ]] ; then
467 get_url http://downloads.factorcode.org/images/latest/checksums.txt
468 factorcode_md5=`cat checksums.txt|grep $BOOT_IMAGE|cut -f2 -d' '`;
471 netbsd) disk_md5=`md5 $BOOT_IMAGE | cut -f4 -d' '`;;
472 *) disk_md5=`$MD5SUM $BOOT_IMAGE|cut -f1 -d' '` ;;
474 $ECHO "Factorcode md5: $factorcode_md5";
475 $ECHO "Disk md5: $disk_md5";
476 if [[ "$factorcode_md5" == "$disk_md5" ]] ; then
477 $ECHO "Your disk boot image matches the one on factorcode.org."
479 $DELETE $BOOT_IMAGE > /dev/null 2>&1
488 $ECHO "Downloading boot image $BOOT_IMAGE."
489 get_url http://downloads.factorcode.org/images/latest/$BOOT_IMAGE
493 if [[ -z $DOWNLOADER ]] ; then
496 $ECHO $DOWNLOADER $1 ;
498 check_ret $DOWNLOADER
503 check_installed_programs
508 $ECHO "Copying $FACTOR_IMAGE to $FACTOR_IMAGE_FRESH..."
509 $COPY $FACTOR_IMAGE $FACTOR_IMAGE_FRESH
513 ./$FACTOR_BINARY -i=$BOOT_IMAGE
542 ./$FACTOR_BINARY -script -e="USING: vocabs.loader vocabs.refresh system memory ; refresh-all save 0 exit"
547 ./$FACTOR_BINARY -script -e="\"$MAKE_IMAGE_TARGET\" USING: system bootstrap.image memory ; make-image save 0 exit"
552 install_deps_linux() {
553 sudo apt-get --yes install libc6-dev libpango1.0-dev libx11-dev xorg-dev libgtk2.0-dev libgtkglext1-dev wget git-core git-doc rlwrap gcc make
557 install_deps_macosx() {
558 test_program_installed git
559 if [[ $? -ne 1 ]] ; then
560 ensure_program_installed yes
561 $ECHO "git not found."
562 $ECHO "This script requires either git-core or port."
563 $ECHO "If it fails, install git-core or port and try again."
564 ensure_program_installed port
565 $ECHO "Installing git-core with port...this will take awhile."
566 yes | sudo port install git-core
571 $ECHO "usage: $0 install|install-x11|install-macosx|self-update|quick-update|update|bootstrap|net-bootstrap|make-target|report [optional-target]"
572 $ECHO "If you are behind a firewall, invoke as:"
573 $ECHO "env GIT_PROTOCOL=http $0 <command>"
575 $ECHO "Example for overriding the default target:"
576 $ECHO " $0 update macosx-x86-32"
581 # -n is nonzero length, -z is zero length
582 if [[ -n "$2" ]] ; then
591 deps-linux) install_deps_linux ;;
592 deps-macosx) install_deps_macosx ;;
593 self-update) update; make_boot_image; bootstrap;;
594 quick-update) update; refresh_image ;;
595 update) update; update_bootstrap ;;
596 bootstrap) get_config_info; bootstrap ;;
597 report) find_build_info ;;
598 net-bootstrap) get_config_info; update_boot_images; bootstrap ;;
599 make-target) FIND_MAKE_TARGET=true; ECHO=false; find_build_info; exit_script ;;