blob: 09226c81c37300bfdee7f7305c8ebd9bb26f656b [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
22
23def tfm_copy_files():
24 doc_files = []
25
26 # Recursively list all files with extensions and add them
27 for _path in document_scan_dirs:
28 _path = os.path.abspath(os.path.join(tfm_def_root_dir, _path))
29 for ext in document_scan_ext:
30 doc_files.extend([f for f in glob(os.path.join(_path,
31 "**/*%s" % ext),
32 recursive=True)])
33
34 # Add the extra files
35 for _doc_file in documents_extra:
36 _doc_file = os.path.abspath(os.path.join(tfm_def_root_dir, _doc_file))
37 if os.path.isfile(_doc_file):
38 doc_files.append(_doc_file)
39
40 # Clean up all files in target dir except conf.py and tfm_env.py
41 files = [f for f in
42 glob("*", recursive=False) if f not in ["conf.py",
43 tfm_def_conf_out_file,
44 os.path.basename(
45 tfm_def_doxy_output_dir),
46 "tfm_env.py"]]
47 for f in files:
48 if os.path.isfile(f):
49 os.remove(f)
50 elif os.path.isdir(f):
51 rmtree(f)
52
53 # Copy the documentation folder as is
54 copytree(tfm_def_doc_root, tfm_def_copy_doc_root)
55
56 # Move the index to the intermediate build directory
57 # docs/index.rst --> ./index.rst
58 move(os.path.join(tfm_def_copy_doc_root, "index.rst"), tfm_def_copy_dir)
59
60 for df in list(doc_files):
61 # Set the target filename to be cwd + relative to root path of origin
62 target_f = os.path.relpath(df, tfm_def_root_dir)
63 target_f = os.path.join(tfm_def_copy_dir, target_f)
64 # Create path for file (nested) without exception if exists
65 os.makedirs(os.path.dirname(target_f), exist_ok=True)
66 # Copy the file to new location
67 copy2(df, target_f)
68
69
70# Build Doxygen Documnetation
71if tfm_def_build_doxygen:
72 # if conf file is not provided by cmake
73 if tfm_def_render_cmake:
74 render_cmake_file(cmake_env,
75 tfm_def_doxygen_in_file,
76 tfm_def_doxygen_out_file)
77 # Call doxygen to generate the documentation
78 doxygen_bin = find_package("doxygen")
79 call([doxygen_bin, tfm_def_doxygen_out_file])
80
81# Only act if requested by defaults
82if tfm_def_copy_files:
83 tfm_copy_files()
84
85if tfm_def_render_cmake:
86 # Render the conf_py file
87 render_cmake_file(cmake_env, tfm_def_conf_in_file, tfm_def_conf_out_file)