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
)
91 name
= "A_HAVE_" + func
.upper()
92 if LIBA_FLOAT
== 0x10:
94 if LIBA_FLOAT
== 0x04:
100 text
+= "#define %s 1\n" % (name
)
104 def configure(config
):
105 with
open("setup.cfg", "r") as f
:
106 version
= findall(r
"version = (\S+)", f
.read())[0]
107 major
, minor
, patch
= findall(r
"(\d+).(\d+).(\d+)", version
)[0]
108 order
= {"little": 1234, "big": 4321}.get(byteorder
)
109 vsize
= ctypes
.sizeof(ctypes
.c_void_p(0))
110 text
= """/* autogenerated by setup.py */
111 #define A_VERSION "{}"
112 #define A_VERSION_MAJOR {}
113 #define A_VERSION_MINOR {}
114 #define A_VERSION_PATCH {}
115 #if !defined A_SIZE_POINTER
116 #define A_SIZE_POINTER {}
117 #endif /* A_SIZE_POINTER */
118 #if !defined A_BYTE_ORDER
119 #define A_BYTE_ORDER {}
120 #endif /* A_BYTE_ORDER */
122 version
, major
, minor
, patch
, vsize
, order
, check_math()
124 with
open(config
, "wb") as f
:
125 f
.write(text
.encode("UTF-8"))
128 parser
= ArgumentParser(add_help
=False)
129 parser
.add_argument("-b", "--build-base", default
="build")
130 parser
.add_argument("-O", "--link-objects")
131 args
= parser
.parse_known_args(sys
.argv
[1:])
132 base
= args
[0].build_base
134 sources
, objects
= [], []
135 config_h
= os
.path
.join(base
, "a.setup.h")
136 a_have_h
= os
.path
.relpath(config_h
, "include/a")
137 define_macros
= [("A_HAVE_H", '"' + a_have_h
+ '"'), ("A_EXPORTS", None)]
139 define_macros
+= [("A_SIZE_FLOAT", LIBA_FLOAT
)]
140 if USE_CYTHON
and os
.path
.exists("python/src/liba.pyx"):
141 sources
+= ["python/src/liba.pyx"]
142 elif CYTHON
or os
.path
.exists("python/src/liba.c"):
143 sources
+= ["python/src/liba.c"]
144 if not os
.path
.exists(base
):
148 for dirpath
, dirnames
, filenames
in os
.walk("src"):
149 if args
[0].link_objects
:
151 for filename
in filenames
:
152 source
= os
.path
.join(dirpath
, filename
)
153 if os
.path
.splitext(source
)[-1] == ".c":
154 sources
.append(source
)
160 sources
=sorted(sources
),
161 include_dirs
=["include"],
162 define_macros
=define_macros
,
166 ext_modules
= cythonize(ext_modules
, quiet
=True)
168 Popen([CYTHON
, "--fast-fail", "python/src/liba.pyx"]).wait()
171 class Build(build_ext
): # type: ignore
172 def build_extensions(self
):
173 if self
.compiler
.compiler_type
== "msvc":
174 for e
in self
.extensions
:
176 e
.extra_compile_args
+= ["/openmp"]
177 if not self
.compiler
.compiler_type
== "msvc":
178 for e
in self
.extensions
:
180 e
.extra_compile_args
+= ["-fopenmp"]
181 e
.extra_link_args
+= ["-fopenmp"]
182 if self
.compiler
.compiler_type
== "mingw32":
183 self
.compiler
.define_macro("__USE_MINGW_ANSI_STDIO", 1)
184 for e
in self
.extensions
:
185 if e
.language
== "c++":
186 e
.extra_link_args
+= ["-static-libstdc++"]
187 e
.extra_link_args
+= ["-static-libgcc"]
188 e
.extra_link_args
+= [
189 "-Wl,-Bstatic,--whole-archive",
191 "-Wl,--no-whole-archive",
193 build_ext
.build_extensions(self
)
197 ext_modules
=ext_modules
, # type: ignore
198 cmdclass
={"build_ext": Build
},