Fix AppVeyor (#577)
[hadrian.git] / build.cabal.sh
index 5f20c1b..d2bdb85 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,17 +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 ghc-shake --             \
-    --lint                         \
-    --directory "$absoluteRoot/.." \
-    --colour                       \
-    "$@"
+CABVERSTR=$("$CABAL" --numeric-version)
+
+CABVER=( ${CABVERSTR//./ } )
+
+if [ "${CABVER[0]}" -gt 2 -o "${CABVER[0]}" -eq 2 -a "${CABVER[1]}" -ge 1 ]; then
+    # New enough Cabal version detected, so let's use the superior new-build + new-run
+    # modes. Note that pre-2.1 Cabal does not support passing additional parameters
+    # to the executable (hadrian) after the separator '--', see #438.
+
+    "$CABAL" new-build --disable-profiling --disable-documentation -j exe:hadrian
+    "$CABAL" new-run hadrian --        \
+        --lint                         \
+        --directory "$absoluteRoot/.." \
+        "$@"
+
+else
+    # The logic below is quite fragile, but it's better than nothing for pre-2.1 Cabal.
+    echo "Old pre cabal 2.1 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" sandbox add-source ../libraries/Cabal/Cabal
+        "$CABAL" install                \
+            --dependencies-only         \
+            --disable-library-profiling \
+            --disable-shared
+    fi
+
+    "$CABAL" run hadrian --            \
+        --lint                         \
+        --directory "$absoluteRoot/.." \
+        "$@"
+fi