Merge changes I6bb76601,I552ee6a7 into studio-1.4-dev
automerge: 1815bcb

* commit '1815bcb8d6d6b5e1f506b50ec164a7194f0f2296':
  android-rebuild.sh: Copy e2fsprogs to $OUT_DIR/bin
  build-e2fsprogs.sh: New build script.
diff --git a/android-rebuild.sh b/android-rebuild.sh
index 6e1d385..5c11ed4 100755
--- a/android-rebuild.sh
+++ b/android-rebuild.sh
@@ -260,6 +260,33 @@
     fi
 fi
 
+# Copy e2fsprogs binaries.
+if true; then
+    E2FSPROGS_DIR=$PREBUILTS_DIR/e2fsprogs
+    if [ ! -d "$E2FSPROGS_DIR" ]; then
+        echo "Warning: Missing e2fsprogs prebuilts directory: $E2FSPROGS_DIR"
+    else
+    # NOTE: Bad indentation here is intentional, the echo/else above will
+    # be replaced by a panic/fi in a future patch.
+    echo "Copying e2fsprogs binaries."
+    for E2FS_ARCH in x86 x86_64; do
+        E2FS_SRCDIR=$E2FSPROGS_DIR/$TARGET_OS-$E2FS_ARCH
+        case $E2FS_ARCH in
+            x86) E2FS_DSTDIR=$OUT_DIR/bin;;
+            x86_64) E2FS_DSTDIR=$OUT_DIR/bin64;;
+            *) panic "Invalid e2fsprogs host architecture: $E2FS_ARCH"
+        esac
+        run mkdir -p "$E2FS_DSTDIR" || panic "Could not create sub-directory: $E2FS_DSTDIR"
+        if [ ! -d "$E2FS_SRCDIR" ]; then
+            continue
+        fi
+        run cp -a "$E2FS_SRCDIR"/sbin/* "$E2FS_DSTDIR" || "Could not copy e2fsprogs binaries!"
+        # TODO: For Windows, probably copy the Cygwin DLL from somewhere
+        # and place it into $E2FS_DSTDIR
+    done
+    fi  # Again, intentional, will disappear in a future patch.
+fi
+
 RUN_32BIT_TESTS=
 RUN_64BIT_TESTS=true
 RUN_EMUGEN_TESTS=true
diff --git a/android/dependencies/PACKAGES.TXT b/android/dependencies/PACKAGES.TXT
index f6b6c29..1c2960a 100644
--- a/android/dependencies/PACKAGES.TXT
+++ b/android/dependencies/PACKAGES.TXT
@@ -22,3 +22,6 @@
 # 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/dependencies/e2fsprogs-1.42.13-patches.tar.xz b/android/dependencies/e2fsprogs-1.42.13-patches.tar.xz
new file mode 100644
index 0000000..5a101c8
--- /dev/null
+++ b/android/dependencies/e2fsprogs-1.42.13-patches.tar.xz
Binary files differ
diff --git a/android/scripts/build-e2fsprogs.sh b/android/scripts/build-e2fsprogs.sh
new file mode 100755
index 0000000..d045ea1
--- /dev/null
+++ b/android/scripts/build-e2fsprogs.sh
@@ -0,0 +1,208 @@
+#!/bin/sh
+
+# Copyright 2015 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+. $(dirname "$0")/utils/common.shi
+
+shell_import utils/aosp_dir.shi
+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
+
+# This script cannot build Windows binaries on Linux, so limit the default
+# host systems list to linux ones.
+case $(get_build_os) in
+    linux)
+        DEFAULT_HOST_SYSTEMS="linux-x86,linux-x86_64"
+        ;;
+esac
+
+PROGRAM_PARAMETERS=""
+
+PROGRAM_DESCRIPTION=\
+"Build prebuilt e2fsprogs for Linux and Darwin."
+
+package_builder_register_options
+
+aosp_dir_register_option
+prebuilts_dir_register_option
+install_dir_register_option e2fsprogs
+
+option_parse "$@"
+
+if [ "$PARAMETER_COUNT" != 0 ]; then
+    panic "This script takes no arguments. See --help for details."
+fi
+
+prebuilts_dir_parse_option
+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 e2fsprogs
+
+package_list_parse_file "$PACKAGE_LIST"
+
+BUILD_SRC_DIR=$TEMP_DIR/src
+
+# 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
+    fi
+}
+
+# $1: Package basename (e.g. 'libpthread-stubs-0.3')
+# $2+: Extra configuration options.
+build_package () {
+    local PKG_NAME PKG_SRC_DIR PKG_BUILD_DIR PKG_SRC_TIMESTAMP PKG_TIMESTAMP
+    PKG_NAME=$(package_list_get_src_dir $1)
+    unpack_package_source "$1"
+    shift
+    PKG_SRC_DIR="$BUILD_SRC_DIR/$PKG_NAME"
+    PKG_BUILD_DIR=$TEMP_DIR/build-$SYSTEM/$PKG_NAME
+    PKG_TIMESTAMP=$TEMP_DIR/build-$SYSTEM/$PKG_NAME-timestamp
+    if [ ! -f "$PKG_TIMESTAMP" -o -n "$OPT_FORCE" ]; then
+        case $SYSTEM in
+            darwin*)
+                # Required for proper build on Darwin!
+                build_disable_verbose_install
+                ;;
+        esac
+        builder_build_autotools_package \
+            "$PKG_SRC_DIR" \
+            "$PKG_BUILD_DIR" \
+            "$@"
+
+        touch "$PKG_TIMESTAMP"
+    fi
+}
+
+# Perform a Darwin build through ssh to a remote machine.
+# $1: Darwin host name.
+# $2: List of darwin target systems to build for.
+do_remote_darwin_build () {
+    builder_prepare_remote_darwin_build \
+            "/tmp/$USER-rebuild-darwin-ssh-$$/e2fsprogs-build"
+
+    copy_directory "$ARCHIVE_DIR" "$DARWIN_PKG_DIR"/archive
+
+    local PKG_DIR="$DARWIN_PKG_DIR"
+    local REMOTE_DIR=/tmp/$DARWIN_PKG_NAME
+    # Generate a script to rebuild all binaries from sources.
+    # Note that the use of the '-l' flag is important to ensure
+    # that this is run under a login shell. This ensures that
+    # ~/.bash_profile is sourced before running the script, which
+    # puts MacPorts' /opt/local/bin in the PATH properly.
+    #
+    # If not, the build is likely to fail with a cryptic error message
+    # like "readlink: illegal option -- f"
+    cat > $PKG_DIR/build.sh <<EOF
+#!/bin/bash -l
+PROGDIR=\$(dirname \$0)
+\$PROGDIR/scripts/$(program_name) \\
+    --build-dir=$REMOTE_DIR/build \\
+    --host=$(spaces_to_commas "$DARWIN_SYSTEMS") \\
+    --install-dir=$REMOTE_DIR/install-prefix \\
+    --prebuilts-dir=$REMOTE_DIR \\
+    --aosp-dir=$REMOTE_DIR/aosp \\
+    $DARWIN_BUILD_FLAGS
+EOF
+    builder_run_remote_darwin_build
+
+    run rm -rf "$PKG_DIR"
+
+    run mkdir -p "$INSTALL_DIR" ||
+            panic "Could not create final directory: $INSTALL_DIR"
+
+    for SYSTEM in $DARWIN_SYSTEMS; do
+        dump "[$SYSTEM] Retrieving remote darwin binaries"
+        run rm -rf "$INSTALL_DIR"/* &&
+        run rsync -haz --delete --exclude=intermediates --exclude=libs \
+                $DARWIN_SSH:$REMOTE_DIR/install-prefix/$SYSTEM \
+                $INSTALL_DIR
+    done
+}
+
+if [ "$DARWIN_SSH" -a "$DARWIN_SYSTEMS" ]; then
+    # Perform remote Darwin build first.
+    dump "Remote e2fsprogs build for: $DARWIN_SYSTEMS"
+    do_remote_darwin_build "$DARWIN_SSH" "$DARWIN_SYSTEMS"
+fi
+
+for SYSTEM in $LOCAL_HOST_SYSTEMS; do
+    (
+        case $SYSTEM in
+            windows*)
+                echo "ERROR: Sorry, cannot build Windows binaries with this script!" >&2
+                echo "Please use Cygwin on Windows to build e2fsprogs instead!"
+                exit 1
+                ;;
+            *)
+                builder_prepare_for_host_no_binprefix "$SYSTEM" "$AOSP_DIR"
+                ;;
+        esac
+
+        dump "$(builder_text) Building e2fsprogs"
+
+        CONFIGURE_FLAGS=
+        var_append CONFIGURE_FLAGS \
+                --disable-nls \
+                --disable-defrag \
+                --disable-jbd-debug \
+                --disable-profile \
+                --disable-testio-debug \
+                --disable-rpath \
+
+        build_package e2fsprogs $CONFIGURE_FLAGS
+
+        # Copy binaries necessary for the build itself as well as static
+        # libraries.
+        copy_directory_files \
+                "$(builder_install_prefix)" \
+                "$INSTALL_DIR/$SYSTEM" \
+                sbin/e2fsck \
+                sbin/fsck.ext4 \
+                sbin/mkfs.ext4 \
+                sbin/resize2fs \
+                sbin/tune2fs \
+
+    ) || panic "[$SYSTEM] Could not build e2fsprogs!"
+
+done
+
+log "Done building e2fsprogs."
diff --git a/android/scripts/utils/package_builder.shi b/android/scripts/utils/package_builder.shi
index f97c461..2c132f6 100644
--- a/android/scripts/utils/package_builder.shi
+++ b/android/scripts/utils/package_builder.shi
@@ -537,6 +537,22 @@
     var_append _SHU_BUILDER_CONFIGURE_FLAGS "$*"
 }
 
+# Call this function to disable verbose output during the 'make install' pass
+# of builder_build_autotools_package(). This is necessary for certain packages
+# with broken Makefiles (e.g. e2fsprogs when built on Darwin).
+_SHU_BUILDER_DISABLE_VERBOSE_INSTALL=
+builder_disable_verbose_install () {
+    _SHU_BUILDER_DISABLE_VERBOSE_INSTALL=true
+}
+
+# Call this function to disable parallel jobs during the 'make install' pass
+# of builder_build_autotools_package(). This is necessary for certain packages
+# with broken Makefiles.
+_SHU_BUILDER_DISABLE_PARALLEL_INSTALL=
+builder_disable_parallel_install () {
+    _SHU_BUILDER_DISABLE_PARALLEL_INSTALL=true
+}
+
 # Build a given autotools based package
 #
 # $1: Source directory.
@@ -548,6 +564,13 @@
 
     local PKG_SRC_DIR="$1"
     local PKG_BUILD_DIR="$2"
+    local INSTALL_FLAGS
+    if [ -z "$_SHU_BUILDER_DISABLE_PARALLEL_INSTALL" ]; then
+        var_append INSTALL_FLAGS "-j$NUM_JOBS"
+    fi
+    if [ -z "$_SHU_BUILDER_DISABLE_VERBOSE_INSTALL" ]; then
+        var_append INSTALL_FLAGS "V=1";
+    fi
     shift; shift;
     (
         run mkdir -p "$PKG_BUILD_DIR" &&
@@ -562,7 +585,7 @@
             $_SHU_BUILDER_CONFIGURE_FLAGS \
             "$@" &&
         run make -j$NUM_JOBS V=1 &&
-        run make install -j$NUM_JOBS V=1
+        run make install $INSTALL_FLAGS
     ) ||
     panic "Could not build and install $PKG_FULLNAME"
 }