Skip to content

Commit d4e3f00

Browse files
committed
Add header builders script for env.GLSL_HEADER and SVG icons
1 parent af4f05e commit d4e3f00

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

tools/godotcpp.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import platform
33
import sys
44

5+
import header_builders
6+
57
from SCons import __version__ as scons_raw_version
68
from SCons.Action import Action
79
from SCons.Builder import Builder
@@ -524,6 +526,10 @@ def generate(env):
524526
BUILDERS={
525527
"GodotCPPBindings": Builder(action=Action(scons_generate_bindings, "$GENCOMSTR"), emitter=scons_emit_files),
526528
"GodotCPPDocData": Builder(action=scons_generate_doc_source),
529+
"GLSL_HEADER": Builder(
530+
action=header_builders.build_raw_headers_action,
531+
suffix="glsl.gen.h",
532+
),
527533
}
528534
)
529535
env.AddMethod(_godot_cpp, "GodotCPP")

tools/header_builders.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import os.path
2+
3+
4+
## See https://github.com/godotengine/godot/blob/master/glsl_builders.py
5+
def build_raw_header(source_filename: str, constant_name: str) -> None:
6+
# Read the source file content.
7+
with open(source_filename, "r") as source_file:
8+
source_content = source_file.read()
9+
constant_name = constant_name.replace(".", "_")
10+
# Build header content using a C raw string literal.
11+
header_content = (
12+
"/* THIS FILE IS GENERATED. EDITS WILL BE LOST. */\n\n"
13+
"#pragma once\n\n"
14+
f"inline constexpr const char *{constant_name}" " = "
15+
f'R"<!>({source_content})<!>"'
16+
";\n"
17+
)
18+
# Write the header to the provided file name with a ".gen.h" suffix.
19+
header_filename = f"{source_filename}.gen.h"
20+
with open(header_filename, "w") as header_file:
21+
header_file.write(header_content)
22+
23+
24+
def build_raw_headers_action(target, source, env):
25+
env.NoCache(target)
26+
for src in source:
27+
source_filename = str(src)
28+
# To match Godot, replace ".glsl" with "_shader_glsl". Does nothing for non-GLSL files.
29+
constant_name = os.path.basename(source_filename).replace(".glsl", "_shader_glsl")
30+
build_raw_header(source_filename, constant_name)
31+
32+
33+
def escape_svg(filename: str) -> str:
34+
with open(filename, encoding="utf-8", newline="\n") as svg_file:
35+
svg_content = svg_file.read()
36+
return f'R"<!>({svg_content})<!>"'
37+
38+
39+
## See https://github.com/godotengine/godot/blob/master/editor/icons/editor_icons_builders.py
40+
## See https://github.com/godotengine/godot/blob/master/scene/theme/icons/default_theme_icons_builders.py
41+
def make_svg_icons_action(target, source, env):
42+
destination = str(target[0])
43+
constant_prefix = os.path.basename(destination).replace(".gen.h", "")
44+
svg_icons = [str(x) for x in source]
45+
# Convert the SVG icons to escaped strings and convert their names to C strings.
46+
icon_names = [f'"{os.path.basename(fname)[:-4]}"' for fname in svg_icons]
47+
icon_sources = [escape_svg(fname) for fname in svg_icons]
48+
# Join them as indented comma-separated items for use in an array initializer.
49+
icon_names_str = ",\n\t".join(icon_names)
50+
icon_sources_str = ",\n\t".join(icon_sources)
51+
# Write the file to disk.
52+
with open(destination, "w", encoding="utf-8", newline="\n") as destination_file:
53+
destination_file.write(f"""\
54+
/* THIS FILE IS GENERATED. EDITS WILL BE LOST. */
55+
56+
#pragma once
57+
58+
inline constexpr int {constant_prefix}_count = {len(icon_names)};
59+
inline constexpr const char *{constant_prefix}_sources[] = {{
60+
{icon_sources_str}
61+
}};
62+
63+
inline constexpr const char *{constant_prefix}_names[] = {{
64+
{icon_names_str}
65+
}};
66+
""")

0 commit comments

Comments
 (0)