import os
from os.path import join
from typing import List

import imm.build_ext
from cffi import FFI

ffibuilder = FFI()
libs = ["nmm"]

ffibuilder.include(imm.build_ext.ffibuilder)

folder = os.path.dirname(os.path.abspath(__file__))

with open(join(folder, "nmm.h"), "r") as f:
    imm_float = imm.build_ext.imm_float
    ffibuilder.cdef(f.read().replace("__IMM_FLOAT__", imm_float))

extra_link_args: List[str] = []
if "NMM_EXTRA_LINK_ARGS" in os.environ:
    extra_link_args += os.environ["NMM_EXTRA_LINK_ARGS"].split(os.pathsep)

ffibuilder.set_source(
    "nmm._ffi",
    r"""
    #include "nmm/nmm.h"
    """,
    libraries=libs,
    extra_link_args=extra_link_args,
    language="c",
)

if __name__ == "__main__":
    ffibuilder.compile(verbose=True)
