Add json merger tool

Add a simple script to merge two json files. It's necessary for creating
the sp_layout.json required by TF-A for using FIP SPs.

Signed-off-by: Balint Dobszay <balint.dobszay@arm.com>
Change-Id: I944770d263921efcbb12217db3df5170c59f8fec
diff --git a/tools/cmake/common/ExportSp.cmake b/tools/cmake/common/ExportSp.cmake
index eeac256..faa69fe 100644
--- a/tools/cmake/common/ExportSp.cmake
+++ b/tools/cmake/common/ExportSp.cmake
@@ -92,5 +92,11 @@
 	if (DEFINED EXPORT_JSON_IN)
 		configure_file(${EXPORT_JSON_IN} ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_NAME}.json @ONLY NEWLINE_STYLE UNIX)
 		install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_NAME}.json DESTINATION ${TS_ENV}/json)
+
+		find_package(Python3 REQUIRED COMPONENTS Interpreter)
+		execute_process(COMMAND ${Python3_EXECUTABLE} ${TS_ROOT}/tools/python/merge_json.py
+				${CMAKE_INSTALL_PREFIX}/${TS_ENV}/json/sp_layout.json
+				${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_SP_NAME}.json
+		)
 	endif()
 endfunction()
diff --git a/tools/python/merge_json.py b/tools/python/merge_json.py
new file mode 100644
index 0000000..68ad10b
--- /dev/null
+++ b/tools/python/merge_json.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: BSD-3-Clause
+#
+# Copyright (c) 2022, Arm Limited. All rights reserved.
+
+"""
+Merge two json files, the second is merged into the first. If the first file
+doesn't exists yet, it will be created, along with its parent directories.
+"""
+
+import json
+import os.path
+import sys
+
+if os.path.isfile(sys.argv[1]):
+    with open(sys.argv[1], "rt", encoding="ascii") as f:
+        combined = json.load(f)
+else:
+    os.makedirs(os.path.dirname(sys.argv[1]), exist_ok=True)
+    combined = {}
+
+with open(sys.argv[2], "rt", encoding="ascii") as f:
+    current = json.load(f)
+    combined = {**combined, **current}
+
+with open(sys.argv[1], "wt", encoding="ascii") as f:
+    json.dump(combined, f, indent=4)