compile.sh 10.3 KB
Newer Older
1
#! /bin/sh
2
set -e
3

4 5 6 7
#############
# FUNCTIONS #
#############

8 9 10 11 12
diagnostic()
{
    echo "$@" 1>&2;
}

13 14 15 16 17 18 19 20
checkfail()
{
    if [ ! $? -eq 0 ];then
        diagnostic "$1"
        exit 1
    fi
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
21 22
# Read the Android Wiki http://wiki.videolan.org/AndroidCompile
# Setup all that stuff correctly.
23
# 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
24

25 26
while [ $# -gt 0 ]; do
    case $1 in
27
        help|--help|-h)
28
            echo "Use -a to set the ARCH:"
29 30
            echo "  ARM:     (armeabi-v7a|arm)"
            echo "  ARM64:   (arm64-v8a|arm64)"
31
            echo "  X86:     x86, x86_64"
32
            echo "Use --release to build in release mode"
33
            echo "Use --signrelease to build in release mode and sign apk, see vlc-android/build.gradle"
34
            echo "Use -s to set your keystore file and -p for the password"
Thomas Guillem's avatar
Thomas Guillem committed
35
            echo "Use -c to get a ChromeOS build"
36
            echo "Use -l to build only LibVLC"
37
            exit 0
38 39 40 41 42 43 44 45
            ;;
        a|-a)
            ANDROID_ABI=$2
            shift
            ;;
        -r|release|--release)
            RELEASE=1
            ;;
46 47 48 49
        signrelease|--signrelease)
            SIGNED_RELEASE=1
            RELEASE=1
            ;;
50 51 52 53 54 55 56 57
        -s|--signature)
            KEYSTORE_FILE=$2
            shift
            ;;
        -p|--password)
            PASSWORD_KEYSTORE=$2
            shift
            ;;
58 59
        -l)
            BUILD_LIBVLC=1
60
            NO_ML=1
61
            ;;
62 63 64
        -ml)
            BUILD_MEDIALIB=1
            ;;
65 66 67
        run)
            RUN=1
            ;;
68 69 70
        --asan)
            ASAN=1
            ;;
71 72 73
        --no-ml)
            NO_ML=1
            ;;
74 75 76 77
        --publish)
            RELEASE=1
            PUBLISH=1
            ;;
78 79 80
        --init)
            GRADLE_SETUP=1
            ;;
81
        *)
82 83
            diagnostic "$0: Invalid option '$1'."
            diagnostic "$0: Try --help for more information."
84 85
            exit 1
            ;;
86
    esac
87
    shift
88 89
done

90
if [ -z "$ANDROID_NDK" -o -z "$ANDROID_SDK" ]; then
91 92
   diagnostic "You must define ANDROID_NDK, ANDROID_SDK before starting."
   diagnostic "They must point to your NDK and SDK directories."
93 94 95
   exit 1
fi

96
if [ -z "$ANDROID_ABI" ]; then
97
   diagnostic "*** No ANDROID_ABI defined architecture: using ARMv7"
98
   ANDROID_ABI="armeabi-v7a"
99 100
fi

101
if [ "$ANDROID_ABI" = "armeabi-v7a" -o "$ANDROID_ABI" = "arm" ]; then
102
    GRADLE_ABI="ARMv7"
103
elif [ "$ANDROID_ABI" = "arm64-v8a" -o "$ANDROID_ABI" = "arm64" ]; then
104 105 106 107 108
    GRADLE_ABI="ARMv8"
elif [ "$ANDROID_ABI" = "x86" ]; then
    GRADLE_ABI="x86"
elif [ "$ANDROID_ABI" = "x86_64" ]; then
    GRADLE_ABI="x86_64"
109 110
elif [ "$ANDROID_ABI" = "all" ]; then
    GRADLE_ABI="all"
111 112 113 114 115 116
else
    diagnostic "Invalid arch specified: '$ANDROID_ABI'."
    diagnostic "Try --help for more information"
    exit 1
fi

117 118 119
##########
# GRADLE #
##########
120

121
if [ ! -d "gradle/wrapper" ]; then
122
    diagnostic "Downloading gradle"
123
    GRADLE_VERSION=4.10.1
124
    GRADLE_URL=https://download.videolan.org/pub/contrib/gradle/gradle-${GRADLE_VERSION}-bin.zip
125
    wget ${GRADLE_URL} 2>/dev/null || curl -O ${GRADLE_URL}
126
    checkfail "gradle: download failed"
127

128
    unzip -o gradle-${GRADLE_VERSION}-bin.zip
129
    checkfail "gradle: unzip failed"
130

131
    cd gradle-${GRADLE_VERSION}
132

133
    ./bin/gradle --offline wrapper
134
    checkfail "gradle: wrapper failed"
135

136 137 138 139 140 141
    ./gradlew -version
    checkfail "gradle: wrapper failed"
    cd ..
    mkdir -p gradle
    mv gradle-${GRADLE_VERSION}/gradle/wrapper/ gradle
    mv gradle-${GRADLE_VERSION}/gradlew .
142
    chmod a+x gradlew
143
    rm -rf gradle-${GRADLE_VERSION}-bin.zip
144
fi
145

146 147 148
####################
# Configure gradle #
####################
149

150 151 152 153 154
if [ -z "$KEYSTORE_FILE" ]; then
    KEYSTORE_FILE="$HOME/.android/debug.keystore"
    STOREALIAS="androiddebugkey"
else
    if [ -z "$PASSWORD_KEYSTORE" ]; then
155
        diagnostic "No password"
156 157 158 159 160 161
        exit 1
    fi
    rm -f gradle.properties
    STOREALIAS="vlc"
fi

162
if [ ! -f gradle.properties ]; then
163 164 165
    echo android.enableJetifier=true > gradle.properties
    echo android.useAndroidX=true >> gradle.properties
    echo keyStoreFile=$KEYSTORE_FILE >> gradle.properties
166
    echo storealias=$STOREALIAS >> gradle.properties
Geoffrey Métais's avatar
Geoffrey Métais committed
167
    if [ -z "$PASSWORD_KEYSTORE" ]; then
168 169
        echo storepwd=android >> gradle.properties
    fi
170
fi
171 172 173 174 175 176 177 178 179 180 181 182 183 184

init_local_props() {
    (
    # initialize the local.properties file,
    # or fix it if it was modified (by Android Studio, for example).
    echo_props() {
        echo "sdk.dir=$ANDROID_SDK"
        echo "ndk.dir=$ANDROID_NDK"
    }
    # first check if the file just needs to be created for the first time
    if [ ! -f "$1" ]; then
        echo_props > "$1"
        return 0
    fi
185 186 187 188 189 190
    # escape special chars to get regex that matches string
    make_regex() {
        echo "$1" | sed -e 's/\([[\^$.*]\)/\\\1/g' -
    }
    android_sdk_regex=`make_regex "${ANDROID_SDK}"`
    android_ndk_regex=`make_regex "${ANDROID_NDK}"`
191 192 193
    # check for lines setting the SDK directory
    sdk_line_start="^sdk\.dir="
    total_sdk_count=`grep -c "${sdk_line_start}" "$1"`
194
    good_sdk_count=`grep -c "${sdk_line_start}${android_sdk_regex}\$" "$1"`
195 196 197
    # check for lines setting the NDK directory
    ndk_line_start="^ndk\.dir="
    total_ndk_count=`grep -c "${ndk_line_start}" "$1"`
198
    good_ndk_count=`grep -c "${ndk_line_start}${android_ndk_regex}\$" "$1"`
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    # if one of each is found and both match the environment vars, no action needed
    if [ "$total_sdk_count" -eq "1" -a "$good_sdk_count" -eq "1" \
	    -a "$total_ndk_count" -eq "1" -a "$good_ndk_count" -eq "1" ]
    then
        return 0
    fi
    # if neither property is set they can simply be appended to the file
    if [ "$total_sdk_count" -eq "0" -a "$total_ndk_count" -eq "0" ]; then
        echo_props >> "$1"
        return 0
    fi
    # if a property is set incorrectly or too many times,
    # remove all instances of both properties and append correct ones.
    replace_props() {
        temp_props="$1.tmp"
        while IFS= read -r LINE || [ -n "$LINE" ]; do
            line_sdk_dir="${LINE#sdk.dir=}"
            line_ndk_dir="${LINE#ndk.dir=}"
            if [ "x$line_sdk_dir" = "x$LINE" -a "x$line_ndk_dir" = "x$LINE" ]; then
                echo "$LINE"
            fi
        done <"$1" >"$temp_props"
        echo_props >> "$temp_props"
        mv -f -- "$temp_props" "$1"
    }
    echo "local.properties: Contains incompatible sdk.dir and/or ndk.dir properties. Replacing..."
    replace_props "$1"
    echo "local.properties: Finished replacing sdk.dir and/or ndk.dir with current environment variables."
    )
}
init_local_props local.properties || { echo "Error initializing local.properties"; exit $?; }

231 232
if [ ! -d "$ANDROID_SDK/licenses" ]; then
    mkdir "$ANDROID_SDK/licenses"
233
    echo "24333f8a63b6825ea9c5514f83c2829b004d1fee" > "$ANDROID_SDK/licenses/android-sdk-license"
234
    echo "d56f5187479451eabf01fb78af6dfcb131a6481e" >> "$ANDROID_SDK/licenses/android-sdk-license"
235
fi
236

237
if [ "$GRADLE_SETUP" = 1 ]; then
238 239
    exit 0
fi
240 241 242
####################
# Fetch VLC source #
####################
243

Geoffrey Métais's avatar
Geoffrey Métais committed
244
TESTED_HASH=135e7ff
245
if [ ! -d "vlc" ]; then
246
    diagnostic "VLC source not found, cloning"
247
    git clone https://git.videolan.org/git/vlc/vlc-3.0.git vlc
248
    checkfail "vlc source: git clone failed"
249 250 251 252 253
fi
diagnostic "VLC source found"
cd vlc
if ! git cat-file -e ${TESTED_HASH}; then
    cat 1>&2 << EOF
254 255 256 257
***
*** Error: Your vlc checkout does not contain the latest tested commit: ${TESTED_HASH}
***
EOF
258
    exit 1
259
fi
260 261
if [ "$RELEASE" = 1 ]; then
    git reset --hard ${TESTED_HASH}
262
    git am ../libvlc/patches/*.patch
263 264 265
fi
cd ..

266

267 268 269
############
# Make VLC #
############
270

271
diagnostic "Configuring"
272 273 274 275 276 277 278 279 280 281 282
compile() {
    OPTS="-a $1"
    if [ "$RELEASE" = 1 ]; then
        OPTS="$OPTS release"
    fi
    if [ "$CHROME_OS" = 1 ]; then
        OPTS="$OPTS -c"
    fi
    if [ "$ASAN" = 1 ]; then
        OPTS="$OPTS --asan"
    fi
283

284
    # Build LibVLC if asked for it, or needed by medialibrary
285
    if [ "$BUILD_MEDIALIB" != 1 -o ! -d "libvlc/jni/libs/$1" ]; then
286 287
        ./compile-libvlc.sh $OPTS
    fi
288

289 290 291 292
    if [ "$NO_ML" != 1 ]; then
        ./compile-medialibrary.sh $OPTS
    fi
}
293 294
if [ "$ANDROID_ABI" = "all" ]; then
    if [ -d tmp ]; then
295
        rm -rf tmp
296
    fi
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    mkdir tmp
    LIB_DIR="libvlc"
    if [ "$NO_ML" != 1 ]; then
        LIB_DIR="medialibrary"
    fi
    compile armeabi-v7a
    cp -r $LIB_DIR/jni/libs/armeabi-v7a tmp
    compile arm64-v8a
    cp -r $LIB_DIR/jni/libs/arm64-v8a tmp
    compile x86
    cp -r $LIB_DIR/jni/libs/x86 tmp
    compile x86_64
    mv tmp/* $LIB_DIR/jni/libs
    rm -rf tmp
else
    compile $ANDROID_ABI
313
fi
314

315 316 317
##################
# Compile the UI #
##################
318
BUILDTYPE="Dev"
319 320 321
if [ "$SIGNED_RELEASE" = 1 ]; then
    BUILDTYPE="signedRelease"
elif [ "$RELEASE" = 1 ]; then
322 323
    BUILDTYPE="Release"
fi
324
if [ "$BUILD_LIBVLC" = 1 ];then
325
    GRADLE_ABI=$GRADLE_ABI ./gradlew -p libvlc assemble${BUILDTYPE}
326 327
    RUN=0
    CHROME_OS=0
328 329 330
    if [ "$PUBLISH" = 1 ];then
        GRADLE_ABI=$GRADLE_ABI ./gradlew -p libvlc install bintrayUpload
    fi
331
elif [ "$BUILD_MEDIALIB" = 1 ]; then
332
    GRADLE_ABI=$GRADLE_ABI ./gradlew -p medialibrary assemble${BUILDTYPE}
333 334
    RUN=0
    CHROME_OS=0
335 336 337
    if [ "$PUBLISH" = 1 ];then
        GRADLE_ABI=$GRADLE_ABI ./gradlew -p medialibrary install bintrayUpload
    fi
338
else
339 340 341 342 343
    if [ "$RUN" = 1 ]; then
        ACTION="install"
    else
        ACTION="assemble"
    fi
344
    TARGET="${ACTION}${BUILDTYPE}"
345
    CLI="" GRADLE_ABI=$GRADLE_ABI ./gradlew $TARGET
346
fi
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
347

348 349 350 351
#######
# RUN #
#######
if [ "$RUN" = 1 ]; then
352
    export PATH="${ANDROID_SDK}/platform-tools/:$PATH"
353 354
    adb wait-for-device
    if [ "$RELEASE" = 1 ]; then
355
        adb shell am start -n org.videolan.vlc/org.videolan.vlc.StartActivity
356
    else
357
        adb shell am start -n org.videolan.vlc.debug/org.videolan.vlc.StartActivity
358 359
    fi
fi
360 361 362 363 364

#########################
# Chrome OS repackaging #
#########################
# You need to run the armv7 version first, then relaunch this script for x86
365
if [ "$CHROME_OS" = 1 -a "$ANDROID_ABI" = "x86" ]; then
366 367 368 369 370 371
    unzip -o vlc-android/build/outputs/apk/VLC-Android-CHROME-*-ARMv7.apk lib/armeabi-v7a/libvlcjni.so
    zip -rv vlc-android/build/outputs/apk/VLC-Android-CHROME-*-x86.apk lib/armeabi-v7a/libvlcjni.so
    rm -rf lib/
    apk_to_crx.py --zip vlc-android/build/outputs/apk/VLC-Android-CHROME-*-x86.apk --metadata vlc-android/flavors/chrome/VLC-Android-CHROME.crx.json
    mv vlc-android/build/outputs/apk/VLC-Android-CHROME-*-x86.zip vlc-android/build/outputs/apk/VLC-Android-CHROME-ALL.zip
fi