5 os
.chdir(os
.path
.dirname(os
.path
.abspath(argv
[0])))
7 sys
.argv
+= ["--quiet", "build_ext", "--inplace"]
9 from setuptools
.command
.build_ext
import build_ext
10 from setuptools
import setup
, Extension
12 from distutils
.command
.build_ext
import build_ext
13 from distutils
.extension
import Extension
14 from distutils
.core
import setup
15 LIBA_OPENMP
= os
.environ
.get("LIBA_OPENMP")
16 LIBA_FLOAT
= os
.environ
.get("LIBA_FLOAT")
19 from sys
import version_info
20 from Cython
.Build
import cythonize
23 from argparse
import ArgumentParser
24 from sys
import byteorder
25 from re
import findall
30 def check_math(define_macros
=[], text
=""):
31 if sys
.platform
== "win32":
32 path_libm
= ctypes
.util
.find_library("ucrtbase")
34 path_libm
= ctypes
.util
.find_msvcrt()
36 path_libm
= ctypes
.util
.find_library("m")
38 libm
= ctypes
.CDLL(path_libm
)
42 A_SIZE_FLOAT
= int(LIBA_FLOAT
)
67 name
= "A_HAVE_" + func
.upper()
68 if A_SIZE_FLOAT
== 0x10:
70 if A_SIZE_FLOAT
== 0x04:
76 text
+= "#define %s 1\n" % (name
)
80 def configure(config
, define_macros
=[]):
81 with
open("setup.cfg", "r") as f
:
82 version
= findall(r
"version = (.+)", f
.read())[0]
83 major
, minor
, patch
= findall(r
"(\d+).(\d+).(\d+)", version
)[0]
84 order
= {"little": 1234, "big": 4321}.get(byteorder
)
85 vsize
= ctypes
.sizeof(ctypes
.c_void_p(0))
86 check
= check_math(define_macros
)
87 tweak
= time
.strftime("%Y%m%d")
88 text
= """/* autogenerated by setup.py */
89 #define A_VERSION "{}"
90 #define A_VERSION_MAJOR {}
91 #define A_VERSION_MINOR {}
92 #define A_VERSION_PATCH {}
93 #define A_VERSION_TWEAK {}
94 #if !defined A_SIZE_POINTER
95 #define A_SIZE_POINTER {}
96 #endif /* A_SIZE_POINTER */
97 #if !defined A_BYTE_ORDER
98 #define A_BYTE_ORDER {}
99 #endif /* A_BYTE_ORDER */
101 version
, major
, minor
, patch
, tweak
, vsize
, order
, check
103 with
open(config
, "wb") as f
:
104 f
.write(text
.encode("UTF-8"))
107 parser
= ArgumentParser(add_help
=False)
108 parser
.add_argument("-b", "--build-base", default
="build")
109 parser
.add_argument("-O", "--link-objects")
110 args
= parser
.parse_known_args(argv
[1:])
111 base
= args
[0].build_base
113 sources
, objects
= [], []
114 config_h
= os
.path
.join(base
, "a.setup.h")
115 a_have_h
= os
.path
.relpath(config_h
, "include/a")
116 define_macros
= [("A_HAVE_H", '"' + a_have_h
+ '"'), ("A_EXPORTS", None)]
118 define_macros
+= [("A_SIZE_FLOAT", LIBA_FLOAT
)]
119 if USE_CYTHON
and os
.path
.exists("python/src/a.pyx"):
120 sources
+= ["python/src/a.pyx"]
121 elif os
.path
.exists("python/src/a.c"):
122 sources
+= ["python/src/a.c"]
123 if not os
.path
.exists(base
):
125 configure(config_h
, define_macros
)
127 for dirpath
, dirnames
, filenames
in os
.walk("src"):
128 if args
[0].link_objects
:
130 for filename
in filenames
:
131 source
= os
.path
.join(dirpath
, filename
)
132 if os
.path
.splitext(source
)[-1] == ".c":
133 sources
.append(source
)
140 include_dirs
=["include"],
141 define_macros
=define_macros
,
145 ext_modules
= cythonize( # type: ignore
147 language_level
=version_info
[0], # type: ignore
152 class Build(build_ext
): # type: ignore
153 def build_extensions(self
):
154 if self
.compiler
.compiler_type
== "msvc":
155 for e
in self
.extensions
:
157 e
.extra_compile_args
+= ["/openmp"]
158 if not self
.compiler
.compiler_type
== "msvc":
159 for e
in self
.extensions
:
161 e
.extra_compile_args
+= ["-fopenmp"]
162 e
.extra_link_args
+= ["-fopenmp"]
163 if self
.compiler
.compiler_type
== "mingw32":
164 self
.compiler
.define_macro("__USE_MINGW_ANSI_STDIO", 1)
165 for e
in self
.extensions
:
166 if e
.language
== "c++":
167 e
.extra_link_args
+= ["-static-libstdc++"]
168 e
.extra_link_args
+= ["-static-libgcc"]
169 e
.extra_link_args
+= [
170 "-Wl,-Bstatic,--whole-archive",
172 "-Wl,--no-whole-archive",
174 build_ext
.build_extensions(self
)
178 ext_modules
=ext_modules
, # type: ignore
179 cmdclass
={"build_ext": Build
},