psa_autogen.py: improve management of output files

While at this, fix also Makefile so that "make clean" does not
complain if some of the files to be cancelled do not exist.

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
diff --git a/tests/psa-client-server/psasim/tools/psa_autogen.py b/tests/psa-client-server/psasim/tools/psa_autogen.py
index cece2b7..fbc9806 100755
--- a/tests/psa-client-server/psasim/tools/psa_autogen.py
+++ b/tests/psa-client-server/psasim/tools/psa_autogen.py
@@ -19,6 +19,10 @@
 GENERATED_H_PATH = os.path.join(SCRIPT_PATH, "..", "include", "psa_manifest")
 GENERATED_C_PATH = os.path.join(SCRIPT_PATH, "..", "src")
 
+MANIFEST_FILE = os.path.join(GENERATED_H_PATH, "manifest.h")
+PID_FILE = os.path.join(GENERATED_H_PATH, "pid.h")
+SID_FILE = os.path.join(GENERATED_H_PATH, "sid.h")
+
 with open(str(FILENAME), "r") as read_file:
     data = json.load(read_file)
     FILENAME = os.path.basename(FILENAME)
@@ -38,11 +42,11 @@
             os.mkdir(GENERATED_H_PATH)
             print("Generating psa_manifest directory")
         except OSError:
-            print ("PSA manifest directory already exists")
+            print("PSA manifest directory already exists")
 
-        man  = open(os.path.join(GENERATED_H_PATH, FILENAME + ".h"), "w")
-        pids = open(os.path.join(GENERATED_H_PATH, "pid.h"), "a")
-        sids = open(os.path.join(GENERATED_H_PATH, "sid.h"), "a")
+        manifest_content = []
+        pids_content = []
+        sids_content = []
 
         if len(services) > 28:
            print ("Unsupported number of services")
@@ -63,8 +67,8 @@
 
         # Go through all the services to make sid.h and pid.h
         for svc in services:
-            man.write("#define {}_SIGNAL    0x{:08x}\n".format(svc['signal'], 2**count))
-            sids.write("#define {}_SID    {}\n".format(svc['name'], svc['sid']))
+            manifest_content.append("#define {}_SIGNAL    0x{:08x}".format(svc['signal'], 2**count))
+            sids_content.append("#define {}_SID    {}".format(svc['name'], svc['sid']))
             qcode = qcode + "\"" + queue_path + str(int(svc['sid'], 16)) + "\","
             ns_clients = svc['non_secure_clients']
             print(str(svc))
@@ -94,7 +98,7 @@
         handlercode = "void __sig_handler(int signo) {\n"
         irqcount = count
         for irq in irqs:
-            man.write("#define {}    0x{:08x}\n".format(irq['signal'], 2**irqcount))
+            manifest_content.append("#define {}    0x{:08x}".format(irq['signal'], 2**irqcount))
             sigcode = sigcode + "    signal({}, __sig_handler);\n".format(irq['source'])
             handlercode = handlercode + \
                           "    if (signo == {}) {{ raise_signal(0x{:08x}); }};\n".format(irq['source'], 2**irqcount)
@@ -114,9 +118,12 @@
         versions = versions + "};\n"
         policy = policy + "};\n"
 
-        pids.close()
-        sids.close()
-        man.close()
+        with open(MANIFEST_FILE, "wt") as output:
+            output.write("\n".join(manifest_content))
+        with open(SID_FILE, "wt") as output:
+            output.write("\n".join(sids_content))
+        with open(PID_FILE, "wt") as output:
+            output.write("\n".join(pids_content))
 
         symbols = []
 
@@ -144,23 +151,24 @@
             print("Duplicate entrypoint symbol detected: " + str(symbols))
             sys.exit(2)
         else:
-            bs = open(os.path.join(GENERATED_C_PATH, "psa_ff_bootstrap_" + partition_name + ".c"),
-                      "w")
-            bs.write("#include <init.h>\n")
-            bs.write("#include \"" + symbols[0] + "\"\n")
-            bs.write("#include <signal.h>\n\n")
-            bs.write(qcode)
-            bs.write(nsacl)
-            bs.write(policy)
-            bs.write(versions)
-            bs.write("\n")
-            bs.write(handlercode)
-            bs.write("\n")
-            bs.write("int main(int argc, char *argv[]) {\n")
-            bs.write("    (void) argc;\n")
-            bs.write(sigcode)
-            bs.write("    __init_psasim(psa_queues, 32, ns_allowed, versions, strict_policy);\n")
-            bs.write("    " + entry_point + "(argc, argv);\n}\n")
-            bs.close()
+            C_FILENAME = os.path.join(GENERATED_C_PATH, "psa_ff_bootstrap_" + partition_name + ".c")
+            c_content = []
+            c_content.append("#include <init.h>")
+            c_content.append("#include \"" + symbols[0] + "\"")
+            c_content.append("#include <signal.h>")
+            c_content.append(qcode)
+            c_content.append(nsacl)
+            c_content.append(policy)
+            c_content.append(versions)
+            c_content.append(handlercode)
+            c_content.append("int main(int argc, char *argv[]) {")
+            c_content.append("    (void) argc;")
+            c_content.append(sigcode)
+            c_content.append("    __init_psasim(psa_queues, 32, ns_allowed, versions,"
+                             "strict_policy);")
+            c_content.append("    " + entry_point + "(argc, argv);")
+            c_content.append("}")
+            with open(C_FILENAME, "wt") as output:
+                output.write("\n".join(c_content))
 
             print("Success")