Skip to content

Commit 4d1237f

Browse files
dsnopekenetheru
authored andcommitted
Add CMake support for XML documentation
1 parent ae198fe commit 4d1237f

File tree

2 files changed

+58
-46
lines changed

2 files changed

+58
-46
lines changed

doc_source_generator.py

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

tools/godotcpp.py

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from binding_generator import _generate_bindings, _get_file_list, get_file_list
1414
from build_profile import generate_trimmed_api
1515

16+
from binding_generator import scons_generate_bindings, scons_emit_files
17+
from doc_source_generator import scons_generate_doc_source
1618

1719
def add_sources(sources, dir, extension):
1820
for f in os.listdir(dir):
@@ -377,51 +379,6 @@ def options(opts, env):
377379
tool.options(opts)
378380

379381

380-
def make_doc_source(target, source, env):
381-
import zlib
382-
383-
dst = str(target[0])
384-
g = open(dst, "w", encoding="utf-8")
385-
buf = ""
386-
docbegin = ""
387-
docend = ""
388-
for src in source:
389-
src_path = str(src)
390-
if not src_path.endswith(".xml"):
391-
continue
392-
with open(src_path, "r", encoding="utf-8") as f:
393-
content = f.read()
394-
buf += content
395-
396-
buf = (docbegin + buf + docend).encode("utf-8")
397-
decomp_size = len(buf)
398-
399-
# Use maximum zlib compression level to further reduce file size
400-
# (at the cost of initial build times).
401-
buf = zlib.compress(buf, zlib.Z_BEST_COMPRESSION)
402-
403-
g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
404-
g.write("\n")
405-
g.write("#include <godot_cpp/godot.hpp>\n")
406-
g.write("\n")
407-
408-
g.write('static const char *_doc_data_hash = "' + str(hash(buf)) + '";\n')
409-
g.write("static const int _doc_data_uncompressed_size = " + str(decomp_size) + ";\n")
410-
g.write("static const int _doc_data_compressed_size = " + str(len(buf)) + ";\n")
411-
g.write("static const unsigned char _doc_data_compressed[] = {\n")
412-
for i in range(len(buf)):
413-
g.write("\t" + str(buf[i]) + ",\n")
414-
g.write("};\n")
415-
g.write("\n")
416-
417-
g.write(
418-
"static godot::internal::DocDataRegistration _doc_data_registration(_doc_data_hash, _doc_data_uncompressed_size, _doc_data_compressed_size, _doc_data_compressed);\n"
419-
)
420-
g.write("\n")
421-
422-
g.close()
423-
424-
425382
def generate(env):
426383
# Default num_jobs to local cpu count if not user specified.
427384
# SCons has a peculiarity where user-specified options won't be overridden
@@ -554,7 +511,7 @@ def generate(env):
554511
env.Append(
555512
BUILDERS={
556513
"GodotCPPBindings": Builder(action=Action(scons_generate_bindings, "$GENCOMSTR"), emitter=scons_emit_files),
557-
"GodotCPPDocData": Builder(action=make_doc_source),
514+
"GodotCPPDocData": Builder(action=scons_generate_doc_source),
558515
}
559516
)
560517
env.AddMethod(_godot_cpp, "GodotCPP")

0 commit comments

Comments
 (0)