Added compatibility define
[platform/upstream/freerdp.git] / scripts / android-build-common.sh
1 #!/bin/bash
2
3 SCRIPT_PATH=$(dirname "${BASH_SOURCE[0]}")
4 SCRIPT_PATH=$(realpath "$SCRIPT_PATH")
5
6 if [ -z $BUILD_ARCH ]; then
7         BUILD_ARCH="armeabi-v7a x86 x86_64 arm64-v8a"
8 fi
9
10 if [ -z $NDK_TARGET ]; then
11         NDK_TARGET=21
12 fi
13
14 if [ -z $CMAKE_PROGRAM ]; then
15         CMAKE_PROGRAM=$(find $ANDROID_SDK/cmake -name cmake -type f -executable -print -quit)
16 fi
17
18 if [ -z $CCACHE ]; then
19         CCACHE=$(which ccache)
20 fi
21
22 if [ -z $ANDROID_NDK ]; then
23         ANDROID_NDK="missing"
24 fi
25
26 if [ -z $ANDROID_SDK ]; then
27         ANDROID_SDK="missing"
28 fi
29
30 if [ -z $BUILD_DST ]; then
31         BUILD_DST=$(pwd)/libs
32 fi
33
34 if [ -z $BUILD_SRC ]; then
35         BUILD_SRC=$(pwd)/src
36 fi
37
38 if [ -z $SCM_URL ]; then
39         SCM_URL="missing"
40 fi
41
42 if [ -z $SCM_TAG ]; then
43         SCM_TAG=master
44 fi
45
46 CLEAN_BUILD_DIR=0
47
48 function common_help {
49         echo "$(BASHSOURCE[0]) supports the following arguments:"
50         echo "  --ndk   The base directory of your android NDK defa"
51         echo "                  ANDROID_NDK=$ANDROID_NDK"
52         echo "  --sdk   The base directory of your android SDK defa"
53         echo "                  ANDROID_SDK=$ANDROID_SDK"
54         echo "  --arch  A list of architectures to build"
55         echo "                  BUILD_ARCH=$BUILD_ARCH"
56         echo "  --dst   The destination directory for include and library files"
57         echo "                  BUILD_DST=$BUILD_DST"
58         echo "  --src   The source directory for SCM checkout"
59         echo "                  BUILD_SRC=$BUILD_SRC"
60         echo "  --url   The SCM source url"
61         echo "                  SCM_URL=$SCM_URL"
62         echo "  --tag   The SCM branch or tag to check out"
63         echo "                  SCM_TAG=$SCM_TAG"
64         echo "  --clean Clean the destination before build"
65         echo "  --help  Display this help"
66         exit 0
67 }
68
69 function common_run {
70         echo "[RUN] $@"
71         "$@"
72         RES=$?
73         if [[ $RES -ne 0 ]];
74         then
75                 echo "[ERROR] $@ retured $RES"
76                 exit 1
77         fi
78 }
79
80 function common_parse_arguments {
81         while [[ $# > 0 ]]
82         do
83                 key="$1"
84                 case $key in
85                     --conf)
86             source "$2"
87             shift
88             ;;
89
90                         --target)
91                         NDK_TARGET="$2"
92                         shift
93                         ;;
94
95                         --ndk)
96                         ANDROID_NDK="$2"
97                         shift
98                         ;;
99
100                         --sdk)
101                         ANDROID_SDK="$2"
102                 CMAKE_PROGRAM=$(find $ANDROID_SDK/cmake -name cmake -type f -executable -print -quit)
103                         shift
104                         ;;
105
106                         --arch)
107                         BUILD_ARCH="$2"
108                         shift
109                         ;;
110
111                         --dst)
112                         BUILD_DST="$2"
113                         shift
114                         ;;
115
116                         --src)
117                         BUILD_SRC="$2"
118                         shift
119                         ;;
120
121                         --url)
122                         SCM_URL="$2"
123                         shift
124                         ;;
125
126                         --tag)
127                         SCM_TAG="$2"
128                         shift
129                         ;;
130
131                         --clean)
132                         CLEAN_BUILD_DIR=1
133                         shift
134                         ;;
135
136                         --help)
137                         common_help
138                         shift
139                         ;;
140
141                         *) # Unknown
142                         ;;
143                 esac
144                 shift
145         done
146 }
147
148 function common_check_requirements {
149         if [[ ! -d $ANDROID_NDK ]];
150         then
151                 echo "export ANDROID_NDK to point to your NDK location."
152                 exit 1
153         fi
154
155         if [[ ! -d $ANDROID_SDK ]];
156         then
157                 echo "export ANDROID_SDK to point to your SDK location."
158                 exit 1
159         fi
160         if [[ -z $BUILD_DST ]];
161         then
162                 echo "Destination directory not valid"
163                 exit 1
164         fi
165
166         if [[ -z $BUILD_SRC ]];
167         then
168                 echo "Source directory not valid"
169                 exit 1
170         fi
171
172         if [[ -z $SCM_URL ]];
173         then
174                 echo "Source URL not defined! Define SCM_URL"
175                 exit 1
176         fi
177
178         if [[ -z $SCM_TAG ]];
179         then
180                 echo "SCM TAG / BRANCH not defined! Define SCM_TAG"
181                 exit 1
182         fi
183
184         if [[ -z $NDK_TARGET ]];
185         then
186                 echo "Android platform NDK_TARGET not defined"
187                 exit 1
188         fi
189
190         if [ -x $ANDROID_NDK/ndk-build ]; then
191                 NDK_BUILD=$ANDROID_NDK/ndk-build
192         else
193                 echo "ndk-build not found in NDK directory $ANDROID_NDK"
194                 echo "assuming ndk-build is in path..."
195                 NDK_BUILD=ndk-build
196         fi
197
198     if [ -z $CMAKE_PROGRAM ]; then
199         CMAKE_PROGRAM=$(find $ANDROID_SDK/cmake -name cmake -type f -executable -print -quit)
200     fi
201
202         for CMD in make git $CMAKE_PROGRAM $NDK_BUILD
203         do
204                 if ! type $CMD >/dev/null; then
205                         echo "Command $CMD not found. Install and add it to the PATH."
206                         exit 1
207                 fi
208         done
209
210         if [ "${BUILD_SRC:0:1}" != "/" ];
211         then
212                 BUILD_SRC=$(pwd)/$BUILD_SRC
213         fi
214         if [ "${BUILD_DST:0:1}" != "/" ];
215         then
216                 BUILD_DST=$(pwd)/$BUILD_DST
217         fi
218 }
219
220 function common_update {
221         if [ $# -ne 3 ];
222         then
223                 echo "Invalid arguments to update function $@"
224                 exit 1
225         fi
226         SCM_URL=$1
227         SCM_TAG=$2
228         BUILD_SRC=$3
229
230         echo "Preparing checkout..."
231         BASE=$(pwd)
232         CACHE=$SCRIPT_PATH/../cache
233         common_run mkdir -p $CACHE
234         TARFILE="$CACHE/$SCM_TAG.tar.gz"
235         
236         
237         if [[ ! -f "$TARFILE" ]];
238         then
239                 common_run wget -O "$TARFILE" "$SCM_URL/archive/$SCM_TAG.tar.gz"
240         fi
241
242         if [[ -d $BUILD_SRC ]];
243         then
244                 common_run rm -rf $BUILD_SRC
245         fi
246         common_run mkdir -p $BUILD_SRC
247         common_run cd $BUILD_SRC
248         common_run tar zxf "$TARFILE" --strip 1
249         common_run cd $BASE
250
251 }
252
253 function common_clean {
254         if [ $CLEAN_BUILD_DIR -ne 1 ];
255         then
256                 return
257         fi
258
259         if [ $# -ne 1 ];
260         then
261                 echo "Invalid arguments to clean function $@"
262                 exit 1
263         fi
264
265         echo "Cleaning up $1..."
266         common_run rm -rf $1
267 }
268
269 function common_copy {
270         if [ $# -ne 2 ];
271         then
272                 echo "Invalid arguments to copy function $@"
273                 exit 1
274         fi
275         if [ ! -d $1 ] || [ ! -d $1/include ] || [ ! -d $1/libs ];
276         then
277                 echo "Invalid source $1"
278                 exit 1
279         fi
280         if [ -z $2 ];
281         then
282                 echo "Invalid destination $2"
283                 exit 1
284         fi
285
286         if [ ! -d $2 ];
287         then
288                 common_run mkdir -p $2
289         fi
290         common_run cp -L -r $1/include $2
291         common_run cp -L -r $1/libs/* $2
292 }