build.cabal.sh 2.18 KB
Newer Older
1 2
#!/usr/bin/env bash

3 4
CABAL=cabal

5 6
set -euo pipefail

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# 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"
}

joehillen's avatar
joehillen committed
33 34
absoluteRoot="$(dirname "$(rl "$0")")"
cd "$absoluteRoot"
35

36 37 38
if ! type "$CABAL" > /dev/null; then
    echo "Please make sure 'cabal' is in your PATH"
    exit 2
39 40
fi

41 42 43 44
CABVERSTR=$("$CABAL" --numeric-version)

CABVER=( ${CABVERSTR//./ } )

45
if [ "${CABVER[0]}" -eq 2 -o "${CABVER[0]}" -eq 1 -a "${CABVER[1]}" -ge 24 ]; then
46 47 48 49 50
    # 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
51
    $(find ./dist-newstyle -type f -name hadrian | head -n 1) \
52 53 54 55 56 57 58 59 60 61 62
        --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
63
        "$CABAL" sandbox add-source ../libraries/Cabal/Cabal
64 65 66 67 68 69 70 71 72 73 74
        "$CABAL" install                \
            --dependencies-only         \
            --disable-library-profiling \
            --disable-shared
    fi

    "$CABAL" run hadrian --            \
        --lint                         \
        --directory "$absoluteRoot/.." \
        "$@"
fi