]> begriffs open source - cmsis-freertos/blob - Test/litani/doc/configure
Updated pack to FreeRTOS 10.4.6
[cmsis-freertos] / Test / litani / doc / configure
1 #!/usr/bin/env python3
2 #
3 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License").
6 # You may not use this file except in compliance with the License.
7 # A copy of the License is located at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # or in the "license" file accompanying this file. This file is distributed
12 # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 # express or implied. See the License for the specific language governing
14 # permissions and limitations under the License.
15
16
17 import sys
18 import pathlib
19
20 import jinja2
21
22
23 DOC_DIR = pathlib.Path(sys.path[0]).resolve()
24 sys.path.insert(1, str(DOC_DIR.parent))
25
26 # pylint: disable=wrong-import-position,import-error
27 import lib.ninja_syntax
28
29
30 OUT_DIR = DOC_DIR / "out"
31
32 BIN_DIR = DOC_DIR / "bin"
33
34 SRC_DIR = DOC_DIR / "src"
35
36 TEMPLATE_DIR = DOC_DIR / "templates"
37 TMP_DIR = DOC_DIR / "tmp"
38
39 ROFF_DIR = OUT_DIR / "man"
40
41 HTML_MAN_SRC_DIR = TMP_DIR / "roff_to_html"
42 HTML_UNIQUE_DIR = TMP_DIR / "html_unique"
43
44
45 RULES = [{
46     "name": "sc_to_roff",
47     "description": "converting ${man-name} to roff",
48     "command": "scdoc < ${in} > ${out}"
49 }, {
50     "name": "voluptuous_to_roff",
51     "description": "converting ${man-name} to roff",
52     "command": f"{BIN_DIR / 'schema-to-scdoc'}"
53                f" --project-root { DOC_DIR.parent }"
54                " --page-name ${man-name}"
55                " --data-path ${data-path}"
56                " --template"
57                f" { TEMPLATE_DIR / 'voluptuous-man.jinja.scdoc' }"
58                " | scdoc > ${out}"
59 }, {
60     "name": "roff_to_html",
61     "description": "converting ${man-name}.roff HTML",
62     "command": "mandoc -O fragment -Thtml < ${in} > ${out}"
63 }, {
64     "name": "uniquify_header_ids",
65     "description": "Giving unique header IDs to ${man-name}.html",
66     "command": f"sed -f {BIN_DIR}/uniquify-header-ids"
67                " -e 's/@MAN_NAME@/${man-name}/g'"
68                " ${in-file}"
69                # Get rid of header and footer
70                " | tail -n +8"
71                " | tac"
72                " | tail -n +7"
73                " | tac"
74                " > ${out}"
75 }, {
76     "name": "build_html_doc",
77     "description": "Building final HTML page",
78     "command": f"{BIN_DIR}/build-html-doc"
79                "  --html-manuals ${html-mans}"
80                f" --template-dir {TEMPLATE_DIR}"
81                "  --out-file ${out}"
82                "  --roff-html-dir ${roff-html-dir}"
83 }]
84
85
86 def make_html_unique(man, html_man, html_mans, builds):
87     html_unique = HTML_UNIQUE_DIR / f"{man.stem}.html"
88     builds.append({
89         "inputs": [html_man, BIN_DIR / "uniquify-header-ids"],
90         "outputs": [html_unique],
91         "rule": "uniquify_header_ids",
92         "variables": {
93             "man-name": man.stem,
94             "in-file": html_man,
95         }
96     })
97     html_mans.append(html_unique)
98
99
100 def roff_to_html(man, roff_out, builds):
101     html_man = HTML_MAN_SRC_DIR / f"{man.stem}.html"
102     builds.append({
103         "inputs": [roff_out],
104         "outputs": [html_man],
105         "rule": "roff_to_html",
106         "variables": {
107             "man-name": man.stem,
108             "in-file": html_man,
109         }
110     })
111     return html_man
112
113
114 def convert_man_dir_to_roff(src_dir, dst_dir, rule, html_mans, builds):
115     for man in (src_dir).iterdir():
116         roff_out = dst_dir / f"{man.stem}.roff"
117         builds.append({
118             "inputs": [man],
119             "outputs": [roff_out],
120             "rule": rule,
121             "variables": {
122                 "man-name": man.stem,
123                 "data-path": man.resolve(),
124             }
125         })
126         html_man = roff_to_html(man, roff_out, builds)
127         make_html_unique(man, html_man, html_mans, builds)
128
129
130 def main():
131     builds = []
132     html_mans = []
133
134     convert_man_dir_to_roff(
135         SRC_DIR / "man", ROFF_DIR, "sc_to_roff", html_mans, builds)
136     convert_man_dir_to_roff(
137         SRC_DIR / "voluptuous-man", ROFF_DIR, "voluptuous_to_roff", html_mans,
138         builds)
139
140     builds.append({
141         "inputs": html_mans + [
142             BIN_DIR / "build-html-doc",
143             TEMPLATE_DIR / "index.jinja.html",
144         ],
145         "outputs": [DOC_DIR / "out" / "html"/ "index.html"],
146         "rule": "build_html_doc",
147         "variables": {
148             "html-mans": " ".join([str(h) for h in html_mans]),
149             "roff-html-dir": HTML_MAN_SRC_DIR,
150         }
151     })
152
153     for build in builds:
154         for k, v in build.items():
155             if isinstance(v, list):
156                 build[k] = [str(s) for s in v]
157         try:
158             build["implicit"].append(str(DOC_DIR / "configure"))
159         except KeyError:
160             build["implicit"] = [str(DOC_DIR / "configure")]
161
162     with open("build.ninja", "w") as handle:
163         ninja = lib.ninja_syntax.Writer(handle)
164         for rule in RULES:
165             ninja.rule(**rule)
166         for build in builds:
167             ninja.build(**build)
168
169
170 if __name__ == "__main__":
171     main()