]> gitweb.factorcode.org Git - factor.git/blobdiff - build.sh
add xdg-state-dir
[factor.git] / build.sh
index ef3adb9ceec4e33c16206a4d9faba6e75d05aafa..df725ab8b6c4f252850d4eb5a8fc584ac8f59276 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -12,7 +12,7 @@ OS=
 ARCH=
 WORD=
 GIT_PROTOCOL=${GIT_PROTOCOL:="git"}
-GIT_URL=${GIT_URL:=$GIT_PROTOCOL"://factorcode.org/git/factor.git"}
+GIT_URL=${GIT_URL:=$GIT_PROTOCOL"://github.com/factor/factor.git"}
 SCRIPT_ARGS="$*"
 
 REQUIRE_CLANG_VERSION=3.1
@@ -82,6 +82,8 @@ ensure_program_installed() {
 }
 
 check_ret() {
+    # Can't execute any commands before saving $?
+    # $1 is the name of the command we are checking
     RET=$?
     if [[ $RET -ne 0 ]] ; then
        $ECHO $1 failed
@@ -92,13 +94,13 @@ check_ret() {
 set_downloader() {
     test_program_installed wget
     if [[ $? -ne 0 ]] ; then
-        DOWNLOADER=wget
+        DOWNLOADER="wget -nd"
         DOWNLOADER_NAME=wget
         return
     fi
     test_program_installed curl
     if [[ $? -ne 0 ]] ; then
-        DOWNLOADER="curl -f -O"
+        DOWNLOADER="curl -L -f -O"
         DOWNLOADER_NAME=curl
         return
     fi
@@ -154,6 +156,26 @@ clang_version_ok() {
 }
 
 set_cc() {
+    # on Cygwin we MUST use the MinGW "cross-compiler", therefore check these first
+    # furthermore, we prefer 64 bit over 32 bit versions if both are available
+
+    # we need this condition so we don't find a mingw32 compiler on linux
+    if [[ $OS == windows ]] ; then
+        test_programs_installed x86_64-w64-mingw32-gcc x86_64-w64-mingw32-g++
+        if [[ $? -ne 0 ]] ; then
+            [ -z "$CC" ] && CC=x86_64-w64-mingw32-gcc
+            [ -z "$CXX" ] && CXX=x86_64-w64-mingw32-g++
+            return
+        fi
+
+        test_programs_installed i686-w64-mingw32-gcc i686-w64-mingw32-g++
+        if [[ $? -ne 0 ]] ; then
+            [ -z "$CC" ] && CC=i686-w64-mingw32-gcc
+            [ -z "$CXX" ] && CXX=i686-w64-mingw32-g++
+            return
+        fi
+    fi
+
     test_programs_installed clang clang++
     if [[ $? -ne 0 ]] && clang_version_ok ; then
         [ -z "$CC" ] && CC=clang
@@ -161,6 +183,7 @@ set_cc() {
         return
     fi
 
+    # gcc and g++ will fail to correctly build Factor on Cygwin
     test_programs_installed gcc g++
     if [[ $? -ne 0 ]] ; then
         [ -z "$CC" ] && CC=gcc
@@ -187,8 +210,8 @@ check_installed_programs() {
     ensure_program_installed uname
     ensure_program_installed git
     ensure_program_installed wget curl
-    ensure_program_installed clang gcc
-    ensure_program_installed clang++ g++ cl
+    ensure_program_installed clang x86_64-w64-mingw32-gcc i686-w64-mingw32-gcc gcc
+    ensure_program_installed clang++ x86_64-w64-mingw32-g++ i686-w64-mingw32-g++ g++ cl
     ensure_program_installed make gmake
     ensure_program_installed md5sum md5
     ensure_program_installed cut
@@ -232,9 +255,9 @@ check_gtk_libraries() {
 
 check_libraries() {
     case $OS in
-            linux) check_X11_libraries
-                   check_gtk_libraries;;
-            unix) check_gtk_libraries;;
+        linux) check_X11_libraries
+               check_gtk_libraries;;
+        unix) check_gtk_libraries;;
     esac
 }
 
@@ -256,12 +279,14 @@ find_os() {
         *CYGWIN_NT*) OS=windows;;
         *CYGWIN*) OS=windows;;
         MINGW32*) OS=windows;;
+        MINGW64*) OS=windows;;
         MSYS_NT*) OS=windows;;
         *darwin*) OS=macosx;;
         *Darwin*) OS=macosx;;
         *linux*) OS=linux;;
         *Linux*) OS=linux;;
         FreeBSD) OS=freebsd;;
+        Haiku) OS=haiku;;
     esac
 }
 
@@ -278,57 +303,59 @@ find_architecture() {
        ppc64) ARCH=ppc;;
        *86) ARCH=x86;;
        *86_64) ARCH=x86;;
+       aarch64) ARCH=arm64;;
+       arm64) ARCH=arm64;;
+       iPhone5*[3-9]) ARCH=arm64;;
+       iPhone[6-9]*) ARCH=arm64;;
+       iPhone[1-9][0-9]*) ARCH=arm64;;
+       iPad[4-9]*) ARCH=arm64;;
+       iPad[1-9][0-9]*) ARCH=arm64;;
+       AppleTV[5-9]*) ARCH=arm64;;
+       AppleTV[1-9][0-9]*) ARCH=arm64;;
        "Power Macintosh") ARCH=ppc;;
     esac
 }
 
 find_num_cores() {
-    $ECHO "Finding num cores..."
+    $ECHO "Finding NUM_CORES..."
     NUM_CORES=1
     uname_s=$(uname -s)
     check_ret uname
     case $uname_s in
         CYGWIN_NT-5.2-WOW64 | *CYGWIN_NT* | *CYGWIN* | MINGW32*) NUM_CORES=$NUMBER_OF_PROCESSORS;;
-        *darwin* | *Darwin* | *linux* | *Linux*) NUM_CORES=$(getconf _NPROCESSORS_ONLN);;
-        freebsd) NUM_CORES=$(sysctl -n hw.ncpu);;
+        *linux* | *Linux*) NUM_CORES=$(getconf _NPROCESSORS_ONLN || nproc);;
+        *darwin* | *Darwin* | freebsd) NUM_CORES=$(sysctl -n hw.ncpu);;
     esac
 }
 
-echo_test_program() {
-    #! Must be 'echo'
-    echo -e "int main(){ return (long)(8*sizeof(void*)); }"
+find_word_size() {
+    if [[ -n $WORD ]] ; then return; fi
+    $ECHO "Finding WORD..."
+    WORD=$(getconf LONG_BIT || find_word_size_cpp || find_word_size_c)
 }
 
-c_find_word_size() {
-    $ECHO "Finding WORD..."
+find_word_size_cpp() {
+    SIXTY_FOUR='defined(__aarch64__) || defined(__x86_64__) || defined(_M_AMD64) || defined(__PPC64__) || defined(__64BIT__)'
+    THIRTY_TWO='defined(i386) || defined(__i386) || defined(__i386__) || defined(_MIX86)'
+    $CC -E -xc <(echo -e "#if ${SIXTY_FOUR}\n64\n#elif ${THIRTY_TWO}\n32\n#endif") | tail -1
+}
+
+find_word_size_c() {
     C_WORD="factor-word-size"
-    echo_test_program | $CC -o $C_WORD -xc -
+    TEST_PROGRAM="int main(){ return (long)(8*sizeof(void*)); }"
+    echo $TEST_PROGRAM | $CC -o $C_WORD -xc -
     check_ret $CC
     ./$C_WORD
-    WORD=$?
+    WORD_OUT=$?
+    case $WORD_OUT in
+        32) ;;
+        64) ;;
+        *)
+            echo "Word size should be 32/64, got '$WORD_OUT'"
+            exit_script 15;;
+    esac
     $DELETE -f $C_WORD
-}
-
-intel_macosx_word_size() {
-    ensure_program_installed sysctl
-    $ECHO -n "Testing if your Intel Mac supports 64bit binaries..."
-    sysctl machdep.cpu.extfeatures | grep EM64T >/dev/null
-    if [[ $? -eq 0 ]] ; then
-        WORD=64
-        $ECHO "yes!"
-    else
-        WORD=32
-        $ECHO "no."
-    fi
-}
-
-find_word_size() {
-    if [[ -n $WORD ]] ; then return; fi
-    if [[ $OS == macosx && $ARCH == x86 ]] ; then
-        intel_macosx_word_size
-    else
-        c_find_word_size
-    fi
+    echo "$WORD_OUT"
 }
 
 set_factor_binary() {
@@ -357,6 +384,7 @@ echo_build_info() {
     $ECHO NUM_CORES=$NUM_CORES
     $ECHO WORD=$WORD
     $ECHO DEBUG=$DEBUG
+    $ECHO REPRODUCIBLE=$REPRODUCIBLE
     $ECHO CURRENT_BRANCH=$CURRENT_BRANCH
     $ECHO FACTOR_BINARY=$FACTOR_BINARY
     $ECHO FACTOR_LIBRARY=$FACTOR_LIBRARY
@@ -391,6 +419,12 @@ set_build_info() {
     if [[ $OS == linux && $ARCH == ppc ]] ; then
         MAKE_IMAGE_TARGET=linux-ppc.32
         MAKE_TARGET=linux-ppc-32
+    elif [[ $OS == linux && $ARCH == arm64 ]] ; then
+        MAKE_IMAGE_TARGET=unix-arm.64
+        MAKE_TARGET=linux-arm-64
+    elif [[ $OS == macosx && $ARCH == arm64 ]] ; then
+        MAKE_IMAGE_TARGET=unix-arm.64
+        MAKE_TARGET=macosx-arm64
     elif [[ $OS == windows && $ARCH == x86 && $WORD == 64 ]] ; then
         MAKE_IMAGE_TARGET=windows-x86.64
         MAKE_TARGET=windows-x86-64
@@ -448,7 +482,7 @@ invoke_git() {
 }
 
 git_clone() {
-    $ECHO "Downloading the git repository from factorcode.org..."
+    $ECHO "Downloading the git repository from github.com..."
     invoke_git clone $GIT_URL
 }
 
@@ -459,8 +493,9 @@ update_script_name() {
 update_script() {
     local -r update_script=$(update_script_name)
     local -r bash_path=$(which bash)
+    $ECHO "updating from ${CURRENT_BRANCH}"
     $ECHO "#!$bash_path" >"$update_script"
-    $ECHO "git pull \"$GIT_URL\" master" >>"$update_script"
+    $ECHO "git pull \"$GIT_URL\" ${CURRENT_BRANCH}" >>"$update_script"
     $ECHO "if [[ \$? -eq 0 ]]; then exec \"$0\" $SCRIPT_ARGS; else echo \"git pull failed\"; exit 2; fi" \
         >>"$update_script"
     $ECHO "exit 0" >>"$update_script"
@@ -473,18 +508,20 @@ update_script_changed() {
     invoke_git diff --stat "$(invoke_git merge-base HEAD FETCH_HEAD)" FETCH_HEAD | grep 'build\.sh' >/dev/null
 }
 
-git_fetch_factorcode() {
-    $ECHO "Fetching the git repository from factorcode.org..."
+git_fetch() {
+    $ECHO "Fetching the git repository from github.com..."
+    set_current_branch
 
     rm -f "$(update_script_name)"
-    invoke_git fetch "$GIT_URL" master
+    $ECHO git fetch "$GIT_URL" "${CURRENT_BRANCH}"
+    invoke_git fetch "$GIT_URL" "${CURRENT_BRANCH}"
 
     if update_script_changed; then
         $ECHO "Updating and restarting the build.sh script..."
         update_script
     else
         $ECHO "Updating the working tree..."
-        invoke_git pull "$GIT_URL" master
+        invoke_git pull "$GIT_URL" "${CURRENT_BRANCH}"
     fi
 }
 
@@ -548,7 +585,9 @@ make_clean_factor() {
 }
 
 current_git_branch() {
-    git rev-parse --abbrev-ref HEAD
+    # git rev-parse --abbrev-ref HEAD # outputs HEAD for detached head
+    # outputs nothing for detached HEAD, which is fine for ``git fetch``
+    git describe --all --exact-match 2>/dev/null | sed 's=.*/=='
 }
 
 check_url() {
@@ -577,6 +616,8 @@ set_boot_image_vars() {
         CHECKSUM_URL="$url"
         BOOT_IMAGE_URL="http://downloads.factorcode.org/images/${CURRENT_BRANCH}/${BOOT_IMAGE}"
     else
+        $ECHO "boot image for branch \`${CURRENT_BRANCH}\` is not on server, trying master instead"
+        $ECHO "  tried nonexistent url: ${url}"
         CHECKSUM_URL="http://downloads.factorcode.org/images/master/checksums.txt"
         BOOT_IMAGE_URL="http://downloads.factorcode.org/images/master/${BOOT_IMAGE}"
     fi
@@ -605,7 +646,7 @@ update_boot_image() {
         $ECHO "Factorcode md5: $factorcode_md5";
         $ECHO "Disk md5: $disk_md5";
         if [[ "$factorcode_md5" == "$disk_md5" ]] ; then
-            $ECHO "Your disk boot image matches the one on factorcode.org."
+            $ECHO "Your disk boot image matches the one on downloads.factorcode.org."
         else
             $DELETE $BOOT_IMAGE > /dev/null 2>&1
             get_boot_image
@@ -642,6 +683,7 @@ copy_fresh_image() {
 
 bootstrap() {
     ./$FACTOR_BINARY -i=$BOOT_IMAGE
+    check_ret "./$FACTOR_BINARY bootstrap failed"
     copy_fresh_image
 }
 
@@ -658,7 +700,7 @@ install() {
 
 update() {
     get_config_info
-    git_fetch_factorcode
+    git_fetch
     backup_factor
     make_clean_factor
 }
@@ -680,12 +722,12 @@ refresh_image() {
 }
 
 make_boot_image() {
-    ./$FACTOR_BINARY -e="\"$MAKE_IMAGE_TARGET\" USING: system bootstrap.image memory ; make-image save 0 exit"
+    ./$FACTOR_BINARY -run="bootstrap.image" "$MAKE_IMAGE_TARGET"
     check_ret factor
 }
 
 install_deps_apt() {
-    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++
+    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 make screen tmux libssl-dev
     check_ret sudo
 }
 
@@ -700,7 +742,7 @@ install_deps_dnf() {
 }
 
 install_deps_pkg() {
-    sudo pkg install --yes git gcc rlwrap ripgrep curl gmake
+    sudo pkg install --yes git gmake gcc rlwrap ripgrep curl gmake x11-toolkits/gtk30 x11-toolkits/gtkglext pango cairo vim
 }
 
 
@@ -728,11 +770,13 @@ usage() {
     $ECHO "  self-update - git pull, recompile, make local boot image, bootstrap"
     $ECHO "  quick-update - git pull, refresh-all, save"
     $ECHO "  update|latest - git pull, recompile, download a boot image, bootstrap"
+    $ECHO "  compile - compile the binary"
+    $ECHO "  recompile - recompile the binary"
     $ECHO "  bootstrap - bootstrap with existing boot image"
     $ECHO "  net-bootstrap - recompile, download a boot image, bootstrap"
     $ECHO "  make-target - find and print the os-arch-cpu string"
-    $ECHO "  report - print the build variables"
-    $ECHO "  update-boot-image - get the boot image for the current branch of for master"
+    $ECHO "  report|info - print the build variables"
+    $ECHO "  update-boot-image - get the boot image for the current branch"
     $ECHO ""
     $ECHO "If you are behind a firewall, invoke as:"
     $ECHO "env GIT_PROTOCOL=http $0 <command>"
@@ -748,6 +792,13 @@ if [[ -n "$2" ]] ; then
     parse_build_info $2
 fi
 
+if [ "$#" -gt 3 ]; then
+    usage
+    $ECHO "error: too many arguments"
+    exit 1
+fi
+
+
 set_copy
 set_delete
 
@@ -758,14 +809,16 @@ case "$1" in
     deps-macosx) install_deps_macosx ;;
     deps-dnf) install_deps_dnf ;;
     deps-pkg) install_deps_pkg ;;
+    self-bootstrap) get_config_info; make_boot_image; bootstrap;;
     self-update) update; make_boot_image; bootstrap;;
     quick-update) update; refresh_image ;;
-    update) update; download_and_bootstrap ;;
-    latest) update; download_and_bootstrap ;;
+    update|latest) update; download_and_bootstrap ;;
+    compile) find_build_info; make_factor ;;
+    recompile) find_build_info; make_clean; make_factor ;;
     bootstrap) get_config_info; bootstrap ;;
     net-bootstrap) net_bootstrap_no_pull ;;
     make-target) FIND_MAKE_TARGET=true; ECHO=false; find_build_info; exit_script ;;
-    report) find_build_info ;;
+    report|info) find_build_info ;;
     full-report) find_build_info; check_installed_programs; check_libraries ;;
     update-boot-image) find_build_info; check_installed_programs; update_boot_image;;
     *) usage ;;