Skip to content

Commit 9d92beb

Browse files
committed
Add CMake support for XML documentation
1 parent 0efc6cd commit 9d92beb

File tree

2 files changed

+54
-46
lines changed

2 files changed

+54
-46
lines changed

doc_source_generator.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import glob
5+
import zlib
6+
7+
def generate_doc_source(dst, source):
8+
g = open(dst, "w", encoding="utf-8")
9+
buf = ""
10+
docbegin = ""
11+
docend = ""
12+
for src in source:
13+
src_path = str(src)
14+
if not src_path.endswith(".xml"):
15+
continue
16+
with open(src_path, "r", encoding="utf-8") as f:
17+
content = f.read()
18+
buf += content
19+
20+
buf = (docbegin + buf + docend).encode("utf-8")
21+
decomp_size = len(buf)
22+
23+
# Use maximum zlib compression level to further reduce file size
24+
# (at the cost of initial build times).
25+
buf = zlib.compress(buf, zlib.Z_BEST_COMPRESSION)
26+
27+
g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
28+
g.write("\n")
29+
g.write("#include <godot_cpp/godot.hpp>\n")
30+
g.write("\n")
31+
32+
g.write('static const char *_doc_data_hash = "' + str(hash(buf)) + '";\n')
33+
g.write("static const int _doc_data_uncompressed_size = " + str(decomp_size) + ";\n")
34+
g.write("static const int _doc_data_compressed_size = " + str(len(buf)) + ";\n")
35+
g.write("static const unsigned char _doc_data_compressed[] = {\n")
36+
for i in range(len(buf)):
37+
g.write("\t" + str(buf[i]) + ",\n")
38+
g.write("};\n")
39+
g.write("\n")
40+
41+
g.write(
42+
"static godot::internal::DocDataRegistration _doc_data_registration(_doc_data_hash, _doc_data_uncompressed_size, _doc_data_compressed_size, _doc_data_compressed);\n"
43+
)
44+
g.write("\n")
45+
46+
g.close()
47+
48+
def scons_generate_doc_source(target, source, env):
49+
generate_doc_source(str(target[0]), source)
50+
51+
def generate_doc_source_from_directory(target, directory):
52+
generate_doc_source(target, glob.glob(os.path.join(directory, '*.xml')))

tools/godotcpp.py

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
from binding_generator import scons_generate_bindings, scons_emit_files
13+
from doc_source_generator import scons_generate_doc_source
1314

1415

1516
def add_sources(sources, dir, extension):
@@ -336,51 +337,6 @@ def options(opts, env):
336337
tool.options(opts)
337338

338339

339-
def make_doc_source(target, source, env):
340-
import zlib
341-
342-
dst = str(target[0])
343-
g = open(dst, "w", encoding="utf-8")
344-
buf = ""
345-
docbegin = ""
346-
docend = ""
347-
for src in source:
348-
src_path = str(src)
349-
if not src_path.endswith(".xml"):
350-
continue
351-
with open(src_path, "r", encoding="utf-8") as f:
352-
content = f.read()
353-
buf += content
354-
355-
buf = (docbegin + buf + docend).encode("utf-8")
356-
decomp_size = len(buf)
357-
358-
# Use maximum zlib compression level to further reduce file size
359-
# (at the cost of initial build times).
360-
buf = zlib.compress(buf, zlib.Z_BEST_COMPRESSION)
361-
362-
g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
363-
g.write("\n")
364-
g.write("#include <godot_cpp/godot.hpp>\n")
365-
g.write("\n")
366-
367-
g.write('static const char *_doc_data_hash = "' + str(hash(buf)) + '";\n')
368-
g.write("static const int _doc_data_uncompressed_size = " + str(decomp_size) + ";\n")
369-
g.write("static const int _doc_data_compressed_size = " + str(len(buf)) + ";\n")
370-
g.write("static const unsigned char _doc_data_compressed[] = {\n")
371-
for i in range(len(buf)):
372-
g.write("\t" + str(buf[i]) + ",\n")
373-
g.write("};\n")
374-
g.write("\n")
375-
376-
g.write(
377-
"static godot::internal::DocDataRegistration _doc_data_registration(_doc_data_hash, _doc_data_uncompressed_size, _doc_data_compressed_size, _doc_data_compressed);\n"
378-
)
379-
g.write("\n")
380-
381-
g.close()
382-
383-
384340
def generate(env):
385341
# Default num_jobs to local cpu count if not user specified.
386342
# SCons has a peculiarity where user-specified options won't be overridden
@@ -513,7 +469,7 @@ def generate(env):
513469
env.Append(
514470
BUILDERS={
515471
"GodotCPPBindings": Builder(action=Action(scons_generate_bindings, "$GENCOMSTR"), emitter=scons_emit_files),
516-
"GodotCPPDocData": Builder(action=make_doc_source),
472+
"GodotCPPDocData": Builder(action=scons_generate_doc_source),
517473
}
518474
)
519475
env.AddMethod(_godot_cpp, "GodotCPP")

0 commit comments

Comments
 (0)