Skip to content

Commit 6877a0a

Browse files
committed
[SCons] Fix custom API file/dir relative paths.
1 parent 2f785c9 commit 6877a0a

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

SConstruct

+29-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import platform
55
import sys
66
import subprocess
77
from binding_generator import scons_generate_bindings, scons_emit_files
8+
from SCons.Errors import UserError
89

910
EnsureSConsVersion(4, 0)
1011

@@ -15,6 +16,28 @@ def add_sources(sources, dir, extension):
1516
sources.append(dir + "/" + f)
1617

1718

19+
def normalize_path(val):
20+
return val if os.path.isabs(val) else os.path.join(env.Dir("#").abspath, val)
21+
22+
23+
def validate_api_file(key, val, env):
24+
if not os.path.isfile(normalize_path(val)):
25+
raise UserError("GDExtension API file ('%s') does not exist: %s" % (key, val))
26+
27+
28+
def validate_gdextension_dir(key, val, env):
29+
if not os.path.isdir(normalize_path(val)):
30+
raise UserError("GDExtension directory ('%s') does not exist: %s" % (key, val))
31+
32+
33+
def get_gdextension_dir(env):
34+
return normalize_path(env.get("gdextension_dir", env.Dir("gdextension").abspath))
35+
36+
37+
def get_api_file(env):
38+
return normalize_path(env.get("custom_api_file", os.path.join(get_gdextension_dir(env), "extension_api.json")))
39+
40+
1841
# Try to detect the host platform automatically.
1942
# This is used if no `platform` argument is passed
2043
if sys.platform.startswith("linux"):
@@ -80,17 +103,17 @@ opts.Add(
80103
opts.Add(
81104
PathVariable(
82105
"gdextension_dir",
83-
"Path to the directory containing GDExtension interface header and API JSON file",
84-
"gdextension",
85-
PathVariable.PathIsDir,
106+
"Path to a custom directory containing GDExtension interface header and API JSON file",
107+
None,
108+
validate_gdextension_dir,
86109
)
87110
)
88111
opts.Add(
89112
PathVariable(
90113
"custom_api_file",
91114
"Path to a custom GDExtension API JSON file (takes precedence over `gdextension_dir`)",
92115
None,
93-
PathVariable.PathIsFile,
116+
validate_api_file,
94117
)
95118
)
96119
opts.Add(
@@ -186,16 +209,10 @@ if env["float"] == "64":
186209

187210
# Generate bindings
188211
env.Append(BUILDERS={"GenerateBindings": Builder(action=scons_generate_bindings, emitter=scons_emit_files)})
189-
json_api_file = ""
190-
191-
if "custom_api_file" in env:
192-
json_api_file = env["custom_api_file"]
193-
else:
194-
json_api_file = os.path.join(os.getcwd(), env["gdextension_dir"], "extension_api.json")
195212

196213
bindings = env.GenerateBindings(
197214
env.Dir("."),
198-
[json_api_file, os.path.join(env["gdextension_dir"], "gdextension_interface.h"), "binding_generator.py"],
215+
[get_api_file(env), os.path.join(get_gdextension_dir(env), "gdextension_interface.h"), "binding_generator.py"],
199216
)
200217

201218
scons_cache_path = os.environ.get("SCONS_CACHE")
@@ -209,7 +226,7 @@ if env["generate_bindings"]:
209226
NoCache(bindings)
210227

211228
# Includes
212-
env.Append(CPPPATH=[[env.Dir(d) for d in [env["gdextension_dir"], "include", os.path.join("gen", "include")]]])
229+
env.Append(CPPPATH=[[env.Dir(d) for d in [get_gdextension_dir(env), "include", os.path.join("gen", "include")]]])
213230

214231
# Sources to compile
215232
sources = []

0 commit comments

Comments
 (0)