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
# return 1 on found
test_programs_installed() {
- local installed=0;
+ local installed=0
$ECHO -n "Checking for all($*)..."
for cmd in "$@" ;
do
}
ensure_program_installed() {
- local installed=0;
+ local installed=0
$ECHO -n "Checking for any($*)..."
for cmd in "$@" ;
do
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
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() {
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 ;;
+ aarch64) ARCH=arm ;;
+ arm64) ARCH=arm ;;
+ iPhone5*[3-9]) ARCH=arm ;;
+ iPhone[6-9]*) ARCH=arm ;;
+ iPhone[1-9][0-9]*) ARCH=arm ;;
+ iPad[4-9]*) ARCH=arm ;;
+ iPad[1-9][0-9]*) ARCH=arm ;;
+ AppleTV[5-9]*) ARCH=arm ;;
+ AppleTV[1-9][0-9]*) ARCH=arm ;;
"Power Macintosh") ARCH=ppc ;;
esac
}
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"
}
$ECHO "DEBUG=$DEBUG"
$ECHO "REPRODUCIBLE=$REPRODUCIBLE"
$ECHO "CURRENT_BRANCH=$CURRENT_BRANCH"
+ $ECHO "CURRENT_BRANCH_FULL=$CURRENT_BRANCH_FULL"
$ECHO "FACTOR_BINARY=$FACTOR_BINARY"
$ECHO "FACTOR_LIBRARY=$FACTOR_LIBRARY"
$ECHO "FACTOR_IMAGE=$FACTOR_IMAGE"
$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() {
set_build_info() {
check_os_arch_word
- 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
- elif [[ $OS == windows && $ARCH == x86 && $WORD == 32 ]] ; then
- MAKE_IMAGE_TARGET=windows-x86.32
- MAKE_TARGET=windows-x86-32
- elif [[ $ARCH == x86 && $WORD == 64 ]] ; then
- MAKE_IMAGE_TARGET=unix-x86.64
- MAKE_TARGET=$OS-x86-64
- elif [[ $ARCH == x86 && $WORD == 32 ]] ; then
- MAKE_IMAGE_TARGET=unix-x86.32
- MAKE_TARGET=$OS-x86-32
+ if [[ $OS == "windows" ]] ; then
+ MAKE_IMAGE_TARGET=windows-$ARCH.$WORD
+ MAKE_TARGET=$OS-$ARCH-$WORD
else
- MAKE_IMAGE_TARGET=$ARCH.$WORD
+ MAKE_IMAGE_TARGET=unix-$ARCH.$WORD
MAKE_TARGET=$OS-$ARCH-$WORD
fi
BOOT_IMAGE=boot.$MAKE_IMAGE_TARGET.image
$ECHO "WORD=$WORD"
}
-find_build_info() {
+prepare_build_info() {
find_os
find_architecture
find_num_cores
set_downloader
set_boot_image_vars
set_make
+}
+
+find_build_info() {
+ prepare_build_info
echo_build_info
}
}
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} ${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"
-
- chmod 755 "$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() {
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."
}
current_git_branch() {
# 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=.*/=='
+ 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
}
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}"
if [ -n "${CI_BRANCH}" ]; then
CURRENT_BRANCH="${CI_BRANCH}"
else
- CURRENT_BRANCH=$(current_git_branch)
+ CURRENT_BRANCH_FULL=$(current_git_branch)
+ 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
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
}
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() {
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=
+ check_ret "Could not launch ./$FACTOR_BINARY"
+}
+
+is_boot_image_outdated() {
+ "./$FACTOR_BINARY" "-e=USE: system \"\" to-refresh 2drop length 0 > 1 0 ? exit"
+ return $?
+}
+
+info_boot_image() {
+ prepare_build_info
+ if [[ -f $BOOT_IMAGE ]] ; then
+ get_url "$CHECKSUM_URL"
+ local factorcode_md5
+ factorcode_md5=$(grep "$BOOT_IMAGE" checksums.txt | cut -f2 -d' ')
+ set_md5sum
+ local disk_md5
+ disk_md5=$($MD5SUM "$BOOT_IMAGE" | cut -f1 -d' ')
+ $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
+
+ if is_boot_image_outdated; then
+ $ECHO "Your Factor image is consistent with the local source code."
+ else
+ $ECHO "Your Factor image is not consistent with the local source code."
+ fi
}
bootstrap() {
$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"
+ $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"
exit 1
fi
-
-set_copy
-set_delete
-
case "$1" in
install) install ;;
deps-apt) install_deps_apt ;;
deps-macosx) install_deps_macosx ;;
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 ;;
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