blob: 1820b6a4e1ec6a8f65acfc131f4f8dfad60a179f [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
Anton Komlev2d2a6fc2022-02-20 15:47:53 +0000131 try:
132 vrex = re.compile(r'TF-M(?P<GIT_VERSION>v.+?)'
133 r'(-[0-9]+-g)?(?P<GIT_SHA>[a-f0-9]{7,})?$')
134
135 tfm_def_tfm_version = check_output("git describe --tags --always",
136 shell = True, encoding = 'UTF-8')
137
138 _v = vrex.match(tfm_def_tfm_version)
139
140 tfm_def_tfm_version = _v.group("GIT_VERSION")
141 if _v.group("GIT_SHA"):
142 tfm_def_tfm_version += "+" + _v.group("GIT_SHA")[:7]
143
144 except Exception as E:
145 try:
146 tfm_def_tfm_version
147 except NameError:
148 tfm_def_tfm_version = "Unknown"
149
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100150else:
151 # #################### Cmake Defaults ################################## #
152 tfm_def_root_dir = os.path.abspath(cmake_env["TFM_ROOT_DIR"])
153 tfm_def_copy_dir = os.path.abspath(cmake_env["SPHINX_TMP_DOC_DIR"])
154 tfm_def_plantum_loc = os.path.abspath(cmake_env["PLANTUML_JAR_PATH"])
155 tfm_def_java_binary = os.path.abspath(cmake_env["Java_JAVA_EXECUTABLE"])
Anton Komlevf7836d12021-12-23 16:27:46 +0000156 tfm_def_tfm_version = cmake_env["SPHINXCFG_TFM_VERSION"]
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100157 tfm_def_conf_in_file = cmake_env["SPHINXCFG_TEMPLATE_FILE"]
158
159 tfm_def_copy_files = True if cmake_env["SPHINXCFG_COPY_FILES"] == "True" \
160 else False
161 tfm_def_render_cmake = True \
162 if cmake_env["SPHINXCFG_RENDER_CONF"] == "True" else False
163
164 tfm_def_build_doxygen = True \
165 if cmake_env["DOXYCFG_DOXYGEN_BUILD"] == "True" else False
166
167 if tfm_def_build_doxygen:
168 tfm_def_doxy_root = cmake_env["DOXYCFG_DOXYGEN_CFG_DIR"]
169 tfm_def_doxygen_in_file = os.path.join(tfm_def_doxy_root,
170 "Doxyfile.in")
171
172 # Documentation base path
173 tfm_def_doc_root = os.path.join(tfm_def_root_dir, "docs")
Anton Komlev3356ba32022-03-31 22:02:11 +0100174 tfm_def_copy_doc_root = tfm_def_copy_dir
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100175
176 # Disable copyfiles for next invocation
177 cmake_env["SPHINXCFG_COPY_FILES"] = "False"
178 with open("tfm_env.py", "w", encoding='utf-8') as F:
179 F.write("cmake_env =" + json.dumps(cmake_env))
180
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100181# #################### User Defaults ######################################## #
182
183# Directories, referenced by TF-M root, which may contain releval documents
184# which need to be broughtt over
185document_scan_dirs = ["tools", "platform"]
186
187document_scan_ext = [".rst", ".md", ".jpg", ".png"]
188
189# Other documents that should be added to the root documentation folder
190documents_extra = ["license.rst", "dco.txt"]
191
192# Output files ( After CMAKE variables have been evaluated )
193tfm_def_conf_out_file = os.path.join(tfm_def_copy_dir, "conf_rendered.py")
194if tfm_def_build_doxygen:
195 tfm_def_doxygen_out_file = os.path.join(tfm_def_doxy_root,
196 "DoxyfileCfg.in")
197
198# If env is none, the script is running as standalone. Generate it with the
199# auto-detected values set above
200if cmake_env is None:
201 cmake_env = {"TFM_ROOT_DIR": tfm_def_root_dir,
202 "DOXYGEN_EXECUTABLE": tfm_def_doxygen_loc,
203 "DOXYGEN_DOT_EXECUTABLE": tfm_def_doxygen_dot_loc,
204 "PLANTUML_JAR_PATH": tfm_def_plantum_loc,
Anton Komlevf7836d12021-12-23 16:27:46 +0000205 "SPHINXCFG_TFM_VERSION": tfm_def_tfm_version,
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100206 "Java_JAVA_EXECUTABLE": tfm_def_java_binary,
207 "DOXYCFG_OUTPUT_PATH": tfm_def_doxy_output_dir,
Anton Komlevf7836d12021-12-23 16:27:46 +0000208 "DOXYCFG_TFM_VERSION": tfm_def_tfm_version,
Minos Galanakisd19a19f2020-06-03 15:38:03 +0100209 }
210# Only Override the version
211else:
Anton Komlevf7836d12021-12-23 16:27:46 +0000212 cmake_env["SPHINXCFG_TFM_VERSION"] = tfm_def_tfm_version