blob: 5b15e5f817dbd7de5c33c399f9e8e6df8e72e3f0 [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"])
Anton Komlevf7836d12021-12-23 16:27:46 +0000137 tfm_def_tfm_version = cmake_env["SPHINXCFG_TFM_VERSION"]
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100138 tfm_def_conf_in_file = cmake_env["SPHINXCFG_TEMPLATE_FILE"]
139
140 tfm_def_copy_files = True if cmake_env["SPHINXCFG_COPY_FILES"] == "True" \
141 else False
142 tfm_def_render_cmake = True \
143 if cmake_env["SPHINXCFG_RENDER_CONF"] == "True" else False
144
145 tfm_def_build_doxygen = True \
146 if cmake_env["DOXYCFG_DOXYGEN_BUILD"] == "True" else False
147
148 if tfm_def_build_doxygen:
149 tfm_def_doxy_root = cmake_env["DOXYCFG_DOXYGEN_CFG_DIR"]
150 tfm_def_doxygen_in_file = os.path.join(tfm_def_doxy_root,
151 "Doxyfile.in")
152
153 # Documentation base path
154 tfm_def_doc_root = os.path.join(tfm_def_root_dir, "docs")
155 tfm_def_copy_doc_root = os.path.join(tfm_def_copy_dir, "docs")
156
157 # Disable copyfiles for next invocation
158 cmake_env["SPHINXCFG_COPY_FILES"] = "False"
159 with open("tfm_env.py", "w", encoding='utf-8') as F:
160 F.write("cmake_env =" + json.dumps(cmake_env))
161
162
163# Version will be retrieved in that order Git -> Cmake -> Boilerplate
164try:
Anton Komleveb5433d2021-12-02 21:11:03 +0000165 vrex = re.compile(r'(?P<GIT_HASH>[a-f0-9]{40})'
166 r'+\s+tag\s+refs\/tags\/TF-Mv(?P<VER_MAJ>\d+).'
167 r'(?P<VER_MIN>\d+).?(?P<VER_HOT>\d+)(?P<RC>-RC\d+)?')
Minos Galanakis3568bea2020-11-16 20:15:48 +0000168
Anton Komlevf7836d12021-12-23 16:27:46 +0000169 tfm_def_tfm_version = check_output("git for-each-ref refs/tags --sort=-taggerdate --count=1",
170 shell = True, encoding = 'UTF-8')
171
172 _v = vrex.search(tfm_def_tfm_version)
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]
Anton Komlevf7836d12021-12-23 16:27:46 +0000181 tfm_def_tfm_version = "v"+".".join(version)
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100182
Anton Komleveb5433d2021-12-02 21:11:03 +0000183 vlrex = re.compile(r'^(?P<GIT_HASH_LATEST>[a-f0-9]{40})')
184
Anton Komlevf7836d12021-12-23 16:27:46 +0000185 git_hash_latest = check_output("git rev-parse HEAD",
186 shell = True, encoding = 'UTF-8')
Anton Komleveb5433d2021-12-02 21:11:03 +0000187
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]
Anton Komlevf7836d12021-12-23 16:27:46 +0000192 tfm_def_tfm_version += "+ ({})".format(git_hash_latest)
Minos Galanakis3568bea2020-11-16 20:15:48 +0000193
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100194except Exception as E:
195 try:
Anton Komlevf7836d12021-12-23 16:27:46 +0000196 tfm_def_tfm_version
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100197 except NameError:
Anton Komlevf7836d12021-12-23 16:27:46 +0000198 tfm_def_tfm_version = "Unknown"
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100199
200# #################### User Defaults ######################################## #
201
202# Directories, referenced by TF-M root, which may contain releval documents
203# which need to be broughtt over
204document_scan_dirs = ["tools", "platform"]
205
206document_scan_ext = [".rst", ".md", ".jpg", ".png"]
207
208# Other documents that should be added to the root documentation folder
209documents_extra = ["license.rst", "dco.txt"]
210
211# Output files ( After CMAKE variables have been evaluated )
212tfm_def_conf_out_file = os.path.join(tfm_def_copy_dir, "conf_rendered.py")
213if tfm_def_build_doxygen:
214 tfm_def_doxygen_out_file = os.path.join(tfm_def_doxy_root,
215 "DoxyfileCfg.in")
216
217# If env is none, the script is running as standalone. Generate it with the
218# auto-detected values set above
219if cmake_env is None:
220 cmake_env = {"TFM_ROOT_DIR": tfm_def_root_dir,
221 "DOXYGEN_EXECUTABLE": tfm_def_doxygen_loc,
222 "DOXYGEN_DOT_EXECUTABLE": tfm_def_doxygen_dot_loc,
223 "PLANTUML_JAR_PATH": tfm_def_plantum_loc,
Anton Komlevf7836d12021-12-23 16:27:46 +0000224 "SPHINXCFG_TFM_VERSION": tfm_def_tfm_version,
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100225 "Java_JAVA_EXECUTABLE": tfm_def_java_binary,
226 "DOXYCFG_OUTPUT_PATH": tfm_def_doxy_output_dir,
Anton Komlevf7836d12021-12-23 16:27:46 +0000227 "DOXYCFG_TFM_VERSION": tfm_def_tfm_version,
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100228 }
229# Only Override the version
230else:
Anton Komlevf7836d12021-12-23 16:27:46 +0000231 cmake_env["SPHINXCFG_TFM_VERSION"] = tfm_def_tfm_version