blob: b1eb9b0b906c916cd78c7199e5e13207b47f36ae [file] [log] [blame]
Minos Galanakisd19a19f2020-06-03 15:38:03 +01001# -----------------------------------------------------------------------------
2# Copyright (c) 2020, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6# -----------------------------------------------------------------------------
7
8# Interface module for pre-processing the documentation content
9# before sphynx-build is called
10#
11# It collects files from multiple sources in a intermediate location
12# before calling sphinx-build, thus overriding the limitation
13# of having the documentation share a common root directory.
14#
15# It can be triggered by simply importing the module
16import os
17from shutil import copy2, copytree, rmtree, move
18from glob import glob
19from tfm_cmake_defaults import *
20from subprocess import call
21
Anton Komlev3356ba32022-03-31 22:02:11 +010022def copytree_overwrite(src, dest):
23 if os.path.isdir(src):
24 if not os.path.isdir(dest):
25 os.makedirs(dest)
26 files = os.listdir(src)
27 for f in files:
28 copytree_overwrite(os.path.join(src, f),
29 os.path.join(dest, f))
30 else:
31 copy2(src, dest)
Minos Galanakisd19a19f2020-06-03 15:38:03 +010032
33def tfm_copy_files():
34 doc_files = []
35
36 # Recursively list all files with extensions and add them
37 for _path in document_scan_dirs:
38 _path = os.path.abspath(os.path.join(tfm_def_root_dir, _path))
39 for ext in document_scan_ext:
40 doc_files.extend([f for f in glob(os.path.join(_path,
41 "**/*%s" % ext),
42 recursive=True)])
43
44 # Add the extra files
45 for _doc_file in documents_extra:
46 _doc_file = os.path.abspath(os.path.join(tfm_def_root_dir, _doc_file))
47 if os.path.isfile(_doc_file):
48 doc_files.append(_doc_file)
49
50 # Clean up all files in target dir except conf.py and tfm_env.py
51 files = [f for f in
52 glob("*", recursive=False) if f not in ["conf.py",
53 tfm_def_conf_out_file,
54 os.path.basename(
55 tfm_def_doxy_output_dir),
56 "tfm_env.py"]]
57 for f in files:
58 if os.path.isfile(f):
59 os.remove(f)
60 elif os.path.isdir(f):
61 rmtree(f)
62
63 # Copy the documentation folder as is
Anton Komlev3356ba32022-03-31 22:02:11 +010064 copytree_overwrite(tfm_def_doc_root, tfm_def_copy_doc_root)
Minos Galanakisd19a19f2020-06-03 15:38:03 +010065
66 for df in list(doc_files):
67 # Set the target filename to be cwd + relative to root path of origin
68 target_f = os.path.relpath(df, tfm_def_root_dir)
69 target_f = os.path.join(tfm_def_copy_dir, target_f)
70 # Create path for file (nested) without exception if exists
71 os.makedirs(os.path.dirname(target_f), exist_ok=True)
72 # Copy the file to new location
73 copy2(df, target_f)
74
75
76# Build Doxygen Documnetation
77if tfm_def_build_doxygen:
78 # if conf file is not provided by cmake
79 if tfm_def_render_cmake:
80 render_cmake_file(cmake_env,
81 tfm_def_doxygen_in_file,
82 tfm_def_doxygen_out_file)
83 # Call doxygen to generate the documentation
84 doxygen_bin = find_package("doxygen")
85 call([doxygen_bin, tfm_def_doxygen_out_file])
86
87# Only act if requested by defaults
88if tfm_def_copy_files:
89 tfm_copy_files()
90
91if tfm_def_render_cmake:
92 # Render the conf_py file
93 render_cmake_file(cmake_env, tfm_def_conf_in_file, tfm_def_conf_out_file)