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
97 test_program_installed curl
98 if [[ $?
-ne 0 ]] ; then
99 DOWNLOADER
="curl -f -O"
103 $ECHO "error: wget or curl required"
108 test_program_installed
md5sum
109 if [[ $?
-ne 0 ]] ; then
117 RE_SEMVER
="^([0-9]*)\.([0-9]*)\.([0-9]*)-?(.*)?$" # 3.3.3-5
118 CLANG_RE_OLD
="^([0-9]*)\.([0-9]*)-?(.*)?$" # 3.3-5
119 if [[ $1 =~
$RE_SEMVER ]] ; then
120 export "$2=${BASH_REMATCH[1]}"
121 export "$3=${BASH_REMATCH[2]}"
122 export "$4=${BASH_REMATCH[3]}"
123 export "$5=${BASH_REMATCH[4]}"
124 elif [[ $1 =~
$CLANG_RE_OLD ]] ; then
125 export "$2=${BASH_REMATCH[1]}"
126 export "$3=${BASH_REMATCH[2]}"
128 export "$5=${BASH_REMATCH[3]}"
130 echo "unsupported version number, please report a bug: $1"
136 CLANG_VERSION
=`clang --version | head -n1`
137 CLANG_VERSION_RE
='^[a-zA-Z0-9 ]* version (.*)$' # 3.3-5
138 if [[ $CLANG_VERSION =~
$CLANG_VERSION_RE ]] ; then
139 export "CLANG_VERSION=${BASH_REMATCH[1]}"
140 local CLANG_MAJOR
local CLANG_MINOR
local CLANG_PATCH
local CLANG_SPECIAL
141 semver_into
"$CLANG_VERSION" CLANG_MAJOR CLANG_MINOR CLANG_PATCH CLANG_SPECIAL
142 if [[ $CLANG_MAJOR -lt 3
143 ||
( $CLANG_MAJOR -eq 3 && $CLANG_MINOR -le 1 )
145 echo "clang version required >= 3.1, got $CLANG_VERSION"
155 test_programs_installed clang clang
++
156 if [[ $?
-ne 0 ]] && clang_version_ok
; then
157 [ -z "$CC" ] && CC
=clang
158 [ -z "$CXX" ] && CXX
=clang
++
162 test_programs_installed gcc g
++
163 if [[ $?
-ne 0 ]] ; then
164 [ -z "$CC" ] && CC
=gcc
165 [ -z "$CXX" ] && CXX
=g
++
169 $ECHO "error: high enough version of either (clang/clang++) or (gcc/g++) required!"
177 check_installed_programs
() {
178 ensure_program_installed
chmod
179 ensure_program_installed uname
180 ensure_program_installed git
181 ensure_program_installed wget curl
182 ensure_program_installed clang gcc
183 ensure_program_installed clang
++ g
++ cl
184 ensure_program_installed
make gmake
185 ensure_program_installed
md5sum md5
186 ensure_program_installed cut
189 check_library_exists
() {
190 GCC_TEST
=factor-library-test.c
191 GCC_OUT
=factor-library-test.out
192 $ECHO -n "Checking for library $1..."
193 $ECHO "int main(){return 0;}" > $GCC_TEST
194 $CC $GCC_TEST -o $GCC_OUT -l $1 2>&-
195 if [[ $?
-ne 0 ]] ; then
206 check_X11_libraries
() {
207 check_library_exists GL
208 check_library_exists X11
209 check_library_exists pango-1.0
212 check_gtk_libraries
() {
213 check_library_exists gobject-2.0
214 check_library_exists gtk-x11-2.0
215 check_library_exists gdk-x11-2.0
216 check_library_exists gdk_pixbuf-2.0
217 check_library_exists gtkglext-x11-1.0
218 check_library_exists atk-1.0
219 check_library_exists gio-2.0
220 check_library_exists gdkglext-x11-1.0
221 check_library_exists pango-1.0
227 linux
) check_X11_libraries
228 check_gtk_libraries
;;
229 unix
) check_gtk_libraries
;;
233 check_factor_exists
() {
234 if [[ -d "factor" ]] ; then
235 $ECHO "A directory called 'factor' already exists."
236 $ECHO "Rename or delete it and try again."
242 if [[ -n $OS ]] ; then return; fi
243 $ECHO "Finding OS..."
247 CYGWIN_NT-5.2
-WOW64) OS
=windows
;;
248 *CYGWIN_NT
*) OS
=windows
;;
249 *CYGWIN
*) OS
=windows
;;
250 MINGW32
*) OS
=windows
;;
251 *darwin
*) OS
=macosx
;;
252 *Darwin
*) OS
=macosx
;;
258 find_architecture
() {
259 if [[ -n $ARCH ]] ; then return; fi
260 $ECHO "Finding ARCH..."
271 "Power Macintosh") ARCH
=ppc
;;
276 $ECHO "Finding num cores..."
281 CYGWIN_NT-5.2
-WOW64 |
*CYGWIN_NT
* |
*CYGWIN
* | MINGW32
*) NUM_CORES
=$NUMBER_OF_PROCESSORS;;
282 *darwin
* |
*Darwin
* |
*linux
* |
*Linux
*) NUM_CORES
=$
(getconf _NPROCESSORS_ONLN
);;
286 write_test_program
() {
288 echo "#include <stdio.h>" > $C_WORD.c
289 echo "int main(){printf(\"%ld\", (long)(8*sizeof(void*))); return 0; }" >> $C_WORD.c
293 $ECHO "Finding WORD..."
294 C_WORD
=factor-word-size
296 $CC -o $C_WORD $C_WORD.c
302 intel_macosx_word_size
() {
303 ensure_program_installed sysctl
304 $ECHO -n "Testing if your Intel Mac supports 64bit binaries..."
305 sysctl machdep.cpu.extfeatures |
grep EM64T
>/dev
/null
306 if [[ $?
-eq 0 ]] ; then
316 if [[ -n $WORD ]] ; then return; fi
317 if [[ $OS == macosx
&& $ARCH == x86
]] ; then
318 intel_macosx_word_size
324 set_factor_binary
() {
326 windows
) FACTOR_BINARY
=factor.com
;;
327 *) FACTOR_BINARY
=factor;;
331 set_factor_library
() {
333 windows
) FACTOR_LIBRARY
=factor.dll
;;
334 macosx
) FACTOR_LIBRARY
=libfactor.dylib
;;
335 *) FACTOR_LIBRARY
=libfactor.a
;;
340 FACTOR_IMAGE
=factor.image
341 FACTOR_IMAGE_FRESH
=factor.image.fresh
347 $ECHO NUM_CORES
=$NUM_CORES
350 $ECHO CURRENT_BRANCH
=$CURRENT_BRANCH
351 $ECHO FACTOR_BINARY
=$FACTOR_BINARY
352 $ECHO FACTOR_LIBRARY
=$FACTOR_LIBRARY
353 $ECHO FACTOR_IMAGE
=$FACTOR_IMAGE
354 $ECHO MAKE_TARGET
=$MAKE_TARGET
355 $ECHO BOOT_IMAGE
=$BOOT_IMAGE
356 $ECHO MAKE_IMAGE_TARGET
=$MAKE_IMAGE_TARGET
357 $ECHO GIT_PROTOCOL
=$GIT_PROTOCOL
358 $ECHO GIT_URL
=$GIT_URL
359 $ECHO DOWNLOADER
=$DOWNLOADER
367 check_os_arch_word
() {
368 if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
372 $ECHO "OS, ARCH, or WORD is empty. Please report this."
381 if [[ $OS == linux
&& $ARCH == ppc
]] ; then
382 MAKE_IMAGE_TARGET
=linux-ppc
.32
383 MAKE_TARGET
=linux-ppc-32
384 elif [[ $OS == windows
&& $ARCH == x86
&& $WORD == 64 ]] ; then
385 MAKE_IMAGE_TARGET
=windows-x86.64
386 MAKE_TARGET
=windows-x86-64
387 elif [[ $OS == windows
&& $ARCH == x86
&& $WORD == 32 ]] ; then
388 MAKE_IMAGE_TARGET
=windows-x86.32
389 MAKE_TARGET
=windows-x86-32
390 elif [[ $ARCH == x86
&& $WORD == 64 ]] ; then
391 MAKE_IMAGE_TARGET
=unix-x86.64
392 MAKE_TARGET
=$OS-x86-64
393 elif [[ $ARCH == x86
&& $WORD == 32 ]] ; then
394 MAKE_IMAGE_TARGET
=unix-x86.32
395 MAKE_TARGET
=$OS-x86-32
397 MAKE_IMAGE_TARGET
=$ARCH.
$WORD
398 MAKE_TARGET
=$OS-$ARCH-$WORD
400 BOOT_IMAGE
=boot.
$MAKE_IMAGE_TARGET.image
404 ensure_program_installed cut
405 $ECHO "Parsing make target from command line: $1"
406 OS
=`echo $1 | cut -d '-' -f 1`
407 ARCH
=`echo $1 | cut -d '-' -f 2`
408 WORD
=`echo $1 | cut -d '-' -f 3`
410 if [[ $OS == linux
&& $ARCH == ppc
]] ; then WORD
=32; fi
411 if [[ $OS == linux
&& $ARCH == arm
]] ; then WORD
=32; fi
412 if [[ $OS == macosx
&& $ARCH == ppc
]] ; then WORD
=32; fi
441 $ECHO "Downloading the git repository from factorcode.org..."
442 invoke_git clone
$GIT_URL
445 update_script_name
() {
446 $ECHO `dirname $0`/_update.sh
450 update_script
=`update_script_name`
451 bash_path
=`which bash`
452 $ECHO "#!$bash_path" >"$update_script"
453 $ECHO "git pull \"$GIT_URL\" master" >>"$update_script"
454 $ECHO "if [[ \$? -eq 0 ]]; then exec \"$0\" $SCRIPT_ARGS; else echo \"git pull failed\"; exit 2; fi" \
456 $ECHO "exit 0" >>"$update_script"
458 chmod 755 "$update_script"
459 exec "$update_script"
462 update_script_changed
() {
463 invoke_git
diff --stat `invoke_git merge-base HEAD FETCH_HEAD` FETCH_HEAD |
grep 'build\.sh' >/dev
/null
466 git_fetch_factorcode
() {
467 $ECHO "Fetching the git repository from factorcode.org..."
469 rm -f `update_script_name`
470 invoke_git fetch
"$GIT_URL" master
472 if update_script_changed
; then
473 $ECHO "Updating and restarting the build.sh script..."
476 $ECHO "Updating the working tree..."
477 invoke_git pull
"$GIT_URL" master
501 $ECHO "Backing up factor..."
502 $COPY $FACTOR_BINARY $FACTOR_BINARY.bak
503 $COPY $FACTOR_LIBRARY $FACTOR_LIBRARY.bak
504 $COPY $BOOT_IMAGE $BOOT_IMAGE.bak
505 $COPY $FACTOR_IMAGE $FACTOR_IMAGE.bak
506 $ECHO "Done with backup."
509 check_makefile_exists
() {
510 if [[ ! -e "GNUmakefile" ]] ; then
512 $ECHO "***GNUmakefile not found***"
513 $ECHO "You are likely in the wrong directory."
514 $ECHO "Run this script from your factor directory:"
521 check_makefile_exists
531 $ECHO "Building factor with $NUM_CORES cores"
532 invoke_make CC
=$CC CXX
=$CXX $MAKE_TARGET -j$NUM_CORES
535 make_clean_factor
() {
540 current_git_branch
() {
541 git rev-parse
--abbrev-ref HEAD
545 if [[ $DOWNLOADER_NAME == 'wget' ]]; then
546 if [[ `wget -S --spider $1 2>&1 | grep 'HTTP/1.1 200 OK'` ]]; then
551 elif [[ $DOWNLOADER_NAME == 'curl' ]]; then
552 code
=`curl -sL -w "%{http_code}\\n" "$1" -o /dev/null`
553 if [[ $code -eq 200 ]]; then return 0; else return 1; fi
555 $ECHO "error: wget or curl required in check_url"
560 # If we are on a branch, first try to get a boot image for that branch.
561 # Otherwise, just use `master`
562 set_boot_image_vars
() {
564 url
="http://downloads.factorcode.org/images/${CURRENT_BRANCH}/checksums.txt"
566 if [[ $?
-eq 0 ]]; then
567 CHECKSUM_URL
="http://downloads.factorcode.org/images/${CURRENT_BRANCH}/checksums.txt"
568 BOOT_IMAGE_URL
="http://downloads.factorcode.org/images/${CURRENT_BRANCH}/${BOOT_IMAGE}"
570 CHECKSUM_URL
="http://downloads.factorcode.org/images/master/checksums.txt"
571 BOOT_IMAGE_URL
="http://downloads.factorcode.org/images/master/${BOOT_IMAGE}"
575 set_current_branch
() {
576 if [ -n "${CI_BRANCH}" ]; then
577 CURRENT_BRANCH
="${CI_BRANCH}"
579 CURRENT_BRANCH
=$
(current_git_branch
)
583 update_boot_image
() {
585 $ECHO "Deleting old images..."
586 $DELETE checksums.txt
* > /dev
/null
2>&1
587 # delete boot images with one or two characters after the dot
588 $DELETE $BOOT_IMAGE.
{?
,??
} > /dev
/null
2>&1
589 $DELETE temp
/staging.
*.image
> /dev
/null
2>&1
590 if [[ -f $BOOT_IMAGE ]] ; then
591 get_url
$CHECKSUM_URL
592 factorcode_md5
=`cat checksums.txt|grep $BOOT_IMAGE|cut -f2 -d' '`
594 disk_md5
=`$MD5SUM $BOOT_IMAGE|cut -f1 -d' '`
595 $ECHO "Factorcode md5: $factorcode_md5";
596 $ECHO "Disk md5: $disk_md5";
597 if [[ "$factorcode_md5" == "$disk_md5" ]] ; then
598 $ECHO "Your disk boot image matches the one on factorcode.org."
600 $DELETE $BOOT_IMAGE > /dev
/null
2>&1
609 $ECHO "Downloading boot image $BOOT_IMAGE."
610 get_url
"${BOOT_IMAGE_URL}"
614 if [[ -z $DOWNLOADER ]] ; then
617 $ECHO $DOWNLOADER $1 ;
619 check_ret
$DOWNLOADER
624 check_installed_programs
629 $ECHO "Copying $FACTOR_IMAGE to $FACTOR_IMAGE_FRESH..."
630 $COPY $FACTOR_IMAGE $FACTOR_IMAGE_FRESH
634 .
/$FACTOR_BINARY -i=$BOOT_IMAGE
656 download_and_bootstrap
() {
661 net_bootstrap_no_pull
() {
664 download_and_bootstrap
668 .
/$FACTOR_BINARY -script -e="USING: vocabs.loader vocabs.refresh system memory ; refresh-all save 0 exit"
673 .
/$FACTOR_BINARY -script -e="\"$MAKE_IMAGE_TARGET\" USING: system bootstrap.image memory ; make-image save 0 exit"
678 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 gcc
make screen tmux libssl-dev g
++
682 install_deps_pacman
() {
683 sudo pacman
--noconfirm -S gcc clang
make rlwrap git wget pango glibc gtk2 gtk3 gtkglext gtk-engines gdk-pixbuf2 libx11 screen tmux
688 sudo dnf
--assumeyes install gcc gcc-c
++ glibc-devel binutils libX11-devel pango-devel gtk3-devel gdk-pixbuf2-devel gtkglext-devel tmux rlwrap wget
693 install_deps_macosx
() {
694 test_program_installed git
695 if [[ $?
-ne 1 ]] ; then
696 ensure_program_installed
yes
697 $ECHO "git not found."
698 $ECHO "This script requires either git-core or port."
699 $ECHO "If it fails, install git-core or port and try again."
700 ensure_program_installed port
701 $ECHO "Installing git-core with port...this will take awhile."
702 yes | sudo port
install git-core
707 $ECHO "usage: $0 command [optional-target]"
708 $ECHO " install - git clone, compile, bootstrap"
709 $ECHO " deps-apt - install required packages for Factor on Linux using apt"
710 $ECHO " deps-pacman - install required packages for Factor on Linux using pacman"
711 $ECHO " deps-dnf - install required packages for Factor on Linux using dnf"
712 $ECHO " deps-macosx - install git on MacOSX using port"
713 $ECHO " self-update - git pull, recompile, make local boot image, bootstrap"
714 $ECHO " quick-update - git pull, refresh-all, save"
715 $ECHO " update|latest - git pull, recompile, download a boot image, bootstrap"
716 $ECHO " bootstrap - bootstrap with existing boot image"
717 $ECHO " net-bootstrap - recompile, download a boot image, bootstrap"
718 $ECHO " make-target - find and print the os-arch-cpu string"
719 $ECHO " report - print the build variables"
720 $ECHO " update-boot-image - get the boot image for the current branch of for master"
722 $ECHO "If you are behind a firewall, invoke as:"
723 $ECHO "env GIT_PROTOCOL=http $0 <command>"
725 $ECHO "Example for overriding the default target:"
726 $ECHO " $0 update macosx-x86-32"
731 # -n is nonzero length, -z is zero length
732 if [[ -n "$2" ]] ; then
741 deps-apt
) install_deps_apt
;;
742 deps-pacman
) install_deps_pacman
;;
743 deps-macosx
) install_deps_macosx
;;
744 deps-dnf
) install_deps_dnf
;;
745 self-update
) update
; make_boot_image
; bootstrap
;;
746 quick-update
) update
; refresh_image
;;
747 update
) update
; download_and_bootstrap
;;
748 latest
) update
; download_and_bootstrap
;;
749 bootstrap
) get_config_info
; bootstrap
;;
750 net-bootstrap
) net_bootstrap_no_pull
;;
751 make-target
) FIND_MAKE_TARGET
=true
; ECHO
=false
; find_build_info
; exit_script
;;
752 report
) find_build_info
;;
753 full-report
) find_build_info
; check_installed_programs
; check_libraries
;;
754 update-boot-image
) find_build_info
; check_installed_programs
; update_boot_image
;;