compile.sh 9.24 KB
Newer Older
Rémi Duraffort's avatar
Rémi Duraffort committed
1
#! /bin/sh
2
set -e
Rémi Duraffort's avatar
Rémi Duraffort committed
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
Thomas Guillem's avatar
Thomas Guillem committed
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 32
            echo "  X86:     x86, x86_64"
            echo "  MIPS:    mips, mips64."
33
            echo "Use --release to build in release mode"
34
            echo "Use --signrelease to build in release mode and sign apk, see vlc-android/build.gradle"
35
            echo "Use -s to set your keystore file and -p for the password"
Thomas Guillem's avatar
Thomas Guillem committed
36
            echo "Use -c to get a ChromeOS build"
37
            echo "Use -l to build only LibVLC"
38
            exit 0
39 40 41 42 43
            ;;
        a|-a)
            ANDROID_ABI=$2
            shift
            ;;
Thomas Guillem's avatar
Thomas Guillem committed
44 45 46
        -c)
            CHROME_OS=1
            ;;
47 48 49
        -r|release|--release)
            RELEASE=1
            ;;
50 51 52 53
        signrelease|--signrelease)
            SIGNED_RELEASE=1
            RELEASE=1
            ;;
54 55 56 57 58 59 60 61
        -s|--signature)
            KEYSTORE_FILE=$2
            shift
            ;;
        -p|--password)
            PASSWORD_KEYSTORE=$2
            shift
            ;;
62 63
        -l)
            BUILD_LIBVLC=1
64
            NO_ML=1
65
            ;;
66 67 68
        run)
            RUN=1
            ;;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
69 70 71
        --asan)
            ASAN=1
            ;;
72 73 74
        --no-ml)
            NO_ML=1
            ;;
75
        *)
76 77
            diagnostic "$0: Invalid option '$1'."
            diagnostic "$0: Try --help for more information."
78 79
            exit 1
            ;;
80
    esac
81
    shift
82 83
done

84
if [ -z "$ANDROID_NDK" -o -z "$ANDROID_SDK" ]; then
85 86
   diagnostic "You must define ANDROID_NDK, ANDROID_SDK before starting."
   diagnostic "They must point to your NDK and SDK directories."
87 88 89
   exit 1
fi

90
if [ -z "$ANDROID_ABI" ]; then
91
   diagnostic "*** No ANDROID_ABI defined architecture: using ARMv7"
92
   ANDROID_ABI="armeabi-v7a"
93 94
fi

95
if [ "$ANDROID_ABI" = "armeabi-v7a" -o "$ANDROID_ABI" = "arm" ]; then
96
    GRADLE_ABI="ARMv7"
97
elif [ "$ANDROID_ABI" = "arm64-v8a" -o "$ANDROID_ABI" = "arm64" ]; then
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    GRADLE_ABI="ARMv8"
elif [ "$ANDROID_ABI" = "x86" ]; then
    GRADLE_ABI="x86"
elif [ "$ANDROID_ABI" = "x86_64" ]; then
    GRADLE_ABI="x86_64"
elif [ "$ANDROID_ABI" = "mips" ]; then
    GRADLE_ABI="MIPS"
elif [ "$ANDROID_ABI" = "mips64" ]; then
    GRADLE_ABI="MIPS64"
else
    diagnostic "Invalid arch specified: '$ANDROID_ABI'."
    diagnostic "Try --help for more information"
    exit 1
fi

113 114 115
##########
# GRADLE #
##########
Rémi Duraffort's avatar
Rémi Duraffort committed
116

117
if [ ! -d "gradle/wrapper" ]; then
118
    diagnostic "Downloading gradle"
Geoffrey Métais's avatar
Geoffrey Métais committed
119
    GRADLE_VERSION=4.1
120
    GRADLE_URL=https://download.videolan.org/pub/contrib/gradle/gradle-${GRADLE_VERSION}-bin.zip
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
121
    wget ${GRADLE_URL} 2>/dev/null || curl -O ${GRADLE_URL}
122
    checkfail "gradle: download failed"
123

124
    unzip -o gradle-${GRADLE_VERSION}-bin.zip
125
    checkfail "gradle: unzip failed"
126

127
    cd gradle-${GRADLE_VERSION}
128

129
    ./bin/gradle --offline wrapper
130
    checkfail "gradle: wrapper failed"
Rémi Duraffort's avatar
Rémi Duraffort committed
131

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

142 143 144
####################
# Configure gradle #
####################
Rémi Duraffort's avatar
Rémi Duraffort committed
145

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

158 159 160
if [ ! -f gradle.properties ]; then
    echo keyStoreFile=$KEYSTORE_FILE > gradle.properties
    echo storealias=$STOREALIAS >> gradle.properties
Geoffrey Métais's avatar
Geoffrey Métais committed
161
    if [ -z "$PASSWORD_KEYSTORE" ]; then
162 163
        echo storepwd=android >> gradle.properties
    fi
164
fi
165 166 167 168 169 170 171 172 173 174 175 176 177 178

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
179 180 181 182 183 184
    # 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}"`
185 186 187
    # check for lines setting the SDK directory
    sdk_line_start="^sdk\.dir="
    total_sdk_count=`grep -c "${sdk_line_start}" "$1"`
188
    good_sdk_count=`grep -c "${sdk_line_start}${android_sdk_regex}\$" "$1"`
189 190 191
    # check for lines setting the NDK directory
    ndk_line_start="^ndk\.dir="
    total_ndk_count=`grep -c "${ndk_line_start}" "$1"`
192
    good_ndk_count=`grep -c "${ndk_line_start}${android_ndk_regex}\$" "$1"`
193 194 195 196 197 198 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
    # 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 $?; }

225 226 227
if [ ! -d "$ANDROID_SDK/licenses" ]; then
    mkdir "$ANDROID_SDK/licenses"
    echo "8933bad161af4178b1185d1a37fbf41ea5269c55" > "$ANDROID_SDK/licenses/android-sdk-license"
228
    echo "d56f5187479451eabf01fb78af6dfcb131a6481e" >> "$ANDROID_SDK/licenses/android-sdk-license"
229 230
    echo "84831b9409646a918e30573bab4c9c91346d8abd" > "$ANDROID_SDK/licenses/android-sdk-preview-license"
fi
231

232 233 234
####################
# Fetch VLC source #
####################
235

Geoffrey Métais's avatar
Geoffrey Métais committed
236
TESTED_HASH=8ab7869
237
if [ ! -d "vlc" ]; then
238
    diagnostic "VLC source not found, cloning"
239
    git clone https://git.videolan.org/git/vlc/vlc-3.0.git vlc
240
    checkfail "vlc source: git clone failed"
Geoffrey Métais's avatar
Geoffrey Métais committed
241 242 243 244 245
fi
diagnostic "VLC source found"
cd vlc
if ! git cat-file -e ${TESTED_HASH}; then
    cat 1>&2 << EOF
246 247 248 249
***
*** Error: Your vlc checkout does not contain the latest tested commit: ${TESTED_HASH}
***
EOF
Geoffrey Métais's avatar
Geoffrey Métais committed
250
    exit 1
251
fi
Geoffrey Métais's avatar
Geoffrey Métais committed
252 253 254 255 256
if [ "$RELEASE" = 1 ]; then
    git reset --hard ${TESTED_HASH}
fi
cd ..

257

258 259 260
############
# Make VLC #
############
261

262
diagnostic "Configuring"
263 264
OPTS="-a ${ANDROID_ABI}"
if [ "$RELEASE" = 1 ]; then
Geoffrey Métais's avatar
Geoffrey Métais committed
265
    OPTS="$OPTS release"
266
fi
Thomas Guillem's avatar
Thomas Guillem committed
267 268 269
if [ "$CHROME_OS" = 1 ]; then
    OPTS="$OPTS -c"
fi
Thomas Guillem's avatar
Thomas Guillem committed
270 271 272
if [ "$ASAN" = 1 ]; then
    OPTS="$OPTS --asan"
fi
273 274 275
if [ "$NO_ML" = 1 ]; then
    OPTS="$OPTS --no-ml"
fi
276

277
./compile-libvlc.sh $OPTS
278

279 280 281
##################
# Compile the UI #
##################
282
PLATFORM="Vanilla"
283
BUILDTYPE="Debug"
284 285 286
if [ "$SIGNED_RELEASE" = 1 ]; then
    BUILDTYPE="signedRelease"
elif [ "$RELEASE" = 1 ]; then
287 288
    BUILDTYPE="Release"
fi
289 290 291
if [ "$CHROME_OS" = 1 ]; then
    PLATFORM="Chrome"
fi
292 293 294 295 296
if [ "$BUILD_LIBVLC" = 1 ];then
    ./gradlew -p libvlc assemble${BUILDTYPE}
    RUN=0
    CHROME_OS=0
else
297 298 299 300 301
    if [ "$RUN" = 1 ]; then
        ACTION="install"
    else
        ACTION="assemble"
    fi
302
    TARGET="${ACTION}${PLATFORM}${GRADLE_ABI}${BUILDTYPE}"
303
    CLI="" ./gradlew $TARGET
304
fi
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
305

306 307 308 309
#######
# RUN #
#######
if [ "$RUN" = 1 ]; then
310
    export PATH="${ANDROID_SDK}/platform-tools/:$PATH"
311 312
    adb wait-for-device
    if [ "$RELEASE" = 1 ]; then
313
        adb shell am start -n org.videolan.vlc/org.videolan.vlc.StartActivity
314
    else
315
        adb shell am start -n org.videolan.vlc.debug/org.videolan.vlc.StartActivity
316 317
    fi
fi
318 319 320 321 322

#########################
# Chrome OS repackaging #
#########################
# You need to run the armv7 version first, then relaunch this script for x86
323
if [ "$CHROME_OS" = 1 -a "$ANDROID_ABI" = "x86" ]; then
324 325 326 327 328 329
    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