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/docs/index.rst.in b/docs/index.rst.in
index e2151a4..8142efa 100644
--- a/docs/index.rst.in
+++ b/docs/index.rst.in
@@ -21,6 +21,7 @@
     docs/dco
     glossary
     maintainers
+    docs/lic
     docs/user_guides/tfm_sw_requirement
     docs/user_guides/tfm_build_instruction
     docs/coding_guide
diff --git a/docs/lic.rst b/docs/lic.rst
new file mode 100644
index 0000000..9c67c52
--- /dev/null
+++ b/docs/lic.rst
@@ -0,0 +1,12 @@
+
+###############################
+Trusted Firmware-M License
+###############################
+
+.. include:: /license.rst
+   :literal:
+
+
+-----------
+
+*Copyright (c) 2019, Arm Limited. All rights reserved.*
diff --git a/readthedocs/conf.py b/readthedocs/conf.py
new file mode 100644
index 0000000..f733209
--- /dev/null
+++ b/readthedocs/conf.py
@@ -0,0 +1,183 @@
+# -*- coding: utf-8 -*-
+#-------------------------------------------------------------------------------
+# Copyright (c) 2019, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------#
+
+# Configuration file for the Sphinx documentation builder.
+#
+# This file does only contain a selection of the most common options. For a
+# full list see the documentation:
+# http://www.sphinx-doc.org/en/master/config
+
+# -- Path setup --------------------------------------------------------------
+
+# If extensions (or modules to document with autodoc) are in another directory,
+# add these directories to sys.path here. If the directory is relative to the
+# documentation root, use os.path.abspath to make it absolute, like shown here.
+#
+import os
+import sys
+sys.path.insert(0, os.path.abspath('./'))
+import tfm_copy_files
+
+
+# -- Project information -----------------------------------------------------
+
+project = 'TF-M'
+copyright = '2017-2019, ARM CE-OSS'
+author = 'ARM CE-OSS'
+title = 'TF-M user Guide'
+
+# The short X.Y version
+version = '1.0.0-Beta'
+# The full version, including alpha/beta/rc tags
+release = 'Version 1.0.0-Beta'
+
+
+# -- General configuration ---------------------------------------------------
+
+# If your documentation needs a minimal Sphinx version, state it here.
+#
+# needs_sphinx = '1.4'
+
+# Add any Sphinx extension module names here, as strings. They can be
+# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
+# ones.
+extensions = [
+    'sphinx.ext.imgmath',
+    'm2r', #Support markdown files. Needed for external code.
+    'sphinx.ext.autosectionlabel', #Make sphinx generate a label for each section
+    'sphinxcontrib.plantuml' #Add support for PlantUML drawings
+]
+
+#Location of PlantUML
+plantuml = '/usr/bin/java -jar /usr/share/plantuml/plantuml.jar'
+
+#Make auso section labals generated be prefixed with file name.
+autosectionlabel_prefix_document=True
+#Add auso section label for level 1 headers only.
+autosectionlabel_maxdepth=1
+
+# Add any paths that contain templates here, relative to this directory.
+templates_path = ['_templates']
+
+# The suffix(es) of source filenames.
+# You can specify multiple suffix as a list of string:
+#
+source_suffix = ['.rst', '.md']
+
+# The master toctree document.
+master_doc = 'index'
+
+# The language for content autogenerated by Sphinx. Refer to documentation
+# for a list of supported languages.
+#
+# This is also used if you do content translation via gettext catalogs.
+# Usually you set "language" from the command line for these cases.
+language = None
+
+# List of patterns, relative to source directory, that match files and
+# directories to ignore when looking for source files.
+# This pattern also affects html_static_path and html_extra_path .
+exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
+
+# The name of the Pygments (syntax highlighting) style to use.
+pygments_style = 'sphinx'
+
+
+# -- Options for HTML output -------------------------------------------------
+
+# The theme to use for HTML and HTML Help pages.  See the documentation for
+# a list of builtin themes.
+#
+html_theme = 'sphinx_rtd_theme'
+
+# Theme options are theme-specific and customize the look and feel of a theme
+# further.  For a list of options available for each theme, see the
+# documentation.
+#
+# html_theme_options = {}
+#
+# Add any paths that contain custom static files (such as style sheets) here,
+# relative to this directory. They are copied after the builtin static files,
+# so a file named "default.css" will overwrite the builtin "default.css".
+html_static_path = ['../docs/_static']
+
+# Custom sidebar templates, must be a dictionary that maps document names
+# to template names.
+#
+# The default sidebars (for documents that don't match any pattern) are
+# defined by theme itself.  Builtin themes are using these templates by
+# default: ``['localtoc.html', 'relations.html', 'sourcelink.html',
+# 'searchbox.html']``.
+#
+# html_sidebars = {}
+
+#Disable adding conf.py copyright notice to HTML output
+html_show_copyright = False
+
+#Add custom css for HTML. Used to allow full page width rendering
+def setup(app):
+    app.add_stylesheet('css/custom.css')
+
+# -- Options for HTMLHelp output ---------------------------------------------
+
+# Output file base name for HTML help builder.
+htmlhelp_basename = 'TF-M doc'
+
+
+# -- Options for LaTeX output ------------------------------------------------
+
+latex_elements = {
+    # The paper size ('letterpaper' or 'a4paper').
+    #
+    # 'papersize': 'letterpaper',
+
+    # The font size ('10pt', '11pt' or '12pt').
+    #
+    # 'pointsize': '10pt',
+
+    # Additional stuff for the LaTeX preamble.
+    #
+    # 'preamble': '',
+
+    # Latex figure (float) alignment
+    #
+    # 'figure_align': 'htbp',
+}
+
+# Grouping the document tree into LaTeX files. List of tuples
+# (source start file, target name, title,
+#  author, documentclass [howto, manual, or own class]).
+latex_documents = [
+    (master_doc, 'TF-M.tex', title,
+     author, 'manual'),
+]
+
+
+# -- Options for manual page output ------------------------------------------
+
+# One entry per manual page. List of tuples
+# (source start file, name, description, authors, manual section).
+#man_pages = [
+#    (master_doc, 'tf-m', title,
+#     [author], 7)
+#]
+
+
+# -- Options for Texinfo output ----------------------------------------------
+
+# Grouping the document tree into Texinfo files. List of tuples
+# (source start file, target name, title, author,
+#  dir menu entry, description, category)
+#texinfo_documents = [
+#    (master_doc, 'TF-M', title,
+#     author, 'TF-M', 'Trusted Firmware for Cortex-M',
+#     'Miscellaneous'),
+#]
+
+
+# -- Extension configuration -------------------------------------------------
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)