1+ import os
2+ from pathlib import Path
3+ import zipfile
4+ import requests
5+ from pybind11 .setup_helpers import Pybind11Extension , build_ext
6+ # from setuptools.command.build_ext import build_ext
7+
8+ __version__ = "0.0.3"
9+
10+ # The main interface is through Pybind11Extension.
11+ # * You can add cxx_std=11/14/17, and then build_ext can be removed.
12+ # * You can set include_pybind11=false to add the include directory yourself,
13+ # say from a submodule.
14+ #
15+ # Note:
16+ # Sort input source files if you glob sources to ensure bit-for-bit
17+ # reproducible builds (https://github.com/pybind/python_example/pull/53)
18+
19+ SETUP_DIRECTORY = Path (__file__ ).resolve ().parent
20+
21+ # Download Eigen source files
22+ # Modified from https://github.com/tohtsky/irspack/blob/main/setup.py
23+ class get_eigen_include (object ):
24+ EIGEN3_URL = "https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.zip"
25+ EIGEN3_DIRNAME = "eigen-3.4.0"
26+
27+ def __str__ (self ) -> str :
28+ # Test whether the environment variable EIGEN3_INCLUDE_DIR is set
29+ # If yes, directly return this directory
30+ eigen_include_dir = os .environ .get ("EIGEN3_INCLUDE_DIR" , None )
31+ if eigen_include_dir is not None :
32+ return eigen_include_dir
33+
34+ # If the directory already exists (e.g. from previous setup),
35+ # directly return it
36+ target_dir = SETUP_DIRECTORY / self .EIGEN3_DIRNAME
37+ if target_dir .exists ():
38+ return target_dir .name
39+
40+ # Filename for the downloaded Eigen source package
41+ download_target_dir = SETUP_DIRECTORY / "eigen3.zip"
42+ response = requests .get (self .EIGEN3_URL , stream = True )
43+ with download_target_dir .open ("wb" ) as ofs :
44+ for chunk in response .iter_content (chunk_size = 1024 ):
45+ ofs .write (chunk )
46+ # Unzip package
47+ with zipfile .ZipFile (download_target_dir ) as ifs :
48+ ifs .extractall ()
49+
50+ return target_dir .name
51+
52+ ext_modules = [
53+ Pybind11Extension ("rehline._internal" ,
54+ sources = ["src/rehline.cpp" ],
55+ include_dirs = [get_eigen_include ()],
56+ # Example: passing in the version to the compiled code
57+ # define_macros=[('VERSION_INFO', __version__)],
58+ ),
59+ ]
60+
61+ # class BuildFailed(Exception):
62+ # pass
63+
64+ # class ExtBuilder(build_ext):
65+
66+ # def run(self):
67+ # try:
68+ # build_ext.run(self)
69+ # except (DistutilsPlatformError, FileNotFoundError):
70+ # raise BuildFailed('File not found. Could not compile C extension.')
71+
72+ # def build_extension(self, ext):
73+ # try:
74+ # build_ext.build_extension(self, ext)
75+ # except (CCompilerError, DistutilsExecError, DistutilsPlatformError, ValueError):
76+ # raise BuildFailed('Could not compile C extension.')
77+
78+
79+ def build (setup_kwargs ):
80+ """
81+ This function is mandatory in order to build the extensions.
82+ """
83+ setup_kwargs .update (
84+ {"ext_modules" : ext_modules , "cmd_class" : {"build_ext" : build_ext }, "zip_safe" : False }
85+ )
0 commit comments