Fix AppVeyor (#577)
[hadrian.git] / build.cabal.sh
1 #!/usr/bin/env bash
2
3 CABAL=cabal
4
5 set -euo pipefail
6
7 # readlink on os x, doesn't support -f, to prevent the
8 # need of installing coreutils (e.g. through brew, just
9 # for readlink, we use the follownig substitute.
10 #
11 # source: http://stackoverflow.com/a/1116890
12 function rl {
13 TARGET_FILE="$1"
14
15 cd "$(dirname "$TARGET_FILE")"
16 TARGET_FILE="$(basename "$TARGET_FILE")"
17
18 # Iterate down a (possible) chain of symlinks
19 while [ -L "$TARGET_FILE" ]
20 do
21 TARGET_FILE="$(readlink "$TARGET_FILE")"
22 cd "$(dirname "$TARGET_FILE")"
23 TARGET_FILE="$(basename "$TARGET_FILE")"
24 done
25
26 # Compute the canonicalized name by finding the physical path
27 # for the directory we're in and appending the target file.
28 PHYS_DIR="$(pwd -P)"
29 RESULT="$PHYS_DIR/$TARGET_FILE"
30 echo "$RESULT"
31 }
32
33 absoluteRoot="$(dirname "$(rl "$0")")"
34 cd "$absoluteRoot"
35
36 if ! type "$CABAL" > /dev/null; then
37 echo "Please make sure 'cabal' is in your PATH"
38 exit 2
39 fi
40
41 CABVERSTR=$("$CABAL" --numeric-version)
42
43 CABVER=( ${CABVERSTR//./ } )
44
45 if [ "${CABVER[0]}" -gt 2 -o "${CABVER[0]}" -eq 2 -a "${CABVER[1]}" -ge 1 ]; then
46 # New enough Cabal version detected, so let's use the superior new-build + new-run
47 # modes. Note that pre-2.1 Cabal does not support passing additional parameters
48 # to the executable (hadrian) after the separator '--', see #438.
49
50 "$CABAL" new-build --disable-profiling --disable-documentation -j exe:hadrian
51 "$CABAL" new-run hadrian -- \
52 --lint \
53 --directory "$absoluteRoot/.." \
54 "$@"
55
56 else
57 # The logic below is quite fragile, but it's better than nothing for pre-2.1 Cabal.
58 echo "Old pre cabal 2.1 version detected. Falling back to legacy 'cabal sandbox' mode."
59
60 # Initialize sandbox if necessary
61 if ! ( "$CABAL" sandbox hc-pkg list > /dev/null 2>&1); then
62 "$CABAL" sandbox init
63 "$CABAL" sandbox add-source ../libraries/Cabal/Cabal
64 "$CABAL" install \
65 --dependencies-only \
66 --disable-library-profiling \
67 --disable-shared
68 fi
69
70 "$CABAL" run hadrian -- \
71 --lint \
72 --directory "$absoluteRoot/.." \
73 "$@"
74 fi