Import("env")


SConscript(["_hazmat/SConscript"])


pxds = [
    File(x)
    for x in (
        "__init__.py",  # Not really a .pxd but still needed
        "bindings.pxd",
        "builtins.pxd",
        "hazmat.pxd",
        "pool_arrays.pxd",
    )
]
env.Install("$DIST_SITE_PACKAGES/godot", [File("_version.py"), File("globals.py"), *pxds])
env.AppendUnique(CYTHON_DEPS=pxds)


env.Install("$DIST_SITE_PACKAGES/godot", env.CythonModule("tags", "tags.pyx"))


### Builtins ###


# TODO: merge pool_arrays into builtins
godot_pool_arrays_srcs = env.Command(
    target=("pool_arrays.pyx", "pool_arrays.pxd"),
    source=("#/generation/generate_pool_arrays.py",),
    action="python ${SOURCE} --output ${TARGET}",
)
env.Depends(
    godot_pool_arrays_srcs,
    ["#/generation/generate_pool_arrays.py", env.Glob("#/generation/pool_arrays_templates/*")],
)


godot_builtins_srcs = env.Command(
    target=("builtins.pyx", "builtins.pxd"),
    source=("#/generation/generate_builtins.py", "${godot_headers}/gdnative_api.json"),
    action="python ${SOURCES[0]} --input ${SOURCES[1]} --output ${TARGET}",
)
env.Depends(
    godot_builtins_srcs,
    ["#/generation/generate_builtins.py", env.Glob("#/generation/builtins_templates/*")],
)
# TODO: remove this once pool_array is merged into builtins
env.Depends(godot_builtins_srcs, godot_pool_arrays_srcs)


env.Install("$DIST_SITE_PACKAGES/godot", env.CythonModule("pool_arrays", "pool_arrays.pyx"))
env.Install("$DIST_SITE_PACKAGES/godot", env.CythonModule("builtins", "builtins.pyx"))


### Bindings ###


# Bindings module is a special snowflake given it size
bindings_env = env.Clone()
sample = env["bindings_generate_sample"]

# dont strip on debug builds
if not sample and not env["debug"]:
    if env["CC_IS_GCC"]:
        bindings_env.AppendUnique(LINKFLAGS=["-Wl,--strip-all"])
    elif env["CC_IS_CLANG"]:
        bindings_env.AppendUnique(LINKFLAGS=["-Wl,-s"])

if sample or env["debug"]:
    # Disable optimization for faster dev builds and ease of debugging
    if env["CC_IS_MSVC"]:
        bindings_env.AppendUnique(CFLAGS=["/Od"])
    else:
        bindings_env.AppendUnique(CFLAGS=["-O0"])
else:
    if env["CC_IS_GCC"]:
        bindings_env.AppendUnique(CFLAGS=["-Os", "-Wno-misleading-indentation"])
    elif env["CC_IS_CLANG"]:
        bindings_env.AppendUnique(CFLAGS=["-Os"])
    elif env["CC_IS_MSVC"]:
        bindings_env.AppendUnique(CFLAGS=["/Os"])


godot_bindings_srcs = bindings_env.Command(
    target=("bindings.pyx", "bindings.pxd", "bindings.pyi"),
    source=("#/generation/generate_bindings.py", "${godot_headers}/api.json"),
    action=("python ${SOURCES[0]} ${opts} --input ${SOURCES[1]} --output ${TARGET} "),
    opts="--sample" if sample else "",
)
bindings_env.Depends(
    godot_bindings_srcs,
    ["#/generation/generate_bindings.py", bindings_env.Glob("#/generation/bindings_templates/*")],
)


bindings_env.Install(
    "$DIST_SITE_PACKAGES/godot", bindings_env.CythonModule("bindings", "bindings.pyx")
)
bindings_env.Install("$DIST_SITE_PACKAGES/godot", "bindings.pyi")
