|
2 | 2 | set -euo pipefail
|
3 | 3 | shopt -s failglob
|
4 | 4 |
|
| 5 | +# 'Debug', 'Release' or empty string for both |
| 6 | +BUILD_TYPE= |
| 7 | + |
5 | 8 | THIS_FILE=$(readlink -f "$0")
|
6 | 9 | BASEDIR=$(dirname "$THIS_FILE")
|
7 | 10 |
|
8 |
| -APP=$BASEDIR/android |
9 |
| -LIB=$BASEDIR/lib-android |
10 |
| -THIRD_PARTY_DIR=$BASEDIR/3rdparty |
11 |
| -THIRD_PARTY_DIR_SLASH_ESCAPED="${THIRD_PARTY_DIR//\//\\\/}\/" |
12 |
| - |
13 |
| -# Generate library gradle project based on Android app |
14 |
| -if ! test -d "$LIB" |
15 |
| -then |
16 |
| - echo "Preparing $LIB" |
17 |
| - |
18 |
| - # Copy necessary files from android app to android lib |
19 |
| - mkdir --parents --verbose $LIB/lib/src/main |
20 |
| - |
21 |
| - ln --symbolic --verbose $APP/gradle $LIB/gradle |
22 |
| - ln --symbolic --verbose $APP/build.gradle $LIB/build.gradle |
23 |
| - ln --symbolic --verbose $APP/gradle.properties $LIB/gradle.properties |
24 |
| - ln --symbolic --verbose $APP/gradlew $LIB/gradlew |
| 11 | +ANDROID_3RDPARTY_DIR=$BASEDIR/android-3rdparty |
| 12 | +ANDROID_LIB_DIR=$BASEDIR/android-libpdf2htmlex |
| 13 | +ANDROID_APP_DIR=$BASEDIR/android-sample-app |
25 | 14 |
|
26 |
| - echo "include ':lib'" >> $LIB/settings.gradle |
| 15 | +# This is the .externalNativeBuild |
| 16 | +BUILD_3RDPARTY_DIR=$BASEDIR/build/3rdparty |
| 17 | +BUILD_LIB_DIR=$BASEDIR/build/libpdf2htmlex |
| 18 | +BUILD_APP_DIR=$BASEDIR/build/sample-app |
27 | 19 |
|
28 |
| - cp --verbose $APP/app/build.gradle $LIB/lib/build.gradle |
| 20 | +# This is android/app/build |
| 21 | +GRADLE_BUILD_3RDPARTY_DIR=$BASEDIR/build/gradle_3rdparty |
| 22 | +GRADLE_BUILD_LIB_DIR=$BASEDIR/build/gradle_libpdf2htmlex |
| 23 | +GRADLE_BUILD_APP_DIR=$BASEDIR/build/gradle_sample-app |
29 | 24 |
|
30 |
| - # Change application id |
31 |
| - sed -i -E 's/applicationId "(.+)"/applicationId "\1.lib"/g' $LIB/lib/build.gradle |
32 |
| - |
33 |
| - # Change CMakeLists from pdf2htmlEX/CMakeLists.txt to 3rdparty/CMakeLists.txt |
34 |
| - sed -i "s/path \"..\/..\/CMakeLists.txt\"/path \"${THIRD_PARTY_DIR_SLASH_ESCAPED}CMakeLists.txt\"/g" $LIB/lib/build.gradle |
35 |
| - |
36 |
| - # Append buildStatingDirectory |
37 |
| - sed -i "/path \"${THIRD_PARTY_DIR_SLASH_ESCAPED}CMakeLists.txt\"/a buildStagingDirectory \"${THIRD_PARTY_DIR_SLASH_ESCAPED}built\"" $LIB/lib/build.gradle |
38 |
| - |
39 |
| - # Clear out dependecies |
40 |
| - sed -i -e '/dependencies/,/}/d' $LIB/lib/build.gradle |
41 |
| - echo 'dependencies { }' >> $LIB/lib/build.gradle |
| 25 | +# Use android-libpdf2htmlex as a template for android-3rdparty Android Gradle project |
| 26 | +if ! test -d $ANDROID_3RDPARTY_DIR |
| 27 | +then |
| 28 | + mkdir $ANDROID_3RDPARTY_DIR --verbose |
| 29 | + cp --recursive $ANDROID_LIB_DIR/app $ANDROID_3RDPARTY_DIR/app |
42 | 30 |
|
43 |
| - # Generate Manifest |
44 |
| - echo '<?xml version="1.0" encoding="utf-8"?>' > $LIB/lib/src/main/AndroidManifest.xml |
45 |
| - echo '<manifest xmlns:android="http://schemas.android.com/apk/res/android"' >> $LIB/lib/src/main/AndroidManifest.xml |
| 31 | + # Prepare build.gradle |
| 32 | + sed -i "s/path \"..\/..\/CMakeLists.txt\"/path \"..\/..\/3rdparty\/CMakeLists.txt\"/g" $ANDROID_3RDPARTY_DIR/app/build.gradle |
46 | 33 |
|
47 |
| - # Extract package name |
48 |
| - grep 'package="' $APP/app/src/main/AndroidManifest.xml | |
49 |
| - sed -E 's/package="(.+)"/package="\1.lib"/' >> $LIB/lib/src/main/AndroidManifest.xml |
| 34 | + sed -i "s/buildStagingDirectory \"..\/..\/build\/libpdf2htmlex\/\"/buildStagingDirectory \"${BUILD_3RDPARTY_DIR//\//\\\/}\/\"/g" $ANDROID_3RDPARTY_DIR/app/build.gradle |
50 | 35 |
|
51 |
| - echo '</manifest>' >> $LIB/lib/src/main/AndroidManifest.xml |
| 36 | + # gradle.properties defines buildDir |
| 37 | + grep -v 'buildDir=' $ANDROID_LIB_DIR/gradle.properties > $ANDROID_3RDPARTY_DIR/gradle.properties |
| 38 | + echo "buildDir=$GRADLE_BUILD_3RDPARTY_DIR" >> $ANDROID_3RDPARTY_DIR/gradle.properties |
52 | 39 | fi
|
53 | 40 |
|
54 |
| -# Build 3rdparty libraries |
55 |
| -cd $LIB |
56 |
| -./gradlew assemble |
| 41 | +to_symlink="gradle build.gradle gradlew settings.gradle" |
| 42 | +for f in $to_symlink |
| 43 | +do |
| 44 | + if ! test -e $ANDROID_3RDPARTY_DIR/$f |
| 45 | + then |
| 46 | + ln --symbolic $ANDROID_LIB_DIR/$f $ANDROID_3RDPARTY_DIR/$f --verbose |
| 47 | + fi |
| 48 | +done |
57 | 49 |
|
58 |
| -function wait_on_children_processes() { |
59 |
| - for pid in $pids |
60 |
| - do |
61 |
| - if ! wait $pid |
62 |
| - then |
63 |
| - echo "Build failed. Waiting for other subprocesses..." |
64 |
| - wait |
65 |
| - exit 1 |
66 |
| - fi |
67 |
| - done |
68 |
| -} |
| 50 | +# Build 3rdparty libraries |
| 51 | +cd $ANDROID_3RDPARTY_DIR |
| 52 | +./gradlew assemble$BUILD_TYPE |
69 | 53 |
|
70 | 54 | pids=
|
71 |
| -for build_target in $THIRD_PARTY_DIR/built/cmake/*/* |
| 55 | +for build_target in $BUILD_3RDPARTY_DIR/cmake/*/* |
72 | 56 | do
|
73 | 57 | cmake --build $build_target &
|
74 | 58 | pids="$pids $!"
|
75 | 59 | done
|
76 |
| -wait_on_children_processes $pids |
77 |
| - |
78 |
| -# Build pdf2htmlEX |
79 |
| -cd $APP |
80 |
| -./gradlew assemble |
81 | 60 |
|
82 |
| -pids= |
83 |
| -for build_target in $APP/app/.externalNativeBuild/cmake/*/* |
| 61 | +for pid in $pids |
84 | 62 | do
|
85 |
| - ( |
86 |
| - set -euo pipefail |
87 |
| - cmake --build $build_target --target install |
88 |
| - |
89 |
| - abi=$(basename $build_target) |
90 |
| - build_type=$(basename ${build_target%$abi}) |
91 |
| - |
92 |
| - # UPX only works on armeabi-v7a |
93 |
| - # Other ABIs produce segfaults. |
94 |
| - if test $abi = "armeabi-v7a" |
95 |
| - then |
96 |
| - upx --ultra-brute --8mib-ram $build_target/built/bin/pdf2htmlEX |
97 |
| - fi |
98 |
| - |
99 |
| - # Compress binaries and other files into .tar's |
100 |
| - mkdir --parents $build_target/built/sample_pdfs |
101 |
| - cp $BASEDIR/test/browser_tests/*.pdf $build_target/built/sample_pdfs/ |
102 |
| - |
103 |
| - tar --create --file $build_target/$build_type-$abi-pdf2htmlEX.tar --directory $build_target built |
104 |
| - ) & |
105 |
| - pids="$pids $!" |
| 63 | + if ! wait $pid |
| 64 | + then |
| 65 | + echo "Build failed. Waiting for other subprocesses..." |
| 66 | + wait |
| 67 | + exit 1 |
| 68 | + fi |
106 | 69 | done
|
107 |
| -wait_on_children_processes $pids |
108 | 70 |
|
109 |
| -for ft in $APP/app/.externalNativeBuild/cmake/*/*/*-pdf2htmlEX.tar |
| 71 | +# Build libpdf2htmlEX |
| 72 | +cd $ANDROID_LIB_DIR |
| 73 | +./gradlew assemble$BUILD_TYPE |
| 74 | + |
| 75 | +for build_target in $BUILD_LIB_DIR/cmake/*/* |
110 | 76 | do
|
111 |
| - echo "$ft is ready!" |
| 77 | + cmake --build $build_target --target install |
| 78 | + |
| 79 | + abi=$(basename $build_target) |
| 80 | + build_type=$(basename ${build_target%$abi}) |
| 81 | + |
| 82 | + # UPX only works on armeabi-v7a |
| 83 | + # Other ABIs produce segfaults. |
| 84 | + # Also no point in compressing debug builds. |
| 85 | + if test $abi = "armeabi-v7a" && test "$build_type" != "debug" |
| 86 | + then |
| 87 | + upx --ultra-brute --8mib-ram $build_target/built/lib/libpdf2htmlEX.so |
| 88 | + fi |
112 | 89 | done
|
113 | 90 |
|
| 91 | +# Rename .cmake.in to .cmake and pack it into .tar |
| 92 | +tar --create --file $BASEDIR/build/pdf2htmlEX-release.tar --directory=$BASEDIR pdf2htmlEX.cmake.in --transform 's,^pdf2htmlEX.cmake.in$,jniLibs/pdf2htmlEX.cmake,' |
| 93 | +tar --create --file $BASEDIR/build/pdf2htmlEX-debug.tar --directory=$BASEDIR pdf2htmlEX.cmake.in --transform 's,^pdf2htmlEX.cmake.in$,jniLibs/pdf2htmlEX.cmake,' |
| 94 | + |
| 95 | +function add_to_tar() { |
| 96 | + folder=$1 |
| 97 | + prefix_in_tar=$2 |
| 98 | + include_abi=$3 |
| 99 | + |
| 100 | + for build_type_ in $BUILD_LIB_DIR/cmake/* |
| 101 | + do |
| 102 | + build_type=$(basename $build_type_) |
| 103 | + tar_file=$BASEDIR/build/pdf2htmlEX-$build_type.tar |
| 104 | + |
| 105 | + for build_target in $build_type_/* |
| 106 | + do |
| 107 | + abi=$(basename $build_target) |
| 108 | + |
| 109 | + if $include_abi |
| 110 | + then |
| 111 | + prefix_in_tar=$2/$abi |
| 112 | + fi |
| 113 | + |
| 114 | + for f in $build_target/built/$folder/* |
| 115 | + do |
| 116 | + fname=$(basename $f) |
| 117 | + |
| 118 | + # Check if this file is the same as those provided by other ABIs |
| 119 | + if ! $include_abi && ! diff --brief --from-file $BUILD_LIB_DIR/cmake/$build_type/*/built/$folder/$fname |
| 120 | + then |
| 121 | + echo "ERROR: Included file $f is not the same in all ABIs!" |
| 122 | + ls -lha $BUILD_LIB_DIR/cmake/$build_type/*/built/$folder/$fname |
| 123 | + exit 1 |
| 124 | + fi |
| 125 | + |
| 126 | + tar --append --file $tar_file --directory=$build_target/built/$folder $fname --transform "s,^,$prefix_in_tar/," |
| 127 | + done |
| 128 | + |
| 129 | + if ! $include_abi |
| 130 | + then |
| 131 | + # Do not process other ABIs, everything already included from this one |
| 132 | + break |
| 133 | + fi |
| 134 | + done |
| 135 | + done |
| 136 | +} |
| 137 | + |
| 138 | +add_to_tar "lib" "jniLibs" true |
| 139 | +add_to_tar "include" "jniLibs/include" false |
| 140 | +add_to_tar "share/pdf2htmlEX" "assets/pdf2htmlEX" false |
| 141 | + |
| 142 | +# Load libpdf2htmlEX into sample android app |
| 143 | +tar_to_load=$BASEDIR/build/pdf2htmlEX-release.tar |
| 144 | +if test "$BUILD_TYPE" = "Debug" |
| 145 | +then |
| 146 | + tar_to_load=$BASEDIR/build/pdf2htmlEX-debug.tar |
| 147 | +fi |
| 148 | + |
| 149 | +tar --extract --file $tar_to_load --directory=$ANDROID_APP_DIR/app/src/main jniLibs |
| 150 | + |
| 151 | +mkdir --parents $ANDROID_APP_DIR/app/src/main/assets |
| 152 | +tar --extract --file $tar_to_load --directory=$ANDROID_APP_DIR/app/src/main assets |
| 153 | + |
| 154 | +# Build sample android app |
| 155 | +cd $ANDROID_APP_DIR |
| 156 | +./gradlew assemble$BUILD_TYPE |
| 157 | + |
0 commit comments