Debian Codegraph @

editor/editor_builders.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 """Functions used to generate source files during build time"""
2
•3 import hashlib
4 import os
5 import os.path
6 import subprocess
7 import tempfile
8 import uuid
9
10 import methods
11
12
13 def doc_data_class_path_builder(target, source, env):
14 paths = dict(sorted(source[0].read().items()))
15 data = "\n".join([f'\t{{"{key}", "{value}"}},' for key, value in paths.items()])
16 with methods.generated_wrapper(str(target[0])) as file:
17 file.write(
18 f"""\
19 struct _DocDataClassPath {{
20 const char *name;
21 const char *path;
22 }};
23
24 inline constexpr int _doc_data_class_path_count = {len(paths)};
25 inline constexpr _DocDataClassPath _doc_data_class_paths[{len(paths) + 1}] = {{
26 {data}
27 {{nullptr, nullptr}},
28 }};
29 """
30 )
31
32
33 def register_exporters_builder(target, source, env):
34 platforms = source[0].read()
35 exp_inc = "\n".join([f'#include "platform/{p}/export/export.h"' for p in platforms])
36 exp_reg = "\n\t".join([f"register_{p}_exporter();" for p in platforms])
37 exp_type = "\n\t".join([f"register_{p}_exporter_types();" for p in platforms])
38 with methods.generated_wrapper(str(target[0])) as file:
39 file.write(
40 f"""\
41 #include "register_exporters.h"
42
43 {exp_inc}
44
45 void register_exporters() {{
46 {exp_reg}
47 }}
48
49 void register_exporter_types() {{
50 {exp_type}
51 }}
52 """
53 )
54
55
56 def make_doc_header(target, source, env):
57 buffer = b"".join([methods.get_buffer(src) for src in map(str, source)])
58 decomp_size = len(buffer)
59 buffer = methods.compress_buffer(buffer)
60
61 with methods.generated_wrapper(str(target[0])) as file:
62 file.write(f"""\
•63 inline constexpr const char *_doc_data_hash = "{hashlib.sha256(buffer).hexdigest()}";
64 inline constexpr int _doc_data_compressed_size = {len(buffer)};
65 inline constexpr int _doc_data_uncompressed_size = {decomp_size};
66 inline constexpr const unsigned char _doc_data_compressed[] = {{
67 {methods.format_buffer(buffer, 1)}
68 }};
69 """)
70
71
72 def make_translations(target, source, env):
73 target_h, target_cpp = str(target[0]), str(target[1])
74
75 category = os.path.basename(target_h).split("_")[0]
76 sorted_paths = sorted([src.abspath for src in source], key=lambda path: os.path.splitext(os.path.basename(path))[0])
77
78 xl_names = []
79 msgfmt = env.Detect("msgfmt")
80 if not msgfmt:
81 methods.print_warning("msgfmt not found, using .po files instead of .mo")
82
83 with methods.generated_wrapper(target_cpp) as file:
84 for path in sorted_paths:
85 name = os.path.splitext(os.path.basename(path))[0]
86 # msgfmt erases non-translated messages, so avoid using it if exporting the POT.
87 if msgfmt and name != category:
88 mo_path = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex + ".mo")
89 cmd = f'{msgfmt} "{path}" --no-hash -o "{mo_path}"'
90 try:
91 subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate()
92 buffer = methods.get_buffer(mo_path)
93 except OSError as e:
94 methods.print_warning(
95 "msgfmt execution failed, using .po file instead of .mo: path=%r; [%s] %s"
96 % (path, e.__class__.__name__, e)
97 )
98 buffer = methods.get_buffer(path)
99 finally:
100 try:
101 if os.path.exists(mo_path):
102 os.remove(mo_path)
103 except OSError as e:
104 # Do not fail the entire build if it cannot delete a temporary file.
105 methods.print_warning(
106 "Could not delete temporary .mo file: path=%r; [%s] %s" % (mo_path, e.__class__.__name__, e)
107 )
108 else:
109 buffer = methods.get_buffer(path)
110 if name == category:
111 name = "source"
112
113 decomp_size = len(buffer)
114 buffer = methods.compress_buffer(buffer)
115
116 file.write(f"""\
117 inline constexpr const unsigned char _{category}_translation_{name}_compressed[] = {{
118 {methods.format_buffer(buffer, 1)}
119 }};
120
121 """)
122
123 xl_names.append([name, len(buffer), decomp_size])
124
125 file.write(f"""\
126 #include "{target_h}"
127
128 const EditorTranslationList _{category}_translations[] = {{
129 """)
130
131 for x in xl_names:
132 file.write(f'\t{{ "{x[0]}", {x[1]}, {x[2]}, _{category}_translation_{x[0]}_compressed }},\n')
133
134 file.write("""\
135 { nullptr, 0, 0, nullptr },
136 };
137 """)
138
139 with methods.generated_wrapper(target_h) as file:
140 file.write(f"""\
141
142 #ifndef EDITOR_TRANSLATION_LIST
143 #define EDITOR_TRANSLATION_LIST
144
145 struct EditorTranslationList {{
146 const char* lang;
147 int comp_size;
148 int uncomp_size;
149 const unsigned char* data;
150 }};
151
152 #endif // EDITOR_TRANSLATION_LIST
153
154 extern const EditorTranslationList _{category}_translations[];
155 """)