blob: 5034b8b6065800b14089d08a1dbdb215100cfdd4 [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:
Anton Komleveb5433d2021-12-02 21:11:03 +0000166 vrex = re.compile(r'(?P<GIT_HASH>[a-f0-9]{40})'
167 r'+\s+tag\s+refs\/tags\/TF-Mv(?P<VER_MAJ>\d+).'
168 r'(?P<VER_MIN>\d+).?(?P<VER_HOT>\d+)(?P<RC>-RC\d+)?')
Minos Galanakis3568bea2020-11-16 20:15:48 +0000169
Anton Komleveb5433d2021-12-02 21:11:03 +0000170 tfm_def_tfm_ver_full = check_output("git for-each-ref refs/tags --sort=-taggerdate --count=1",
171 encoding = 'UTF-8')
Minos Galanakis3568bea2020-11-16 20:15:48 +0000172 _v = vrex.search(tfm_def_tfm_ver_full)
Minos Galanakis3568bea2020-11-16 20:15:48 +0000173 version = [ _v.group("VER_MAJ"),
174 _v.group("VER_MIN"),
175 _v.group("VER_HOT"),
176 _v.group("RC")]
Minos Galanakis3568bea2020-11-16 20:15:48 +0000177 git_hash = _v.group("GIT_HASH")
178
179 # Sanitize the verison and remove empty entries
180 version = [i.replace("-","") for i in version if i]
181 tfm_def_tfm_ver_full = ".".join(version)
182 tfm_def_tfm_ver_shrt = ".".join(version[:2])
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100183
Anton Komleveb5433d2021-12-02 21:11:03 +0000184 vlrex = re.compile(r'^(?P<GIT_HASH_LATEST>[a-f0-9]{40})')
185
186 git_hash_latest = check_output("git rev-parse HEAD", encoding = 'UTF-8')
187
188 git_hash_latest = vlrex.search(git_hash_latest).group('GIT_HASH_LATEST')
189
190 if git_hash != git_hash_latest:
191 git_hash_latest = git_hash_latest[:7]
192 tfm_def_tfm_ver_full = "%s+ ( %s )" % (tfm_def_tfm_ver_full, git_hash_latest)
193 tfm_def_tfm_ver_shrt = "%s+ ( %s )" % (tfm_def_tfm_ver_shrt, git_hash_latest)
Minos Galanakis3568bea2020-11-16 20:15:48 +0000194
195 tfm_def_tfm_ver_shrt = tfm_def_tfm_ver_full
196
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100197except Exception as E:
198 try:
199 tfm_def_tfm_ver_shrt
200 except NameError:
201 tfm_def_tfm_ver_shrt = "v1.0.0-B"
202 try:
203 tfm_def_tfm_ver_full
204 except NameError:
205 tfm_def_tfm_ver_full = "v1.0.0-B"
206
207# #################### User Defaults ######################################## #
208
209# Directories, referenced by TF-M root, which may contain releval documents
210# which need to be broughtt over
211document_scan_dirs = ["tools", "platform"]
212
213document_scan_ext = [".rst", ".md", ".jpg", ".png"]
214
215# Other documents that should be added to the root documentation folder
216documents_extra = ["license.rst", "dco.txt"]
217
218# Output files ( After CMAKE variables have been evaluated )
219tfm_def_conf_out_file = os.path.join(tfm_def_copy_dir, "conf_rendered.py")
220if tfm_def_build_doxygen:
221 tfm_def_doxygen_out_file = os.path.join(tfm_def_doxy_root,
222 "DoxyfileCfg.in")
223
224# If env is none, the script is running as standalone. Generate it with the
225# auto-detected values set above
226if cmake_env is None:
227 cmake_env = {"TFM_ROOT_DIR": tfm_def_root_dir,
228 "DOXYGEN_EXECUTABLE": tfm_def_doxygen_loc,
229 "DOXYGEN_DOT_EXECUTABLE": tfm_def_doxygen_dot_loc,
230 "PLANTUML_JAR_PATH": tfm_def_plantum_loc,
231 "SPHINXCFG_TFM_VERSION": tfm_def_tfm_ver_shrt,
232 "SPHINXCFG_TFM_VERSION_FULL": tfm_def_tfm_ver_full,
233 "Java_JAVA_EXECUTABLE": tfm_def_java_binary,
234 "DOXYCFG_OUTPUT_PATH": tfm_def_doxy_output_dir,
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100235 "DOXYCFG_TFM_VERSION": tfm_def_tfm_ver_full,
236 }
237# Only Override the version
238else:
239 cmake_env["SPHINXCFG_TFM_VERSION"] = tfm_def_tfm_ver_shrt
240 cmake_env["SPHINXCFG_TFM_VERSION_FULL"] = tfm_def_tfm_ver_full