Cleanup package dependencies, incremental build and unit testing scripts

* Removed stale dependency / use of fakeroot unit test invocation.

* Separated incremental build and unit test invocation from
  gen_coverage_html.sh into their own scripts.

* Added checks for required binaries in gen_coverage_html.sh.

* Renamed setup_dev_packages to have a .sh suffix, for uniformity. Also
  requires that it installs packages regardless of whether or not
  they're already installed; this is to prevent a situation where
  packages were removed from the portage tree but happen to still live
  in the local chroot, so the discrepancy goes unnoticed (for example,
  sys-apps/fakeroot and dev-util/lcov).

* Cosmetics: switched to long options when applicable, for clarity.

BUG=None
TEST=Update engine builds, tests, and coverage generation correctly
invoked

CQ-DEPEND=I949f6b4abad52d04245e5982ac95884d1d0a05fc

Change-Id: I439e74eb9772f8f368256a0adf13503e3257e66c
Reviewed-on: https://gerrit.chromium.org/gerrit/23229
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
Commit-Ready: Gilad Arnold <garnold@chromium.org>
diff --git a/build b/build
new file mode 100755
index 0000000..b4dbb20
--- /dev/null
+++ b/build
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# Invokes an incremental, parallel build of the update engine binaries. For the
+# number of build threads, it uses the number of processing cores plus one.
+
+NUM_CORES=$(cat /proc/cpuinfo | grep '^processor' | wc -l)
+scons -j $((NUM_CORES + 1)) "$@"
diff --git a/gen_coverage_html b/gen_coverage_html
new file mode 100755
index 0000000..93a6f56
--- /dev/null
+++ b/gen_coverage_html
@@ -0,0 +1,32 @@
+#!/bin/bash
+
+# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# Builds, runs unit tests, then collects and processes coverage data for update
+# engine binaries. In the case where lcov/genhtml are missing, it will just
+# build and run the unit tests.
+
+DO_COVERAGE=$(which lcov genhtml > /dev/null 2>&1 && echo 1)
+
+set -ex
+
+./build debug=1
+if [[ $DO_COVERAGE ]]; then
+  lcov --directory . --zerocounters
+fi
+./run_unittests
+if [[ $DO_COVERAGE ]]; then
+  lcov --directory . --capture --output-file app.info
+
+  # We try to use genhtml with --no-function-coverage, if it is supported.  The
+  # problem w/ function coverage is that every template instantiation of a
+  # method counts as a different method, so if we instantiate a method twice,
+  # once for testing and once for prod, the method is tested, but it shows only
+  # 50% function coverage b/c it thinks we didn't test the prod version.
+  GENHTML_NO_FUNC_COV=$(genhtml --help | grep -q function-coverage &&
+                        echo --no-function-coverage)
+  genhtml $GENHTML_NO_FUNC_CONV --output-directory html app.info
+  ./local_coverage_rate
+fi
diff --git a/gen_coverage_html.sh b/gen_coverage_html.sh
deleted file mode 100755
index 39e8c61..0000000
--- a/gen_coverage_html.sh
+++ /dev/null
@@ -1,23 +0,0 @@
-#!/bin/bash
-
-# Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style license that can be
-# found in the LICENSE file.
-
-set -ex
-
-scons debug=1 -j $(cat /proc/cpuinfo |grep '^processor' | wc -l)
-lcov -d . --zerocounters
-./update_engine_unittests --gtest_filter='-*.RunAsRoot*:*.Fakeroot*'
-fakeroot ./update_engine_unittests --gtest_filter='*.Fakeroot*'
-sudo ./update_engine_unittests --gtest_filter='*.RunAsRoot*'
-lcov --directory . --capture --output-file app.info
-
-# some versions of genhtml support the --no-function-coverage argument,
-# which we want. The problem w/ function coverage is that every template
-# instantiation of a method counts as a different method, so if we
-# instantiate a method twice, once for testing and once for prod, the method
-# is tested, but it shows only 50% function coverage b/c it thinks we didn't
-# test the prod version.
-genhtml --no-function-coverage -o html ./app.info || genhtml -o html ./app.info
-./local_coverage_rate.sh
diff --git a/local_coverage_rate.sh b/local_coverage_rate
similarity index 94%
rename from local_coverage_rate.sh
rename to local_coverage_rate
index 33c06a7..8a44f73 100755
--- a/local_coverage_rate.sh
+++ b/local_coverage_rate
@@ -82,6 +82,8 @@
 END {
   printfile();
   print "---\nSummary: tested " GOOD_LINES " / " (BAD_LINES + GOOD_LINES);
-  printf "Test coverage: %.1f%%\n", ((GOOD_LINES * 100) / (BAD_LINES + GOOD_LINES));
+  printf(
+    "Test coverage: %.1f%%\n",
+    ((GOOD_LINES * 100) / (BAD_LINES + GOOD_LINES)));
 }
 '
diff --git a/run_unittests b/run_unittests
new file mode 100755
index 0000000..a61e90c
--- /dev/null
+++ b/run_unittests
@@ -0,0 +1,16 @@
+#!/bin/bash
+
+# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# Runs the update engine unit tests, including both userland and run-as-root
+# tests.
+
+if [ ! -e ./update_engine_unittests ]; then
+  echo 'Error: unit test binary missing' >&2
+  exit 1
+fi
+
+./update_engine_unittests --gtest_filter='-*.RunAsRoot*'
+sudo ./update_engine_unittests --gtest_filter='*.RunAsRoot*'
diff --git a/setup_dev_packages b/setup_dev_packages
index 64d063e..4ea2069 100755
--- a/setup_dev_packages
+++ b/setup_dev_packages
@@ -1,12 +1,15 @@
 #!/bin/bash
 
-# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
+# Emerges necessary packages for building and testing the update engine. This
+# allows incremental building, bypassing ebuild dependency resolution.
+
 set -ex
 
-sudo USE="-crash cros-debug" emerge -DNauv1 \
+sudo USE="-crash cros-debug" emerge -DNuv1 --selective=n "$@" \
   chromeos-base/hard-host-depends \
   chromeos-base/libchrome \
   chromeos-base/metrics \
@@ -15,5 +18,4 @@
   dev-cpp/gtest \
   dev-util/bsdiff \
   dev-util/lcov \
-  sys-apps/fakeroot \
   sys-apps/rootdev