Skip to content

Commit db3bf2f

Browse files
committed
Exit early when GHC_LIBDIR and GHC_BIN are set
And improve debug messages.
1 parent ae442fb commit db3bf2f

File tree

1 file changed

+46
-27
lines changed

1 file changed

+46
-27
lines changed

bindist/wrapper.in

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,50 @@ GHC_VERSION="@@GHC_VERSION@@"
66
ABI_HASHES="@@ABI_HASHES@@"
77

88
debug_msg() {
9-
if [ -n "$HLS_WRAPPER_DEBUG" ] ; then
10-
echo >&2 "$1"
11-
fi
9+
if [ -n "$HLS_WRAPPER_DEBUG" ] ; then
10+
(>&2 printf "\\033[0;34m%s\\033[0m\\n" "$1")
11+
fi
12+
}
13+
14+
err_msg() {
15+
if [ -n "$HLS_WRAPPER_DEBUG" ] ; then
16+
(>&2 printf "\\033[0;31m%s\\033[0m\\n" "$1")
17+
elif [ -n "$HLS_WRAPPER_VERBOSE" ] ; then
18+
(>&2 printf "\\033[0;31m%s\\033[0m\\n" "$1")
19+
fi
20+
}
21+
22+
instruction_msg() {
23+
(>&2 printf "\\033[0;35m%s\\033[0m\\n" "$1")
24+
}
25+
26+
err_fix() {
27+
instruction_msg ""
28+
instruction_msg "Consider installing ghc-${GHC_VERSION} via ghcup"
29+
instruction_msg "or build HLS from source."
1230
}
1331

1432
err_ghc_pkg() {
15-
debug_msg >&2 "Could not find a ghc-pkg binary (found: $1)!"
16-
debug_msg >&2 "Trying other methods..."
33+
err_msg "Could not find a ghc-pkg binary (found: $1)!"
1734
}
1835

1936
err_abi() {
20-
debug_msg >&2 "GHC ABIs don't match!"
21-
debug_msg >&2 ""
22-
debug_msg >&2 "Expected: ${ABI_HASHES}"
23-
debug_msg >&2 "Got: $1"
24-
debug_msg >&2 "Trying other methods..."
37+
err_msg "GHC ABIs don't match!"
38+
err_msg ""
39+
err_msg "Expected: ${ABI_HASHES}"
40+
err_msg "Got: $1"
2541
}
2642

2743
err_ver() {
28-
debug_msg >&2 "GHC versions don't match!"
29-
debug_msg >&2 ""
30-
debug_msg >&2 "Expected: ${GHC_VERSION}"
31-
debug_msg >&2 "Got: $1"
32-
debug_msg >&2 "Trying other methods..."
44+
err_msg "GHC versions don't match!"
45+
err_msg ""
46+
err_msg "Expected: ${GHC_VERSION}"
47+
err_msg "Got: $1"
3348
}
3449

3550
# Check the version of GHC and the ABI.
3651
check_ghc() {
37-
{ [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] ;} && debug_msg "internal error: not enough arguments to check_ghc: 1:$1,2:$2,3:$3,4:$4" && return 4
38-
39-
debug_msg >&2 "Trying method $4"
52+
{ [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] ;} && debug_msg "internal error: not enough arguments to check_ghc: 1:$1,2:$2,3:$3" && return 4
4053

4154
check_ghc_libdir=$1
4255
check_ghc_bin=$2
@@ -92,45 +105,47 @@ infer_ghc_pkg() {
92105
# try GHC_LIBDIR from the environment (e.g. user set it, or haskell-language-server-wrapper)
93106
if [ -n "${GHC_LIBDIR}" ] &&
94107
[ -n "${GHC_BIN}" ] &&
95-
check_ghc "${GHC_LIBDIR}" "${GHC_BIN}" "$(infer_ghc_pkg "${GHC_BIN}")" "GHC_LIBDIR and GHC_BIN from env"
108+
{ debug_msg "Trying method: GHC_LIBDIR and GHC_BIN from env" ; HLS_WRAPPER_VERBOSE=1 ; check_ghc "${GHC_LIBDIR}" "${GHC_BIN}" "$(infer_ghc_pkg "${GHC_BIN}")" || { err_fix ; exit 1 ; } ; }
96109
then
97110
:
98111
# try GHC_BIN from the environment (e.g. user set it)
99112
elif [ -n "${GHC_BIN}" ] &&
100113
GHC_LIBDIR="$("${GHC_BIN}" --print-libdir)" &&
101-
check_ghc "${GHC_LIBDIR}" "${GHC_BIN}" "$(infer_ghc_pkg "${GHC_BIN}")" "GHC_BIN from env"
114+
{ debug_msg "Trying method: GHC_BIN from env" ; HLS_WRAPPER_VERBOSE=1 ; check_ghc "${GHC_LIBDIR}" "${GHC_BIN}" "$(infer_ghc_pkg "${GHC_BIN}")" || { err_fix ; exit 2 ; } ; }
102115
then
103116
:
104117
# try ghcup
105118
elif command -v ghcup >/dev/null &&
106119
GHC_BIN="$(ghcup whereis ghc "${GHC_VERSION}")" &&
107120
GHC_LIBDIR="$("${GHC_BIN}" --print-libdir)" &&
108-
check_ghc "${GHC_LIBDIR}" "${GHC_BIN}" "$(infer_ghc_pkg "${GHC_BIN}")" "ghcup"
121+
{ debug_msg "Trying method: ghcup" ; check_ghc "${GHC_LIBDIR}" "${GHC_BIN}" "$(infer_ghc_pkg "${GHC_BIN}")" ; }
109122
then
110123
:
111124
# try ghc-${GHC_VERSION}
112125
elif command -v ghc-${GHC_VERSION} >/dev/null &&
113126
GHC_LIBDIR="$("ghc-${GHC_VERSION}" --print-libdir)" &&
114-
check_ghc "${GHC_LIBDIR}" "ghc-${GHC_VERSION}" "$(infer_ghc_pkg "ghc-${GHC_VERSION}")" "ghc-${GHC_VERSION} in PATH"
127+
{ debug_msg "Trying method: ghc-${GHC_VERSION} in PATH" ; check_ghc "${GHC_LIBDIR}" "ghc-${GHC_VERSION}" "$(infer_ghc_pkg "ghc-${GHC_VERSION}")" ; }
115128
then
116129
:
117130
# try ghc
118131
elif command -v ghc >/dev/null &&
119132
GHC_LIBDIR="$(ghc --print-libdir)" &&
120-
check_ghc "${GHC_LIBDIR}" "ghc" "$(infer_ghc_pkg "ghc")" "ghc in PATH"
133+
{ debug_msg "Trying method: ghc in PATH" ; check_ghc "${GHC_LIBDIR}" "ghc" "$(infer_ghc_pkg "ghc")" ; }
121134
then
122135
:
123136
# try stack
124137
elif command -v stack >/dev/null &&
125138
GHC_BIN="$(cd "$(mktemp -d)" && stack --no-system-ghc --no-install-ghc --resolver "ghc-${GHC_VERSION}" exec sh -- -c 'command -v ghc')" &&
126139
GHC_LIBDIR="$("${GHC_BIN}" --print-libdir)" &&
127-
check_ghc "${GHC_LIBDIR}" "${GHC_BIN}" "$(infer_ghc_pkg "${GHC_BIN}")" "stack"
140+
{ debug_msg "Trying method: stack" ; check_ghc "${GHC_LIBDIR}" "${GHC_BIN}" "$(infer_ghc_pkg "${GHC_BIN}")" ; }
128141
then
129142
:
130143
else
131-
echo >&2 "All methods exhausted!"
132-
echo >&2 "Couldn't find a working/matching GHC installation"
133-
echo >&2 "exiting..."
144+
HLS_WRAPPER_VERBOSE=1
145+
err_msg "All methods exhausted!"
146+
err_msg "Couldn't find a working/matching GHC installation"
147+
err_fix
148+
err_msg "exiting..."
134149
exit 42
135150
fi
136151

@@ -140,18 +155,22 @@ case "$(uname -s)" in
140155
"Darwin"|"darwin")
141156
if [ -n "$DYLD_LIBRARY_PATH" ] ; then
142157
DYLD_LIBRARY_PATH="$(for i in "${GHC_LIBDIR}"/* ; do [ -d "$i" ] && printf "%s" "$i:" ; done)$DYLD_LIBRARY_PATH"
158+
debug_msg "Exporting DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}"
143159
export DYLD_LIBRARY_PATH
144160
else
145161
DYLD_LIBRARY_PATH="$(for i in "${GHC_LIBDIR}"/* ; do [ -d "$i" ] && printf "%s" "$i:" ; done | sed 's/:$//')"
162+
debug_msg "Exporting DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}"
146163
export DYLD_LIBRARY_PATH
147164
fi
148165
;;
149166
*)
150167
if [ -n "$LD_LIBRARY_PATH" ] ; then
151168
LD_LIBRARY_PATH="$(for i in "${GHC_LIBDIR}"/* ; do [ -d "$i" ] && printf "%s" "$i:" ; done)$LD_LIBRARY_PATH"
169+
debug_msg "Exporting LD_LIBRARY_PATH=${LD_LIBRARY_PATH}"
152170
export LD_LIBRARY_PATH
153171
else
154172
LD_LIBRARY_PATH="$(for i in "${GHC_LIBDIR}"/* ; do [ -d "$i" ] && printf "%s" "$i:" ; done | sed 's/:$//')"
173+
debug_msg "Exporting LD_LIBRARY_PATH=${LD_LIBRARY_PATH}"
155174
export LD_LIBRARY_PATH
156175
fi
157176
;;

0 commit comments

Comments
 (0)