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

import numpy as np

class BuildExtThenCopySWIGPy(build_ext):
    def run(self):
        super().run()


class BuildSWIGPyFiles(build_py):
    def run(self):
        # This is a workaround for https://bugs.python.org/issue37247
        # Run regular copy of files, required to run SWIG build
        super().run()
        # Causes SWIG python files to be built. Results in SWIG build happening
        # twice, maybe but not sure
        cmd = BuildExtThenCopySWIGPy(self.distribution)
        cmd.finalize_options()
        cmd.run()
        # Run copy of py files again, now that SWIG has generated py files
        super().run()


swig_opts = ['-c++', '-py3', '-keyword', '-doxygen', '-extranative']

darkprop_module = Extension(name='darkprop._darkprop',
                            sources=['python/darkprop/darkprop.i'],
                            swig_opts=swig_opts,
                            include_dirs=['.',
                                          '/usr/include/eigen3',
                                          np.get_include()],
                            extra_compile_args=['-std=c++17'],
                            libraries=['gsl', 'gslcblas'])


if __name__ == '__main__':
    setup(cmdclass={"build_ext": BuildExtThenCopySWIGPy,
                    "build_py": BuildSWIGPyFiles},
          py_modules=['darkprop.darkprop'],
          ext_modules=[darkprop_module])
