|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
| 3 | +# We want to build multiple go binaries, so we use a custom build step on CI. |
| 4 | +cd $(dirname "${BASH_SOURCE[0]}")/../.. |
3 | 5 | set -e |
4 | 6 |
|
5 | | -SYMBOLS_IMAGE="${SYMBOLS_IMAGE:=dev-symbols}" |
6 | | -CTAGS_IMAGE="${CTAGS_IMAGE:=ctags}" |
7 | | -BUILD_TYPE="${BUILD_TYPE:=dev}" |
8 | | - |
9 | | -repositoryRoot="$PWD" |
10 | | -symbolsExecutablePath="$repositoryRoot/.bin/symbols" |
11 | | -case "$OSTYPE" in |
12 | | - darwin*) |
13 | | - libsqlite3PcrePath="$repositoryRoot/libsqlite3-pcre.dylib" |
14 | | - ;; |
15 | | - linux*) |
16 | | - libsqlite3PcrePath="$repositoryRoot/libsqlite3-pcre.so" |
17 | | - ;; |
18 | | - *) |
19 | | - echo "Unknown platform $OSTYPE" |
20 | | - exit 1 |
21 | | - ;; |
22 | | -esac |
23 | | - |
24 | | -# Builds the PCRE extension to sqlite3. |
25 | | -function buildLibsqlite3Pcre() { |
26 | | - if ! command -v pkg-config >/dev/null 2>&1 || ! command -v pkg-config --cflags sqlite3 libpcre >/dev/null 2>&1; then |
27 | | - echo "Missing sqlite dependencies." |
28 | | - case "$OSTYPE" in |
29 | | - darwin*) |
30 | | - echo "Install them by running `brew install pkg-config sqlite pcre FiloSottile/musl-cross/musl-cross`" |
31 | | - ;; |
32 | | - linux*) |
33 | | - echo "Install them by running `apt-get install libpcre3-dev libsqlite3-dev pkg-config musl-tools`" |
34 | | - ;; |
35 | | - *) |
36 | | - echo "See the local development documentation: https://github.com/sourcegraph/sourcegraph/blob/master/doc/dev/local_development.md#step-2-install-dependencies" |
37 | | - ;; |
38 | | - esac |
39 | | - exit 1 |
40 | | - fi |
41 | | - |
42 | | - |
43 | | - if [ -f "$libsqlite3PcrePath" ]; then |
44 | | - return |
45 | | - fi |
46 | | - |
47 | | - sqlite3PcreRepositoryDirectory="$(mktemp -d)" |
48 | | - trap "rm -rf $sqlite3PcreRepositoryDirectory" EXIT |
49 | | - |
50 | | - echo "Building $libsqlite3PcrePath..." |
51 | | - curl -fsSL https://codeload.github.com/ralight/sqlite3-pcre/tar.gz/c98da412b431edb4db22d3245c99e6c198d49f7a | tar -C "$sqlite3PcreRepositoryDirectory" -xzvf - --strip 1 |
52 | | - pushd "$sqlite3PcreRepositoryDirectory" |
53 | | - case "$OSTYPE" in |
54 | | - darwin*) |
55 | | - # pkg-config spits out multiple arguments and must not be quoted. |
56 | | - gcc -fno-common -dynamiclib pcre.c -o "$libsqlite3PcrePath" $(pkg-config --cflags sqlite3 libpcre) $(pkg-config --libs libpcre) -fPIC |
57 | | - ;; |
58 | | - linux*) |
59 | | - # pkg-config spits out multiple arguments and must not be quoted. |
60 | | - gcc -shared -o "$libsqlite3PcrePath" $(pkg-config --cflags sqlite3 libpcre) -fPIC -W -Werror pcre.c $(pkg-config --libs libpcre) -Wl,-z,defs |
61 | | - ;; |
62 | | - *) |
63 | | - echo "Unknown platform $OSTYPE" |
64 | | - exit 1 |
65 | | - ;; |
66 | | - esac |
67 | | - popd |
68 | | - echo "Building $libsqlite3PcrePath... done" |
| 7 | +OUTPUT=`mktemp -d -t sgdockerbuild_XXXXXXX` |
| 8 | +cleanup() { |
| 9 | + rm -rf "$OUTPUT" |
69 | 10 | } |
| 11 | +trap cleanup EXIT |
70 | 12 |
|
71 | | -# Builds the symbols executable. |
72 | | -function buildExecutable() { |
73 | | - symbolsPackage="github.com/sourcegraph/sourcegraph/cmd/symbols" |
74 | | - |
75 | | - case "$BUILD_TYPE" in |
76 | | - dev) |
77 | | - gcFlags="all=-N -l" |
78 | | - tags="dev delve" |
79 | | - ;; |
80 | | - dist) |
81 | | - gcFlags="" |
82 | | - tags="dist" |
83 | | - ;; |
84 | | - esac |
85 | | - |
86 | | - echo "Building the $symbolsExecutablePath executable..." |
87 | | - go build -buildmode exe -gcflags "$gcFlags" -tags "$tags" -o "$symbolsExecutablePath" "$symbolsPackage" |
88 | | - echo "Building the $symbolsExecutablePath executable... done" |
89 | | -} |
90 | | - |
91 | | -# Builds and runs the symbols executable. |
92 | | -function execute() { |
93 | | - buildLibsqlite3Pcre |
94 | | - buildExecutable |
95 | | - buildCtagsDockerImage |
96 | | - export LIBSQLITE3_PCRE="$libsqlite3PcrePath" |
97 | | - export CTAGS_COMMAND="${CTAGS_COMMAND:=cmd/symbols/universal-ctags-dev}" |
98 | | - export CTAGS_PROCESSES="${CTAGS_PROCESSES:=1}" |
99 | | - "$symbolsExecutablePath" |
100 | | -} |
| 13 | +# Environment for building linux binaries |
| 14 | +export GO111MODULE=on |
| 15 | +export GOARCH=amd64 |
| 16 | +export GOOS=linux |
| 17 | +export CGO_ENABLED=0 |
101 | 18 |
|
102 | | -# Builds the symbols Docker image. |
103 | | -function buildSymbolsDockerImage() { |
104 | | - case "$OSTYPE" in |
105 | | - darwin*) |
106 | | - muslGcc="x86_64-linux-musl-gcc" |
107 | | - if ! command -v "$muslGcc" >/dev/null 2>&1; then |
108 | | - echo "Couldn't find musl C compiler $muslGcc. Run `brew install FiloSottile/musl-cross/musl-cross`." |
109 | | - exit 1 |
110 | | - fi |
111 | | - ;; |
112 | | - linux*) |
113 | | - muslGcc="musl-gcc" |
114 | | - if ! command -v "$muslGcc" >/dev/null 2>&1; then |
115 | | - echo "Couldn't find musl C compiler $muslGcc. Install the musl-tools package (e.g. on Ubuntu, run `apt-get install musl-tools`)." |
116 | | - exit 1 |
117 | | - fi |
118 | | - ;; |
119 | | - *) |
120 | | - echo "Unknown platform $OSTYPE" |
121 | | - exit 1 |
122 | | - ;; |
123 | | - esac |
| 19 | +echo "Compiling the symbols service..." |
| 20 | +for pkg in github.com/sourcegraph/sourcegraph/cmd/symbols; do |
| 21 | + go build -ldflags "-X github.com/sourcegraph/sourcegraph/pkg/version.version=$VERSION" -buildmode exe -tags dist -o $OUTPUT/$(basename $pkg) $pkg |
| 22 | +done |
124 | 23 |
|
125 | | - symbolsDockerBuildContext="$(mktemp -d)" |
126 | | - trap "rm -rf $symbolsDockerBuildContext" EXIT |
| 24 | +mkdir "$OUTPUT/.ctags.d" |
| 25 | +cp cmd/symbols/.ctags.d/additional-languages.ctags "$OUTPUT/.ctags.d/additional-languages.ctags" |
127 | 26 |
|
128 | | - export CC="$muslGcc" |
129 | | - export GO111MODULE=on |
130 | | - export GOARCH=amd64 |
131 | | - export GOOS=linux |
132 | | - export CGO_ENABLED=1 # to build the sqlite3 library |
133 | | - symbolsExecutablePath="$symbolsDockerBuildContext/symbols" |
134 | | - buildExecutable |
135 | | - |
136 | | - buildCtagsDockerImage |
137 | | - |
138 | | - cp -R cmd/symbols/.ctags.d "$symbolsDockerBuildContext" |
139 | | - |
140 | | - echo "Building the $SYMBOLS_IMAGE Docker image..." |
141 | | - docker build --quiet -f cmd/symbols/Dockerfile -t "$SYMBOLS_IMAGE" "$symbolsDockerBuildContext" |
142 | | - echo "Building the $SYMBOLS_IMAGE Docker image... done" |
143 | | -} |
144 | | - |
145 | | -# Builds the ctags docker image, used by universal-ctags-dev and the symbols Docker image. |
146 | | -function buildCtagsDockerImage() { |
147 | | - ctagsDockerBuildContext="$(mktemp -d)" |
148 | | - trap "rm -rf $ctagsDockerBuildContext" EXIT |
149 | | - |
150 | | - cp -R cmd/symbols/.ctags.d "$ctagsDockerBuildContext" |
151 | | - |
152 | | - echo "Building the $CTAGS_IMAGE Docker image..." |
153 | | - docker build --quiet -f cmd/symbols/internal/pkg/ctags/Dockerfile -t "$CTAGS_IMAGE" "$ctagsDockerBuildContext" |
154 | | - echo "Building the $CTAGS_IMAGE Docker image... done" |
155 | | -} |
156 | | - |
157 | | -command="$1" |
158 | | -if type -t "$command" >/dev/null 2>&1; then |
159 | | - "$command" |
160 | | -else |
161 | | - echo "Unknown command: $command (must be: execute, buildLibsqlite3Pcre, or buildSymbolsDockerImage)" |
| 27 | +if [ -z "$IMAGE" ]; then |
| 28 | + echo "You have to set \$IMAGE, the tag for the symbols image." |
| 29 | + exit 1 |
162 | 30 | fi |
| 31 | + |
| 32 | +echo "Building symbols image $IMAGE..." |
| 33 | +docker build --quiet -f cmd/symbols/Dockerfile -t $IMAGE $OUTPUT |
0 commit comments