Split stlport into a module.

This also finishes off build-target-prebuilts.sh and by extension
rebuild-all-prebuilt.sh, so clean that up. This means that all of the
build is now driven by the simpler Python scripts that can be run
independently with reasonable defaults and without having to guess
what you need in your environment.

Bug: http://b/23362228
Change-Id: I7fab1a47d10500c8127a5823d1ebe581451dbc86
diff --git a/build/tools/README.md b/build/tools/README.md
index 1c2e456..e7a1b64 100644
--- a/build/tools/README.md
+++ b/build/tools/README.md
@@ -440,24 +440,13 @@
     $NDK/build/tools/build-cxx-stl.sh --stl=stlport \
         --package-dir=/tmp/ndk-$USER/prebuilt
 
-A dev-script is provided to rebuild _and_ package all prebuilts. It is called
-`rebuild-all-prebuilt.sh`. Note that by default, it will automatically place the
-prebuilt tarballs under /tmp/ndk-$USER/prebuilt-$DATE, where $DATE is the
-current date in ISO order.
+The `rebuilt-all-prebuilt.sh` script has been entirely replaced by checkbuild.py
+in the root of the NDK.  Note that by default, it will automatically place the
+prebuilt tarballs under `$ANDROID_BUILD_TOP/out/ndk`.
 
 By default, this only rebuilds the host prebuilts for the current host system.
-You can use --mingw to force the generation of Windows binaries on Linux.
-
-Additionally, you can use the --darwin-ssh=<hostname> option to launch the build
-of the Darwin binaries from a Linux machine, by using ssh to access a remote
-Darwin machine. The script will package all required sources into a temporary
-tarball, copy it to the remote machine, launch the build there, then copy back
-all binaries to your own machine.
-
-This means that it is possible to generate the host binaries for all supported
-host systems from Linux (provided you have ssh access to a Darwin machine).
-
-Alternatively, you can run `rebuild-all-prebuilt.sh` on a Darwin machine.
+You can use `--system windows` or `--system windows64` to build Windows binaries
+on Linux.
 
 Once you have used the script three times (once per supported host systems), you
 should have plenty of files under /tmp/ndk-$USER/prebuilt-$DATE.  For the
diff --git a/build/tools/build-target-prebuilts.sh b/build/tools/build-target-prebuilts.sh
deleted file mode 100755
index 66bb45e..0000000
--- a/build/tools/build-target-prebuilts.sh
+++ /dev/null
@@ -1,118 +0,0 @@
-#!/bin/sh
-#
-# Copyright (C) 2011 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.
-#
-# Rebuild all target-specific prebuilts
-#
-
-PROGDIR=$(dirname $0)
-. $PROGDIR/prebuilt-common.sh
-
-NDK_DIR=$ANDROID_NDK_ROOT
-register_var_option "--ndk-dir=<path>" NDK_DIR "NDK installation directory"
-
-ARCHS="$DEFAULT_ARCHS"
-register_var_option "--arch=<list>" ARCHS "List of target archs to build for"
-
-NO_GEN_PLATFORMS=
-register_var_option "--no-gen-platforms" NO_GEN_PLATFORMS "Don't generate platforms/ directory, use existing one"
-
-GCC_VERSION_LIST="default" # it's arch defined by default so use default keyword
-register_var_option "--gcc-version-list=<vers>" GCC_VERSION_LIST "GCC version list (libgnustl should be built per each GCC version)"
-
-LLVM_VERSION=
-register_var_option "--llvm-version=<vers>" LLVM_VERSION "LLVM version"
-
-PACKAGE_DIR=
-register_var_option "--package-dir=<path>" PACKAGE_DIR "Package toolchain into this directory"
-
-VISIBLE_LIBGNUSTL_STATIC=
-register_var_option "--visible-libgnustl-static" VISIBLE_LIBGNUSTL_STATIC "Do not use hidden visibility for libgnustl_static.a"
-
-register_jobs_option
-
-register_try64_option
-
-PROGRAM_PARAMETERS="<toolchain-src-dir>"
-PROGRAM_DESCRIPTION="This script can be used to rebuild all the target NDK prebuilts at once."
-
-extract_parameters "$@"
-
-# Pickup one GCC_VERSION for the cases where we want only one build
-# That's actually all cases except libgnustl where we are building for each GCC version
-GCC_VERSION=
-if [ "$GCC_VERSION_LIST" != "default" ]; then
-   GCC_VERSIONS=$(commas_to_spaces $GCC_VERSION_LIST)
-   GCC_VERSION=${GCC_VERSIONS%% *}
-fi
-
-# Use DEFAULT_LLVM_VERSION to build targets unless we want to build with some particular version
-if [ -z "$GCC_VERSION_LIST" -a -z "$LLVM_VERSION" ]; then
-   LLVM_VERSION=$DEFAULT_LLVM_VERSION
-fi
-
-if [ ! -z "$LLVM_VERSION" ]; then
-   BUILD_TOOLCHAIN="--llvm-version=$LLVM_VERSION"
-elif [ ! -z "$GCC_VERSION" ]; then
-   BUILD_TOOLCHAIN="--gcc-version=$GCC_VERSION"
-fi
-
-# Check toolchain source path
-SRC_DIR="$PARAMETERS"
-check_toolchain_src_dir "$SRC_DIR"
-SRC_DIR=`cd $SRC_DIR; pwd`
-
-# Now we can do the build
-BUILDTOOLS=$ANDROID_NDK_ROOT/build/tools
-
-PACKAGE_FLAGS=
-if [ "$PACKAGE_DIR" ]; then
-    PACKAGE_FLAGS="--package-dir=$PACKAGE_DIR"
-fi
-
-ARCHS=$(commas_to_spaces $ARCHS)
-
-FLAGS=
-if [ "$DRYRUN" = "yes" ]; then
-    FLAGS=$FLAGS" --dryrun"
-fi
-if [ "$VERBOSE" = "yes" ]; then
-    FLAGS=$FLAGS" --verbose"
-fi
-if [ "$PACKAGE_DIR" ]; then
-    mkdir -p "$PACKAGE_DIR"
-    fail_panic "Could not create package directory: $PACKAGE_DIR"
-    FLAGS=$FLAGS" --package-dir=\"$PACKAGE_DIR\""
-fi
-FLAGS=$FLAGS" -j$NUM_JOBS"
-
-if [ "$TRY64" = "yes" ]; then
-    FLAGS=$FLAGS" --try-64"
-fi
-
-FLAGS=$FLAGS" --ndk-dir=\"$NDK_DIR\""
-ABIS=$(convert_archs_to_abis $ARCHS)
-
-dump "Building $ABIS stlport binaries..."
-run $BUILDTOOLS/build-cxx-stl.sh --stl=stlport --abis="$ABIS" $FLAGS --with-debug-info $BUILD_TOOLCHAIN
-fail_panic "Could not build stlport with debug info!"
-
-if [ "$PACKAGE_DIR" ]; then
-    dump "Done, see $PACKAGE_DIR"
-else
-    dump "Done"
-fi
-
-exit 0
diff --git a/build/tools/package-release.sh b/build/tools/package-release.sh
index 5b9539b..2039005 100755
--- a/build/tools/package-release.sh
+++ b/build/tools/package-release.sh
@@ -91,9 +91,9 @@
 "Package a new set of release packages for the Android NDK.
 
 You will need to have generated one or more prebuilt binary tarballs
-with the build/tools/rebuild-all-prebuilts.sh script. These files should
-be named like <toolname>-<system>.tar.bz2, where <toolname> is an arbitrary
-tool name, and <system> is one of: $SYSTEMS
+with the checkbuild.py script. These files should be named like
+<toolname>-<system>.tar.bz2, where <toolname> is an arbitrary tool name, and
+<system> is one of: $SYSTEMS
 
 Use the --prebuilt-dir=<path> option to build release packages from the
 binary tarballs stored in <path>.
diff --git a/build/tools/rebuild-all-prebuilt.sh b/build/tools/rebuild-all-prebuilt.sh
deleted file mode 100755
index 6bde775..0000000
--- a/build/tools/rebuild-all-prebuilt.sh
+++ /dev/null
@@ -1,156 +0,0 @@
-#!/bin/sh
-#
-# Copyright (C) 2010 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.
-#
-
-# Rebuild all prebuilts. This requires that you have a toolchain source tree
-#
-
-if [ -z "$ANDROID_BUILD_TOP" ]; then
-    >&2 echo 'ANDROID_BUILD_TOP not set. Cannot continue.'
-    >&2 echo 'Please set ANDROID_BUILD_TOP to point to the root of an Android' \
-             'tree.'
-    exit 1
-fi
-
-. `dirname $0`/prebuilt-common.sh
-PROGDIR=`dirname $0`
-
-NDK_DIR=$ANDROID_NDK_ROOT
-register_var_option "--ndk-dir=<path>" NDK_DIR "Put binaries into NDK install directory"
-
-BUILD_DIR=$TMPDIR/build
-register_var_option "--build-dir=<path>" BUILD_DIR "Specify temporary build directory"
-
-ARCHS="$DEFAULT_ARCHS"
-register_var_option "--arch=<arch>" ARCHS "Specify target architectures"
-
-NO_GEN_PLATFORMS=
-register_var_option "--no-gen-platforms" NO_GEN_PLATFORMS "Don't generate platforms/ directory, use existing one"
-
-GCC_VERSION_LIST="default" # it's arch defined by default so use default keyword
-register_var_option "--gcc-version-list=<vers>" GCC_VERSION_LIST "List of GCC release versions"
-
-LLVM_VERSION_LIST=$DEFAULT_LLVM_VERSION_LIST
-register_var_option "--llvm-version-list=<vers>" LLVM_VERSION_LIST "List of LLVM release versions"
-
-SYSTEMS=$HOST_TAG32
-CUSTOM_SYSTEMS=
-register_option "--systems=<list>" do_SYSTEMS "Specify host systems"
-do_SYSTEMS () { CUSTOM_SYSTEMS=true; SYSTEMS=$1; }
-
-ALSO_64=
-register_option "--also-64" do_ALSO_64 "Also build 64-bit host toolchain"
-do_ALSO_64 () { ALSO_64=yes; }
-
-RELEASE=`date +%Y%m%d`
-PACKAGE_DIR=$TMPDIR/prebuilt-$RELEASE
-register_var_option "--package-dir=<path>" PACKAGE_DIR "Put prebuilt tarballs into <path>."
-
-DARWIN_SSH=
-if [ "$HOST_OS" = "linux" ] ; then
-register_var_option "--darwin-ssh=<hostname>" DARWIN_SSH "Specify Darwin hostname for remote build."
-fi
-
-register_try64_option
-
-PROGRAM_PARAMETERS="<toolchain-src-dir>"
-PROGRAM_DESCRIPTION=\
-"This script is used to rebuild all host and target prebuilts from scratch.
-
-Unless you use the --ndk-dir option, all binaries will be installed to the
-current NDK directory.
-
-All prebuilts will then be archived into tarball that will be stored into a
-specific 'package directory'. Unless you use the --package-dir option, this
-will be: $PACKAGE_DIR
-
-Please read docs/DEV-SCRIPTS-USAGE.TXT for more usage information about this
-script.
-"
-
-extract_parameters "$@"
-
-SRC_DIR="$PARAMETERS"
-check_toolchain_src_dir "$SRC_DIR"
-
-if [ "$DARWIN_SSH" -a -z "$CUSTOM_SYSTEMS" ]; then
-    SYSTEMS=$SYSTEMS",darwin-x86"
-fi
-
-FLAGS=
-if [ "$DRYRUN" = "yes" ]; then
-    FLAGS=$FLAGS" --dryrun"
-fi
-if [ "$VERBOSE" = "yes" ]; then
-    FLAGS=$FLAGS" --verbose"
-fi
-FLAGS=$FLAGS" --ndk-dir=$NDK_DIR"
-FLAGS=$FLAGS" --package-dir=$PACKAGE_DIR"
-FLAGS=$FLAGS" --arch=$(spaces_to_commas $ARCHS)"
-
-if [ ! -z "$NO_GEN_PLATFORMS" ]; then
-    FLAGS=$FLAGS" --no-gen-platforms"
-fi
-
-HOST_FLAGS=$FLAGS" --systems=$(spaces_to_commas $SYSTEMS)"
-if [ "$GCC_VERSION_LIST" != "default" ]; then
-    HOST_FLAGS=$HOST_FLAGS" --gcc-version-list=$(spaces_to_commas $GCC_VERSION_LIST)"
-fi
-HOST_FLAGS=$HOST_FLAGS" --llvm-version-list=$(spaces_to_commas $LLVM_VERSION_LIST)"
-
-TARGET_FLAGS=$FLAGS
-
-if [ "$TRY64" = "yes" ]; then
-    HOST_FLAGS=$HOST_FLAGS" --try-64"
-    # If we build only 64-bit host we need to use this flag as well so that correct toolchain is found on target tools build
-    TARGET_FLAGS=$TARGET_FLAGS" --try-64"
-fi
-if [ "$DARWIN_SSH" ]; then
-    HOST_FLAGS=$HOST_FLAGS" --darwin-ssh=$DARWIN_SSH"
-fi
-
-if [ ! -z "$LLVM_VERSION_LIST" ]; then
-    LLVM_VERSIONS=$(commas_to_spaces $LLVM_VERSION_LIST)
-    LLVM_VERSION=${LLVM_VERSIONS%% *}
-    TARGET_FLAGS=$TARGET_FLAGS" --llvm-version=$LLVM_VERSION"
-    if [ "$GCC_VERSION_LIST" != "default" ]; then
-       for ARCH in $(commas_to_spaces $ARCHS); do
-         if [ "$ARCH" != "${ARCH%%64*}" ] ; then
-           if [ "${GCC_VERSION_LIST%%$DEFAULT_LLVM_GCC64_VERSION*}" = "$GCC_VERSION_LIST" ]; then
-              echo "ERROR: LLVM $LLVM_VERSION require GCC $DEFAULT_LLVM_GCC64_VERSION for $ARCH to be available. Try to include it in build list."
-              exit 1
-           fi
-         else
-           if [ "${GCC_VERSION_LIST%%$DEFAULT_LLVM_GCC32_VERSION*}" = "$GCC_VERSION_LIST" ]; then
-              echo "ERROR: LLVM $LLVM_VERSION require GCC $DEFAULT_LLVM_GCC32_VERSION for $ARCH to be available. Try to include it in build list."
-              exit 1
-           fi
-         fi
-       done
-    fi
-fi
-if [ "$GCC_VERSION_LIST" != "default" ]; then
-   TARGET_FLAGS=$TARGET_FLAGS" --gcc-version-list=$(spaces_to_commas $GCC_VERSION_LIST)"
-fi
-
-echo "COMMAND: $PROGDIR/build-target-prebuilts.sh $TARGET_FLAGS $SRC_DIR"
-$PROGDIR/build-target-prebuilts.sh $TARGET_FLAGS "$SRC_DIR"
-fail_panic "Could not build target prebuilts!"
-
-echo "Done, see $PACKAGE_DIR:"
-ls -l $PACKAGE_DIR
-
-exit 0
diff --git a/checkbuild.py b/checkbuild.py
index e88b79f..8c0da7f 100644
--- a/checkbuild.py
+++ b/checkbuild.py
@@ -42,7 +42,7 @@
     'host-tools',
     'libc++',
     'platforms',
-    'target',
+    'stlport',
 }
 
 
@@ -131,12 +131,12 @@
     return build_args
 
 
-def build_clang(out_dir, args, _):
+def build_clang(out_dir, args):
     print('Building Clang...')
     invoke_build('build-llvm.py', common_build_args(out_dir, args))
 
 
-def build_gcc(out_dir, args, _):
+def build_gcc(out_dir, args):
     gcc_build_args = common_build_args(out_dir, args)
     if args.arch is not None:
         toolchain_name = build_support.arch_to_toolchain(args.arch)
@@ -145,7 +145,7 @@
     invoke_external_build('toolchain/gcc/build.py', gcc_build_args)
 
 
-def build_host_tools(out_dir, args, _):
+def build_host_tools(out_dir, args):
     build_args = common_build_args(out_dir, args)
 
     print('Building ndk-stack...')
@@ -179,7 +179,7 @@
     invoke_external_build('toolchain/yasm/build.py', build_args)
 
 
-def build_gdbserver(out_dir, args, _):
+def build_gdbserver(out_dir, args):
     print('Building gdbserver...')
     build_args = common_build_args(out_dir, args)
     if args.arch is not None:
@@ -187,7 +187,7 @@
     invoke_build('build-gdbserver.py', build_args)
 
 
-def build_gnustl(out_dir, args, _):
+def build_gnustl(out_dir, args):
     print('Building gnustl...')
     build_args = common_build_args(out_dir, args)
     if args.arch is not None:
@@ -195,7 +195,7 @@
     invoke_build('build-gnustl.py', build_args)
 
 
-def build_libcxx(out_dir, args, _):
+def build_libcxx(out_dir, args):
     print('Building libc++...')
     build_args = common_build_args(out_dir, args)
     if args.arch is not None:
@@ -204,29 +204,22 @@
         'ndk/sources/cxx-stl/llvm-libc++/build.py', build_args)
 
 
-def build_platforms(out_dir, args, _):
+def build_stlport(out_dir, args):
+    print('Building stlport...')
+    build_args = common_build_args(out_dir, args)
+    if args.arch is not None:
+        build_args.append('--arch={}'.format(args.arch))
+    invoke_external_build(
+        'ndk/sources/cxx-stl/stlport/build.py', build_args)
+
+
+def build_platforms(out_dir, args):
     print('Building platforms...')
     invoke_build('build-platforms.py', common_build_args(out_dir, args))
 
 
-# TODO: Split up the target build.
-# TODO: Kill build_tools_args.
-# `build_tools_args` are the arguments that need to be passed to the scripts in
-# build/tools. `args`, by comparison, are the arguments parsed by ArgParser.
-# The build/tools scripts need to be called differently than the Python build
-# scripts we've built up because they typically were not called directly and
-# therefore do not have reasonable defaults.
-def build_target(out_dir, _, build_tools_args):
-    print('Building target modules...')
-    build_args = list(build_tools_args)
-    build_args.append('--package-dir={}'.format(out_dir))
-    build_args.append('--verbose')
-
-    invoke_build('rebuild-all-prebuilt.sh', build_args)
-
-
 def main():
-    args, build_args = ArgParser().parse_known_args()
+    args, package_args = ArgParser().parse_known_args()
 
     # Disable buffering on stdout so the build output doesn't hide all of our
     # "Building..." messages.
@@ -241,7 +234,7 @@
 
     system = args.system
     if system != 'windows':
-        build_args.append('--try-64')
+        package_args.append('--try-64')
 
     if system is not None:
         # TODO(danalbert): Update build server to pass just 'linux'.
@@ -256,7 +249,7 @@
         if system not in ('darwin-x86', 'linux-x86', 'windows'):
             sys.exit('Unknown system requested: {}'.format(original_system))
 
-        build_args.append('--systems={}'.format(system))
+        package_args.append('--systems={}'.format(system))
     else:
         # No flag provided. Use the current OS.
         if platform.system() == 'Darwin':
@@ -266,7 +259,7 @@
         else:
             sys.exit('Unknown build host: {}'.format(platform.system()))
 
-    build_args.append(os.path.join(build_top, 'toolchain'))
+    package_args.append(os.path.join(build_top, 'toolchain'))
 
     DEFAULT_OUT_DIR = os.path.join(build_top, 'out/ndk')
     out_dir = os.path.realpath(os.getenv('DIST_DIR', DEFAULT_OUT_DIR))
@@ -289,7 +282,7 @@
         modules = modules - {'gcc'}
 
     if args.arch is not None:
-        build_args.append('--arch={}'.format(args.arch))
+        package_args.append('--arch={}'.format(args.arch))
 
     print('Cleaning up...')
     invoke_build('dev-cleanup.sh')
@@ -302,15 +295,15 @@
         ('host-tools', build_host_tools),
         ('libc++', build_libcxx),
         ('platforms', build_platforms),
-        ('target', build_target),
+        ('stlport', build_stlport),
     ])
 
     print('Building modules: {}'.format(' '.join(modules)))
     for module in modules:
-        module_builds[module](out_dir, args, build_args)
+        module_builds[module](out_dir, args)
 
     if args.package and modules == ALL_MODULES:
-        package_ndk(args.release, system, out_dir, build_args)
+        package_ndk(args.release, system, out_dir, package_args)
 
 
 if __name__ == '__main__':
diff --git a/sources/cxx-stl/stlport/build.py b/sources/cxx-stl/stlport/build.py
new file mode 100755
index 0000000..9cc166b
--- /dev/null
+++ b/sources/cxx-stl/stlport/build.py
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 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.
+#
+"""Builds stlport for Android."""
+from __future__ import print_function
+
+import os
+import site
+
+site.addsitedir(os.path.join(os.path.dirname(__file__), '../../../build/lib'))
+
+import build_support
+
+
+class ArgParser(build_support.ArgParser):
+    def __init__(self):
+        super(ArgParser, self).__init__()
+
+        self.add_argument(
+            '--arch', choices=build_support.ALL_ARCHITECTURES,
+            help='Architectures to build. Builds all if not present.')
+
+
+def main(args):
+    arches = build_support.ALL_ARCHITECTURES
+    if args.arch is not None:
+        arches = [args.arch]
+
+    abis = []
+    for arch in arches:
+        abis.extend(build_support.arch_to_abis(arch))
+
+    print('Building stlport for ABIs: {}'.format(' '.join(abis)))
+
+    abis_arg = '--abis={}'.format(','.join(abis))
+    ndk_dir_arg = '--ndk-dir={}'.format(build_support.ndk_path())
+    script = build_support.ndk_path('build/tools/build-cxx-stl.sh')
+    build_cmd = [
+        'bash', script, '--stl=stlport', abis_arg, ndk_dir_arg,
+        build_support.jobs_arg(), build_support.toolchain_path(),
+        '--with-debug-info', '--llvm-version=3.6',
+    ]
+
+    build_support.build(build_cmd, args)
+
+if __name__ == '__main__':
+    build_support.run(main, ArgParser)