bin/coverage: a GN version of the coverage script

intended to replace tools/coverage when GYP is gone.

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2343243002

Review-Url: https://codereview.chromium.org/2343243002
diff --git a/bin/coverage b/bin/coverage
new file mode 100755
index 0000000..7390d7e
--- /dev/null
+++ b/bin/coverage
@@ -0,0 +1,58 @@
+#!/bin/sh
+# Copyright 2016 Google Inc.
+#
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+if [ -z "$1" ]; then
+    cat <<-EOM
+	Usage:
+	  $0 SKIA_EXECUTABLE [ARGUMENTS_FOR_EXECUTABLE...]
+
+	Run something like this:
+	  $0 dm --src tests
+	or
+	  $0 dm --src gm skp
+
+	EOM
+    exit 1
+fi
+
+set -x
+set -e
+
+cd "$(dirname "$0")/.."
+
+EXECUTABLE="$1"
+shift
+
+DIR="$(mktemp -d "${TMPDIR:-/tmp}/skia_coverage_XXXXXXXXXX")"
+BUILD=out/coverage
+
+# Build $EXECUTABLE
+bin/sync
+bin/fetch-gn
+
+#TODO: make this work with Clang.
+ARGS='cc="gcc" cxx="g++" extra_cflags="--coverage" extra_ldflags="--coverage"'
+gn gen --args="$ARGS" "$BUILD"
+
+ninja -C "$BUILD" "$EXECUTABLE"
+
+GCOV="$(realpath tools/gcov_shim)"
+
+# Generate a zero-baseline so files not covered by $EXECUTABLE $@ will
+# still show up in the report.  This reads the .gcno files that are
+# created at compile time.
+lcov -q --gcov-tool="$GCOV" -c -b "$BUILD" -d "$BUILD" -o "$DIR"/baseline -i
+
+# Running the binary generates the real coverage information, the .gcda files.
+"$BUILD"/"$EXECUTABLE" "$@"
+
+lcov -q --gcov-tool="$GCOV" -c -b "$BUILD" -d "$BUILD" -o "$DIR"/coverage
+
+lcov -q -a "$DIR"/baseline -a "$DIR"/coverage -o "$DIR"/merged
+
+genhtml -q "$DIR"/merged --legend -o "$DIR"/coverage_report --ignore-errors source
+
+xdg-open "$DIR"/coverage_report/index.html
diff --git a/tools/coverage.sh b/tools/coverage.sh
deleted file mode 100755
index 8fe75c5..0000000
--- a/tools/coverage.sh
+++ /dev/null
@@ -1,31 +0,0 @@
-#!/bin/sh
-
-# Run from Skia trunk something like this:
-#   $ tools/coverage.sh tests
-# or
-#   $ tools/coverage.sh gm
-
-set -x
-set -e
-
-COMMAND=$@
-GCOV=$(realpath tools/gcov_shim)
-
-QUIET=-q
-
-# Build all of Skia.
-./gyp_skia
-ninja -C out/Coverage
-
-# Generate a zero-baseline so files not covered by $COMMAND will still show up in the report.
-# This reads the .gcno files that are created at compile time.
-lcov $QUIET --gcov-tool=$GCOV -c -b out/Coverage -d out/Coverage -o /tmp/baseline -i
-
-# Running the binary generates the real coverage information, the .gcda files.
-out/Coverage/$COMMAND
-lcov $QUIET --gcov-tool=$GCOV -c -b out/Coverage -d out/Coverage -o /tmp/coverage
-
-lcov $QUIET -a /tmp/baseline -a /tmp/coverage -o /tmp/merged
-
-genhtml $QUIET /tmp/merged --legend -o out/Coverage/report
-xdg-open out/Coverage/report/index.html