Update compyte
[pycuda.git] / setup.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from __future__ import absolute_import, print_function
5 from os.path import dirname, join, normpath
6
7
8 def search_on_path(filenames):
9     """Find file on system path."""
10     # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52224
11
12     from os.path import exists, abspath
13     from os import pathsep, environ
14
15     search_path = environ["PATH"]
16
17     paths = search_path.split(pathsep)
18     for path in paths:
19         for filename in filenames:
20             if exists(join(path, filename)):
21                 return abspath(join(path, filename))
22
23
24 def get_config_schema():
25     from aksetup_helper import ConfigSchema, Option, \
26             IncludeDir, LibraryDir, Libraries, BoostLibraries, \
27             Switch, StringListOption, make_boost_base_options
28
29     nvcc_path = search_on_path(["nvcc", "nvcc.exe"])
30     if nvcc_path is None:
31         print("***************************************************************")
32         print("*** WARNING: nvcc not in path.")
33         print("*** May need to set CUDA_INC_DIR for installation to succeed.")
34         print("***************************************************************")
35         cuda_root_default = None
36     else:
37         cuda_root_default = normpath(join(dirname(nvcc_path), ".."))
38
39     cxxflags_default = []
40     ldflags_default = []
41
42     lib64 = "lib64"
43     import sys
44     if sys.platform.startswith("win"):
45         # https://github.com/inducer/pycuda/issues/113
46         lib64 = "lib/x64"
47
48         cxxflags_default.extend(['/EHsc'])
49         ldflags_default.extend(['/FORCE'])
50
51     default_lib_dirs = [
52         "${CUDA_ROOT}/lib",
53         "${CUDA_ROOT}/"+lib64,
54         # https://github.com/inducer/pycuda/issues/98
55         "${CUDA_ROOT}/lib/stubs",
56         "${CUDA_ROOT}/%s/stubs" % lib64,
57         ]
58
59     return ConfigSchema(make_boost_base_options() + [
60         Switch("USE_SHIPPED_BOOST", True, "Use included Boost library"),
61
62         BoostLibraries("python"),
63         BoostLibraries("thread"),
64
65         Switch("CUDA_TRACE", False, "Enable CUDA API tracing"),
66         Option("CUDA_ROOT", default=cuda_root_default,
67             help="Path to the CUDA toolkit"),
68         Option("CUDA_PRETEND_VERSION",
69             help="Assumed CUDA version, in the form 3010 for 3.1."),
70         IncludeDir("CUDA", None),
71
72         Switch("CUDA_ENABLE_GL", False, "Enable CUDA GL interoperability"),
73         Switch("CUDA_ENABLE_CURAND", True, "Enable CURAND library"),
74
75         LibraryDir("CUDADRV", default_lib_dirs),
76         Libraries("CUDADRV", ["cuda"]),
77
78         LibraryDir("CUDART", default_lib_dirs),
79         Libraries("CUDART", ["cudart"]),
80
81         LibraryDir("CURAND", default_lib_dirs),
82         Libraries("CURAND", ["curand"]),
83
84         StringListOption("CXXFLAGS", cxxflags_default,
85             help="Any extra C++ compiler options to include"),
86         StringListOption("LDFLAGS", ldflags_default,
87             help="Any extra linker options to include"),
88         ])
89
90
91 def main():
92     import sys
93
94     from aksetup_helper import (hack_distutils, get_config, setup,
95             NumpyExtension, set_up_shipped_boost_if_requested,
96             check_git_submodules)
97
98     check_git_submodules()
99
100     hack_distutils()
101     conf = get_config(get_config_schema())
102
103     EXTRA_SOURCES, EXTRA_DEFINES = set_up_shipped_boost_if_requested("pycuda", conf)
104
105     EXTRA_DEFINES["PYGPU_PACKAGE"] = "pycuda"
106     EXTRA_DEFINES["PYGPU_PYCUDA"] = "1"
107
108     LIBRARY_DIRS = conf["BOOST_LIB_DIR"] + conf["CUDADRV_LIB_DIR"]
109     LIBRARIES = (conf["BOOST_PYTHON_LIBNAME"] + conf["BOOST_THREAD_LIBNAME"]
110             + conf["CUDADRV_LIBNAME"])
111
112     if not conf["CUDA_INC_DIR"] and conf["CUDA_ROOT"]:
113         conf["CUDA_INC_DIR"] = [join(conf["CUDA_ROOT"], "include")]
114
115     if conf["CUDA_TRACE"]:
116         EXTRA_DEFINES["CUDAPP_TRACE_CUDA"] = 1
117
118     if conf["CUDA_PRETEND_VERSION"]:
119         EXTRA_DEFINES["CUDAPP_PRETEND_CUDA_VERSION"] = conf["CUDA_PRETEND_VERSION"]
120
121     INCLUDE_DIRS = ['src/cpp'] + conf["BOOST_INC_DIR"]
122     if conf["CUDA_INC_DIR"]:
123         INCLUDE_DIRS += conf["CUDA_INC_DIR"]
124
125     conf["USE_CUDA"] = True
126
127     if 'darwin' in sys.platform and sys.maxsize == 2147483647:
128         # The Python interpreter is running in 32 bit mode on OS X
129         if "-arch" not in conf["CXXFLAGS"]:
130             conf["CXXFLAGS"].extend(['-arch', 'i386', '-m32'])
131         if "-arch" not in conf["LDFLAGS"]:
132             conf["LDFLAGS"].extend(['-arch', 'i386', '-m32'])
133
134     if 'darwin' in sys.platform:
135         # set path to Cuda dynamic libraries,
136         # as a safe substitute for DYLD_LIBRARY_PATH
137         for lib_dir in conf["CUDADRV_LIB_DIR"]:
138             conf["LDFLAGS"].extend(["-Xlinker", "-rpath", "-Xlinker", lib_dir])
139
140     if conf["CUDA_ENABLE_GL"]:
141         EXTRA_SOURCES.append("src/wrapper/wrap_cudagl.cpp")
142         EXTRA_DEFINES["HAVE_GL"] = 1
143
144     if conf["CUDA_ENABLE_CURAND"]:
145         EXTRA_DEFINES["HAVE_CURAND"] = 1
146         EXTRA_SOURCES.extend([
147             "src/wrapper/wrap_curand.cpp"
148             ])
149         LIBRARIES.extend(conf["CURAND_LIBNAME"])
150         LIBRARY_DIRS.extend(conf["CURAND_LIB_DIR"])
151
152     ver_dic = {}
153     exec(compile(open("pycuda/__init__.py").read(), "pycuda/__init__.py", 'exec'),
154             ver_dic)
155
156     import sys
157     if sys.version_info >= (3,):
158         pvt_struct_source = "src/wrapper/_pvt_struct_v3.cpp"
159     else:
160         pvt_struct_source = "src/wrapper/_pvt_struct_v2.cpp"
161
162     setup(name="pycuda",
163             # metadata
164             version=ver_dic["VERSION_TEXT"],
165             description="Python wrapper for Nvidia CUDA",
166             long_description=open("README.rst", "rt").read(),
167             author="Andreas Kloeckner",
168             author_email="inform@tiker.net",
169             license="MIT",
170             url="http://mathema.tician.de/software/pycuda",
171             classifiers=[
172                 'Environment :: Console',
173                 'Development Status :: 5 - Production/Stable',
174                 'Intended Audience :: Developers',
175                 'Intended Audience :: Other Audience',
176                 'Intended Audience :: Science/Research',
177                 'License :: OSI Approved :: MIT License',
178                 'Natural Language :: English',
179                 'Programming Language :: C++',
180                 'Programming Language :: Python',
181                 'Programming Language :: Python :: 3',
182                 'Programming Language :: Python :: 2.6',
183                 'Programming Language :: Python :: 2.7',
184                 'Programming Language :: Python :: 3.3',
185                 'Programming Language :: Python :: 3.4',
186                 'Topic :: Scientific/Engineering',
187                 'Topic :: Scientific/Engineering :: Mathematics',
188                 'Topic :: Scientific/Engineering :: Physics',
189                 'Topic :: Scientific/Engineering :: Visualization',
190                 ],
191
192             # build info
193             packages=["pycuda", "pycuda.gl", "pycuda.sparse", "pycuda.compyte"],
194
195             setup_requires=[
196                 "numpy>=1.6",
197                 ],
198
199             install_requires=[
200                 "pytools>=2011.2",
201                 "pytest>=2",
202                 "decorator>=3.2.0",
203                 "appdirs>=1.4.0"
204                 ],
205
206             ext_package="pycuda",
207             ext_modules=[
208                 NumpyExtension("_driver",
209                     [
210                         "src/cpp/cuda.cpp",
211                         "src/cpp/bitlog.cpp",
212                         "src/wrapper/wrap_cudadrv.cpp",
213                         "src/wrapper/mempool.cpp",
214                         ]+EXTRA_SOURCES,
215                     include_dirs=INCLUDE_DIRS,
216                     library_dirs=LIBRARY_DIRS,
217                     libraries=LIBRARIES,
218                     define_macros=list(EXTRA_DEFINES.items()),
219                     extra_compile_args=conf["CXXFLAGS"],
220                     extra_link_args=conf["LDFLAGS"],
221                     ),
222                 NumpyExtension("_pvt_struct",
223                     [pvt_struct_source],
224                     extra_compile_args=conf["CXXFLAGS"],
225                     extra_link_args=conf["LDFLAGS"],
226                     ),
227                 ],
228
229             include_package_data=True,
230             package_data={
231                     "pycuda": [
232                         "cuda/*.hpp",
233                         ]
234                     },
235
236             zip_safe=False)
237
238
239 if __name__ == '__main__':
240     main()