summaryrefslogtreecommitdiff
path: root/go/android_build_golib.sh
blob: a54a42afd352c6a37cc773752b84c71da5504edb (plain)
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
#!/bin/bash

# Copyright (C) 2016 Andrew Jiang (TunnelBear Inc.)
# Convenience script for generating shapeshifter-dispatcher binaries for Android devices
# adapted for Bitmask by cyberta

BUILD_LIBRARY="false"
BUILD_SHAPESHIFTER_DISPATCHER="false"
BUILD_SNOWFLAKE="false"

function quit {
    echo "$1."
    exit 1
}

if [[ "$1" == "removeAll" ]]; then
        echo "removing golang, sources and generated files"
        for folder in /tmp/android-toolchain-*; do
                if [[ -d $folder ]]; then
                        rm -rf $folder
                fi
        done
        if [[ -d "./out" ]]; then
                rm -rf ./out
        fi

        if [[ -d "./bin" ]]; then
                rm -rf ./bin
        fi

        if [[ -d "./golang" ]]; then
                rm -rf ./golang
        fi

        if [[ -d "./src" ]]; then
                rm -rf ./src
        fi
        echo "Done!"
        exit 0

elif [[ "$1" == "clean" ]]; then
        echo "Cleaning up..."
        for folder in /tmp/android-toolchain-*; do
                if [[ -d $folder ]]; then
                        rm -rf $folder
                fi
        done
        if [[ -d "./out" ]]; then
                rm -rf ./out
        fi

        if [[ -d "./bin" ]]; then
                rm -rf ./bin
        fi
        echo "Done!"
        exit 0

elif [[ "$1" == "shapeshifterlib" ]]; then
        BUILD_LIBRARY="true"
elif [[ "$1" == "shapeshifter-dispatcher" ]]; then
        BUILD_SHAPESHIFTER_DISPATCHER="true"
elif [[ "$1" == "snowflake-client" ]]; then 
        BUILD_SNOWFLAKE="true"
        echo "BUILD_SNOWFLAKE!"
fi


if [ -z $ANDROID_NDK_HOME ]; then
        echo "Android NDK path not specified!"
        echo "Please set \$ANDROID_NDK_HOME before starting this script!"
        exit 1;
fi

if [[ ! -f ./bin/gomobile && $BUILD_LIBRARY == true ]]; then
        echo "gomobile not installed"
        echo please run "install_go.sh first"
        exit 1
fi

# Our targets are x86, x86_64, armeabi, armeabi-v7a, armv8;
# To remove targets, simply delete them from the bracket.
# NOTE: We are only currently shipping the armeabi-v7a binary
# on Android, for space reasons.
targets=(386 x86_64 armv7 arm64)
export GOOS=android

for arch in ${targets[@]}; do
        # Initialize variables
        go_arch=$arch
        ndk_arch=$arch
        ndk_platform="android-16"
        suffix=$arch

        if [ "$arch" = "386" ]; then
                export CGO_ENABLED=1
                ndk_arch="x86"
                suffix="x86"
                binary="i686-linux-android-gcc"
        elif [ "$arch" = "x86_64" ]; then
                ndk_platform="android-21"
                ndk_arch="x86_64"
                suffix="x86_64"
                binary="x86_64-linux-android-gcc"
        elif [ "$arch" = "armv5" ]; then
                export GOARM=5
                export CGO_ENABLED=1
                go_arch="arm"
                ndk_arch="arm"
                suffix="armeabi"
                binary="arm-linux-androideabi-gcc"
        elif [ "$arch" = "armv7" ]; then
                export GOARM=7
                export CGO_ENABLED=1
                go_arch="arm"
                ndk_arch="arm"
                suffix="armeabi-v7a"
                binary="arm-linux-androideabi-gcc"
        elif [ "$arch" = "arm64" ]; then
                suffix="arm64-v8a"
                ndk_platform="android-21"
                binary="aarch64-linux-android-gcc"
        elif [ "$arch" = "mips" ]; then
                binary="mipsel-linux-android-gcc"
        fi
        export GOARCH=${go_arch}
        export GOPATH=`pwd`
        export NDK_TOOLCHAIN=/tmp/android-toolchain-${ndk_arch}

        # Only generate toolchain if it does not already exist
        if [ ! -d $NDK_TOOLCHAIN ]; then
                echo "Generating ${suffix} toolchain..."
                $ANDROID_NDK_HOME/build/tools/make-standalone-toolchain.sh \
                --arch=${ndk_arch} --platform=${ndk_platform} --install-dir=$NDK_TOOLCHAIN || quit "Failed to generate toolchain"
                echo "Toolchain generated!"
        fi

        export CC=$NDK_TOOLCHAIN/bin/clang
        echo "Starting compilation for $suffix..."


       # if [[ BUILD_SNOWFLAKE ==  "true" ]]; then 
                echo "cross compiling snowflake-client executable..."
                pwd
                ./golang/go/bin/go get github.com/cyBerta/snowflake/client
                ./golang/go/bin/go build -buildmode=pie -ldflags '-w -s -extldflags=-pie' -o ./out/${suffix}/piesnowflakeclient github.com/cyBerta/snowflake/client || quit "Failed to cross-compile shapeshifter-dispatcher"
       # elif [[ BUILD_LIBRARY == "true" ]]; then
       #         echo "cross compiling shapeshifter lib..."
       #         ./bin/gomobile bind -target=android -o ./lib/shapeshifter.aar se.leap.bitmaskclient/shapeshifter/
       #         cp lib/shapeshifter* ../shapeshifter/.
       #         #./android_build_shapeshifter_lib.sh || quit "Failed to cross-compile shapeshifter-dispatcher-library"
       # elif [[ BUILD_SHAPESHIFTER_DISPATCHER == "true" ]]; then
       #         echo "cross compiling shapeshifter-dispatcher executable..."
       #         ./golang/go/bin/go build -buildmode=pie -ldflags '-w -s -extldflags=-pie' -o ./out/${suffix}/piedispatcher github.com/OperatorFoundation/shapeshifter-dispatcher/shapeshifter-dispatcher || quit "Failed to cross-compile shapeshifter-dispatcher"
       # fi
        echo "Build succeeded!"

done