-
Notifications
You must be signed in to change notification settings - Fork 511
/
compile
executable file
·170 lines (147 loc) · 4.25 KB
/
compile
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
#!/bin/bash
# usage: bin/compile <build-dir> <cache-dir> <env-dir>
set -eo pipefail
# Go releases for Darwin beginning with 1.2rc1
# have included more than one build, depending
# on the specific version of Mac OS X. Try to
# account for that, but don't try too hard.
# This doesn't affect Heroku builds, it's only
# for testing on Darwin systems.
platext() {
case $1 in
go1.0*|go1.1beta*|go1.1rc*|go1.1|go1.1.*) return ;;
esac
case $(uname|tr A-Z a-z) in
darwin) printf %s -osx10.8 ;;
esac
}
# Go releases have moved to a new URL scheme
# starting with Go version 1.2.2. Return the old
# location for known old versions and the new
# location otherwise.
urlfor() {
ver=$1
file=$2
case $ver in
go1.0*|go1.1beta*|go1.1rc*|go1.1|go1.1.*|go1.2beta*|go1.2rc*|go1.2|go1.2.1)
echo http://go.googlecode.com/files/$file
;;
*)
echo https://storage.googleapis.com/golang/$file
;;
esac
}
mkdir -p "$1" "$2"
build=$(cd "$1/" && pwd)
cache=$(cd "$2/" && pwd)
buildpack=$(cd "$(dirname $0)/.." && pwd)
arch=$(uname -m|tr A-Z a-z)
if test $arch = x86_64
then arch=amd64
fi
plat=$(uname|tr A-Z a-z)-$arch
# Python
venv=$cache/venv
mkdir -p $cache/pip
python=python2.7
PATH=$buildpack/$plat/bin:$venv/bin:$PATH
virtualenv() {
python "$buildpack/vendor/virtualenv-1.11.6/virtualenv.py" "$@"
}
if test -f $build/Godeps
then
name=$(<$build/Godeps jq -r .ImportPath)
ver=$(<$build/Godeps jq -r .GoVersion)
elif test -d $build/Godeps
then
name=$(<$build/Godeps/Godeps.json jq -r .ImportPath)
ver=$(<$build/Godeps/Godeps.json jq -r .GoVersion)
elif test -f $build/.godir
then
name=$(cat $build/.godir)
ver=go${GOVERSION:-1.4.2}
else
echo >&2 " ! Godeps are required. For instructions:"
echo >&2 " ! https://medium.com/@freeformz/hello-world-with-go-heroku-38295332f07b"
exit 1
fi
file=${GOFILE:-$ver.$(uname|tr A-Z a-z)-amd64$(platext $ver).tar.gz}
url=${GOURL:-$(urlfor $ver $file)}
if test -e $build/bin && ! test -d $build/bin
then
echo >&2 " ! File bin exists and is not a directory."
exit 1
fi
if test -d $cache/$ver/go
then
echo "-----> Using $ver"
else
rm -rf $cache/* # be sure not to build up cruft
mkdir -p $cache/$ver
cd $cache/$ver
echo -n "-----> Installing $ver..."
curl -sO $url
tar zxf $file
rm -f $file
echo " done"
cd - >/dev/null
fi
mkdir -p $build/bin
GOBIN=$build/bin export GOBIN
GOROOT=$cache/$ver/go export GOROOT
GOPATH=$build/.heroku/g export GOPATH
PATH=$GOROOT/bin:$PATH
if ! (test -d $build/Godeps || (which hg >/dev/null && which bzr >/dev/null))
then
echo
echo " Tired of waiting for bzr and hg?"
echo " Try github.com/kr/godep for faster deploys."
echo
echo -n " Installing Virtualenv..."
virtualenv --python $python --distribute --never-download --prompt='(venv) ' $venv > /dev/null 2>&1
. $venv/bin/activate > /dev/null 2>&1
echo " done"
echo -n " Installing Mercurial..."
pip install mercurial > /dev/null 2>&1
echo " done"
echo -n " Installing Bazaar..."
pip install bzr > /dev/null 2>&1
echo " done"
fi
p=$GOPATH/src/$name
mkdir -p $p
cp -R $build/* $p
# Default to $SOURCE_VERSION environment variable
GO_LINKER_VALUE=${SOURCE_VERSION}
# allow apps to specify cgo flags and set up /app symlink so things like CGO_CFLAGS=-I/app/... work
env_dir="$3"
if [ -d "$env_dir" ]
then
ln -sfn $build /app/code
for key in CGO_CFLAGS CGO_CPPFLAGS CGO_CXXFLAGS CGO_LDFLAGS GO_LINKER_SYMBOL GO_LINKER_VALUE
do
if [ -f "$env_dir/$key" ]
then
export "$key=$(cat "$env_dir/$key")"
fi
done
fi
# If $GO_LINKER_SYMBOL and GO_LINKER_VALUE are set, tell the linker to DTRT
FLAGS=(-tags heroku)
if [ -n "$GO_LINKER_SYMBOL" -a -n "$GO_LINKER_VALUE" ]
then
FLAGS=(${FLAGS[@]} -ldflags "-X $GO_LINKER_SYMBOL $GO_LINKER_VALUE")
fi
unset GIT_DIR # unset git dir or it will mess with goinstall
cd $p
if test -e $build/Godeps
then
echo "-----> Running: godep go install ${FLAGS[@]} ./..."
godep go install "${FLAGS[@]}" ./...
else
echo "-----> Running: go get ${FLAGS[@]} ./..."
go get "${FLAGS[@]}" ./...
fi
rm -rf $build/.heroku
mkdir -p $build/.profile.d
echo 'PATH=$PATH:$HOME/bin' > $build/.profile.d/go.sh