Doc: Enabling sphinx-build

sphinx-build relies on the assumption that all files
lie under a common directory. TF-M's build system
uses CMAKE (./cmake/SphinxCopyDoc.cmake)to fetch all
files to an intermediate directory and invoke
sphinx-build.

This patch creates a python interface which achieves
similar behaviour by injecting the copy function
before sphinx-build starts processing the conf.py
configuration file. By doing so it provides
compatibility with third-party documentation providers,
without affecting the existing user experience.

The patch also includes an include directive to
fetch the licence and build it into the documentation.

Change-Id: I47ead73df4f02c2fe49866a62f551a4510881778
Signed-off-by: Galanakis, Minos <minos.galanakis@arm.com>
diff --git a/readthedocs/tfm_copy_files.py b/readthedocs/tfm_copy_files.py
new file mode 100644
index 0000000..360b1dc
--- /dev/null
+++ b/readthedocs/tfm_copy_files.py
@@ -0,0 +1,65 @@
+# -*- coding: utf-8 -*-
+#-------------------------------------------------------------------------------
+# Copyright (c) 2019, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#------------------------------------------------------------------------------#
+
+# Interface module for alligning project's Sphinx Compatibility with
+# readthedocs service
+
+#
+# This file is mimicing the behavior of tf-m/cmake/SphinxCopyDoc.cmake
+# which when triggered by CMAKE collects all documents in a intermediate
+# location before calling sphinx-build.
+
+# It can be triggered by simply importing the module will should produce a
+# an identical output, while retaining standard Sphinx behavior.
+
+
+import os
+import re
+from shutil import copy2
+from glob import glob
+
+# Determine absolute paths for tf-m project and current directory
+read_the_doc_root = os.path.dirname(os.path.abspath(__file__))
+tfm_root = os.path.dirname(read_the_doc_root)
+doc_root = os.path.join(tfm_root, "docs")
+
+doc_files = []
+
+# Recursively list all files with extensions and add them
+for ext in [".rst", ".md", ".txt", ".png", ".jpg"]:
+    doc_files.extend([f for f in glob(os.path.join(tfm_root, "**/*%s" % ext),
+                      recursive=True)])
+
+# Do not add files from this folder
+doc_files = filter(lambda x: read_the_doc_root not in x, doc_files)
+
+for df in list(doc_files):
+
+    # Set the target filename to be cwd + relative to root path of origin
+    target_f = df.replace(tfm_root, "").lstrip("/")
+    target_f = os.path.join(os.getcwd(), target_f)
+    # Create path for file (nested) without exception if exists
+    os.makedirs(os.path.dirname(target_f), exist_ok=True)
+
+    # Copy the file to new location
+    print("Copying %s %s -> %s" % (df, " " * (90 - len(df)), target_f))
+    copy2(df, target_f)
+
+index_f_origin = os.path.join(doc_root, "index.rst.in")
+index_f = os.path.join(read_the_doc_root, "index.rst")
+
+
+# Copy the index from docs directory and strip the CMAKE variable references
+copy2(index_f_origin, index_f)
+
+with open(index_f, "r") as F:
+    index_data = F.read()
+    index_data = re.sub(r'@[A-Z\_]+@', "", index_data)
+
+with open(index_f, "w") as F:
+    F.write(index_data)