-
Notifications
You must be signed in to change notification settings - Fork 546
/
install
executable file
·350 lines (311 loc) · 11.4 KB
/
install
1
2
3
4
5
6
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env bash
. "$GVM_ROOT/scripts/functions"
function show_usage() {
echo "Usage: gvm install [version] [options]"
echo " -s, --source=SOURCE Install Go from specified source."
echo " -n, --name=NAME Override the default name for this version."
echo " -pb, --with-protobuf Install Go protocol buffers."
echo " -b, --with-build-tools Install package build tools."
echo " -B, --binary Only install from binary."
echo " --prefer-binary Attempt a binary install, falling back to source."
echo " -h, --help Display this message."
}
read_command_line() {
VERSION=$1
if [[ "${VERSION:0:1}" != "-" ]]; then
shift
else
display_warning "Invalid version: $1"
show_usage
exit 1
fi
GO_SOURCE_URL=https://github.com/golang/go
for i in "$@"; do
case $i in
-s=*|--source=*)
GO_SOURCE_URL=$(echo "$i" | sed 's/[-a-zA-Z0-9]*=//')
;;
-n=*|--name=*)
GO_NAME=$(echo "$i" | sed 's/[-a-zA-Z0-9]*=//')
;;
-pb|--with-protobuf)
INSTALL_PB="true"
;;
-b|--with-build-tools)
INSTALL_BUILD_TOOLS="true"
;;
-B|--binary)
INSTALL_SOURCE="binary"
;;
--prefer-binary)
INSTALL_SOURCE="prefer-binary"
;;
-h|--help)
show_usage
exit 0
;;
*)
display_warning "Invalid option $i"
show_usage
exit 65 # Bad arguments
;;
esac
done
}
download_source() {
GO_CACHE_PATH=$GVM_ROOT/archive/go
[[ -d $GO_CACHE_PATH ]] && return
display_message "Downloading Go source..."
git clone "$GO_SOURCE_URL" "$GO_CACHE_PATH" >> "$GVM_ROOT/logs/go-download.log" 2>&1 ||
display_fatal "Couldn't download Go source. Check the logs $GVM_ROOT/logs/go-download.log"
}
check_tag() {
version=$(builtin cd "$GO_CACHE_PATH" && git show-ref --heads --tags | awk -F/ '{ print $NF }' | $SORT_PATH | $GREP_PATH "$VERSION" | $HEAD_PATH -n 1 | $GREP_PATH -w "$VERSION")
}
update_source() {
display_message "Updating Go source..."
(builtin cd "$GO_CACHE_PATH" && git pull --tags >> "$GVM_ROOT/logs/go-download.log" 2>&1 ||
display_fatal "Couldn't get latest Go version info. Check the logs $GVM_ROOT/logs/go-download.log")
}
copy_source() {
git clone -b "$version" "$GO_CACHE_PATH" "$GO_INSTALL_ROOT" >> "$GVM_ROOT/logs/go-$GO_NAME-install.log" 2>&1 ||
display_fatal "Couldn't copy source to target folder. Check the logs $GVM_ROOT/logs/go-$GO_NAME-install.log"
}
compile_go() {
display_message " * Compiling..."
# Test for Windows
case "$(uname)" in
*MINGW* | *WIN32* | *CYGWIN*)
MAKE_SCRIPT=make.bat
;;
*)
MAKE_SCRIPT=make.bash
;;
esac
[ -z "$GOROOT_BOOTSTRAP" ] && export GOROOT_BOOTSTRAP=$(go env GOROOT)
unset GOARCH && unset GOOS && unset GOPATH && unset GOBIN && unset GOROOT &&
export GOBIN=$GO_INSTALL_ROOT/bin &&
export PATH=$GOBIN:$PATH &&
export GOROOT=$GO_INSTALL_ROOT &&
if [ ! -f "$GO_INSTALL_ROOT/VERSION" ]; then echo "$GO_NAME" > "$GO_INSTALL_ROOT/VERSION"; fi &&
#builtin cd $GO_INSTALL_ROOT/src && ./all.bash &> $GVM_ROOT/logs/go-$GO_NAME-compile.log ||
builtin cd "$GO_INSTALL_ROOT/src" && chmod -f +x $MAKE_SCRIPT && ./$MAKE_SCRIPT &> "$GVM_ROOT/logs/go-$GO_NAME-compile.log" ||
(rm -rf "$GO_INSTALL_ROOT" && display_fatal "Failed to compile. Check the logs at $GVM_ROOT/logs/go-$GO_NAME-compile.log")
}
create_enviroment() {
new_env_file=$GVM_ROOT/environments/$GO_NAME
echo "export GVM_ROOT; GVM_ROOT=\"$GVM_ROOT\"" > "$new_env_file"
echo "export gvm_go_name; gvm_go_name=\"$GO_NAME\"" >> "$new_env_file"
echo "export gvm_pkgset_name; gvm_pkgset_name=\"global\"" >> "$new_env_file"
echo "export GOROOT; GOROOT=\"\$GVM_ROOT/gos/$GO_NAME\"" >> "$new_env_file"
echo "export GOPATH; GOPATH=\"\$GVM_ROOT/pkgsets/$GO_NAME/global\"" >> "$new_env_file"
echo "export GVM_OVERLAY_PREFIX; GVM_OVERLAY_PREFIX=\"\${GVM_ROOT}/pkgsets/${GO_NAME}/global/overlay\"" >> "$new_env_file"
echo "export PATH; PATH=\"\${GVM_ROOT}/pkgsets/${GO_NAME}/global/bin:\${GVM_ROOT}/gos/${GO_NAME}/bin:\${GVM_OVERLAY_PREFIX}/bin:\${GVM_ROOT}/bin:\${PATH}\"" >> "$new_env_file"
echo "export LD_LIBRARY_PATH; LD_LIBRARY_PATH=\"\${GVM_OVERLAY_PREFIX}/lib\${LD_LIBRARY_PATH:+:\$LD_LIBRARY_PATH}\"" >> "$new_env_file"
echo "export DYLD_LIBRARY_PATH; DYLD_LIBRARY_PATH=\"\${GVM_OVERLAY_PREFIX}/lib\${DYLD_LIBRARY_PATH:+:\$DYLD_LIBRARY_PATH}\"" >> "$new_env_file"
echo "export PKG_CONFIG_PATH; PKG_CONFIG_PATH=\"\${GVM_OVERLAY_PREFIX}/lib/pkgconfig:\${PKG_CONFIG_PATH}\"" >> "$new_env_file"
. "$GVM_ROOT/scripts/env/use"
. "$GVM_ROOT/scripts/env/implode"
gvm_use "$GO_NAME" &> /dev/null ||
display_fatal "Failed to use installed version"
gvm pkgset create global
unset GOPATH
}
create_global_package_set() {
# Create the global package set folder
mkdir -p "$GVM_ROOT/pkgsets/$GO_NAME" >> "$GVM_ROOT/logs/go-$GO_NAME-install.log" 2>&1 ||
display_fatal "Couldn't create global package set folder. Check the logs at ${GVM_ROOT}/logs/go-${GO_NAME}-install.log"
GVM_OVERLAY_ROOT="${GVM_ROOT}/pkgsets/${GO_NAME}/global/overlay"
mkdir -p "${GVM_OVERLAY_ROOT}/lib/pkgconfig" >> "${GVM_ROOT}/logs/go-${GO_NAME}-install.log" 2>&1 ||
display_fatal "Couldn't create global overlay library directory. Check the logs at ${GVM_ROOT}/logs/go-${GO_NAME}-install.log"
mkdir -p "${GVM_OVERLAY_ROOT}/bin" >> "${GVM_ROOT}/logs/go-${GO_NAME}-install.log" 2>&1 ||
display_fatal "Couldn't create global overlay bin directory. Check the logs at ${GVM_ROOT}/logs/go-${GO_NAME}-install.log"
}
install_go() {
GO_INSTALL_ROOT=$GVM_ROOT/gos/$GO_NAME
trap 'rm -rf $GO_INSTALL_ROOT; display_fatal "Cancelled!"' INT
# Check for existing install
if [[ -d "$GO_INSTALL_ROOT" ]]; then
if [[ -f "$GO_INSTALL_ROOT/manifest" ]]; then
display_message "Already installed!"
exit 0
fi
display_warning "Removing corrupt install..."
gvm uninstall "$GO_NAME"
fi
if [[ "$version" != "$GO_NAME" ]]; then
display_message "Installing $version as $GO_NAME..."
else
display_message "Installing $version..."
fi
create_global_package_set
copy_source
compile_go
create_enviroment
display_message "$GO_NAME successfully installed!"
}
download_binary() {
mkdir -p $GO_INSTALL_ROOT >> "${GVM_ROOT}/logs/go-${GO_NAME}-download-binary" 2>&1
if [ "$(uname)" == "Darwin" ]; then
GVM_OS="darwin"
osx_major_version="$(sw_vers -productVersion | cut -d "." -f 1)"
osx_minor_version="$(sw_vers -productVersion | cut -d "." -f 2)"
if [ "${osx_major_version}" -le 10 ]; then
if [ "${osx_minor_version}" -ge 8 ]; then
GVM_OS_VERSION="-osx10.8"
elif [ "${osx_minor_version}" -ge 6 ]; then
GVM_OS_VERSION="-osx10.6"
else
display_error "Binary Go unavailable for this platform"
rm -rf $GO_INSTALL_ROOT
rm -f $GO_BINARY_PATH
exit 1
fi
fi
else
GVM_OS="linux"
fi
if [[ "$(uname -m)" == "x86_64" ]]; then
GVM_ARCH="amd64"
elif [[ "$(uname -m)" == "ppc64le" ]]; then
GVM_ARCH="ppc64le"
elif [[ "$(uname -m)" == "aarch64" || "$(uname -m)" == "arm64" ]]; then
GVM_ARCH="arm64"
elif [[ "$(uname -m)" == "i386" || "$(uname -m)" == "i686" ]]; then
GVM_ARCH="i386"
elif [[ "$(uname -m)" == "armv7l" || "$(uname -m)" == "armv6l" ]]; then
GVM_ARCH="armv6l"
else
GVM_ARCH="s390x"
fi
SEMVER=$(extract_version $VERSION)
compare_version $SEMVER "1.4.3"
if [ $? -eq 2 ]; then
GO_BINARY_FILE=${VERSION}.${GVM_OS}-${GVM_ARCH}${GVM_OS_VERSION}.tar.gz
else
GO_BINARY_FILE=${VERSION}.${GVM_OS}-${GVM_ARCH}.tar.gz
fi
# `GO_BINARY_BASE_URL` env allow user setting base URL for binaries
# download, e.g. "https://dl.google.com/go".
GO_BINARY_BASE_URL=${GO_BINARY_BASE_URL:-"https://go.dev/dl"}
GO_BINARY_URL="${GO_BINARY_BASE_URL}/${GO_BINARY_FILE}"
GO_BINARY_PATH=${GVM_ROOT}/archive/${GO_BINARY_FILE}
if [ ! -f $GO_BINARY_PATH ]; then
curl -s -f -L $GO_BINARY_URL > ${GO_BINARY_PATH}
if [[ $? -ne 0 ]]; then
display_error "Failed to download binary go"
rm -rf $GO_INSTALL_ROOT
rm -f $GO_BINARY_PATH
exit 1
fi
fi
tar zxf ${GO_BINARY_PATH} -C $GO_INSTALL_ROOT --strip-components 1 >> "${GVM_ROOT}/logs/go-${GO_NAME}-download-binary" 2>&1
if [[ $? -ne 0 ]]; then
display_error "Failed to extract binary go"
rm -rf $GO_INSTALL_ROOT
rm -f $GO_BINARY_PATH
exit 1
fi
}
install_go_binary() {
GO_INSTALL_ROOT=$GVM_ROOT/gos/$GO_NAME
trap 'rm -rf $GO_INSTALL_ROOT; display_fatal "Cancelled!"' INT
# Check for existing install
if [[ -d "$GO_INSTALL_ROOT" ]]; then
if [[ -f "$GO_INSTALL_ROOT/manifest" ]]; then
display_message "Already installed!"
exit 0
fi
display_warning "Removing corrupt install..."
gvm uninstall "$GO_NAME"
fi
display_message "Installing $GO_NAME from binary source"
create_global_package_set
download_binary
create_enviroment
}
install_gpkg() {
display_message " * Installing gpkg..."
$GVM_GOINSTALL github.com/moovweb/gpkg > "$GVM_ROOT/logs/$GO_NAME-gpkg.log" 2>&1 || return 1
}
install_gb() {
display_message " * Installing gb..."
$GVM_GOINSTALL github.com/jbussdieker/go-gb/gb > "$GVM_ROOT/logs/$GO_NAME-gb.log" 2>&1 || return 1
}
install_goprotobuf() {
command -v protoc &> /dev/null || display_warning "Could not find protocol buffer compiler
linux: apt-get install protobuf-compiler
mac: brew install protobuf
"
display_message " * Installing goprotobuf..."
if [[ "$GVM_GOINSTALL" == "goinstall" ]]; then
$GVM_GOINSTALL goprotobuf.googlecode.com/hg/proto > "$GVM_ROOT/logs/$GO_NAME-pb-compiler.log" 2>&1
if [[ $? -ne 0 ]]; then
display_warning "Failed to install goprotobuf. Check the logs at $GVM_ROOT/logs/$GO_NAME-pb-compiler.log"
return 1
fi
builtin cd "$GVM_ROOT/gos/$GO_NAME/src/pkg/goprotobuf.googlecode.com/hg/compiler"
make install >> "$GVM_ROOT/logs/$GO_NAME-pb-compiler.log" 2>&1
if [[ $? -ne 0 ]]; then
display_warning "Failed to install goprotobuf compiler. Check the logs at $GVM_ROOT/logs/$GO_NAME-pb-compiler.log"
return 1
fi
else
$GVM_GOINSTALL code.google.com/p/goprotobuf/proto > "$GVM_ROOT/logs/$GO_NAME-pb-compiler.log" 2>&1
if [[ $? -ne 0 ]]; then
display_warning "Failed to install goprotobuf. Check the logs at $GVM_ROOT/logs/$GO_NAME-pb-compiler.log"
return 1
fi
$GVM_GOINSTALL code.google.com/p/goprotobuf/protoc-gen-go > "$GVM_ROOT/logs/$GO_NAME-pb-compiler.log" 2>&1
if [[ $? -ne 0 ]]; then
display_warning "Failed to install goprotobuf compiler. Check the logs at $GVM_ROOT/logs/$GO_NAME-pb-compiler.log"
return 1
fi
fi
}
install_from_source() {
download_source
check_tag
if [[ "$?" == "1" ]]; then
update_source
check_tag || display_fatal "Unrecognized Go version"
fi
if [[ "$GO_NAME" == "" ]]; then
GO_NAME=$version
fi
install_go
GVM_GOINSTALL="goinstall"
command -v goinstall &> /dev/null ||
GVM_GOINSTALL="go get"
x="$(builtin cd "$GO_CACHE_PATH" && git tag)"; echo "${x#*b0819469a6df}" | $GREP_PATH "$version " &> /dev/null
if [[ "$?" == "1" ]]; then
if [[ "$INSTALL_BUILD_TOOLS" == "true" ]]; then
install_gb || display_warning "Failed to install gb"
install_gpkg || display_warning "Failed to install gpkg"
fi
if [[ "$INSTALL_PB" == "true" ]]; then
install_goprotobuf
fi
fi
builtin cd "$GO_INSTALL_ROOT" && find . > manifest
}
main() {
trap 'display_fatal "Canceled!"' INT
read_command_line "$@"
[[ "$VERSION" == "" ]] && display_fatal "No version specified"
if [[ "$GO_NAME" == "" ]]; then
GO_NAME=$VERSION
fi
if [[ "x$INSTALL_SOURCE" == "xbinary" ]]; then
install_go_binary
elif [[ "x$INSTALL_SOURCE" == "xprefer-binary" ]]; then
install_go_binary || {
display_message "Falling back to source installation of $GO_NAME"
install_from_source
}
else
install_from_source
fi
builtin cd "$GO_INSTALL_ROOT" && find . > manifest
}
main "$@"