code-coverage: Add option for include only certain folders in report

Signed-off-by: Saul Romero <saul.romerodominguez@arm.com>
diff --git a/coverage-tool/coverage-reporting/merge.py b/coverage-tool/coverage-reporting/merge.py
index b9c7d24..5e220e6 100755
--- a/coverage-tool/coverage-reporting/merge.py
+++ b/coverage-tool/coverage-reporting/merge.py
@@ -1,6 +1,6 @@
 # !/usr/bin/env python
 ###############################################################################
-# Copyright (c) 2020, ARM Limited and Contributors. All rights reserved.
+# Copyright (c) 2020-2025, ARM Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 ###############################################################################
@@ -73,6 +73,9 @@
                            required=False)
 parser.add_argument("-j", "--json-file", action='append',
                     help="Input json file to be merged.")
+parser.add_argument("-i", "--include-only",
+                           help="File pattern (escaped) to extract coverage data "
+                                "for only a particular set of files from merged trace file.")
 parser.add_argument("-m", "--output-json",
                     help="Name of the output json (merged) file.")
 parser.add_argument("--force", dest='force', action='store_true',
@@ -195,17 +198,31 @@
 
 
 # Exploit LCOV merging capabilities
-# Example of LCOV usage: lcov -rc lcov_branch_coverage=1 -a coverage_1.info \
+# Example of LCOV usage: lcov --rc lcov_branch_coverage=1 -a coverage_1.info \
 # -a coverage_2.info -o coverage_merge.info
-command = ['lcov', '--rc', 'lcov_branch_coverage=1']
+command = ['lcov', '--rc', 'lcov_branch_coverage=1', '--ignore-errors', 'format,format', '--ignore-errors',\
+           'inconsistent,inconsistent', '--ignore-errors', 'unsupported,unsupported']
 
 for file_name in info_files_to_merge:
     command.append('-a')
     command.append(file_name)
 command.append('-o')
-command.append(options.output)
-
-subprocess.call(command)
+if options.include_only:
+    temp_file = options.output + ".temp"
+    command.append(temp_file)
+    print(f"Command to merge reports: {' '.join(command)}\n")
+    subprocess.call(command)
+    command_include = ['lcov', '--rc', 'lcov_branch_coverage=1', '-e', temp_file, options.include_only, '-o', options.output, \
+                       '--ignore-errors', 'inconsistent', '--ignore-errors', 'unsupported,unsupported', '--ignore-errors', 'format']
+    print(f"Command to include files/folders: {' '.join(command_include)}\n")
+    try:
+        output = subprocess.check_output(command_include, stderr=subprocess.STDOUT)
+        print(f"Output={output.decode('ascii')}")
+    except subprocess.CalledProcessError as e:
+        print(f"Command failed with return code {e.returncode}")
+else:
+    command.append(options.output)
+    subprocess.call(command)
 
 # Delete the temporary files
 if options.local_workspace is not None and not options.keep_trans:
diff --git a/coverage-tool/coverage-reporting/merge.sh b/coverage-tool/coverage-reporting/merge.sh
index a3a3113..4a4d86e 100755
--- a/coverage-tool/coverage-reporting/merge.sh
+++ b/coverage-tool/coverage-reporting/merge.sh
@@ -257,6 +257,7 @@
 #   generate_local: Flag to generate local lcov reports
 #   info_files: Array of locations and names of info files
 #   json_files: Array of locations and names of json files
+#   include_only_pattern: Pattern string to include code coverage files
 # Arguments:
 #   1: Location where reside json and info files
 # Outputs:
@@ -280,9 +281,10 @@
   python3 "${DIR}"/merge.py \
       ${info_files[@]/#/-a } \
       ${json_files[@]/#/-j } \
-      -o "$merged_coverage_file" \
-      -m "$merged_json_file" \
-      $lc
+      -o $merged_coverage_file \
+      -m $merged_json_file \
+      $([ -n "$include_only_pattern" ] && echo " -i $include_only_pattern") \
+      $lc 
 
 }
 
@@ -386,6 +388,7 @@
   echo "[-m <JSON filename>]  JSON merged SCM sources. Defaults to ./merged_scm.json"
   echo "[-c]                  Flag to download/copy the source files from the JSON merged SCM into the workspace directory."
   echo "[-g]                  Flag to generate local reports for each info/json instance."
+  echo "[-e <Pattern>]        Pattern (escaped) to include only certain files/dirs in the merged coverage report. Optional"
   echo "[-i]                  Ignore errors on genhtml."
   echo "[-d]                  Enable debug mode for the script."
   echo "$help_message"
@@ -401,6 +404,7 @@
 # File name for merge coverage info
 merged_coverage_file="./merged_coverage.info"
 merged_json_file="./merged_scm.json"
+include_only_pattern=""
 info_files=() # Array of info files
 json_files=() # Array of configuration json files
 list_of_merged_builds=()
@@ -410,7 +414,8 @@
 gen_major=$(echo "$genhtml_version" | cut -d '.' -f1)
 # gen_minor=$(echo "$genhtml_version" | rev | cut -d '.' -f1 | rev)
 unset OPTIND
-while getopts ":hj:o:l:w:idcm:g" opt; do
+while getopts ":hj:o:l:w:idcm:ge:" opt; do
+  echo "Opt=$opt"
   case ${opt} in
     h )
       # shellcheck disable=SC2119
@@ -444,6 +449,9 @@
     g )
       generate_local=true
       ;;
+    e )
+      include_only_pattern=$OPTARG
+      ;;
     \? )
       echo "Invalid option: $OPTARG" 1>&2
       # shellcheck disable=SC2119