1#!/usr/bin/env bash
2
3set -e -u -o pipefail # Fail on error
4
5dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
6cd $dir
7
8arg=${1:-}
9
10if [[ "$arg" != "ios" && "$arg" != "android" ]]; then
11  echo "Nothing to build, you need to specify 'ios' or 'android'"
12  exit 1
13fi
14
15# For CI, this is run like
16#
17#  env KEYBASE_BUILD=ci DEST_DIR=/tmp ... /path/to/gobuild.sh android|ios
18#
19# so make sure doing so doesn't assume anything about where this file is.
20
21# If KEYBASE_BUILD is set and non-empty (e.g., for CI), use it.
22if [[ -n ${KEYBASE_BUILD+x} && "$KEYBASE_BUILD" ]]; then
23    keybase_build="$KEYBASE_BUILD"
24else
25    ## TODO(mm) consolidate this with packaging/prerelease/
26    current_date=`date -u +%Y%m%d%H%M%S` # UTC
27    commit_short=`git log -1 --pretty=format:%h`
28    keybase_build="$current_date+$commit_short"
29fi
30
31skip_gomobile_init=${SKIP_GOMOBILE_INIT:-}
32check_ci=${CHECK_CI:-}
33
34IFS=: read -a GOPATH_ARRAY <<< "$GOPATH"
35GOPATH0=${GOPATH_ARRAY[0]}
36
37# Original sources
38client_dir="$GOPATH0/src/github.com/keybase/client"
39client_go_dir="$client_dir/go"
40
41echo "Using GOPATH: $GOPATH"
42
43# gomobile looks for gobind in $PATH, so put $GOPATH/bin in $PATH. We
44# also want executables from our own GOPATH to override anything
45# already in $PATH (like the old GOPATH), so put $GOPATH/bin first.
46PATH="$GOPATH/bin:$PATH"
47
48# need to whitelist some flags we use
49export CGO_CFLAGS_ALLOW="-fmodules|-fblocks"
50
51if [ "$check_ci" = "1" ]; then
52  "$client_dir/packaging/goinstall.sh" "github.com/keybase/release"
53  release wait-ci --repo="client" --commit="$(git -C $client_dir rev-parse HEAD)" --context="continuous-integration/jenkins/branch" --context="ci/circleci"
54fi
55
56package="github.com/keybase/client/go/bind"
57tags=${TAGS:-"prerelease production"}
58ldflags="-X github.com/keybase/client/go/libkb.PrereleaseBuild=$keybase_build -s -w"
59
60gomobileinit ()
61{
62  echo "Build gomobile..."
63  mkdir -p "$GOPATH/src/golang.org/x"
64  rsync -pr --ignore-times "$client_dir/go/vendor/golang.org/x" "$GOPATH/src/golang.org"
65  go install golang.org/x/mobile/cmd/{gomobile,gobind}
66  echo "Doing gomobile init"
67  if [ "$arg" = "android" ]; then
68    gomobile init -ndk $ANDROID_HOME/ndk-bundle
69  else
70    gomobile init
71  fi
72}
73
74if [ "$arg" = "ios" ]; then
75  ios_dir=${DEST_DIR:-"$dir/../ios"}
76  ios_dest="$ios_dir/keybase.framework"
77  echo "Building for iOS ($ios_dest)..."
78  set +e
79  OUTPUT="$(gomobile bind -target=ios -tags="ios $tags" -ldflags "$ldflags" -o "$ios_dest" "$package" 2>&1)"
80  set -e
81  if [[ $OUTPUT == *gomobile* ]]; then
82    echo "Running gomobile init cause: $OUTPUT"
83    gomobileinit
84    gomobile bind -target=ios -tags="ios $tags" -ldflags "$ldflags" -o "$ios_dest" "$package"
85  else
86    echo $OUTPUT
87  fi
88elif [ "$arg" = "android" ]; then
89  android_dir=${DEST_DIR:-"$dir/../android/keybaselib"}
90  android_dest="$android_dir/keybaselib.aar"
91  echo "Building for Android ($android_dest)..."
92  set +e
93  OUTPUT="$(gomobile bind -target=android -tags="android $tags" -ldflags "$ldflags" -o "$android_dest" "$package" 2>&1)"
94  set -e
95  if [[ $OUTPUT == *gomobile* ]]; then
96    echo "Running gomobile init cause: $OUTPUT"
97    gomobileinit
98    gomobile bind -target=android -tags="android $tags" -ldflags "$ldflags" -o "$android_dest" "$package"
99  else
100    echo $OUTPUT
101  fi
102else
103  # Shouldn't get here.
104  echo "Nothing to build, you need to specify 'ios' or 'android'"
105  exit 1
106fi
107