# setup.py.in
# 
# Setup file for packaging python module with yaramodv4 library.
# Based on cmake_example at: 
# https://github.com/pybind/cmake_example/blob/master/setup.py
#
# Author: Vojtěch Dvořák



import subprocess
from pathlib import Path

from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext



class yaramodv4_build_ext(build_ext):
    def build_extension(self, ext: Extension) -> None:
        ext_dir = Path(self.get_ext_fullpath(ext.name)).parent.resolve() # Get extension directory

        # Run cmake with directory, that is determined by object of Extension class
        subprocess.run(
            [
                "cmake", 
                "@CMAKE_SOURCE_DIR@", 
                f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={ext_dir}"
            ], 
            cwd="@CMAKE_BINARY_DIR@", 
            check=True # Check the return value of cmake
        )

        # Build python module shared object (in the new CMAKE_LIBRARY_OUTPUT_DIRECTORY)
        subprocess.run(
            [
                "cmake", 
                "--build", 
                ".", 
                "--target",
                "@PYTHON_MODULE_NAME@", 
            ], 
            cwd="@CMAKE_BINARY_DIR@", 
            check=True
        )



setup(
    name="@PYTHON_MODULE_NAME@",
    version="@CMAKE_PROJECT_VERSION@",
    author="Vojtěch Dvořák",
    author_email="vojtech.dvorak1@gendigital.com",
    description="Module for incremental parsing of YARA language",
    ext_modules=[Extension("@PYTHON_MODULE_NAME@", [])],
    cmdclass={"build_ext": yaramodv4_build_ext},
    zip_safe=False,
    python_requires=">=3.7",
)

