Minor revision
[hadrian.git] / build.cabal.sh
index 8add516..2a0e8a7 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
@@ -28,20 +30,45 @@ function rl {
     echo "$RESULT"
 }
 
-absoltueRoot="$(dirname "$(rl "$0")")"
-cd "$absoltueRoot"
+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 "$absoltueRoot/.." \
-    --colour                       \
-    "$@"
+CABVERSTR=$("$CABAL" --numeric-version)
+
+CABVER=( ${CABVERSTR//./ } )
+
+if [ "${CABVER[0]}" -eq 2 -o "${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
+    $(find ./dist-newstyle -type f -name hadrian | head -n 1) \
+        --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" sandbox add-source ../libraries/Cabal/Cabal
+        "$CABAL" install                \
+            --dependencies-only         \
+            --disable-library-profiling \
+            --disable-shared
+    fi
+
+    "$CABAL" run hadrian --            \
+        --lint                         \
+        --directory "$absoluteRoot/.." \
+        "$@"
+fi