Skip to content

Commit f766129

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

File tree

2 files changed

+77
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)