[SCons] Add option to generate a compilation database.

pull/1189/head
Fabio Alessandrelli 2023-07-22 19:54:12 +02:00
parent 3162be28e5
commit 2586ad016e
1 changed files with 33 additions and 6 deletions

View File

@ -20,14 +20,19 @@ def normalize_path(val):
return val if os.path.isabs(val) else os.path.join(env.Dir("#").abspath, val) return val if os.path.isabs(val) else os.path.join(env.Dir("#").abspath, val)
def validate_api_file(key, val, env): def validate_file(key, val, env):
if not os.path.isfile(normalize_path(val)): if not os.path.isfile(normalize_path(val)):
raise UserError("GDExtension API file ('%s') does not exist: %s" % (key, val)) raise UserError("'%s' is not a file: %s" % (key, val))
def validate_gdextension_dir(key, val, env): def validate_dir(key, val, env):
if not os.path.isdir(normalize_path(val)): if not os.path.isdir(normalize_path(val)):
raise UserError("GDExtension directory ('%s') does not exist: %s" % (key, val)) raise UserError("'%s' is not a directory: %s" % (key, val))
def validate_parent_dir(key, val, env):
if not os.path.isdir(normalize_path(os.path.dirname(val))):
raise UserError("'%s' is not a directory: %s" % (key, os.path.dirname(val)))
def get_gdextension_dir(env): def get_gdextension_dir(env):
@ -115,7 +120,7 @@ opts.Add(
key="gdextension_dir", key="gdextension_dir",
help="Path to a custom directory containing GDExtension interface header and API JSON file", help="Path to a custom directory containing GDExtension interface header and API JSON file",
default=env.get("gdextension_dir", None), default=env.get("gdextension_dir", None),
validator=validate_gdextension_dir, validator=validate_dir,
) )
) )
opts.Add( opts.Add(
@ -123,7 +128,7 @@ opts.Add(
key="custom_api_file", key="custom_api_file",
help="Path to a custom GDExtension API JSON file (takes precedence over `gdextension_dir`)", help="Path to a custom GDExtension API JSON file (takes precedence over `gdextension_dir`)",
default=env.get("custom_api_file", None), default=env.get("custom_api_file", None),
validator=validate_api_file, validator=validate_file,
) )
) )
opts.Add( opts.Add(
@ -151,6 +156,23 @@ opts.Add(
) )
) )
# compiledb
opts.Add(
BoolVariable(
key="compiledb",
help="Generate compilation DB (`compile_commands.json`) for external tools",
default=env.get("compiledb", False),
)
)
opts.Add(
PathVariable(
key="compiledb_file",
help="Path to a custom `compile_commands.json` file",
default=env.get("compiledb_file", "compile_commands.json"),
validator=validate_parent_dir,
)
)
# Add platform options # Add platform options
tools = {} tools = {}
for pl in platforms: for pl in platforms:
@ -242,6 +264,11 @@ else:
if env["precision"] == "double": if env["precision"] == "double":
env.Append(CPPDEFINES=["REAL_T_IS_DOUBLE"]) env.Append(CPPDEFINES=["REAL_T_IS_DOUBLE"])
# compile_commands.json
if env.get("compiledb", False):
env.Tool("compilation_db")
env.Alias("compiledb", env.CompilationDatabase(normalize_path(env["compiledb_file"])))
# Generate bindings # Generate bindings
env.Append(BUILDERS={"GenerateBindings": Builder(action=scons_generate_bindings, emitter=scons_emit_files)}) env.Append(BUILDERS={"GenerateBindings": Builder(action=scons_generate_bindings, emitter=scons_emit_files)})