#!/usr/bin/env python
"""
SConstruct for the brogue_gen GDExtension.

Build:  cd godot && scons target=template_debug
        (or target=template_release for optimized)

Output: ../demo/bin/libbrogue_gen.<platform>.<target>.<arch>.so
"""

import os
import sys

env = SConscript("godot-cpp/SConstruct")

# Where to find our C generator library headers.
env.Append(CPPPATH=["src/", "../src/"])

# Compile all of our C generator sources directly into the extension — the
# generator is a pure C99 library with no Godot deps. This avoids needing a
# separate libbroguegen.a.
c_sources = [
    "../src/gen/rng.c",
    "../src/gen/grid.c",
    "../src/gen/events.c",
    "../src/gen/room_types.c",
    "../src/gen/accretion.c",
    "../src/gen/dijkstra.c",
    "../src/gen/loops.c",
    "../src/gen/ca.c",
    "../src/gen/lakes.c",
    "../src/gen/walls.c",
    "../src/gen/stairs.c",
    "../src/gen/chokepoints.c",
    "../src/gen/machines.c",
    "../src/gen/blueprints_data.c",
]

# Ensure C files compile with C99; don't inherit godot-cpp's C++ flags verbatim.
c_env = env.Clone()
c_env.Replace(CFLAGS=["-std=c99", "-Wall", "-Wpedantic", "-Werror=implicit",
                      "-O2", "-g", "-fPIC"])
c_objects = [c_env.SharedObject(target="build/" + os.path.basename(s).replace(".c", ""),
                                source=s) for s in c_sources]

cpp_sources = Glob("src/*.cpp")

library_name = "libbrogue_gen{}{}".format(env["suffix"], env["SHLIBSUFFIX"])

# Godot plugin layout convention: addons/<plugin_name>/
#   brogue_gen.gdextension   — manifest, references the .so path relative to res://
#   bin/<name>.so            — binaries per platform/target
import os as _os
import shutil as _shutil
_ADDON_DIR = "../demo/addons/brogue_gen"
_os.makedirs(_ADDON_DIR + "/bin", exist_ok=True)

library = env.SharedLibrary(
    _ADDON_DIR + "/bin/" + library_name,
    source=cpp_sources + c_objects,
)

_shutil.copyfile("brogue_gen.gdextension",
                 _ADDON_DIR + "/brogue_gen.gdextension")

Default(library)
