compile.sh 11.9 KB
Newer Older
Rémi Duraffort's avatar
Rémi Duraffort committed
1 2
#! /bin/sh

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
3 4
# Read the Android Wiki http://wiki.videolan.org/AndroidCompile
# Setup all that stuff correctly.
5
# Get the latest Android SDK Platform or modify numbers in configure.sh and libvlc/default.properties.
Rafaël Carré's avatar
Rafaël Carré committed
6

7 8
set -e

9
if [ -z "$ANDROID_NDK" -o -z "$ANDROID_SDK" ]; then
10 11
   echo "You must define ANDROID_NDK, ANDROID_SDK and ANDROID_ABI before starting."
   echo "They must point to your NDK and SDK directories.\n"
Rafaël Carré's avatar
Rafaël Carré committed
12 13
   exit 1
fi
14 15

if [ -z "$ANDROID_ABI" ]; then
Thomas Guillem's avatar
Thomas Guillem committed
16
   echo "Please set ANDROID_ABI to your architecture: armeabi-v7a, armeabi, arm64-v8a, x86, x86_64 or mips."
17 18 19
   exit 1
fi

20 21 22 23 24 25 26
if [ -z "$NO_FPU" ];then
    NO_FPU=0
fi
if [ -z "$NO_ARMV6" ];then
    NO_ARMV6=0
fi

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
BUILD=0
FETCH=0
RELEASE=0
JNI=0

for i in ${@}; do
    case "$i" in
        --fetch)
        FETCH=1
        ;;
        --build)
        BUILD=1
        ;;
        release|--release)
        RELEASE=1
        ;;
        jni|--jni)
        JNI=1
        ;;
        *)
        ;;
    esac
done

if [ "$BUILD" = 0 -a "$FETCH" = 0 ];then
    BUILD=1
    FETCH=1
fi

56 57 58 59 60 61 62 63 64
if [ `set -- ${ANDROID_ABI}; echo $#` -gt 1 ]; then
    ANDROID_ABI_LIST="${ANDROID_ABI}"
    echo "More than one ABI specified: ${ANDROID_ABI_LIST}"
    for i in ${ANDROID_ABI_LIST}; do
        echo "$i starts building"
        ANDROID_NDK=$ANDROID_NDK ANDROID_SDK=$ANDROID_SDK \
            NO_FPU=$NO_FPU NO_ARMV6=$NO_ARMV6 ANDROID_ABI=$i \
            ./compile.sh $* --jni || { echo "$i build KO"; exit 1; }
        mkdir -p obj/
65
        cp -r libvlc/libs/$i obj
66 67 68
        echo "$i build OK"
    done
    for i in ${ANDROID_ABI_LIST}; do
69
        cp -r obj/$i libvlc/libs/
70 71
        rm -rf obj/$i
    done
72 73 74
    if [ "$JNI" = 0 ];then
      make -b -j1 RELEASE=$RELEASE apk || exit 1
    fi
75 76 77
    exit 0
fi

78 79 80 81 82
HAVE_ARM=0
HAVE_X86=0
HAVE_MIPS=0
HAVE_64=0

Edward Wang's avatar
Edward Wang committed
83 84
# Set up ABI variables
if [ ${ANDROID_ABI} = "x86" ] ; then
85
    TARGET_TUPLE="i686-linux-android"
Edward Wang's avatar
Edward Wang committed
86 87 88
    PATH_HOST="x86"
    HAVE_X86=1
    PLATFORM_SHORT_ARCH="x86"
Thomas Guillem's avatar
Thomas Guillem committed
89 90 91 92 93 94
elif [ ${ANDROID_ABI} = "x86_64" ] ; then
    TARGET_TUPLE="x86_64-linux-android"
    PATH_HOST="x86_64"
    HAVE_X86=1
    HAVE_64=1
    PLATFORM_SHORT_ARCH="x86_64"
Edward Wang's avatar
Edward Wang committed
95 96 97 98 99
elif [ ${ANDROID_ABI} = "mips" ] ; then
    TARGET_TUPLE="mipsel-linux-android"
    PATH_HOST=$TARGET_TUPLE
    HAVE_MIPS=1
    PLATFORM_SHORT_ARCH="mips"
Thomas Guillem's avatar
Thomas Guillem committed
100 101 102 103 104 105
elif [ ${ANDROID_ABI} = "arm64-v8a" ] ; then
    TARGET_TUPLE="aarch64-linux-android"
    PATH_HOST=$TARGET_TUPLE
    HAVE_ARM=1
    HAVE_64=1
    PLATFORM_SHORT_ARCH="arm64"
Edward Wang's avatar
Edward Wang committed
106 107 108 109 110 111
else
    TARGET_TUPLE="arm-linux-androideabi"
    PATH_HOST=$TARGET_TUPLE
    HAVE_ARM=1
    PLATFORM_SHORT_ARCH="arm"
fi
112

113 114 115 116 117 118
# try to detect NDK version
REL=$(grep -o '^r[0-9]*.*' $ANDROID_NDK/RELEASE.TXT 2>/dev/null|cut -b2-)
case "$REL" in
    10*)
        if [ "${HAVE_64}" = 1 ];then
            GCCVER=4.9
119
            ANDROID_API=android-21
120 121 122 123 124 125
        else
            GCCVER=4.8
            ANDROID_API=android-9
        fi
        CXXSTL="/"${GCCVER}
    ;;
126
    *)
127
        echo "You need the NDKv10 or later"
128 129 130 131 132 133 134 135
        exit 1
    ;;
esac

export GCCVER
export CXXSTL
export ANDROID_API

136 137 138 139 140 141 142 143 144 145
# XXX : important!
[ "$HAVE_ARM" = 1 ] && cat << EOF
For an ARMv6 device without FPU:
$ export NO_FPU=1
For an ARMv5 device:
$ export NO_ARMV6=1

If you plan to use a release build, run 'compile.sh release'
EOF

Edward Wang's avatar
Edward Wang committed
146 147 148 149 150
export TARGET_TUPLE
export PATH_HOST
export HAVE_ARM
export HAVE_X86
export HAVE_MIPS
151
export HAVE_64
Edward Wang's avatar
Edward Wang committed
152 153
export PLATFORM_SHORT_ARCH

154 155
# Add the NDK toolchain to the PATH, needed both for contribs and for building
# stub libraries
156
NDK_TOOLCHAIN_PATH=`echo ${ANDROID_NDK}/toolchains/${PATH_HOST}-${GCCVER}/prebuilt/\`uname|tr A-Z a-z\`-*/bin`
157
export PATH=${NDK_TOOLCHAIN_PATH}:${PATH}
158

159 160
ANDROID_PATH="`pwd`"

161
# Fetch VLC source
162
if [ "$FETCH" = 1 ]
163 164
then
    # 1/ libvlc, libvlccore and its plugins
Thomas Guillem's avatar
Thomas Guillem committed
165
    TESTED_HASH=18e445a
166 167
    if [ ! -d "vlc" ]; then
        echo "VLC source not found, cloning"
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
168
        git clone git://git.videolan.org/vlc.git vlc
169 170 171 172 173 174 175
        cd vlc
        git checkout -B android ${TESTED_HASH}
    else
        echo "VLC source found"
        cd vlc
        if ! git cat-file -e ${TESTED_HASH}; then
            cat << EOF
176 177 178 179 180
***
*** Error: Your vlc checkout does not contain the latest tested commit ***
***

Please update your source with something like:
Rémi Duraffort's avatar
Rémi Duraffort committed
181 182

cd vlc
183 184 185 186 187 188 189
git reset --hard origin
git pull origin master
git checkout -B android ${TESTED_HASH}

*** : This will delete any changes you made to the current branch ***

EOF
190 191
            exit 1
        fi
192
    fi
193 194 195 196
else
    cd vlc
fi

197
if [ "$BUILD" = 0 ]
198 199 200
then
    echo "Not building anything, please run $0 --build"
    exit 0
201
fi
Rémi Duraffort's avatar
Rémi Duraffort committed
202

203
# Setup CFLAGS
204
if [ ${ANDROID_ABI} = "armeabi-v7a" ] ; then
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
205
    EXTRA_CFLAGS="-mfpu=vfpv3-d16 -mcpu=cortex-a8"
206 207
    EXTRA_CFLAGS="${EXTRA_CFLAGS} -mthumb -mfloat-abi=softfp"
elif [ ${ANDROID_ABI} = "armeabi" ] ; then
Edward Wang's avatar
Edward Wang committed
208 209
    if [ -n "${NO_ARMV6}" ]; then
        EXTRA_CFLAGS="-march=armv5te -mtune=arm9tdmi -msoft-float"
210
    else
Edward Wang's avatar
Edward Wang committed
211 212 213 214 215
        if [ -n "${NO_FPU}" ]; then
            EXTRA_CFLAGS="-march=armv6j -mtune=arm1136j-s -msoft-float"
        else
            EXTRA_CFLAGS="-mfpu=vfp -mcpu=arm1136jf-s -mfloat-abi=softfp"
        fi
216
    fi
Thomas Guillem's avatar
Thomas Guillem committed
217 218
elif [ ${ANDROID_ABI} = "arm64-v8a" ] ; then
    EXTRA_CFLAGS=""
Edward Wang's avatar
Edward Wang committed
219
elif [ ${ANDROID_ABI} = "x86" ] ; then
220
    EXTRA_CFLAGS="-march=pentium -m32"
Thomas Guillem's avatar
Thomas Guillem committed
221 222
elif [ ${ANDROID_ABI} = "x86_64" ] ; then
    EXTRA_CFLAGS=""
Edward Wang's avatar
Edward Wang committed
223
elif [ ${ANDROID_ABI} = "mips" ] ; then
Edward Wang's avatar
Edward Wang committed
224
    EXTRA_CFLAGS="-march=mips32 -mtune=mips32r2 -mhard-float"
Edward Wang's avatar
Edward Wang committed
225 226 227
    # All MIPS Linux kernels since 2.4.4 will trap any unimplemented FPU
    # instruction and emulate it, so we select -mhard-float.
    # See http://www.linux-mips.org/wiki/Floating_point#The_Linux_kernel_and_floating_point
228 229 230
else
    echo "Unknown ABI. Die, die, die!"
    exit 2
231 232
fi

Rafaël Carré's avatar
Rafaël Carré committed
233 234
EXTRA_CFLAGS="${EXTRA_CFLAGS} -O2"

Rafaël Carré's avatar
Rafaël Carré committed
235 236 237
EXTRA_CFLAGS="${EXTRA_CFLAGS} -I${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++${CXXSTL}/include"
EXTRA_CFLAGS="${EXTRA_CFLAGS} -I${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++${CXXSTL}/libs/${ANDROID_ABI}/include"

238
# Setup LDFLAGS
239
EXTRA_LDFLAGS="-L${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++${CXXSTL}/libs/${ANDROID_ABI} -lgnustl_static"
240

241
# Make in //
242 243 244 245 246 247 248 249
if [ -z "$MAKEFLAGS" ]; then
    UNAMES=$(uname -s)
    MAKEFLAGS=
    if which nproc >/dev/null; then
        MAKEFLAGS=-j`nproc`
    elif [ "$UNAMES" == "Darwin" ] && which sysctl >/dev/null; then
        MAKEFLAGS=-j`sysctl -n machdep.cpu.thread_count`
    fi
250 251
fi

252
# Build buildsystem tools
253 254 255 256 257 258 259
export PATH=`pwd`/extras/tools/build/bin:$PATH
echo "Building tools"
cd extras/tools
./bootstrap
make $MAKEFLAGS
cd ../..

260 261 262
############
# Contribs #
############
263
echo "Building the contribs"
264
mkdir -p contrib/contrib-android-${TARGET_TUPLE}
265 266 267 268 269 270 271 272 273 274 275 276 277 278

gen_pc_file() {
    echo "Generating $1 pkg-config file"
    echo "Name: $1
Description: $1
Version: $2
Libs: -l$1
Cflags:" > contrib/${TARGET_TUPLE}/lib/pkgconfig/`echo $1|tr 'A-Z' 'a-z'`.pc
}

mkdir -p contrib/${TARGET_TUPLE}/lib/pkgconfig
gen_pc_file EGL 1.1
gen_pc_file GLESv2 2

279
cd contrib/contrib-android-${TARGET_TUPLE}
280
../bootstrap --host=${TARGET_TUPLE} --disable-disc --disable-sout \
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
281
    --enable-dvdread \
282
    --enable-dvdnav \
283
    --disable-dca \
Rafaël Carré's avatar
Rafaël Carré committed
284
    --disable-goom \
Rafaël Carré's avatar
Rafaël Carré committed
285
    --disable-chromaprint \
Rafaël Carré's avatar
Rafaël Carré committed
286 287
    --disable-lua \
    --disable-schroedinger \
288 289 290
    --disable-sdl \
    --disable-SDL_image \
    --disable-fontconfig \
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
291
    --enable-zvbi \
292 293 294 295 296 297 298
    --disable-kate \
    --disable-caca \
    --disable-gettext \
    --disable-mpcdec \
    --disable-upnp \
    --disable-gme \
    --disable-tremor \
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
299
    --enable-vorbis \
300 301 302
    --disable-sidplay2 \
    --disable-samplerate \
    --disable-faad2 \
303
    --disable-harfbuzz \
Felix Abecassis's avatar
Felix Abecassis committed
304
    --enable-iconv \
Thomas Guillem's avatar
Thomas Guillem committed
305
    --disable-aribb24 \
306
    --disable-aribb25 \
Thomas Guillem's avatar
Thomas Guillem committed
307 308
    --disable-mpg123 \
    --enable-libdsm
309 310 311

# TODO: mpeg2, theora

Rafaël Carré's avatar
Rafaël Carré committed
312 313
# Some libraries have arm assembly which won't build in thumb mode
# We append -marm to the CFLAGS of these libs to disable thumb mode
Rafaël Carré's avatar
Rafaël Carré committed
314
[ ${ANDROID_ABI} = "armeabi-v7a" ] && echo "NOTHUMB := -marm" >> config.mak
Rafaël Carré's avatar
Rafaël Carré committed
315

316
# Release or not?
317
if [ "$RELEASE" = 1 ]; then
318
    OPTS=""
319
    EXTRA_CFLAGS="${EXTRA_CFLAGS} -DNDEBUG "
320 321 322 323
else
    OPTS="--enable-debug"
fi

324
echo "EXTRA_CFLAGS= -g ${EXTRA_CFLAGS}" >> config.mak
325
echo "EXTRA_LDFLAGS= ${EXTRA_LDFLAGS}" >> config.mak
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
326
export VLC_EXTRA_CFLAGS="${EXTRA_CFLAGS}"
327
export VLC_EXTRA_LDFLAGS="${EXTRA_LDFLAGS}"
328

Rafaël Carré's avatar
Rafaël Carré committed
329
make fetch
330
# We already have zlib available
331
[ -e .zlib ] || (mkdir -p zlib; touch .zlib)
332
which autopoint >/dev/null || make $MAKEFLAGS .gettext
333
export PATH="$PATH:$PWD/../$TARGET_TUPLE/bin"
334
make $MAKEFLAGS
Rémi Duraffort's avatar
Rémi Duraffort committed
335

336 337 338 339
############
# Make VLC #
############
cd ../.. && mkdir -p build-android-${TARGET_TUPLE} && cd build-android-${TARGET_TUPLE}
Rémi Duraffort's avatar
Rémi Duraffort committed
340

341
if [ "$JNI" = 1 ]; then
342
    CLEAN="jniclean"
343
    TARGET="libvlc/obj/local/${ANDROID_ABI}/libvlcjni.so"
344 345
else
    CLEAN="distclean"
Rafaël Carré's avatar
Rafaël Carré committed
346
    TARGET=
347
fi
348 349 350 351 352 353
if [ ! -f config.h ]; then
    echo "Bootstraping"
    ../bootstrap
    echo "Configuring"
    ${ANDROID_PATH}/configure.sh $OPTS
fi
Rémi Duraffort's avatar
Rémi Duraffort committed
354

355 356 357
# ANDROID NDK FIXUP (BLAME GOOGLE)
config_undef ()
{
358 359 360 361 362 363 364 365 366 367 368
    unamestr=`uname`
    if [[ "$unamestr" == 'Darwin' ]]; then
        previous_change=`stat -f "%Sm" -t "%y%m%d%H%M.%S" config.h`
        sed -i '' 's,#define '$1' 1,/\* #undef '$1' \*/,' config.h
        touch -t "$previous_change" config.h
    else
        previous_change=`stat -c "%y" config.h`
        sed -i 's,#define '$1' 1,/\* #undef '$1' \*/,' config.h
        # don't change modified date in order to don't trigger a full build
        touch -d "$previous_change" config.h
    fi
369 370
}

Thomas Guillem's avatar
Thomas Guillem committed
371 372
# if config dependencies change, ./config.status
# is run and overwrite previously hacked config.h. So call make config.h here
Thomas Guillem's avatar
Thomas Guillem committed
373 374
# and hack config.h after.

Thomas Guillem's avatar
Thomas Guillem committed
375
make $MAKEFLAGS config.h
Thomas Guillem's avatar
Thomas Guillem committed
376

377 378 379 380 381 382 383 384 385 386
if [ ${ANDROID_ABI} = "x86" -a ${ANDROID_API} != "android-21" ] ; then
    # NDK x86 libm.so has nanf symbol but no nanf definition, we don't known if
    # intel devices has nanf. Assume they don't have it.
    config_undef HAVE_NANF
fi
if [ ${ANDROID_API} = "android-21" ] ; then
    # android-21 has empty sys/shm.h headers that triggers shm detection but it
    # doesn't have any shm functions and/or symbols. */
    config_undef HAVE_SYS_SHM_H
fi
Thomas Guillem's avatar
Thomas Guillem committed
387
# END OF ANDROID NDK FIXUP
388

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
389
echo "Building"
390
make $MAKEFLAGS
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
391

392 393 394 395
####################################
# VLC android UI and specific code
####################################
echo "Building VLC for Android"
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
396
cd ../../
Rafaël Carré's avatar
Rafaël Carré committed
397

398
export ANDROID_SYS_HEADERS=${PWD}/android-headers
Rafaël Carré's avatar
Rafaël Carré committed
399 400

export ANDROID_LIBS=${PWD}/android-libs
401
export VLC_BUILD_DIR=vlc/build-android-${TARGET_TUPLE}
Rafaël Carré's avatar
Rafaël Carré committed
402

403
make $CLEAN
Rafaël Carré's avatar
Rafaël Carré committed
404
make -j1 TARGET_TUPLE=$TARGET_TUPLE PLATFORM_SHORT_ARCH=$PLATFORM_SHORT_ARCH CXXSTL=$CXXSTL RELEASE=$RELEASE $TARGET
405

406 407 408
#
# Exporting a environment script with all the necessary variables
#
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
echo "Generating environment script."
cat <<EOF
This is a script that will export many of the variables used in this
script. It will allow you to compile parts of the build without having
to rebuild the entire build (e.g. recompile only the Java part).

To use it, include the script into your shell, like this:
    source env.sh

Now, you can use this command to build the Java portion:
    make -e

The file will be automatically regenerated by compile.sh, so if you change
your NDK/SDK locations or any build configurations, just re-run this
script (sh compile.sh) and it will automatically update the file.

EOF

echo "# This file was automatically generated by compile.sh" > env.sh
echo "# Re-run 'sh compile.sh' to update this file." >> env.sh

# The essentials
cat <<EssentialsA >> env.sh
432
export ANDROID_API=$ANDROID_API
433 434 435 436 437
export ANDROID_ABI=$ANDROID_ABI
export ANDROID_SDK=$ANDROID_SDK
export ANDROID_NDK=$ANDROID_NDK
export GCCVER=$GCCVER
export CXXSTL=$CXXSTL
438
export ANDROID_SYS_HEADERS=$ANDROID_SYS_HEADERS
439
export ANDROID_LIBS=$ANDROID_LIBS
Edward Wang's avatar
Edward Wang committed
440
export VLC_BUILD_DIR=$VLC_BUILD_DIR
441
export TARGET_TUPLE=$TARGET_TUPLE
442
export PATH_HOST=$PATH_HOST
443
export PLATFORM_SHORT_ARCH=$PLATFORM_SHORT_ARCH
Thomas Guillem's avatar
Thomas Guillem committed
444
export RELEASE=$RELEASE
445 446 447 448 449 450
EssentialsA

# PATH
echo "export PATH=$NDK_TOOLCHAIN_PATH:\${ANDROID_SDK}/platform-tools:\${PATH}" >> env.sh

# CPU flags
451 452 453
echo "export HAVE_ARM=${HAVE_ARM}" >> env.sh
echo "export HAVE_X86=${HAVE_X86}" >> env.sh
echo "export HAVE_MIPS=${HAVE_MIPS}" >> env.sh
454

455 456 457
echo "export NO_ARMV6=${NO_ARMV6}" >> env.sh
echo "export NO_FPU=${NO_FPU}" >> env.sh
echo "export HAVE_64=${HAVE_64}" >> env.sh