Merge build.cabal-new.sh into build.cabal.sh
authorHerbert Valerio Riedel <hvr@gnu.org>
Fri, 3 Jun 2016 20:19:36 +0000 (22:19 +0200)
committerHerbert Valerio Riedel <hvr@gnu.org>
Fri, 3 Jun 2016 20:19:36 +0000 (22:19 +0200)
The script now detect the cabal version and uses either the robust and
fast 'new-build'-logic (for version 1.24 or later), or falls back to the
fragile sandbox-based legacy logic.

build.cabal-new.sh [deleted file]
build.cabal.sh
build.sh

diff --git a/build.cabal-new.sh b/build.cabal-new.sh
deleted file mode 100755 (executable)
index 65e222a..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-#!/usr/bin/env bash
-
-# This wrapper scripts makes use of cabal 1.24+'s nix-store;
-# In order to clean/reset, remove the `dist-newstyle/` folder
-
-set -euo pipefail
-
-# readlink on os x, doesn't support -f, to prevent the
-# need of installing coreutils (e.g. through brew, just
-# for readlink, we use the follownig substitute.
-#
-# source: http://stackoverflow.com/a/1116890
-function rl {
-    TARGET_FILE="$1"
-
-    cd "$(dirname "$TARGET_FILE")"
-    TARGET_FILE="$(basename "$TARGET_FILE")"
-
-    # Iterate down a (possible) chain of symlinks
-    while [ -L "$TARGET_FILE" ]
-    do
-        TARGET_FILE="$(readlink "$TARGET_FILE")"
-        cd "$(dirname "$TARGET_FILE")"
-        TARGET_FILE="$(basename "$TARGET_FILE")"
-    done
-
-    # Compute the canonicalized name by finding the physical path
-    # for the directory we're in and appending the target file.
-    PHYS_DIR="$(pwd -P)"
-    RESULT="$PHYS_DIR/$TARGET_FILE"
-    echo "$RESULT"
-}
-
-root="$(dirname "$(rl "$0")")"
-
-mkdir -p "$root/.shake"
-
-# Notes/Random thoughts:
-#
-# - if ghc.git had a top-level `cabal.project` file, we could maybe avoid the
-# boilerplate above, as we could simply say `cabal exec hadrian` from within
-# any GHC folder not shadowed by a nearer shadowing `cabal.project` file.
-
-pushd "$root/"
-
-cabal new-build --disable-profiling --disable-documentation -j exe:hadrian
-
-PKGVER="$(awk '/^version:/ { print $2 }' hadrian.cabal)"
-
-cp -v "$root/dist-newstyle/build/hadrian-${PKGVER}/build/hadrian/hadrian" \
-      "$root/.shake/build"
-
-popd
-
-"$root/.shake/build"       \
-    --lint                 \
-    --directory "$root/.." \
-    "$@"
index 08ff972..4a24dac 100755 (executable)
@@ -1,5 +1,7 @@
 #!/usr/bin/env bash
 
+CABAL=cabal
+
 set -euo pipefail
 
 # readlink on os x, doesn't support -f, to prevent the
@@ -31,16 +33,42 @@ function rl {
 absoluteRoot="$(dirname "$(rl "$0")")"
 cd "$absoluteRoot"
 
-# Initialize sandbox if necessary
-if ! ( cabal sandbox hc-pkg list 2>&1 > /dev/null ); then
-    cabal sandbox init
-    cabal install                   \
-        --dependencies-only         \
-        --disable-library-profiling \
-        --disable-shared
+if ! type "$CABAL" > /dev/null; then
+    echo "Please make sure 'cabal' is in your PATH"
+    exit 2
 fi
 
-cabal run hadrian --               \
-    --lint                         \
-    --directory "$absoluteRoot/.." \
-    "$@"
+CABVERSTR=$("$CABAL" --numeric-version)
+
+CABVER=( ${CABVERSTR//./ } )
+
+if [ "${CABVER[0]}" -eq 1 -a "${CABVER[1]}" -ge 24 ]; then
+    # New enough cabal version detected, so
+    # let's use the superior 'cabal new-build' mode
+
+    # there's no 'cabal new-run' yet, but it's easy to emulate
+    "$CABAL" new-build --disable-profiling --disable-documentation -j exe:hadrian
+    PKGVER="$(awk '/^version:/ { print $2 }' hadrian.cabal)"
+    "./dist-newstyle/build/hadrian-${PKGVER}/build/hadrian/hadrian" \
+        --lint                         \
+        --directory "$absoluteRoot/.." \
+        "$@"
+
+else
+    # The logic below is quite fragile, but it's better than nothing for pre-1.24 cabals
+    echo "Old pre cabal 1.24 version detected. Falling back to legacy 'cabal sandbox' mode."
+
+    # Initialize sandbox if necessary
+    if ! ( "$CABAL" sandbox hc-pkg list > /dev/null 2>&1); then
+        "$CABAL" sandbox init
+        "$CABAL" install                \
+            --dependencies-only         \
+            --disable-library-profiling \
+            --disable-shared
+    fi
+
+    "$CABAL" run hadrian --            \
+        --lint                         \
+        --directory "$absoluteRoot/.." \
+        "$@"
+fi
index 24fdc2f..d627c58 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -30,6 +30,15 @@ function rl {
 
 root="$(dirname "$(rl "$0")")"
 
+if type cabal > /dev/null 2>&1; then
+    CABVERSTR=$(cabal --numeric-version)
+    CABVER=( ${CABVERSTR//./ } )
+    if [ "${CABVER[0]}" -eq 1 -a "${CABVER[1]}" -ge 24 ]; then
+        echo "** Cabal 1.24 or later detected. Please consider using the 'build.cabal.sh' script **"
+        echo ""
+    fi
+fi
+
 mkdir -p "$root/../_build/hadrian"
 
 ghc                                      \