]> gitweb.factorcode.org Git - factor.git/blobdiff - build.sh
calendar.format: make duration>human-readable more human readable
[factor.git] / build.sh
index a823611427e4cd50650dfaf8fe0ef7922e1eaf50..f8a3c5090f7bf9cc53a7be59dd5f23b9e0c15735 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -13,7 +13,6 @@ ARCH=
 WORD=
 GIT_PROTOCOL=${GIT_PROTOCOL:="https"}
 GIT_URL=${GIT_URL:=$GIT_PROTOCOL"://github.com/factor/factor.git"}
-SCRIPT_ARGS="$*"
 
 test_program_installed() {
     command -v "$1" >/dev/null 2>&1
@@ -21,7 +20,7 @@ test_program_installed() {
 
 # return 1 on found
 test_programs_installed() {
-    local installed=0;
+    local installed=0
     $ECHO -n "Checking for all($*)..."
     for cmd in "$@" ;
     do
@@ -45,7 +44,7 @@ exit_script() {
 }
 
 ensure_program_installed() {
-    local installed=0;
+    local installed=0
     $ECHO -n "Checking for any($*)..."
     for cmd in "$@" ;
     do
@@ -81,14 +80,43 @@ check_ret() {
     fi
 }
 
+download_with_wget() {
+    local url="$1"
+    local filename
+    filename=$(basename "$url")
+    $ECHO filename is "$filename"
+    $ECHO wget -nd --prefer-family=IPv4 -O "$filename" "$url"
+    wget -nd --prefer-family=IPv4 -O "$filename" "$url"
+}
+
+download_with_curl() {
+    local url="$1"
+    local filename
+    filename=$(basename "$url")
+    curl -L -f -o "$filename" "$url"
+}
+
+download_with_downloader() {
+    local url="$1"
+    if [[ -z $DOWNLOADER_NAME ]] ; then
+        set_downloader
+    fi
+    if [[ $DOWNLOADER_NAME == 'wget' ]]; then
+        download_with_wget "$url"
+    elif [[ $DOWNLOADER_NAME == 'curl' ]]; then
+        download_with_curl "$url"
+    else
+        $ECHO "error: wget or curl required in download_with_downloader"
+        exit_script 12
+    fi
+}
+
 set_downloader() {
     if test_program_installed wget; then
-        DOWNLOADER="wget -nd --prefer-family=IPv4"
         DOWNLOADER_NAME=wget
         return
     fi
     if test_program_installed curl; then
-        DOWNLOADER="curl -L -f -O"
         DOWNLOADER_NAME=curl
         return
     fi
@@ -172,10 +200,10 @@ check_library_exists() {
     else
         $ECHO "found."
     fi
-    $DELETE -f $GCC_TEST
-    check_ret $DELETE
-    $DELETE -f $GCC_OUT
-    check_ret $DELETE
+    rm -f $GCC_TEST
+    check_ret rm
+    rm -f $GCC_OUT
+    check_ret rm
 }
 
 check_X11_libraries() {
@@ -299,7 +327,7 @@ find_word_size_c() {
             echo "Word size should be 32/64, got '$WORD_OUT'"
             exit_script 15 ;;
     esac
-    $DELETE -f $C_WORD
+    rm -f $C_WORD
     echo "$WORD_OUT"
 }
 
@@ -342,12 +370,10 @@ echo_build_info() {
     $ECHO "GIT_URL=$GIT_URL"
     $ECHO "CHECKSUM_URL=$CHECKSUM_URL"
     $ECHO "BOOT_IMAGE_URL=$BOOT_IMAGE_URL"
-    $ECHO "DOWNLOADER=$DOWNLOADER"
+    $ECHO "DOWNLOADER_NAME=$DOWNLOADER_NAME"
     $ECHO "CC=$CC"
     $ECHO "CXX=$CXX"
     $ECHO "MAKE=$MAKE"
-    $ECHO "COPY=$COPY"
-    $ECHO "DELETE=$DELETE"
 }
 
 check_os_arch_word() {
@@ -426,21 +452,22 @@ update_script_name() {
 }
 
 update_script() {
-    set_current_branch
-    local -r update_script=$(update_script_name)
-    local -r bash_path=$(which bash)
-    $ECHO "#!$bash_path" >"$update_script"
-    $ECHO "set -ex" >>"$update_script"
-    $ECHO "git pull ${GIT_URL} ${CURRENT_BRANCH}" >>"$update_script"
-    $ECHO "exit 0" >>"$update_script"
-
-    chmod 755 "$update_script"
-    $ECHO "running the build.sh updater script: $update_script"
-    exec "$update_script"
+  set_current_branch
+  local -r update_script=$(update_script_name)
+  local -r shell_path="$SHELL"
+  {
+    echo "#!$shell_path"
+    echo "set -ex"
+    echo "git pull ${GIT_URL} ${CURRENT_BRANCH}"
+    echo "exit 0"
+  } > "$update_script"
+  chmod 755 "$update_script"
+  $ECHO "Running the build.sh updater script: $update_script"
+  exec "$update_script"
 }
 
 update_script_changed() {
-    invoke_git diff --stat "$(invoke_git merge-base HEAD FETCH_HEAD)" FETCH_HEAD | grep 'build\.sh' >/dev/null
+    invoke_git diff --stat "$(invoke_git merge-base HEAD FETCH_HEAD)" FETCH_HEAD | grep "build.sh" >/dev/null
 }
 
 git_fetch() {
@@ -465,26 +492,12 @@ cd_factor() {
     check_ret cd
 }
 
-set_copy() {
-    case $OS in
-        windows) COPY="cp" ;;
-        *) COPY="cp" ;;
-    esac
-}
-
-set_delete() {
-    case $OS in
-        windows) DELETE="rm" ;;
-        *) DELETE="rm" ;;
-    esac
-}
-
 backup_factor() {
     $ECHO "Backing up factor..."
-    $COPY $FACTOR_BINARY $FACTOR_BINARY.bak
-    $COPY $FACTOR_LIBRARY $FACTOR_LIBRARY.bak
-    $COPY "$BOOT_IMAGE" "$BOOT_IMAGE.bak"
-    $COPY $FACTOR_IMAGE $FACTOR_IMAGE.bak
+    cp "$FACTOR_BINARY" "$FACTOR_BINARY.bak"
+    cp "$FACTOR_LIBRARY" "$FACTOR_LIBRARY.bak"
+    cp "$BOOT_IMAGE" "$BOOT_IMAGE.bak"
+    cp "$FACTOR_IMAGE" "$FACTOR_IMAGE.bak"
     $ECHO "Done with backup."
 }
 
@@ -529,20 +542,15 @@ current_git_branch() {
     git describe --all --exact-match 2>/dev/null
 }
 
+
 check_url() {
     if [[ $DOWNLOADER_NAME == 'wget' ]]; then
-    if wget -S --spider --prefer-family=IPv4 "$1" 2>&1 | grep -q 'HTTP/1.1 200 OK'; then
-            return 0
-        else
-            return 1
-        fi
+        wget -S --spider --prefer-family=IPv4 "$1" 2>&1 | grep -q 'HTTP/[12].[01] [23]..' && return 0 || return 1
     elif [[ $DOWNLOADER_NAME == 'curl' ]]; then
-        local code
-        code=$(curl -4 -sL -w "%{http_code}\\n" "$1" -o /dev/null)
-        if [[ $code -eq 200 ]]; then return 0; else return 1; fi
+        curl -4 -sL -w "%{http_code}\\n" "$1" -o /dev/null | grep -qE '^(2[0-9]{2})$' && return 0 || return 1
     else
-        $ECHO "error: wget or curl required in check_url"
-        exit_script 12
+        echo "error: wget or curl required to check URL"
+        exit 12
     fi
 }
 
@@ -552,8 +560,8 @@ set_boot_image_vars() {
     set_current_branch
     local url="https://downloads.factorcode.org/images/${CURRENT_BRANCH}/checksums.txt"
     $ECHO "Getting checksum from ${url}"
-    check_url $url
-    if [[ $? -eq 0 ]]; then
+
+    if check_url "$url"; then
         $ECHO "got checksum!"
         CHECKSUM_URL="$url"
         BOOT_IMAGE_URL="https://downloads.factorcode.org/images/${CURRENT_BRANCH}/${BOOT_IMAGE}"
@@ -570,17 +578,16 @@ set_current_branch() {
         CURRENT_BRANCH="${CI_BRANCH}"
     else
         CURRENT_BRANCH_FULL=$(current_git_branch)
-        CURRENT_BRANCH=$($ECHO $CURRENT_BRANCH_FULL | sed 's=heads/==;s=remotes/==')
+        CURRENT_BRANCH=$($ECHO "$CURRENT_BRANCH_FULL" | sed 's=heads/==;s=remotes/==')
     fi
 }
 
 update_boot_image() {
     set_boot_image_vars
     $ECHO "Deleting old images..."
-    $DELETE checksums.txt* > /dev/null 2>&1
-    # delete boot images with one or two characters after the dot
-    $DELETE "$BOOT_IMAGE".{?,??} > /dev/null 2>&1
-    $DELETE temp/staging.*.image > /dev/null 2>&1
+    rm -f checksums.txt* > /dev/null 2>&1
+    rm -f "$BOOT_IMAGE".{?,??} > /dev/null 2>&1
+    rm -f temp/staging.*.image > /dev/null 2>&1
     if [[ -f $BOOT_IMAGE ]] ; then
         get_url "$CHECKSUM_URL"
         local factorcode_md5
@@ -588,12 +595,12 @@ update_boot_image() {
         set_md5sum
         local disk_md5
         disk_md5=$($MD5SUM "$BOOT_IMAGE" | cut -f1 -d' ')
-        $ECHO "Factorcode md5: $factorcode_md5";
-        $ECHO "Disk md5: $disk_md5";
+        $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 downloads.factorcode.org."
         else
-            $DELETE "$BOOT_IMAGE" > /dev/null 2>&1
+            rm -f "$BOOT_IMAGE" > /dev/null 2>&1
             get_boot_image
         fi
     else
@@ -607,12 +614,10 @@ get_boot_image() {
 }
 
 get_url() {
-    if [[ -z $DOWNLOADER ]] ; then
-        set_downloader;
+    if [[ -z $DOWNLOADER_NAME ]] ; then
+        set_downloader_name
     fi
-    $ECHO "$DOWNLOADER" "$1" ;
-    $DOWNLOADER "$1"
-    check_ret "$DOWNLOADER"
+    download_with_downloader "$1"
 }
 
 get_config_info() {
@@ -623,16 +628,16 @@ get_config_info() {
 
 copy_fresh_image() {
     $ECHO "Copying $FACTOR_IMAGE to $FACTOR_IMAGE_FRESH..."
-    $COPY $FACTOR_IMAGE $FACTOR_IMAGE_FRESH
+    cp "$FACTOR_IMAGE" "$FACTOR_IMAGE_FRESH"
 }
 
 check_launch_factor() {
-    ./$FACTOR_BINARY -e=
+    "./$FACTOR_BINARY" -e=
     check_ret "Could not launch ./$FACTOR_BINARY"
 }
 
 is_boot_image_outdated() {
-    ./$FACTOR_BINARY "-e=USE: system \"\" to-refresh 2drop length 0 > 1 0 ? exit"
+    "./$FACTOR_BINARY" "-e=USE: system \"\" to-refresh 2drop length 0 > 1 0 ? exit"
     return $?
 }
 
@@ -645,21 +650,24 @@ info_boot_image() {
         set_md5sum
         local disk_md5
         disk_md5=$($MD5SUM "$BOOT_IMAGE" | cut -f1 -d' ')
-        $ECHO "Factorcode md5: $factorcode_md5";
-        $ECHO "Disk md5: $disk_md5";
+        $ECHO "Boot image @factorcode.org md5: $factorcode_md5"
+        $ECHO "Boot image @local disk     md5: $disk_md5"
         if [[ "$factorcode_md5" == "$disk_md5" ]] ; then
             $ECHO "Your disk boot image matches the one on downloads.factorcode.org."
         else
             $ECHO "Your disk boot image and the one on downloads.factorcode.org mismatch"
         fi
     fi
+}
 
+info_check_factor_refresh_all_locally() {
+    prepare_build_info
     check_launch_factor
-    is_boot_image_outdated
-    if [[ $? -eq 0 ]]; then
-        $ECHO "Your disk boot image is up-to-date"
+
+    if is_boot_image_outdated; then
+        $ECHO "Your Factor image is consistent with the local source code."
     else
-        $ECHO "Your disk boot image needs to be updated"
+        $ECHO "Your Factor image is not consistent with the local source code."
     fi
 }
 
@@ -748,7 +756,8 @@ usage() {
     $ECHO "  deps-dnf - install required packages for Factor on Linux using dnf"
     $ECHO "  deps-pkg - install required packages for Factor on FreeBSD using pkg"
     $ECHO "  deps-macosx - install git on MacOSX using port"
-    $ECHO "  info-boot-image - print remote and disk boot image MD5, status disk boot image"
+    $ECHO "  info-boot-image - print remote and disk boot image MD5"
+    $ECHO "  info-check-factor-refresh-all-locally - check if local sources would cause refresh-all to change the image"
     $ECHO "  self-bootstrap - make local boot image, bootstrap"
     $ECHO "  self-update - git pull, recompile, make local boot image, bootstrap"
     $ECHO "  quick-update - git pull, refresh-all, save"
@@ -782,10 +791,6 @@ if [ "$#" -gt 3 ]; then
     exit 1
 fi
 
-
-set_copy
-set_delete
-
 case "$1" in
     install) install ;;
     deps-apt) install_deps_apt ;;
@@ -794,6 +799,8 @@ case "$1" in
     deps-dnf) install_deps_dnf ;;
     deps-pkg) install_deps_pkg ;;
     info-boot-image) info_boot_image ;;
+    info-check-factor-refresh-all-locally) info_check_factor_refresh_all_locally ;;
+    update-boot-image) find_build_info; check_installed_programs; update_boot_image ;;
     self-bootstrap) get_config_info; make_boot_image; bootstrap  ;;
     self-update) update; make_boot_image; bootstrap  ;;
     quick-update) update; refresh_image ;;
@@ -805,7 +812,6 @@ case "$1" in
     make-target) FIND_MAKE_TARGET=true; ECHO=false; find_build_info; exit_script 0;;
     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 ;;
     update-script) update_script ;;
     *) usage ;;
 esac