Skip to content

Commit 5317634

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

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

tools/godotcpp.py

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

5+
import header_builders
56
from SCons import __version__ as scons_raw_version
67
from SCons.Action import Action
78
from SCons.Builder import Builder
@@ -524,6 +525,10 @@ def generate(env):
524525
BUILDERS={
525526
"GodotCPPBindings": Builder(action=Action(scons_generate_bindings, "$GENCOMSTR"), emitter=scons_emit_files),
526527
"GodotCPPDocData": Builder(action=scons_generate_doc_source),
528+
"GLSL_HEADER": Builder(
529+
action=header_builders.build_raw_headers_action,
530+
suffix="glsl.gen.h",
531+
),
527532
}
528533
)
529534
env.AddMethod(_godot_cpp, "GodotCPP")

tools/header_builders.py

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

0 commit comments

Comments
 (0)