Open CI Scripts: Feature Update

    * build_helper: Added --install argument to execute cmake install
    * build_helper: Added the capability to parse axf files for
      code/data/bss sizes and capture it to report
    * build_helper: Added --relative-paths to calculate paths relative
      to the root of the workspace
    * build_helper_configs: Full restructure of config modules.
      Extra build commands and expected artefacts can be defined per
      platform basis
    * Checkpatch: Added directive to ignore --ignore SPDX_LICENSE_TAG
      and added the capability to run only on files changed in patch.
    * CppCheck adjusted suppression directories for new external
      libraries and code-base restructure
    * Added fastmodel dispatcher. It will wrap around fastmodels
      and test against a dynamically defined test_map. Fed with an
      input of the build summary fastmodel dispatcher will detect
      builds which have tests in the map and run them.
    * Added Fastmodel configs for AN519 and AN521 platforms
    * lava_helper. Added arguments for --override-jenkins-job/
      --override-jenkins-url
    * Adjusted JINJA2 template to include build number and
      enable the overrides.
    * Adjusted lava helper configs to support dual platform firmware
      and added CoreIPC config
    * Added report parser module to create/read/evaluate and
      modify reports. Bash scripts for cppcheck checkpatch summaries
      have been removed.
    * Adjusted run_cppcheck/run_checkpatch for new project libraries,
      new codebase structure and other tweaks.
    * Restructured build manager, decoupling it from the tf-m
      cmake requirements. Build manager can now dynamically build a
      configuration from combination of parameters or can just execute
      an array of build commands. Hardcoded tf-m assumptions have been
      removed and moved into the configuration space.
    * Build system can now produce MUSCA_A/ MUSCA_B1 binaries as well
      as intel HEX files.
    * Updated the utilities snippet collection in the tfm-ci-pylib.

Change-Id: Ifad7676e1cd47e3418e851b56dbb71963d85cd88
Signed-off-by: Minos Galanakis <minos.galanakis@linaro.org>
diff --git a/run-cppcheck.sh b/run-cppcheck.sh
index cc0abd4..467e5e5 100755
--- a/run-cppcheck.sh
+++ b/run-cppcheck.sh
@@ -46,6 +46,9 @@
 #The location from where the script executes
 mypath=$(dirname $0)
 
+#The cmake_exported project file in json format
+cmake_commmands=compile_commands.json
+
 . "$mypath/util_cmake.sh"
 
 
@@ -53,31 +56,94 @@
 library_file="$(fix_win_path $(get_full_path $mypath))/cppcheck/arm-cortex-m.cfg"
 suppress_file="$(fix_win_path $(get_full_path $mypath))/cppcheck/tfm-suppress-list.txt"
 
+#Enable all additional checks by default
+additional_checklist="all"
+
 #Run cmake to get the compile_commands.json file
 echo
 echo '******* Generating compile_commandas.json ***************'
 echo
 generate_project $(fix_win_path $(get_full_path ./)) "./" "cppcheck" "-DCMAKE_EXPORT_COMPILE_COMMANDS=1  -DTARGET_PLATFORM=AN521 -DCOMPILER=GNUARM"
+
 #Enter the build directory
 bdir=$(make_build_dir_name "./" "cppcheck")
 pushd "$bdir" >/dev/null
+
+#The following snippet allows cppcheck to be run differentially againist a
+#commit hash passed as first argument $1. It does not
+#affect the legacy functionality of the script, checking the whole codebase,
+#when called without an argument
+if [[ ! -z "$1" ]]
+  then
+    echo "Enabled git-diff mode againist hash:  $1"
+
+    # Do not execute unused functioncheck when running in diff-mode
+    additional_checklist="style,performance,portability,information,missingInclude"
+    # Grep will set exit status to 1 if a commit does not contain c/cpp.. files
+    set +e
+    filtered_cmd_f=compile_commands_filtered.json
+    # Get a list of files modified by the commits between the reference and HEAD
+    flist=$(git diff-tree --no-commit-id --name-only -r $1 | grep -E '\S*\.(c|cpp|cc|cxx|inc|h)$')
+    flist=$(echo $flist | xargs)
+    echo -e "[" > $filtered_cmd_f
+    IFS=$' ' read -ra git_flist <<< "${flist}"
+
+    for fl in "${git_flist[@]}"; do
+        echo "Looking for reference of file: $fl"
+
+        # dry run the command to see if there any ouput
+        JSON_CMD=$(grep -B 3 "\"file\": \".*$fl\"" $cmake_commmands)
+
+        if [ -n "${JSON_CMD}" ]; then
+            command_matched=1
+            grep -B 3 "\"file\": \".*$fl\"" $cmake_commmands >> $filtered_cmd_f
+            echo -e "}," >> $filtered_cmd_f
+        fi
+    done
+    set -e
+
+    # Only continue if files in the patch are included in the build commands
+    if [ -n "${command_matched}" ]; then
+        sed -i '$ d' $filtered_cmd_f
+        echo -e "}\n]" >> $filtered_cmd_f
+
+        cat $filtered_cmd_f > $cmake_commmands
+    else
+        # Always generate an empty file for other stages of ci expecting one
+        echo "CppCheck: Ignoring files not contained in the build config"
+        echo "Files Ignored: $flist"
+				cat <<-EOF > chk-config.xml
+				<?xml version="1.0" encoding="UTF-8"?>
+				<results version="2">
+				    <cppcheck version="$(cppcheck --version)"/>
+				    <errors>
+				    </errors>
+				</results>
+				EOF
+        cp chk-config.xml chk-src.xml
+        exit 0
+    fi
+fi
+
+
 #Build the external projects to get all headers installed to plases from where
 #tf-m code uses them
 echo
 echo '******* Install external projects to their final place ***************'
 echo
-make -j mbedcrypto_lib_install mbedtls_mcuboot_lib_install
+make -j mbedtls_mcuboot_lib_install
 
 #Now run cppcheck.
 echo
 echo '******* checking cppcheck configuration ***************'
 echo
-cppcheck --xml -j 4 --check-config --enable=all --library="$library_file" --project=compile_commands.json --suppressions-list="$suppress_file" --inline-suppr 2>chk-config.xml
+
+cppcheck --xml --check-config --enable="$additional_checklist" --library="$library_file" --project=$cmake_commmands --suppressions-list="$suppress_file" --inline-suppr 2>chk-config.xml
 
 echo
 echo '******* analyzing files with cppcheck ***************'
 echo
-cppcheck --xml -j 4 --enable=all --library="$library_file" --project=compile_commands.json --suppressions-list="$suppress_file" --inline-suppr 2>chk-src.xml
+cppcheck --xml --enable="$additional_checklist" --library="$library_file" --project=$cmake_commmands --suppressions-list="$suppress_file" --inline-suppr 2>chk-src.xml
 popd
 
 echo