blob: e81d5fb6929a0d99588b842afbde5c8c0ba61325 [file] [log] [blame]
Minos Galanakisd19a19f2020-06-03 15:38:03 +01001# -----------------------------------------------------------------------------
Minos Galanakis7bd5b912021-01-12 13:08:21 +00002# Copyright (c) 2020-2021, Arm Limited. All rights reserved.
Minos Galanakisd19a19f2020-06-03 15:38:03 +01003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6# -----------------------------------------------------------------------------
7
8# This module is providing the default parameters for manual Documentation
9# building. ( Without relying on CMake to determine the enviroment )
10#
11# It will however be able to communicate parameters when populated
12# by CMake (using the tfm_env.py file), and use a best-effort approach
13# to determine them when the interface file is not preset.
14
15import os
16import re
17import json
18from subprocess import check_output
19from platform import system
20
21# When called after cmake an evniroment variables file will be present
22try:
23 from tfm_env import cmake_env
24except Exception as E:
25 print("ERROR: Configuration Exception:", E)
26 cmake_env = None
27
28tfm_def_render_cmake = True
29tfm_def_copy_files = True
30tfm_def_build_doxygen = True
31
32
33def find_tfm_root(start_dir=os.path.dirname(os.path.abspath(__file__)),
34 target_files=["license.rst",
35 "dco.txt",
Minos Galanakisb8638642021-01-15 13:40:14 +000036 "toolchain_GNUARM.cmake"],
Minos Galanakisd19a19f2020-06-03 15:38:03 +010037 max_depth=5):
38 """ Method which attempts to find the root of the project
39 by traversing parent directoried and attempts to located each of the
40 files included in target_files list"""
41
42 tfm_root = start_dir
43
44 for i in range(max_depth):
45 tfm_root = os.path.dirname(tfm_root)
46 if set(target_files).issubset(set(os.listdir(tfm_root))):
47 return tfm_root
48 return None
49
50
51def find_package(binary_name):
52 """ Attempts to resolve the abolute path for a given application or return
53 empty string is nothing is found"""
54
55 sys_det = system()
56
57 if sys_det == "Windows":
58 cmd = "where"
59 # Window's where requires the extension
60 binary_name = binary_name + ".exe"
61 elif sys_det in ["Darwin", "Linux"]:
62 cmd = "which"
63 try:
64 return check_output([cmd, binary_name]).decode('UTF-8').strip()
65 except Exception as E:
66 return ""
67
68
69def render_cmake_file(config_map, in_file, out_file):
70 """ Read an input file containing CMAKE variables and try to
71 render them based on a configuration map. Variables not listed
72 on the map will be cleared """
73
74 # Read the input file
75 with open(in_file, "r", encoding='utf-8') as F:
76 _data = F.read()
77
78 # Render all config entires included in the map
79 for k, v in config_map.items():
80 v = v.replace("\\", "\\\\")
81 _data = re.sub(r'@%s@' % k, r'%s' % v, _data)
82
83 # Set all remaining entries to blank
84 _data = re.sub(r'@[A-Z\_]+@', "", _data)
85
86 # Create output file
87 with open(out_file, "w", encoding='utf-8') as F:
88 F.write(_data)
89
90
91# Default output director for reference_manual. It should not be empty
92tfm_def_doxy_output_dir = "reference_manual"
93
94
95if cmake_env is None:
96 # #################### Automatic Defaults ( Standalone )################# #
97
98 # Resolve ../../
99 tfm_def_root_dir = find_tfm_root()
100
101 # Set the copy files directory to whatever will be passed to sphynx-build
102 tfm_def_copy_dir = os.path.abspath(os.getcwd())
103
104 # Documentation base path
105 tfm_def_doc_root = os.path.join(tfm_def_root_dir, "docs")
106 tfm_def_copy_doc_root = os.path.join(tfm_def_copy_dir, "docs")
107
Anton Komlev4c436bf2021-10-18 21:59:55 +0100108 tfm_def_doxy_root = os.path.join(tfm_def_doc_root, "doxygen")
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100109
110 tfm_def_doxy_output_dir = os.path.join(tfm_def_copy_dir,
111 tfm_def_doxy_output_dir)
112
113 # Input files ( Files containing CMAKE variables )
114 tfm_def_conf_in_file = os.path.join(tfm_def_doc_root, "conf.py.in")
115 tfm_def_doxygen_in_file = os.path.join(tfm_def_doxy_root, "Doxyfile.in")
116
117 # Attempt to detect plantUML
118 _ubuntu_plantum_loc = "/usr/share/plantuml/plantuml.jar"
119 if "PLANTUML_JAR_PATH" in os.environ.keys():
120 tfm_def_plantum_loc = os.environ["PLANTUML_JAR_PATH"]
121 elif os.path.isfile(_ubuntu_plantum_loc):
122 tfm_def_plantum_loc = _ubuntu_plantum_loc
123 else:
124 tfm_def_plantum_loc = ""
125
126 # Attempt to detect the java interpreter location
127 tfm_def_java_binary = find_package("java")
128 tfm_def_doxygen_loc = find_package("doxygen")
129 tfm_def_doxygen_dot_loc = find_package("dot")
130
131else:
132 # #################### Cmake Defaults ################################## #
133 tfm_def_root_dir = os.path.abspath(cmake_env["TFM_ROOT_DIR"])
134 tfm_def_copy_dir = os.path.abspath(cmake_env["SPHINX_TMP_DOC_DIR"])
135 tfm_def_plantum_loc = os.path.abspath(cmake_env["PLANTUML_JAR_PATH"])
136 tfm_def_java_binary = os.path.abspath(cmake_env["Java_JAVA_EXECUTABLE"])
137 tfm_def_tfm_ver_shrt = cmake_env["SPHINXCFG_TFM_VERSION"]
138 tfm_def_tfm_ver_full = cmake_env["SPHINXCFG_TFM_VERSION_FULL"]
139 tfm_def_conf_in_file = cmake_env["SPHINXCFG_TEMPLATE_FILE"]
140
141 tfm_def_copy_files = True if cmake_env["SPHINXCFG_COPY_FILES"] == "True" \
142 else False
143 tfm_def_render_cmake = True \
144 if cmake_env["SPHINXCFG_RENDER_CONF"] == "True" else False
145
146 tfm_def_build_doxygen = True \
147 if cmake_env["DOXYCFG_DOXYGEN_BUILD"] == "True" else False
148
149 if tfm_def_build_doxygen:
150 tfm_def_doxy_root = cmake_env["DOXYCFG_DOXYGEN_CFG_DIR"]
151 tfm_def_doxygen_in_file = os.path.join(tfm_def_doxy_root,
152 "Doxyfile.in")
153
154 # Documentation base path
155 tfm_def_doc_root = os.path.join(tfm_def_root_dir, "docs")
156 tfm_def_copy_doc_root = os.path.join(tfm_def_copy_dir, "docs")
157
158 # Disable copyfiles for next invocation
159 cmake_env["SPHINXCFG_COPY_FILES"] = "False"
160 with open("tfm_env.py", "w", encoding='utf-8') as F:
161 F.write("cmake_env =" + json.dumps(cmake_env))
162
163
164# Version will be retrieved in that order Git -> Cmake -> Boilerplate
165try:
Minos Galanakis3568bea2020-11-16 20:15:48 +0000166 vrex = re.compile(r'TF-Mv(?P<VER_MAJ>\d{1,2}).(?P<VER_MIN>\d{1,2}).?'
167 r'(?P<VER_HOT>\d{0,2})(?P<RC>\-RC\d)?-'
168 r'(?P<PATCH_NO>\d+)-(?P<GIT_HASH>[a-g0-9]+)')
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100169 tfm_def_tfm_ver_full = check_output(["git",
170 "describe",
Minos Galanakis3568bea2020-11-16 20:15:48 +0000171 "--tags",
172 "--long"]).decode('UTF-8').strip()
173
174 _v = vrex.search(tfm_def_tfm_ver_full)
175 proj = "TF-M"
176 version = [ _v.group("VER_MAJ"),
177 _v.group("VER_MIN"),
178 _v.group("VER_HOT"),
179 _v.group("RC")]
180 commit_no = _v.group("PATCH_NO")
181 git_hash = _v.group("GIT_HASH")
182
183 # Sanitize the verison and remove empty entries
184 version = [i.replace("-","") for i in version if i]
185 tfm_def_tfm_ver_full = ".".join(version)
186 tfm_def_tfm_ver_shrt = ".".join(version[:2])
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100187
188 if (int(commit_no) > 0):
Minos Galanakis7bd5b912021-01-12 13:08:21 +0000189 tfm_def_tfm_ver_full = "%s+ ( %s )" % (tfm_def_tfm_ver_full, git_hash)
190 tfm_def_tfm_ver_shrt = "%s+ ( %s )" % (tfm_def_tfm_ver_shrt, git_hash)
Minos Galanakis3568bea2020-11-16 20:15:48 +0000191
192 tfm_def_tfm_ver_shrt = tfm_def_tfm_ver_full
193
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100194
195except Exception as E:
196 try:
197 tfm_def_tfm_ver_shrt
198 except NameError:
199 tfm_def_tfm_ver_shrt = "v1.0.0-B"
200 try:
201 tfm_def_tfm_ver_full
202 except NameError:
203 tfm_def_tfm_ver_full = "v1.0.0-B"
204
205# #################### User Defaults ######################################## #
206
207# Directories, referenced by TF-M root, which may contain releval documents
208# which need to be broughtt over
209document_scan_dirs = ["tools", "platform"]
210
211document_scan_ext = [".rst", ".md", ".jpg", ".png"]
212
213# Other documents that should be added to the root documentation folder
214documents_extra = ["license.rst", "dco.txt"]
215
216# Output files ( After CMAKE variables have been evaluated )
217tfm_def_conf_out_file = os.path.join(tfm_def_copy_dir, "conf_rendered.py")
218if tfm_def_build_doxygen:
219 tfm_def_doxygen_out_file = os.path.join(tfm_def_doxy_root,
220 "DoxyfileCfg.in")
221
222# If env is none, the script is running as standalone. Generate it with the
223# auto-detected values set above
224if cmake_env is None:
225 cmake_env = {"TFM_ROOT_DIR": tfm_def_root_dir,
226 "DOXYGEN_EXECUTABLE": tfm_def_doxygen_loc,
227 "DOXYGEN_DOT_EXECUTABLE": tfm_def_doxygen_dot_loc,
228 "PLANTUML_JAR_PATH": tfm_def_plantum_loc,
229 "SPHINXCFG_TFM_VERSION": tfm_def_tfm_ver_shrt,
230 "SPHINXCFG_TFM_VERSION_FULL": tfm_def_tfm_ver_full,
231 "Java_JAVA_EXECUTABLE": tfm_def_java_binary,
232 "DOXYCFG_OUTPUT_PATH": tfm_def_doxy_output_dir,
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100233 "DOXYCFG_TFM_VERSION": tfm_def_tfm_ver_full,
234 }
235# Only Override the version
236else:
237 cmake_env["SPHINXCFG_TFM_VERSION"] = tfm_def_tfm_ver_shrt
238 cmake_env["SPHINXCFG_TFM_VERSION_FULL"] = tfm_def_tfm_ver_full