import os
import sys

from Cython.Build import cythonize
from Cython.Compiler import Options
from setuptools import setup

Options.docstrings = False
Options.embed_pos_in_docstring = False
Options.annotate = False
Options.fast_fail = True

DEBUG = bool(os.getenv("DOPT_DEBUG", None))

linetrace_opt: bool = False
if DEBUG:
    linetrace_opt = True

ext_modules = cythonize(
    ["src/dopt_sensor_anomalies/detection.py"],
    compiler_directives={
        "language_level": 3,
        "boundscheck": False,
        "wraparound": False,
        "embedsignature": False,
        "annotation_typing": True,
        "linetrace": linetrace_opt,
    },
)


if DEBUG:
    ext_modules[0].define_macros.append(("CYTHON_TRACE", "1"))

compiler_args: list[str] = []
linker_args: list[str] = []


if sys.platform.startswith("win") and not DEBUG:
    c_args = ("/O2", "/GL", "/Gy")
    l_args = ("/LTCG", "/OPT:REF", "/OPT:ICF")
else:
    c_args = tuple()
    l_args = tuple()

compiler_args.extend(c_args)
linker_args.extend(l_args)

for ext in ext_modules:
    ext.extra_compile_args.extend(compiler_args)
    ext.extra_link_args.extend(linker_args)

setup(ext_modules=ext_modules)
