Import("env")

c_env = env.Clone()
if env["platform"].startswith("windows"):
    c_env.AppendUnique(LIBS=["python38"])

elif env["platform"].startswith("osx"):
    c_env.AppendUnique(LIBS=["python3.8"])
    # if we don't give the lib a proper install_name, macos won't be able to find it,
    # and will link the cython modules with a relative path
    c_env.AppendUnique(
        LINKFLAGS=["-Wl,-rpath,'@loader_path/lib'", "-install_name", "@rpath/libpythonscript.dylib"]
    )
    c_env.AppendUnique(CFLAGS=["-Werror-implicit-function-declaration"])

else:  # x11
    c_env.AppendUnique(LIBS=["python3.8"])
    c_env.AppendUnique(LINKFLAGS=["-Wl,-rpath,'$$ORIGIN/lib'"])
    c_env.AppendUnique(CFLAGS=["-Werror-implicit-function-declaration"])
c_env.Depends("pythonscript.c", env["cpython_build"])


libpythonscript, *libpythonscript_extra = c_env.SharedLibrary("pythonscript", ["pythonscript.c"])
env.Install("$DIST_PLATFORM", [libpythonscript, *libpythonscript_extra])


# Cython modules depend on libpythonscript
env.AppendUnique(LIBPATH=[Dir(".")])
env.AppendUnique(CYTHON_COMPILE_DEPS=[libpythonscript])


SConscript(["pandemonium/SConscript"])


# `_pandemonium_api.h` is only for internal use between _pandemonium and pythonscript
# libraries, hence no need to provide it as part of the release
*mods, _ = env.CythonModule(
    ["_pandemonium", "_pandemonium_api.h"],
    [
        "_pandemonium.pyx",
        "_pandemonium_editor.pxi",
        "_pandemonium_instance.pxi",
        "_pandemonium_profiling.pxi",
        "_pandemonium_script.pxi",
        "_pandemonium_io.pxi",
    ],
)
env.Install("$DIST_SITE_PACKAGES", mods)
