From fc8fce55955ff9811978b7ba593c20e175357011 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Thu, 15 Sep 2016 16:55:59 +0930 Subject: [PATCH] Improve build-libsodium.sh --- build-libsodium.sh | 121 +++++++++++++++++++++++++++------------------ 1 file changed, 73 insertions(+), 48 deletions(-) diff --git a/build-libsodium.sh b/build-libsodium.sh index ded4d895..ca776e48 100755 --- a/build-libsodium.sh +++ b/build-libsodium.sh @@ -16,17 +16,10 @@ # script. This location can be overridden by giving an alternative directory # path on the command line, which the script will create if it does not exist. -LIBSODIUM_URL_PATH="jedisct1/libsodium.git" -LIBSODIUM_GIT_URL_HTTPS="https://github.com/$LIBSODIUM_URL_PATH" -LIBSODIUM_GIT_URL_SSH="git@github.com:$LIBSODIUM_URL_PATH" -LIBSODIUM_GIT_URL="$LIBSODIUM_GIT_URL_HTTPS" - -# Exit on error -set -e - usage() { echo "Usage: ${0##*/} [--directory PATH] [--ssh] [--make-arg ARG]" - echo "Options: --directory PATH download and build in PATH [$LIBSODIUM_DIR]" + echo "Options: --src PATH download and build in PATH [$LIBSODIUM_BUILD_DIR]" + echo " --prefix PATH install into PATH [$LIBSODIUM_INSTALL_DIR]" echo " --no-update do not update if already downloaded" echo " --no-clean do not clean if already built" echo " --ssh download from GitHub using SSH instead of HTTPS" @@ -34,6 +27,32 @@ usage() { echo " --make-arg ARG pass ARG to the 'make' command" } +# Work out the path of the directory that contains this script. +case "$0" in +*/*) SCRIPT_DIR="${0%/*}";; +*) SCRIPT_DIR=".";; +esac +case "$SCRIPT_DIR" in +*/*) SCRIPT_PARENT_DIR="${SCRIPT_DIR%/*}";; +.) SCRIPT_PARENT_DIR="..";; +*) SCRIPT_PARENT_DIR="$SCRIPT_DIR/..";; +esac + +# Download location. +LIBSODIUM_URL_PATH="jedisct1/libsodium.git" +LIBSODIUM_GIT_URL_HTTPS="https://github.com/$LIBSODIUM_URL_PATH" +LIBSODIUM_GIT_URL_SSH="git@github.com:$LIBSODIUM_URL_PATH" +LIBSODIUM_GIT_URL="$LIBSODIUM_GIT_URL_HTTPS" + +# The directory in which to install the built libsodium. +LIBSODIUM_INSTALL_DIR="$SCRIPT_DIR/libsodium" + +# The directory in which to download and build libsodium. +LIBSODIUM_BUILD_DIR="$SCRIPT_PARENT_DIR/libsodium" + +# Exit on error +set -e + usage_error() { echo "${0##*/}: $*" >&2 usage >&2 @@ -45,21 +64,6 @@ fatal() { exit 1 } -# Work out the absolute path of the directory that contains this script. -case "$0" in -/*) SCRIPT_DIR="${0%/*}";; -./*) SCRIPT_DIR="$PWD";; -*/*) SCRIPT_DIR="$PWD/${0%/*}";; -*) SCRIPT_DIR="$PWD";; -esac - -# The directory in which to install the built libsodium. -LIBSODIUM_DIR_NAME="libsodium" -LIBSODIUM_INSTALL_DIR="$SCRIPT_DIR/$LIBSODIUM_DIR_NAME" - -# The default directory in which to download and build libsodium. -LIBSODIUM_DIR="$SCRIPT_DIR/../libsodium" - # Parse the command-line, preserving all the arguments for later reference. PRESERVED_ARGS=() OPT_UPDATE=true @@ -74,14 +78,24 @@ while [ $# -ne 0 ]; do usage exit 0 ;; - --directory=*) + --src=*) PRESERVED_ARGS+=("$opt") - LIBSODIUM_DIR="${opt#*=}" + LIBSODIUM_BUILD_DIR="${opt#*=}" ;; - --directory) + --src) [ $# -ge 1 ] || usage_error "missing argument after $opt" PRESERVED_ARGS+=("$opt" "$1") - LIBSODIUM_DIR="$1" + LIBSODIUM_BUILD_DIR="$1" + shift + ;; + --prefix=*) + PRESERVED_ARGS+=("$opt") + LIBSODIUM_INSTALL_DIR="${opt#*=}" + ;; + --prefix) + [ $# -ge 1 ] || usage_error "missing argument after $opt" + PRESERVED_ARGS+=("$opt" "$1") + LIBSODIUM_INSTALL_DIR="$1" shift ;; --ssh) @@ -123,34 +137,45 @@ while [ $# -ne 0 ]; do esac done -if [ ! -d "$LIBSODIUM_DIR" ]; then - echo "Create $LIBSODIUM_DIR" - mkdir -p "$LIBSODIUM_DIR" +abspath() { + case "$1" in + /*) echo "$1";; + .) echo "$PWD";; + *) echo "$PWD/${1#./}";; + esac +} + +if [ ! -d "$LIBSODIUM_BUILD_DIR" ]; then + echo "Create $LIBSODIUM_BUILD_DIR" + mkdir -p "$LIBSODIUM_BUILD_DIR" fi +LIBSODIUM_INSTALL_ABSDIR="$(abspath "$LIBSODIUM_INSTALL_DIR")" + is_libsodium_downloaded() { [ -r "$1/src/libsodium/include/sodium.h" -a \ -r "$1/libsodium-uninstalled.pc.in" \ ] } -if ! is_libsodium_downloaded "$LIBSODIUM_DIR"; then +if ! is_libsodium_downloaded "$LIBSODIUM_BUILD_DIR"; then echo "Download libsodium from $LIBSODIUM_GIT_URL..." - git clone --branch stable "$LIBSODIUM_GIT_URL" "$LIBSODIUM_DIR" - cd "$LIBSODIUM_DIR" >/dev/null + git clone --branch stable "$LIBSODIUM_GIT_URL" "$LIBSODIUM_BUILD_DIR" + cd "$LIBSODIUM_BUILD_DIR" >/dev/null is_libsodium_downloaded . || fatal "Download did not produce expected source files" else echo "Libsodium appears to already be downloaded" - cd "$LIBSODIUM_DIR" >/dev/null + cd "$LIBSODIUM_BUILD_DIR" >/dev/null + git checkout stable if $OPT_UPDATE; then echo "Update from" $(git remote get-url origin) - git pull --ff-only + git pull --ff-only origin stable fi fi -if [ -d "$LIBSODIUM_INSTALL_DIR" ]; then +if [ -d "$LIBSODIUM_INSTALL_ABSDIR" ]; then echo "Delete the previous installation" - rm -rf "$LIBSODIUM_INSTALL_DIR" + rm -rf "$LIBSODIUM_INSTALL_ABSDIR" fi if $OPT_CLEAN && [ -r Makefile ]; then @@ -160,7 +185,7 @@ fi if [ -z "$DIST" ]; then echo "Native build..." - [ -r Makefile ] || ./configure --prefix="$LIBSODIUM_INSTALL_DIR" + [ -r Makefile ] || ./configure --prefix="$LIBSODIUM_INSTALL_ABSDIR" make -j3 "${MAKE_ARGS[@]}" check make -j3 "${MAKE_ARGS[@]}" install elif [ -x "dist-build/$DIST.sh" ]; then @@ -172,15 +197,15 @@ elif [ -x "dist-build/$DIST.sh" ]; then echo "Build using 'dist-build/$DIST.sh'..." "dist-build/$DIST.sh" [ -d "$installed" ] || fatal "build did not produce $installed" - echo "Copy built installation into $LIBSODIUM_INSTALL_DIR" - cp -R -p "$installed" "$LIBSODIUM_INSTALL_DIR" + echo "Copy built installation into $LIBSODIUM_INSTALL_ABSDIR" + cp -R -p "$installed" "$LIBSODIUM_INSTALL_ABSDIR" else fatal "script "dist-build/$DIST_BUILD.sh" does not exist." fi # Create a shell script that will set up the environment to use the built and # installed libsodium. -cat >"$LIBSODIUM_INSTALL_DIR/settings.sh" <"$LIBSODIUM_INSTALL_ABSDIR/settings.sh" <"$LIBSODIUM_INSTALL_DIR/settings.sh" <"$LIBSODIUM_INSTALL_DIR/README.txt" <"$LIBSODIUM_INSTALL_ABSDIR/README.txt" <