Merge pull request #257 from valeriosetti/issue598-framework

[framework] tests: pk: add a common function to create a PSA key out of predefined keys
diff --git a/scripts/all-core.sh b/scripts/all-core.sh
index a9070a8..e2fe2e1 100644
--- a/scripts/all-core.sh
+++ b/scripts/all-core.sh
@@ -369,6 +369,19 @@
 EOF
 }
 
+# list_git_files PATTERN...
+# List files known to git, matching pattern.
+# Equivalent to `git ls-files --recurse-submodules PATTERN...`, but
+# works with older Git (especially on Ubuntu 16.04) that understand
+# submodules but not `git ls-files --recurse-submodules`.
+list_git_files ()
+{
+    git ls-files -- "$@"
+    for d in $(git submodule status --recursive | awk '{print $2}'); do
+        git ls-files "$@" | sed "s!^!$d/!"
+    done
+}
+
 # Cleanup before/after running a component.
 # Remove built files as well as the cmake cache/config.
 # Does not remove generated source files.
@@ -378,25 +391,23 @@
         command $MAKE_COMMAND clean
     fi
 
-    # Remove CMake artefacts
-    find . -name .git -prune -o \
-           -iname CMakeFiles -exec rm -rf {} \+ -o \
-           \( -iname cmake_install.cmake -o \
-              -iname CTestTestfile.cmake -o \
-              -iname CMakeCache.txt -o \
-              -path './cmake/*.cmake' \) -exec rm -f {} \+
-    # Remove Makefiles generated by in-tree CMake builds
-    # (Not all files will exist in all branches, but that's OK.)
-    rm -f 3rdparty/Makefile 3rdparty/*/Makefile
-    rm -f pkgconfig/Makefile framework/Makefile
-    rm -f include/Makefile programs/!(fuzz)/Makefile
-    rm -f tf-psa-crypto/Makefile tf-psa-crypto/include/Makefile
-    rm -f tf-psa-crypto/core/Makefile tf-psa-crypto/drivers/Makefile
-    rm -f tf-psa-crypto/tests/Makefile
-    rm -f tf-psa-crypto/drivers/everest/Makefile
-    rm -f tf-psa-crypto/drivers/p256-m/Makefile
-    rm -f tf-psa-crypto/drivers/builtin/Makefile
-    rm -f tf-psa-crypto/drivers/builtin/src/Makefile
+    # Remove files left over by an in-tree CMake build.
+    # Take care to only hit in-tree builds, not out-of-tree builds in
+    # subdirectories.
+    # Remove **/Makefile only if it looks like it was created by an in-tree
+    # CMake build.
+    local cmakelists=($(list_git_files 'CMakeLists.txt' '**/CMakeLists.txt'))
+    for f in "${cmakelists[@]}"; do
+        local d="$(dirname -- "$f")"
+        if [ -d "$d/CMakeFiles" ]; then
+            rm -rf "$d/CMakeFiles" \
+               "$d/cmake_install.cmake" \
+               "$d/CTestTestfile.cmake" \
+               "$d/CMakeCache.txt" \
+               "$d/Makefile"
+            rm -rf "$d/cmake"/*.cmake
+        fi
+    done
 
     # Remove any artifacts from the component_test_cmake_as_subdirectory test.
     rm -rf programs/test/cmake_subproject/build
diff --git a/scripts/make_generated_files.py b/scripts/make_generated_files.py
index 73570fb..99a0512 100755
--- a/scripts/make_generated_files.py
+++ b/scripts/make_generated_files.py
@@ -27,13 +27,13 @@
     def __init__(self, script: Path, files: List[Path],
                  output_dir_option: Optional[str] = None,
                  output_file_option: Optional[str] = None,
-                 optional: bool = False):
+                 optional: bool = False) -> None:
         # Path from the root of Mbed TLS or TF-PSA-Crypto of the generation script
         self.script = script
 
         # Executable to run the script, needed for Windows
         if script.suffix == ".py":
-            self.exe = "python"
+            self.exe = sys.executable
         elif script.suffix == ".pl":
             self.exe = "perl"
 
@@ -55,14 +55,14 @@
         # consuming repository hasn't been updated yet.
         self.optional = optional
 
-def get_generation_script_files(generation_script: str):
+def get_generation_script_files(generation_script: str) -> List[Path]:
     """
     Get the list of the default paths of the files that a given script
     generates. It is assumed that the script supports the "--list" option.
     """
     files = []
     if generation_script.endswith(".py"):
-        cmd = ["python"]
+        cmd = [sys.executable]
     elif generation_script.endswith(".pl"):
         cmd = ["perl"]
     cmd += [generation_script, "--list"]
@@ -182,7 +182,7 @@
                 get_generation_script_files("scripts/generate_visualc_files.pl"),
                 "--directory", None))
 
-def get_generated_files(generation_scripts: List[GenerationScript]):
+def get_generated_files(generation_scripts: List[GenerationScript]) -> List[Path]:
     """
     List the generated files in Mbed TLS or TF-PSA-Crypto. The path from root
     is returned for each generated files.
@@ -193,7 +193,7 @@
 
     return files
 
-def make_generated_files(generation_scripts: List[GenerationScript]):
+def make_generated_files(generation_scripts: List[GenerationScript]) -> None:
     """
     Generate the configuration independent files in their default location in
     the Mbed TLS or TF-PSA-Crypto tree.
@@ -201,11 +201,13 @@
     for generation_script in generation_scripts:
         subprocess.run([generation_script.exe, str(generation_script.script)], check=True)
 
-def check_generated_files(generation_scripts: List[GenerationScript], root: Path):
+def check_generated_files(generation_scripts: List[GenerationScript],
+                          root: Path) -> bool:
     """
     Check that the given root directory contains the generated files as expected/
     generated by this script.
     """
+    ok = True
     for generation_script in generation_scripts:
         for file in generation_script.files:
             file = root / file
@@ -240,6 +242,7 @@
                 # there's nothing to compare to, or clean up.
                 continue
             if not filecmp.cmp(file, bak_file):
+                ok = False
                 ref_file = file.with_name(file.name + ".ref")
                 ref_file = root / ref_file
                 if ref_file.exists():
@@ -248,8 +251,9 @@
                 print(f"Generated file {file} not identical to the reference one {ref_file}.")
             file.unlink()
             bak_file.rename(file)
+    return ok
 
-def main():
+def main() -> int:
     """
     Main function of this program
     """
@@ -280,10 +284,13 @@
         files = get_generated_files(generation_scripts)
         for file in files:
             print(str(file))
+        return 0
     elif args.check:
-        check_generated_files(generation_scripts, Path(args.root or "."))
+        ok = check_generated_files(generation_scripts, Path(args.root or "."))
+        return 0 if ok else 1
     else:
         make_generated_files(generation_scripts)
+        return 0 # Any error causes an exception
 
 if __name__ == "__main__":
     sys.exit(main())