fix(tools/cppcheck): fail build if errors detected

If the XML generated by Cppcheck contains error tags
detected by Cppcheck and the installed Cppcheck version
matches or exceeds the minimum recommended version, then
fail the build. Otherwise, if the installed version is
less than the recommended version, generate only a warning.

NOTE: The minimum recommended Cppcheck version is
now updated to 2.14.0.

Change-Id: Ibffbc555dcc6a7a1ad33a197a180fdb52a581412
Signed-off-by: Sona Mathew <sonarebecca.mathew@arm.com>
diff --git a/tools/cppcheck/CPPCheck.cmake b/tools/cppcheck/CPPCheck.cmake
index 688433c..12d1700 100644
--- a/tools/cppcheck/CPPCheck.cmake
+++ b/tools/cppcheck/CPPCheck.cmake
@@ -7,6 +7,18 @@
 
 if(NOT RMM_CPPCHECK_EXE)
   message(FATAL_ERROR "Could not find cppcheck executable.")
+else()
+  message(cppcheck_path: "${RMM_CPPCHECK_EXE}")
+  execute_process(COMMAND ${RMM_CPPCHECK_EXE} --version
+			OUTPUT_VARIABLE CPPCHECK_INSTALLED_VERSION)
+  string(REGEX MATCH "([0-9]+\\.[0-9]+(\\.[0-9]+)?)" CPPCHECK_INSTALLED ${CPPCHECK_INSTALLED_VERSION})
+  message(Installed version: "${CPPCHECK_INSTALLED}")
+  set(CPPCHECK_MIN_REQD "2.14.0")
+endif()
+
+if("${CPPCHECK_INSTALLED}" VERSION_LESS "${CPPCHECK_MIN_REQD}")
+  message(WARNING "Cppcheck installed version is: ${CPPCHECK_INSTALLED}, minimum required version is ${CPPCHECK_MIN_REQD}.")
+  message(WARNING "Cppcheck output will not be checked for errors.")
 endif()
 
 #
@@ -87,3 +99,15 @@
           --project=${COMPILE_COMMANDS_FILE} ${cppcheck-flags}
     )
 endif()
+
+if((EXISTS "${CPPCHECK_OUTPUT}") AND ("${CPPCHECK_INSTALLED}" VERSION_GREATER_EQUAL "${CPPCHECK_MIN_REQD}"))
+    file(READ "${CPPCHECK_OUTPUT}" cppcheck_xml)
+    string(REGEX MATCHALL "<error id" errtag "${cppcheck_xml}")
+    list(LENGTH errtag errcount)
+
+    if (${errcount} EQUAL 0)
+        message("Good work! No Cppcheck errors detected")
+    else()
+        message(FATAL_ERROR "Cppcheck failed with error count: ${errcount}")
+    endif()
+endif()