3 from setuptools
.command
.build_ext
import build_ext
4 from setuptools
import setup
, Extension
6 from distutils
.command
.build_ext
import build_ext
7 from distutils
.extension
import Extension
8 from distutils
.core
import setup
11 from Cython
.Build
import cythonize
14 from subprocess
import Popen
15 from argparse
import ArgumentParser
16 from sys
import byteorder
17 from re
import findall
18 import os
, sys
, ctypes
22 def find_executable(*executable
):
23 if sys
.platform
== "win32":
26 exe
.append(e
+ ".exe")
28 path
= os
.environ
.get("PATH", "")
29 for p
in path
.split(os
.pathsep
):
31 f
= os
.path
.join(p
, e
)
38 if s
.lower() in ("1", "y", "yes", "true"):
43 os
.chdir(os
.path
.dirname(os
.path
.abspath(sys
.argv
[0])))
45 sys
.argv
+= ["--quiet", "build_ext", "--inplace"]
47 CYTHON
= find_executable("cython3", "cython")
48 LIBA_OPENMP
= os
.environ
.get("LIBA_OPENMP")
50 LIBA_OPENMP
= strtobool(LIBA_OPENMP
)
51 LIBA_FLOAT
= os
.environ
.get("LIBA_FLOAT")
53 LIBA_FLOAT
= int(LIBA_FLOAT
)
58 def check_math(text
=""):
59 if sys
.platform
== "win32":
60 path_libm
= ctypes
.util
.find_library("ucrtbase")
62 path_libm
= ctypes
.util
.find_msvcrt()
64 path_libm
= ctypes
.util
.find_library("m")
66 libm
= ctypes
.CDLL(path_libm
)
94 name
= "A_HAVE_" + func
.upper()
95 if LIBA_FLOAT
== 0x10:
97 if LIBA_FLOAT
== 0x04:
103 text
+= "#define %s 1\n" % (name
)
107 def configure(config
):
108 with
open("setup.cfg", "r") as f
:
109 version
= findall(r
"version = (\S+)", f
.read())[0]
110 major
, minor
, patch
= findall(r
"(\d+).(\d+).(\d+)", version
)[0]
111 order
= {"little": 1234, "big": 4321}.get(byteorder
)
112 vsize
= ctypes
.sizeof(ctypes
.c_void_p(0))
113 text
= """/* autogenerated by setup.py */
114 #define A_VERSION "{}"
115 #define A_VERSION_MAJOR {}
116 #define A_VERSION_MINOR {}
117 #define A_VERSION_PATCH {}
118 #if !defined A_SIZE_POINTER
119 #define A_SIZE_POINTER {}
120 #endif /* A_SIZE_POINTER */
121 #if !defined A_BYTE_ORDER
122 #define A_BYTE_ORDER {}
123 #endif /* A_BYTE_ORDER */
125 version
, major
, minor
, patch
, vsize
, order
, check_math()
127 with
open(config
, "wb") as f
:
128 f
.write(text
.encode("UTF-8"))
131 parser
= ArgumentParser(add_help
=False)
132 parser
.add_argument("-b", "--build-base", default
="build")
133 parser
.add_argument("-O", "--link-objects")
134 args
= parser
.parse_known_args(sys
.argv
[1:])
135 base
= args
[0].build_base
137 sources
, objects
= [], []
138 config_h
= os
.path
.join(base
, "a.setup.h")
139 a_have_h
= os
.path
.relpath(config_h
, "include/a")
140 define_macros
= [("A_HAVE_H", '"' + a_have_h
+ '"'), ("A_EXPORTS", None)]
142 define_macros
+= [("A_SIZE_FLOAT", LIBA_FLOAT
)]
143 if USE_CYTHON
and os
.path
.exists("python/src/liba.pyx"):
144 sources
+= ["python/src/liba.pyx"]
145 elif CYTHON
or os
.path
.exists("python/src/liba.c"):
146 sources
+= ["python/src/liba.c"]
147 if not os
.path
.exists(base
):
151 for dirpath
, dirnames
, filenames
in os
.walk("src"):
152 if args
[0].link_objects
:
154 for filename
in filenames
:
155 source
= os
.path
.join(dirpath
, filename
)
156 if os
.path
.splitext(source
)[-1] == ".c":
157 sources
.append(source
)
163 sources
=sorted(sources
),
164 include_dirs
=["include"],
165 define_macros
=define_macros
,
169 ext_modules
= cythonize(ext_modules
, quiet
=True)
171 Popen([CYTHON
, "--fast-fail", "python/src/liba.pyx"]).wait()
174 class Build(build_ext
): # type: ignore
175 def build_extensions(self
):
176 if self
.compiler
.compiler_type
== "msvc":
177 for e
in self
.extensions
:
179 e
.extra_compile_args
+= ["/openmp"]
180 if not self
.compiler
.compiler_type
== "msvc":
181 for e
in self
.extensions
:
183 e
.extra_compile_args
+= ["-fopenmp"]
184 e
.extra_link_args
+= ["-fopenmp"]
185 if self
.compiler
.compiler_type
== "mingw32":
186 self
.compiler
.define_macro("__USE_MINGW_ANSI_STDIO", 1)
187 for e
in self
.extensions
:
188 if e
.language
== "c++":
189 e
.extra_link_args
+= ["-static-libstdc++"]
190 e
.extra_link_args
+= ["-static-libgcc"]
191 e
.extra_link_args
+= [
192 "-Wl,-Bstatic,--whole-archive",
194 "-Wl,--no-whole-archive",
196 build_ext
.build_extensions(self
)
200 ext_modules
=ext_modules
, # type: ignore
201 cmdclass
={"build_ext": Build
},