[coverage] Add use_coverage flag to add --coverage to clang builds. (#17935)

* [coverage] Add use_coverage flag to add --coverage to clang builds.

* [coverage] Add -o option to run lcov in a prebuilt output directory.

* [coverage] Fixes for review comments.

* [coverage] Rephrase use_coverage comment.
diff --git a/build/config/compiler/BUILD.gn b/build/config/compiler/BUILD.gn
index 325561a..35c5181 100644
--- a/build/config/compiler/BUILD.gn
+++ b/build/config/compiler/BUILD.gn
@@ -439,6 +439,18 @@
   }
 }
 
+config("coverage") {
+  cflags = [ "--coverage" ]
+  ldflags = cflags
+}
+
+config("coverage_default") {
+  configs = []
+  if (use_coverage) {
+    configs += [ ":coverage" ]
+  }
+}
+
 declare_args() {
   # Enable Runtime Type Information (RTTI)
   enable_rtti = false
diff --git a/build/config/compiler/compiler.gni b/build/config/compiler/compiler.gni
index 6e9f0d3..4d502a9 100644
--- a/build/config/compiler/compiler.gni
+++ b/build/config/compiler/compiler.gni
@@ -47,4 +47,7 @@
 
   # enable libfuzzer
   is_libfuzzer = false
+
+  # Generate code coverage analysis artifacts when enabled.
+  use_coverage = false
 }
diff --git a/build/config/defaults.gni b/build/config/defaults.gni
index a36b6f8..62e4257 100644
--- a/build/config/defaults.gni
+++ b/build/config/defaults.gni
@@ -79,6 +79,10 @@
   # Defaults fuzzing configs.
   default_configs_fuzzing = [ "${build_root}/config/compiler:fuzzing_default" ]
 
+  # Defaults coverage configs.
+  default_configs_coverage =
+      [ "${build_root}/config/compiler:coverage_default" ]
+
   # Defaults target-specific configs.
   default_configs_target = [ "${build_root}/config/compiler:target_default" ]
 
@@ -108,6 +112,7 @@
 default_configs += default_configs_aliasing
 default_configs += default_configs_sanitize
 default_configs += default_configs_fuzzing
+default_configs += default_configs_coverage
 default_configs += default_configs_target
 default_configs += default_configs_dead_code
 default_configs += default_configs_extra
diff --git a/scripts/build_coverage.sh b/scripts/build_coverage.sh
new file mode 100755
index 0000000..9beca4a
--- /dev/null
+++ b/scripts/build_coverage.sh
@@ -0,0 +1,79 @@
+#!/usr/bin/env bash
+
+#
+# Copyright (c) 2022 Project CHIP Authors
+#
+# 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.
+#
+
+set -e
+
+_normpath() {
+    python -c "import os.path; print(os.path.normpath('$@'))"
+}
+
+CHIP_ROOT=$(_normpath "$(dirname "$0")/..")
+OUTPUT_ROOT="$CHIP_ROOT/out/coverage"
+COVERAGE_ROOT="$OUTPUT_ROOT/coverage"
+skip_gn=false
+
+help() {
+
+    echo "Usage: $file_name [ options ... ]"
+
+    echo "General Options:
+  -h, --help                Display this information.
+Input Options:
+  -o, --output_root         Set the build output directory.  When set manually, performs only lcov stage
+                            on provided build output.  Assumes output_root has been built with 'use_coverage=true'
+                            and that 'ninja check' was run.
+  "
+}
+
+file_name=${0##*/}
+
+while (($#)); do
+    case $1 in
+        --help | -h)
+            help
+            exit 1
+            ;;
+        --output_root | -o)
+            OUTPUT_ROOT=$2
+            COVERAGE_ROOT="$OUTPUT_ROOT/coverage"
+            skip_gn=true
+            shift
+            ;;
+        -*)
+            help
+            echo "Unknown Option \"$1\""
+            exit 1
+            ;;
+    esac
+    shift
+done
+
+# Ensure we have a compilation environment
+source "$CHIP_ROOT/scripts/activate.sh"
+
+# Generates ninja files
+if [ "$skip_gn" == false ]; then
+    gn --root="$CHIP_ROOT" gen "$OUTPUT_ROOT" --args='use_coverage=true'
+    ninja -C "$OUTPUT_ROOT" check
+fi
+
+mkdir -p "$COVERAGE_ROOT"
+lcov --initial --capture --directory "$OUTPUT_ROOT/obj" --output-file "$COVERAGE_ROOT/lcov_base.info"
+lcov --capture --directory "$OUTPUT_ROOT"/obj --output-file "$COVERAGE_ROOT/lcov_test.info"
+lcov --add-tracefile "$COVERAGE_ROOT/lcov_base.info" --add-tracefile "$COVERAGE_ROOT/lcov_test.info" --output-file "$COVERAGE_ROOT/lcov_final.info"
+genhtml "$COVERAGE_ROOT/lcov_final.info" --output-directory "$COVERAGE_ROOT/html"