release 0.1.15
[liba.git] / setup.py
blob9070c30d42865ea61a44e678ab0347997e0d1d82
1 #!/usr/bin/env python
2 try:
3 from setuptools.command.build_ext import build_ext
4 from setuptools import setup, Extension
5 except ImportError:
6 from distutils.command.build_ext import build_ext
7 from distutils.extension import Extension
8 from distutils.core import setup
9 try:
10 USE_CYTHON = 1
11 from Cython.Build import cythonize
12 except ImportError:
13 USE_CYTHON = 0
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
19 import ctypes.util
22 def find_executable(*executable):
23 if sys.platform == "win32":
24 exe = []
25 for e in executable:
26 exe.append(e + ".exe")
27 executable = exe
28 path = os.environ.get("PATH", "")
29 for p in path.split(os.pathsep):
30 for e in executable:
31 f = os.path.join(p, e)
32 if os.path.isfile(f):
33 return f
34 return None
37 def strtobool(s):
38 if s.lower() in ("1", "y", "yes", "true"):
39 return 1
40 return 0
43 os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))
44 if len(sys.argv) < 2:
45 sys.argv += ["--quiet", "build_ext", "--inplace"]
46 if USE_CYTHON == 0:
47 CYTHON = find_executable("cython3", "cython")
48 LIBA_OPENMP = os.environ.get("LIBA_OPENMP")
49 if LIBA_OPENMP:
50 LIBA_OPENMP = strtobool(LIBA_OPENMP)
51 LIBA_FLOAT = os.environ.get("LIBA_FLOAT")
52 if LIBA_FLOAT:
53 LIBA_FLOAT = int(LIBA_FLOAT)
54 else:
55 LIBA_FLOAT = 8
58 def check_math(text=""):
59 if sys.platform == "win32":
60 path_libm = ctypes.util.find_library("ucrtbase")
61 if not path_libm:
62 path_libm = ctypes.util.find_msvcrt()
63 else:
64 path_libm = ctypes.util.find_library("m")
65 try:
66 libm = ctypes.CDLL(path_libm)
67 except Exception:
68 return text
69 for func in (
70 "expm1",
71 "log1p",
72 "hypot",
73 "atan2",
74 "csqrt",
75 "cpow",
76 "cexp",
77 "clog",
78 "csin",
79 "ccos",
80 "ctan",
81 "csinh",
82 "ccosh",
83 "ctanh",
84 "casin",
85 "cacos",
86 "catan",
87 "casinh",
88 "cacosh",
89 "catanh",
91 name = "A_HAVE_" + func.upper()
92 if LIBA_FLOAT == 0x10:
93 func += "l"
94 if LIBA_FLOAT == 0x04:
95 func += "f"
96 try:
97 libm[func]
98 except Exception:
99 continue
100 text += "#define %s 1\n" % (name)
101 return text
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 */
121 {}""".format(
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)]
138 if LIBA_FLOAT != 8:
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):
145 os.makedirs(base)
146 configure(config_h)
148 for dirpath, dirnames, filenames in os.walk("src"):
149 if args[0].link_objects:
150 break
151 for filename in filenames:
152 source = os.path.join(dirpath, filename)
153 if os.path.splitext(source)[-1] == ".c":
154 sources.append(source)
156 ext_modules = [
157 Extension(
158 name="liba",
159 language="c",
160 sources=sorted(sources),
161 include_dirs=["include"],
162 define_macros=define_macros,
165 if USE_CYTHON:
166 ext_modules = cythonize(ext_modules, quiet=True)
167 elif CYTHON:
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:
175 if LIBA_OPENMP:
176 e.extra_compile_args += ["/openmp"]
177 if not self.compiler.compiler_type == "msvc":
178 for e in self.extensions:
179 if LIBA_OPENMP:
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",
190 "-lwinpthread",
191 "-Wl,--no-whole-archive",
193 build_ext.build_extensions(self)
196 setup(
197 ext_modules=ext_modules, # type: ignore
198 cmdclass={"build_ext": Build},