Merge "build-qt.sh: Fix Qt build script." into studio-1.4-dev
automerge: 7322861

* commit '73228613765d724cc813de4e50c9891f1ca4a208':
  build-qt.sh: Fix Qt build script.
diff --git a/android/dependencies/PACKAGES.TXT b/android/dependencies/PACKAGES.TXT
index 1c2960a..a30b6b2 100644
--- a/android/dependencies/PACKAGES.TXT
+++ b/android/dependencies/PACKAGES.TXT
@@ -19,9 +19,5 @@
 SHA1=79638cf00584b08fd6eeb1e73ea69b331561e7f6    URL=http://llvm.org/releases/3.5.1/llvm-3.5.1.src.tar.xz
 SHA1=53ec316946bce7b63327f73c40ac9be1f50e31b2    URL=ftp://ftp.freedesktop.org/pub/mesa/10.4.2/MesaLib-10.4.2.tar.bz2  SRCDIR=Mesa-10.4.2 PATCHES=MesaLib-10.4.2-patches.tar.xz
 
-# Qt 5.5
-SHA1=3d6734bc6d00e1017c1db40d68309997bdf7bf6f    URL=http://download.qt.io/archive/qt/5.5/5.5.0/submodules/qtbase-opensource-src-5.5.0.tar.xz BASENAME=qt-base SRCDIR=qtbase-opensource-src-5.5.0
-SHA1=9238a1aedd126f84a73014c12b6391267238da0a    URL=http://download.qt.io/archive/qt/5.5/5.5.0/submodules/qtsvg-opensource-src-5.5.0.tar.xz  BASENAME=qt-svg  SRCDIR=qtsvg-opensource-src-5.5.0 DSTDIR=qtbase-opensource-src-5.5.0/qtsvg
-
 # e2fsprogs
 SHA1=2d008b9902a169bd6767058ba98fce334328c763    URL=https://www.kernel.org/pub/linux/kernel/people/tytso/e2fsprogs/v1.42.13/e2fsprogs-1.42.13.tar.xz PATCHES=e2fsprogs-1.42.13-patches.tar.xz
diff --git a/android/scripts/build-qt.sh b/android/scripts/build-qt.sh
index aec2509..acf325e 100755
--- a/android/scripts/build-qt.sh
+++ b/android/scripts/build-qt.sh
@@ -20,19 +20,24 @@
 shell_import utils/emulator_prebuilts.shi
 shell_import utils/install_dir.shi
 shell_import utils/option_parser.shi
-shell_import utils/package_list_parser.shi
 shell_import utils/package_builder.shi
 
 PROGRAM_PARAMETERS=""
 
 PROGRAM_DESCRIPTION=\
-"Build prebuilt Qt host libraries."
+"Build prebuilt Qt host libraries.
 
-package_builder_register_options
+This script is used to download the Qt source tarball from source, then
+extract it and build the Qt host tools and libraries.
+"
 
 aosp_dir_register_option
 prebuilts_dir_register_option
 install_dir_register_option qt
+package_builder_register_options
+
+OPT_DOWNLOAD=
+option_register_var "--download" OPT_DOWNLOAD "Download source tarball."
 
 option_parse "$@"
 
@@ -44,40 +49,63 @@
 aosp_dir_parse_option
 install_dir_parse_option
 
-ARCHIVE_DIR=$PREBUILTS_DIR/archive
-if [ ! -d "$ARCHIVE_DIR" ]; then
-    dump "Downloading dependencies sources first."
-    $(program_directory)/download-sources.sh \
-        --verbosity=$(get_verbosity) \
-        --prebuilts-dir="$PREBUILTS_DIR" ||
-            panic "Could not download source archives!"
-fi
-if [ ! -d "$ARCHIVE_DIR" ]; then
-    panic "Missing archive directory: $ARCHIVE_DIR"
-fi
-PACKAGE_LIST=$ARCHIVE_DIR/PACKAGES.TXT
-if [ ! -f "$PACKAGE_LIST" ]; then
-    panic "Missing package list file, run download-sources.sh: $PACKAGE_LIST"
-fi
-
 package_builder_process_options qt
 
-package_list_parse_file "$PACKAGE_LIST"
+###
+###  Download the source tarball if needed.
+###
+ARCHIVE_DIR=$PREBUILTS_DIR/archive
+if [ ! -d "$ARCHIVE_DIR" ]; then
+    run mkdir -p "$ARCHIVE_DIR" ||
+        panic "Could not create directory: $ARCHIVE_DIR"
+fi
 
-BUILD_SRC_DIR=$TEMP_DIR/src
+QT_SRC_NAME=qt-everywhere-opensource-src-5.5.0
+QT_SRC_PACKAGE=$QT_SRC_NAME.tar.xz
+QT_SRC_URL=http://download.qt.io/archive/qt/5.5/5.5.0/single/$QT_SRC_PACKAGE
+QT_SRC_PACKAGE_SHA1=4409ef12d1017a9b5e6733ea27596a6ca637a88c
 
-# Unpack package source into $BUILD_SRC_DIR if needed.
-# $1: Package basename.
-unpack_package_source () {
-    local PKG_NAME PKG_SRC_DIR PKG_BUILD_DIR PKG_SRC_TIMESTAMP PKG_TIMESTAMP
-    PKG_NAME=$(package_list_get_unpack_src_dir $1)
-    PKG_SRC_TIMESTAMP=$BUILD_SRC_DIR/timestamp-$PKG_NAME
-    if [ ! -f "$PKG_SRC_TIMESTAMP" ]; then
-        package_list_unpack_and_patch "$1" "$ARCHIVE_DIR" "$BUILD_SRC_DIR"
-        touch $PKG_SRC_TIMESTAMP
+if [ -z "$OPT_DOWNLOAD" -a ! -f "$ARCHIVE_DIR/$QT_SRC_PACKAGE" ]; then
+    if [ -z "$OPT_DOWNLOAD" ]; then
+        echo "The following tarball is missing: $ARCHIVE_DIR/$QT_SRC_PACKAGE"
+        echo "Please use the --download option to download it. Note that this is"
+        echo "a huge file over 300 MB, the download will take time."
+        exit 1
+    fi
+fi
+
+# Download a file.
+# $1: Source URL
+# $2: Destination directory.
+# $3: [Optional] expected SHA-1 sum of downloaded file.
+download_package () {
+    # Assume the packages are already downloaded under $ARCHIVE_DIR
+    local DST_DIR PKG_URL PKG_NAME SHA1SUM REAL_SHA1SUM
+
+    PKG_URL=$1
+    PKG_NAME=$(basename "$PKG_URL")
+    DST_DIR=$2
+    SHA1SUM=$3
+
+    log "Downloading $PKG_NAME..."
+    (cd "$DST_DIR" && run curl -L -o "$PKG_NAME" "$PKG_URL") ||
+            panic "Can't download '$PKG_URL'"
+
+    if [ "$SHA1SUM" ]; then
+        log "Checking $PKG_NAME content"
+        REAL_SHA1SUM=$(compute_file_sha1 "$DST_DIR"/$PKG_NAME)
+        if [ "$REAL_SHA1SUM" != "$SHA1SUM" ]; then
+            panic "Error: Invalid SHA-1 sum for $PKG_NAME: $REAL_SHA1SUM (expected $SHA1SUM)"
+        fi
     fi
 }
 
+if [ "$OPT_DOWNLOAD" ]; then
+    download_package "$QT_SRC_URL" "$ARCHIVE_DIR" "$QT_SRC_PACKAGE_SHA1"
+fi
+
+BUILD_SRC_DIR=$TEMP_DIR/src
+
 # Atomically update target directory $1 with the content of $2.
 # This also removes $2 on success.
 # $1: target directory.
@@ -94,11 +122,14 @@
 }
 
 # $1: Package name (e.g. 'qt-base')
-# $2+: Extra configuration options.
+# $2: List of extra modules to build (e.g. 'qtsvg')
+# $3+: Extra configuration options.
 build_qt_package () {
     local PKG_NAME PKG_SRC_DIR PKG_BUILD_DIR PKG_SRC_TIMESTAMP PKG_TIMESTAMP
+    local PKG_MODULES
     PKG_NAME=$(package_list_get_src_dir $1)
-    shift
+    PKG_MODULES=$2
+    shift; shift
     PKG_SRC_DIR="$BUILD_SRC_DIR/$PKG_NAME"
     PKG_BUILD_DIR=$TEMP_DIR/build-$SYSTEM/$PKG_NAME
     (
@@ -113,6 +144,15 @@
             "$@" &&
         run make -j$NUM_JOBS V=1 &&
         run make install -j$NUM_JOBS V=1
+#         export QTDIR=$_SHU_BUILDER_PREFIX &&
+#         export PATH=$QTDIR/bin:$PATH &&
+#         for MODULE in $PKG_MODULES; do
+#             cd "$PKG_SRC_DIR/$MODULE" &&
+#             run qmake &&
+#             run make -j$NUM_JOBS V=1 &&
+#             run make install -j$NUM_JOBS V=1 ||
+#                 panic "Could not build Qt $MODULE module!"
+#         done
     ) ||
     panic "Could not build and install $1"
 }
@@ -189,7 +229,27 @@
                 ;;
         esac
 
-        dump "$(builder_text) Building Qt"
+        # Unpacking the sources if needed.
+        QT_SRC_TIMESTAMP=$TEMP_DIR/timestamp-$QT_SRC_NAME
+        if [ "$OPT_FORCE" ]; then
+            rm -f "$QT_SRC_TIMESTAMP"
+        fi
+        if [ ! -f "$QT_SRC_TIMESTAMP" ]; then
+            dump "Unpacking $QT_SRC_NAME sources."
+            run mkdir -p "$BUILD_SRC_DIR" &&
+            unpack_archive "$ARCHIVE_DIR/$QT_SRC_PACKAGE" "$BUILD_SRC_DIR" ||
+                panic "Failed to unpack source package: $QT_SRC_PACKAGE"
+
+            # Need to patch this file to avoid install syncqt.pl which will
+            # fail horribly with an error like:
+            #  ..../<binprefix>-strip:.../bin/syncqt.pl: File format not recognized
+            # because the generated Makefile tries to strip a Perl script.
+            run sed -i 's|^INSTALLS += syncqt|#INSTALLS += syncqt|g' $BUILD_SRC_DIR/$QT_SRC_NAME/qtbase/qtbase.pro
+            touch "$QT_SRC_TIMESTAMP"
+        fi
+
+        # Configuring the build. This takes a lot of time due to QMake.
+        dump "$(builder_text) Configuring Qt build"
 
         EXTRA_CONFIGURE_FLAGS=
         var_append EXTRA_CONFIGURE_FLAGS \
@@ -200,10 +260,13 @@
                 -no-rpath \
                 -no-gtkstyle \
                 -shared \
-                -v \
                 -nomake examples \
                 -nomake tests \
 
+        if [ "$(get_verbosity)" -gt 2 ]; then
+            var_append EXTRA_CONFIGURE_FLAGS "-v"
+        fi
+
         case $SYSTEM in
             linux-x86)
                 var_append EXTRA_CONFIGURE_FLAGS \
@@ -239,11 +302,45 @@
                 ;;
         esac
 
-        unpack_package_source qt-base
-        unpack_package_source qt-svg
+        QT_BUILD_DIR=$(builder_build_dir)
 
-        build_qt_package qt-base \
+        (
+            run mkdir -p "$QT_BUILD_DIR" &&
+            run cd "$QT_BUILD_DIR" &&
+            export LDFLAGS="-L$_SHU_BUILDER_PREFIX/lib" &&
+            export CPPFLAGS="-I$_SHU_BUILDER_PREFIX/include" &&
+            export PKG_CONFIG_LIBDIR="$_SHU_BUILDER_PREFIX/lib/pkgconfig" &&
+            export PKG_CONFIG_PATH="$PKG_CONFIG_LIBDIR:$_SHU_BUILDER_PKG_CONFIG_PATH" &&
+            run "$BUILD_SRC_DIR"/$QT_SRC_NAME/configure \
+                -prefix $_SHU_BUILDER_PREFIX \
                 $EXTRA_CONFIGURE_FLAGS
+        ) || panic "Could not configure Qt build!"
+
+        # Build everything now.
+        QT_MODULES="qtbase qtsvg"
+        QT_TARGET_BUILD_MODULES=
+        QT_TARGET_INSTALL_MODULES=
+        for QT_MODULE in $QT_MODULES; do
+            var_append QT_TARGET_BUILD_MODULES "module-$QT_MODULE"
+            var_append QT_TARGET_INSTALL_MODULES "module-$QT_MODULE-install_subtargets"
+        done
+
+        QT_MAKE_FLAGS="-j$NUM_JOBS"
+        if [ "$(get_verbosity)" -gt 2 ]; then
+            var_append QT_MAKE_FLAGS "V=1"
+        fi
+
+        dump "$(builder_text) Building Qt binaries"
+        (
+            run cd "$QT_BUILD_DIR" &&
+            run make $QT_MAKE_FLAGS $QT_TARGET_BUILD_MODULES
+        ) || panic "Could not build Qt binaries!"
+
+        dump "$(builder_text) Installing Qt binaries"
+        (
+            run cd "$QT_BUILD_DIR" &&
+            run make $QT_MAKE_FLAGS $QT_TARGET_INSTALL_MODULES
+        ) || panic "Could not install Qt binaries!"
 
         # Find all Qt static libraries.
         case $SYSTEM in