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"}
18 REQUIRE_CLANG_VERSION=3.1
21 test_program_installed() {
22 if ! [[ -n $(type -p $1) ]] ; then
29 test_programs_installed() {
31 $ECHO -n "Checking for all($*)..."
34 test_program_installed $i
35 if [[ $? -eq 1 ]]; then
36 installed=$(( $installed + 1 ))
39 if [[ $installed -eq $# ]] ; then
43 $ECHO "all not found."
49 if [[ $FIND_MAKE_TARGET = true ]] ; then
50 # Must be echo not $ECHO
56 ensure_program_installed() {
58 $ECHO -n "Checking for any($*)..."
61 test_program_installed $i
62 if [[ $? -eq 1 ]]; then
64 installed=$(( $installed + 1 ))
70 if [[ $# -eq 1 ]] ; then
73 $ECHO -n "any of [ $* ]"
75 $ECHO " and try again."
76 if [[ $OS == macosx ]] ; then
77 $ECHO "If you have Xcode 4.3 or higher installed, you must install the"
78 $ECHO "Command Line Tools from Xcode Preferences > Downloads in order"
79 $ECHO "to build Factor."
86 if [[ $RET -ne 0 ]] ; then
93 test_program_installed wget
94 if [[ $? -ne 0 ]] ; then
99 test_program_installed curl
100 if [[ $? -ne 0 ]] ; then
101 DOWNLOADER="curl -f -O"
105 $ECHO "error: wget or curl required"
110 test_program_installed md5sum
111 if [[ $? -ne 0 ]] ; then
119 RE_SEMVER="^([0-9]*)\.([0-9]*)\.([0-9]*)-?(.*)?$" # 3.3.3-5
120 CLANG_RE_OLD="^([0-9]*)\.([0-9]*)-?(.*)?$" # 3.3-5
121 if [[ $1 =~ $RE_SEMVER ]] ; then
122 export "$2=${BASH_REMATCH[1]}"
123 export "$3=${BASH_REMATCH[2]}"
124 export "$4=${BASH_REMATCH[3]}"
125 export "$5=${BASH_REMATCH[4]}"
126 elif [[ $1 =~ $CLANG_RE_OLD ]] ; then
127 export "$2=${BASH_REMATCH[1]}"
128 export "$3=${BASH_REMATCH[2]}"
130 export "$5=${BASH_REMATCH[3]}"
132 echo "unsupported version number, please report a bug: $1"
138 CLANG_VERSION=$(clang --version | head -n1)
139 CLANG_VERSION_RE='^[a-zA-Z0-9 ]* version (.*)$' # 3.3-5
140 if [[ $CLANG_VERSION =~ $CLANG_VERSION_RE ]] ; then
141 export "CLANG_VERSION=${BASH_REMATCH[1]}"
142 local CLANG_MAJOR CLANG_MINOR CLANG_PATCH CLANG_SPECIAL
143 semver_into "$CLANG_VERSION" CLANG_MAJOR CLANG_MINOR CLANG_PATCH CLANG_SPECIAL
144 if [[ $CLANG_MAJOR -lt 3
145 || ( $CLANG_MAJOR -eq 3 && $CLANG_MINOR -le 1 )
147 echo "clang version required >= $REQUIRE_CLANG_VERSION, got $CLANG_VERSION"
157 test_programs_installed clang clang++
158 if [[ $? -ne 0 ]] && clang_version_ok ; then
159 [ -z "$CC" ] && CC=clang
160 [ -z "$CXX" ] && CXX=clang++
164 test_programs_installed gcc g++
165 if [[ $? -ne 0 ]] ; then
166 [ -z "$CC" ] && CC=gcc
167 [ -z "$CXX" ] && CXX=g++
171 $ECHO "error: high enough version of either (clang/clang++) or (gcc/g++) required!"
179 check_installed_programs() {
180 ensure_program_installed chmod
181 ensure_program_installed uname
182 ensure_program_installed git
183 ensure_program_installed wget curl
184 ensure_program_installed clang gcc
185 ensure_program_installed clang++ g++ cl
186 ensure_program_installed make gmake
187 ensure_program_installed md5sum md5
188 ensure_program_installed cut
191 check_library_exists() {
192 GCC_TEST=factor-library-test.c
193 GCC_OUT=factor-library-test.out
194 $ECHO -n "Checking for library $1..."
195 $ECHO "int main(){return 0;}" > $GCC_TEST
196 $CC $GCC_TEST -o $GCC_OUT -l $1 2>&-
197 if [[ $? -ne 0 ]] ; then
208 check_X11_libraries() {
209 check_library_exists GL
210 check_library_exists X11
211 check_library_exists pango-1.0
214 check_gtk_libraries() {
215 check_library_exists gobject-2.0
216 check_library_exists gtk-x11-2.0
217 check_library_exists gdk-x11-2.0
218 check_library_exists gdk_pixbuf-2.0
219 check_library_exists gtkglext-x11-1.0
220 check_library_exists atk-1.0
221 check_library_exists gio-2.0
222 check_library_exists gdkglext-x11-1.0
223 check_library_exists pango-1.0
229 linux) check_X11_libraries
230 check_gtk_libraries;;
231 unix) check_gtk_libraries;;
235 check_factor_exists() {
236 if [[ -d "factor" ]] ; then
237 $ECHO "A directory called 'factor' already exists."
238 $ECHO "Rename or delete it and try again."
244 if [[ -n $OS ]] ; then return; fi
245 $ECHO "Finding OS..."
246 local uname_s=$(uname -s)
249 CYGWIN_NT-5.2-WOW64) OS=windows;;
250 *CYGWIN_NT*) OS=windows;;
251 *CYGWIN*) OS=windows;;
252 MINGW32*) OS=windows;;
253 MSYS_NT*) OS=windows;;
254 *darwin*) OS=macosx;;
255 *Darwin*) OS=macosx;;
261 find_architecture() {
262 if [[ -n $ARCH ]] ; then return; fi
263 $ECHO "Finding ARCH..."
274 "Power Macintosh") ARCH=ppc;;
279 $ECHO "Finding num cores..."
284 CYGWIN_NT-5.2-WOW64 | *CYGWIN_NT* | *CYGWIN* | MINGW32*) NUM_CORES=$NUMBER_OF_PROCESSORS;;
285 *darwin* | *Darwin* | *linux* | *Linux*) NUM_CORES=$(getconf _NPROCESSORS_ONLN);;
289 echo_test_program() {
291 echo -e "int main(){ return (long)(8*sizeof(void*)); }"
295 $ECHO "Finding WORD..."
296 C_WORD="factor-word-size"
297 echo_test_program | $CC -o $C_WORD -xc -
304 intel_macosx_word_size() {
305 ensure_program_installed sysctl
306 $ECHO -n "Testing if your Intel Mac supports 64bit binaries..."
307 sysctl machdep.cpu.extfeatures | grep EM64T >/dev/null
308 if [[ $? -eq 0 ]] ; then
318 if [[ -n $WORD ]] ; then return; fi
319 if [[ $OS == macosx && $ARCH == x86 ]] ; then
320 intel_macosx_word_size
326 set_factor_binary() {
328 windows) FACTOR_BINARY=factor.com;;
329 *) FACTOR_BINARY=factor;;
333 set_factor_library() {
335 windows) FACTOR_LIBRARY=factor.dll;;
336 macosx) FACTOR_LIBRARY=libfactor.dylib;;
337 *) FACTOR_LIBRARY=libfactor.a;;
342 FACTOR_IMAGE=factor.image
343 FACTOR_IMAGE_FRESH=factor.image.fresh
349 $ECHO NUM_CORES=$NUM_CORES
352 $ECHO CURRENT_BRANCH=$CURRENT_BRANCH
353 $ECHO FACTOR_BINARY=$FACTOR_BINARY
354 $ECHO FACTOR_LIBRARY=$FACTOR_LIBRARY
355 $ECHO FACTOR_IMAGE=$FACTOR_IMAGE
356 $ECHO MAKE_TARGET=$MAKE_TARGET
357 $ECHO BOOT_IMAGE=$BOOT_IMAGE
358 $ECHO MAKE_IMAGE_TARGET=$MAKE_IMAGE_TARGET
359 $ECHO GIT_PROTOCOL=$GIT_PROTOCOL
360 $ECHO GIT_URL=$GIT_URL
361 $ECHO DOWNLOADER=$DOWNLOADER
369 check_os_arch_word() {
370 if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
374 $ECHO "OS, ARCH, or WORD is empty. Please report this."
383 if [[ $OS == linux && $ARCH == ppc ]] ; then
384 MAKE_IMAGE_TARGET=linux-ppc.32
385 MAKE_TARGET=linux-ppc-32
386 elif [[ $OS == windows && $ARCH == x86 && $WORD == 64 ]] ; then
387 MAKE_IMAGE_TARGET=windows-x86.64
388 MAKE_TARGET=windows-x86-64
389 elif [[ $OS == windows && $ARCH == x86 && $WORD == 32 ]] ; then
390 MAKE_IMAGE_TARGET=windows-x86.32
391 MAKE_TARGET=windows-x86-32
392 elif [[ $ARCH == x86 && $WORD == 64 ]] ; then
393 MAKE_IMAGE_TARGET=unix-x86.64
394 MAKE_TARGET=$OS-x86-64
395 elif [[ $ARCH == x86 && $WORD == 32 ]] ; then
396 MAKE_IMAGE_TARGET=unix-x86.32
397 MAKE_TARGET=$OS-x86-32
399 MAKE_IMAGE_TARGET=$ARCH.$WORD
400 MAKE_TARGET=$OS-$ARCH-$WORD
402 BOOT_IMAGE=boot.$MAKE_IMAGE_TARGET.image
406 ensure_program_installed cut
407 $ECHO "Parsing make target from command line: $1"
408 OS=$(echo $1 | cut -d '-' -f 1)
409 ARCH=$(echo $1 | cut -d '-' -f 2)
410 WORD=$(echo $1 | cut -d '-' -f 3)
412 if [[ $OS == linux && $ARCH == ppc ]] ; then WORD=32; fi
413 if [[ $OS == linux && $ARCH == arm ]] ; then WORD=32; fi
414 if [[ $OS == macosx && $ARCH == ppc ]] ; then WORD=32; fi
443 $ECHO "Downloading the git repository from factorcode.org..."
444 invoke_git clone $GIT_URL
447 update_script_name() {
448 $ECHO "$(dirname $0)/_update.sh"
452 local -r update_script=$(update_script_name)
453 local -r bash_path=$(which bash)
454 $ECHO "#!$bash_path" >"$update_script"
455 $ECHO "git pull \"$GIT_URL\" master" >>"$update_script"
456 $ECHO "if [[ \$? -eq 0 ]]; then exec \"$0\" $SCRIPT_ARGS; else echo \"git pull failed\"; exit 2; fi" \
458 $ECHO "exit 0" >>"$update_script"
460 chmod 755 "$update_script"
461 exec "$update_script"
464 update_script_changed() {
465 invoke_git diff --stat "$(invoke_git merge-base HEAD FETCH_HEAD)" FETCH_HEAD | grep 'build\.sh' >/dev/null
468 git_fetch_factorcode() {
469 $ECHO "Fetching the git repository from factorcode.org..."
471 rm -f "$(update_script_name)"
472 invoke_git fetch "$GIT_URL" master
474 if update_script_changed; then
475 $ECHO "Updating and restarting the build.sh script..."
478 $ECHO "Updating the working tree..."
479 invoke_git pull "$GIT_URL" master
503 $ECHO "Backing up factor..."
504 $COPY $FACTOR_BINARY $FACTOR_BINARY.bak
505 $COPY $FACTOR_LIBRARY $FACTOR_LIBRARY.bak
506 $COPY $BOOT_IMAGE $BOOT_IMAGE.bak
507 $COPY $FACTOR_IMAGE $FACTOR_IMAGE.bak
508 $ECHO "Done with backup."
511 check_makefile_exists() {
512 if [[ ! -e "GNUmakefile" ]] ; then
514 $ECHO "***GNUmakefile not found***"
515 $ECHO "You are likely in the wrong directory."
516 $ECHO "Run this script from your factor directory:"
523 check_makefile_exists
524 $MAKE $MAKE_OPTS "$@"
533 $ECHO "Building factor with $NUM_CORES cores"
534 invoke_make CC=$CC CXX=$CXX $MAKE_TARGET -j$NUM_CORES
537 make_clean_factor() {
542 current_git_branch() {
543 git rev-parse --abbrev-ref HEAD
547 if [[ $DOWNLOADER_NAME == 'wget' ]]; then
548 if [[ $(wget -S --spider $1 2>&1 | grep 'HTTP/1.1 200 OK') ]]; then
553 elif [[ $DOWNLOADER_NAME == 'curl' ]]; then
554 local code=$(curl -sL -w "%{http_code}\\n" "$1" -o /dev/null)
555 if [[ $code -eq 200 ]]; then return 0; else return 1; fi
557 $ECHO "error: wget or curl required in check_url"
562 # If we are on a branch, first try to get a boot image for that branch.
563 # Otherwise, just use `master`
564 set_boot_image_vars() {
566 local url="http://downloads.factorcode.org/images/${CURRENT_BRANCH}/checksums.txt"
568 if [[ $? -eq 0 ]]; then
570 BOOT_IMAGE_URL="http://downloads.factorcode.org/images/${CURRENT_BRANCH}/${BOOT_IMAGE}"
572 CHECKSUM_URL="http://downloads.factorcode.org/images/master/checksums.txt"
573 BOOT_IMAGE_URL="http://downloads.factorcode.org/images/master/${BOOT_IMAGE}"
577 set_current_branch() {
578 if [ -n "${CI_BRANCH}" ]; then
579 CURRENT_BRANCH="${CI_BRANCH}"
581 CURRENT_BRANCH=$(current_git_branch)
585 update_boot_image() {
587 $ECHO "Deleting old images..."
588 $DELETE checksums.txt* > /dev/null 2>&1
589 # delete boot images with one or two characters after the dot
590 $DELETE $BOOT_IMAGE.{?,??} > /dev/null 2>&1
591 $DELETE temp/staging.*.image > /dev/null 2>&1
592 if [[ -f $BOOT_IMAGE ]] ; then
593 get_url $CHECKSUM_URL
594 local factorcode_md5=$(cat checksums.txt | grep $BOOT_IMAGE | cut -f2 -d' ')
596 local disk_md5=$($MD5SUM $BOOT_IMAGE | cut -f1 -d' ')
597 $ECHO "Factorcode md5: $factorcode_md5";
598 $ECHO "Disk md5: $disk_md5";
599 if [[ "$factorcode_md5" == "$disk_md5" ]] ; then
600 $ECHO "Your disk boot image matches the one on factorcode.org."
602 $DELETE $BOOT_IMAGE > /dev/null 2>&1
611 $ECHO "Downloading boot image $BOOT_IMAGE."
612 get_url "${BOOT_IMAGE_URL}"
616 if [[ -z $DOWNLOADER ]] ; then
619 $ECHO $DOWNLOADER $1 ;
621 check_ret $DOWNLOADER
626 check_installed_programs
631 $ECHO "Copying $FACTOR_IMAGE to $FACTOR_IMAGE_FRESH..."
632 $COPY $FACTOR_IMAGE $FACTOR_IMAGE_FRESH
636 ./$FACTOR_BINARY -i=$BOOT_IMAGE
658 download_and_bootstrap() {
663 net_bootstrap_no_pull() {
666 download_and_bootstrap
670 ./$FACTOR_BINARY -e="USING: vocabs.loader vocabs.refresh system memory ; refresh-all save 0 exit"
675 ./$FACTOR_BINARY -e="\"$MAKE_IMAGE_TARGET\" USING: system bootstrap.image memory ; make-image save 0 exit"
680 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++
684 install_deps_pacman() {
685 sudo pacman --noconfirm -Syu gcc clang make rlwrap git wget pango glibc gtk2 gtk3 gtkglext gtk-engines gdk-pixbuf2 libx11 screen tmux
690 sudo dnf --assumeyes install gcc gcc-c++ glibc-devel binutils libX11-devel pango-devel gtk3-devel gdk-pixbuf2-devel gtkglext-devel tmux rlwrap wget
695 install_deps_macosx() {
696 test_program_installed git
697 if [[ $? -ne 1 ]] ; then
698 ensure_program_installed yes
699 $ECHO "git not found."
700 $ECHO "This script requires either git-core or port."
701 $ECHO "If it fails, install git-core or port and try again."
702 ensure_program_installed port
703 $ECHO "Installing git-core with port...this will take awhile."
704 yes | sudo port install git-core
709 $ECHO "usage: $0 command [optional-target]"
710 $ECHO " install - git clone, compile, bootstrap"
711 $ECHO " deps-apt - install required packages for Factor on Linux using apt"
712 $ECHO " deps-pacman - install required packages for Factor on Linux using pacman"
713 $ECHO " deps-dnf - install required packages for Factor on Linux using dnf"
714 $ECHO " deps-macosx - install git on MacOSX using port"
715 $ECHO " self-update - git pull, recompile, make local boot image, bootstrap"
716 $ECHO " quick-update - git pull, refresh-all, save"
717 $ECHO " update|latest - git pull, recompile, download a boot image, bootstrap"
718 $ECHO " bootstrap - bootstrap with existing boot image"
719 $ECHO " net-bootstrap - recompile, download a boot image, bootstrap"
720 $ECHO " make-target - find and print the os-arch-cpu string"
721 $ECHO " report - print the build variables"
722 $ECHO " update-boot-image - get the boot image for the current branch of for master"
724 $ECHO "If you are behind a firewall, invoke as:"
725 $ECHO "env GIT_PROTOCOL=http $0 <command>"
727 $ECHO "Example for overriding the default target:"
728 $ECHO " $0 update macosx-x86-32"
733 # -n is nonzero length, -z is zero length
734 if [[ -n "$2" ]] ; then
743 deps-apt) install_deps_apt ;;
744 deps-pacman) install_deps_pacman ;;
745 deps-macosx) install_deps_macosx ;;
746 deps-dnf) install_deps_dnf ;;
747 self-update) update; make_boot_image; bootstrap;;
748 quick-update) update; refresh_image ;;
749 update) update; download_and_bootstrap ;;
750 latest) update; download_and_bootstrap ;;
751 bootstrap) get_config_info; bootstrap ;;
752 net-bootstrap) net_bootstrap_no_pull ;;
753 make-target) FIND_MAKE_TARGET=true; ECHO=false; find_build_info; exit_script ;;
754 report) find_build_info ;;
755 full-report) find_build_info; check_installed_programs; check_libraries ;;
756 update-boot-image) find_build_info; check_installed_programs; update_boot_image;;