Merge pull request #262 from alexbiehl/patch-1
[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]}" -eq 1 -a "${CABVER[1]}" -ge 24 ]; then
46 # New enough cabal version detected, so
47 # let's use the superior 'cabal new-build' mode
48
49 # there's no 'cabal new-run' yet, but it's easy to emulate
50 "$CABAL" new-build --disable-profiling --disable-documentation -j exe:hadrian
51 PKGVER="$(awk '/^version:/ { print $2 }' hadrian.cabal)"
52 "./dist-newstyle/build/hadrian-${PKGVER}/build/hadrian/hadrian" \
53 --lint \
54 --directory "$absoluteRoot/.." \
55 "$@"
56
57 else
58 # The logic below is quite fragile, but it's better than nothing for pre-1.24 cabals
59 echo "Old pre cabal 1.24 version detected. Falling back to legacy 'cabal sandbox' mode."
60
61 # Initialize sandbox if necessary
62 if ! ( "$CABAL" sandbox hc-pkg list > /dev/null 2>&1); then
63 "$CABAL" sandbox init
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