Sync static check scripts with internal CI

Sync static check files with platform-ci commit:
539c151d0cd99a5e6ca6c0e6966f6d8579fe864e

Signed-off-by: Zelalem <zelalem.aweke@arm.com>
Change-Id: I5b6b350398a447f32a997c3f2f68baad0e53f04c
diff --git a/script/static-checks/check-include-order.py b/script/static-checks/check-include-order.py
index 4f605f3..aaf84f1 100755
--- a/script/static-checks/check-include-order.py
+++ b/script/static-checks/check-include-order.py
@@ -8,6 +8,7 @@
 import argparse
 import codecs
 import collections
+import functools
 import os
 import re
 import subprocess
@@ -56,6 +57,20 @@
         return None
 
 
+@functools.lru_cache()
+def dir_include_paths(directory):
+    """Generate a set that contains all includes from a directory"""
+    dir_includes = set()
+    for (root, _dirs, files) in os.walk(directory):
+        for fname in files:
+            if fname.endswith(".h"):
+                names = os.path.join(root, fname).split(os.sep)
+                for i in range(len(names)):
+                    suffix_path = "/".join(names[i:])
+                    dir_includes.add(suffix_path)
+    return dir_includes
+
+
 def inc_order_is_correct(inc_list, path, commit_hash=""):
     """Returns true if the provided list is in order. If not, output error
     messages to stdout."""
@@ -67,37 +82,37 @@
     if commit_hash != "":
         commit_hash = commit_hash + ":"
 
-    # Get list of system includes from libc include directory.
-    libc_incs = [f for f in os.listdir("include/lib/libc") if f.endswith(".h")]
-
     # First, check if all includes are in the appropriate group.
     inc_group = "System"
     incs = collections.defaultdict(list)
     error_msgs = []
+    plat_incs = dir_include_paths("plat") | dir_include_paths("include/plat")
+    libc_incs = dir_include_paths("include/lib/libc")
 
     for inc in inc_list:
-        if inc[1:-1] in libc_incs:
+        inc_path = inc[1:-1]
+        if inc_path in libc_incs:
             if inc_group != "System":
-                error_msgs.append(inc[1:-1] + " should be in system group, at the top")
-        elif (
-            "plat/" in inc
-            or "platform" in inc
-            or (inc.startswith('"') and "plat" in path)
-        ):
+                error_msgs.append(inc_path + " should be in system group, at the top")
+        elif inc_path in plat_incs:
             inc_group = "Platform"
         elif inc_group in ("Project", "System"):
             inc_group = "Project"
         else:
             error_msgs.append(
-                inc[1:-1] + " should be in project group, after system group"
+                inc_path + " should be in project group, after system group"
             )
-        incs[inc_group].append(inc[1:-1])
+        incs[inc_group].append(inc_path)
 
     # Then, check alphabetic order (system, project and user separately).
     if not error_msgs:
         for name, inc_list in incs.items():
             if sorted(inc_list) != inc_list:
-                error_msgs.append("{} includes not in order.".format(name))
+                error_msgs.append(
+                    "{} includes not in order. Include order should be {}".format(
+                        name, ", ".join(sorted(inc_list))
+                    )
+                )
 
     # Output error messages.
     if error_msgs:
diff --git a/script/static-checks/static-checks-coding-style-line-endings.sh b/script/static-checks/static-checks-coding-style-line-endings.sh
index 5442f7d..ae7a6db 100755
--- a/script/static-checks/static-checks-coding-style-line-endings.sh
+++ b/script/static-checks/static-checks-coding-style-line-endings.sh
@@ -7,46 +7,43 @@
 
 TEST_CASE="Line endings not valid"
 
-EXIT_VALUE=0
-
 echo "# Check Line Endings"
 
-LOG_FILE=$(mktemp -t common.XXXX)
+LOG_FILE=`mktemp -t common.XXXX`
 
-if [[ "$2" == "patch" ]]; then
-    cd "$1"
-    parent=$(git merge-base HEAD master | head -1)
-    git diff ${parent}..HEAD --no-ext-diff --unified=0 --exit-code -a --no-prefix | grep -E "^\+" | \
-    grep --files-with-matches $'\r$' &> "$LOG_FILE"
-else
-  # For all the source and doc files
-  # We only return the files that contain CRLF
-  find "." -\( \
-      -name '*.S' -or \
-      -name '*.c' -or \
-      -name '*.h' -or \
-      -name '*.i' -or \
-      -name '*.dts' -or \
-      -name '*.dtsi' -or \
-      -name '*.rst' -or \
-      -name 'Makefile' -or \
-      -name '*.mk' \
-  -\) -exec grep --files-with-matches $'\r$' {} \; &> "$LOG_FILE"
-fi
+# For all the source and doc files
+# We only return the files that contain CRLF
+find "." -\( \
+    -name '*.S' -or \
+    -name '*.c' -or \
+    -name '*.h' -or \
+    -name '*.i' -or \
+    -name '*.dts' -or \
+    -name '*.dtsi' -or \
+    -name '*.rst' -or \
+    -name 'Makefile' -or \
+    -name '*.mk' \
+-\) -exec grep --files-with-matches $'\r$' {} \; &> "$LOG_FILE"
 
 if [[ -s "$LOG_FILE" ]]; then
-    EXIT_VALUE=1
+  EXIT_VALUE=1
+else
+  EXIT_VALUE=0
 fi
 
-{ echo; echo "****** $TEST_CASE ******"; echo; } >> "$LOG_TEST_FILENAME"
-
-{ if [[ "$EXIT_VALUE" == 0 ]]; then \
-      echo "Result : SUCCESS"; \
-  else  \
-      echo "Result : FAILURE"; echo; cat "$LOG_FILE"; \
-  fi \
-} | tee -a "$LOG_TEST_FILENAME"
+echo >> "$LOG_TEST_FILENAME"
+echo "****** $TEST_CASE ******" >> "$LOG_TEST_FILENAME"
+echo >> "$LOG_TEST_FILENAME"
+if [[ "$EXIT_VALUE" == 0 ]]; then
+  echo "Result : SUCCESS" >> "$LOG_TEST_FILENAME"
+else
+  echo "Result : FAILURE" >> "$LOG_TEST_FILENAME"
+  echo >> "$LOG_TEST_FILENAME"
+  cat "$LOG_FILE" >> "$LOG_TEST_FILENAME"
+fi
+echo >> "$LOG_TEST_FILENAME"
 
 rm "$LOG_FILE"
 
 exit "$EXIT_VALUE"
+
diff --git a/script/static-checks/static-checks-cppcheck.sh b/script/static-checks/static-checks-cppcheck.sh
index 40d161f..7f5ed8f 100755
--- a/script/static-checks/static-checks-cppcheck.sh
+++ b/script/static-checks/static-checks-cppcheck.sh
@@ -11,8 +11,6 @@
 
 TF_BASE="$(pwd)"
 
-export LOG_TEST_FILENAME=$(pwd)/static-checks-cppcheck.log
-
 # cppcheck configuration
 COMMON_ARGS=(-j 16 -q -f --std=c99 --error-exitcode=1 --relative-paths="$TF_BASE")
 CHECKS_ARGS=(--enable=warning,style,portability)
diff --git a/script/static-checks/static-checks.sh b/script/static-checks/static-checks.sh
index 882e9e1..6bae729 100755
--- a/script/static-checks/static-checks.sh
+++ b/script/static-checks/static-checks.sh
@@ -61,12 +61,7 @@
 
 # Check line endings
 
-if [ "$IS_CONTINUOUS_INTEGRATION" == 1 ]; then
-    "$CI_ROOT"/script/static-checks/static-checks-coding-style-line-endings.sh . patch
-else
-    "$CI_ROOT"/script/static-checks/static-checks-coding-style-line-endings.sh
-fi
-
+"$CI_ROOT"/script/static-checks/static-checks-coding-style-line-endings.sh
 if [ "$?" != 0 ]; then
   echo "Line ending test: FAILURE"
   ((ERROR_COUNT++))