Merge pull request #85 from mbrubake/feature/complex-mod
[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     return ConfigSchema(make_boost_base_options() + [
40         Switch("USE_SHIPPED_BOOST", True, "Use included Boost library"),
41
42         BoostLibraries("python"),
43         BoostLibraries("thread"),
44
45         Switch("CUDA_TRACE", False, "Enable CUDA API tracing"),
46         Option("CUDA_ROOT", default=cuda_root_default,
47             help="Path to the CUDA toolkit"),
48         Option("CUDA_PRETEND_VERSION",
49             help="Assumed CUDA version, in the form 3010 for 3.1."),
50         IncludeDir("CUDA", None),
51
52         Switch("CUDA_ENABLE_GL", False, "Enable CUDA GL interoperability"),
53         Switch("CUDA_ENABLE_CURAND", True, "Enable CURAND library"),
54
55         LibraryDir("CUDADRV", ["${CUDA_ROOT}/lib", "${CUDA_ROOT}/lib64"]),
56         Libraries("CUDADRV", ["cuda"]),
57
58         LibraryDir("CUDART", ["${CUDA_ROOT}/lib", "${CUDA_ROOT}/lib64"]),
59         Libraries("CUDART", ["cudart"]),
60
61         LibraryDir("CURAND", ["${CUDA_ROOT}/lib", "${CUDA_ROOT}/lib64"]),
62         Libraries("CURAND", ["curand"]),
63
64         StringListOption("CXXFLAGS", [],
65             help="Any extra C++ compiler options to include"),
66         StringListOption("LDFLAGS", [],
67             help="Any extra linker options to include"),
68         ])
69
70
71 def main():
72     import sys
73
74     from aksetup_helper import (hack_distutils, get_config, setup,
75             NumpyExtension, set_up_shipped_boost_if_requested,
76             check_git_submodules)
77
78     check_git_submodules()
79
80     hack_distutils()
81     conf = get_config(get_config_schema())
82
83     EXTRA_SOURCES, EXTRA_DEFINES = set_up_shipped_boost_if_requested("pycuda", conf)
84
85     EXTRA_DEFINES["PYGPU_PACKAGE"] = "pycuda"
86     EXTRA_DEFINES["PYGPU_PYCUDA"] = "1"
87
88     LIBRARY_DIRS = conf["BOOST_LIB_DIR"] + conf["CUDADRV_LIB_DIR"]
89     LIBRARIES = (conf["BOOST_PYTHON_LIBNAME"] + conf["BOOST_THREAD_LIBNAME"]
90             + conf["CUDADRV_LIBNAME"])
91
92     if not conf["CUDA_INC_DIR"] and conf["CUDA_ROOT"]:
93         conf["CUDA_INC_DIR"] = [join(conf["CUDA_ROOT"], "include")]
94
95     if conf["CUDA_TRACE"]:
96         EXTRA_DEFINES["CUDAPP_TRACE_CUDA"] = 1
97
98     if conf["CUDA_PRETEND_VERSION"]:
99         EXTRA_DEFINES["CUDAPP_PRETEND_CUDA_VERSION"] = conf["CUDA_PRETEND_VERSION"]
100
101     INCLUDE_DIRS = ['src/cpp'] + conf["BOOST_INC_DIR"]
102     if conf["CUDA_INC_DIR"]:
103         INCLUDE_DIRS += conf["CUDA_INC_DIR"]
104
105     conf["USE_CUDA"] = True
106
107     if 'darwin' in sys.platform and sys.maxsize == 2147483647:
108         # The Python interpreter is running in 32 bit mode on OS X
109         if "-arch" not in conf["CXXFLAGS"]:
110             conf["CXXFLAGS"].extend(['-arch', 'i386', '-m32'])
111         if "-arch" not in conf["LDFLAGS"]:
112             conf["LDFLAGS"].extend(['-arch', 'i386', '-m32'])
113
114     if 'darwin' in sys.platform:
115         # set path to Cuda dynamic libraries,
116         # as a safe substitute for DYLD_LIBRARY_PATH
117         for lib_dir in conf["CUDADRV_LIB_DIR"]:
118             conf["LDFLAGS"].extend(["-Xlinker", "-rpath", "-Xlinker", lib_dir])
119
120     if conf["CUDA_ENABLE_GL"]:
121         EXTRA_SOURCES.append("src/wrapper/wrap_cudagl.cpp")
122         EXTRA_DEFINES["HAVE_GL"] = 1
123
124     if conf["CUDA_ENABLE_CURAND"]:
125         EXTRA_DEFINES["HAVE_CURAND"] = 1
126         EXTRA_SOURCES.extend([
127             "src/wrapper/wrap_curand.cpp"
128             ])
129         LIBRARIES.extend(conf["CURAND_LIBNAME"])
130         LIBRARY_DIRS.extend(conf["CURAND_LIB_DIR"])
131
132     ver_dic = {}
133     exec(compile(open("pycuda/__init__.py").read(), "pycuda/__init__.py", 'exec'),
134             ver_dic)
135
136     try:
137         from distutils.command.build_py import build_py_2to3 as build_py
138     except ImportError:
139         # 2.x
140         from distutils.command.build_py import build_py
141
142     import sys
143     if sys.version_info >= (3,):
144         pvt_struct_source = "src/wrapper/_pvt_struct_v3.cpp"
145     else:
146         pvt_struct_source = "src/wrapper/_pvt_struct_v2.cpp"
147
148     setup(name="pycuda",
149             # metadata
150             version=ver_dic["VERSION_TEXT"],
151             description="Python wrapper for Nvidia CUDA",
152             long_description=open("README.rst", "rt").read(),
153             author="Andreas Kloeckner",
154             author_email="inform@tiker.net",
155             license="MIT",
156             url="http://mathema.tician.de/software/pycuda",
157             classifiers=[
158                 'Environment :: Console',
159                 'Development Status :: 5 - Production/Stable',
160                 'Intended Audience :: Developers',
161                 'Intended Audience :: Other Audience',
162                 'Intended Audience :: Science/Research',
163                 'License :: OSI Approved :: MIT License',
164                 'Natural Language :: English',
165                 'Programming Language :: C++',
166                 'Programming Language :: Python',
167                 'Programming Language :: Python :: 3',
168                 'Programming Language :: Python :: 2.6',
169                 'Programming Language :: Python :: 2.7',
170                 'Programming Language :: Python :: 3.3',
171                 'Programming Language :: Python :: 3.4',
172                 'Topic :: Scientific/Engineering',
173                 'Topic :: Scientific/Engineering :: Mathematics',
174                 'Topic :: Scientific/Engineering :: Physics',
175                 'Topic :: Scientific/Engineering :: Visualization',
176                 ],
177
178             # build info
179             packages=["pycuda", "pycuda.gl", "pycuda.sparse", "pycuda.compyte"],
180
181             setup_requires=[
182                 "numpy>=1.6",
183                 ],
184
185             install_requires=[
186                 "pytools>=2011.2",
187                 "pytest>=2",
188                 "decorator>=3.2.0",
189                 "appdirs>=1.4.0"
190                 ],
191
192             ext_package="pycuda",
193             ext_modules=[
194                 NumpyExtension("_driver",
195                     [
196                         "src/cpp/cuda.cpp",
197                         "src/cpp/bitlog.cpp",
198                         "src/wrapper/wrap_cudadrv.cpp",
199                         "src/wrapper/mempool.cpp",
200                         ]+EXTRA_SOURCES,
201                     include_dirs=INCLUDE_DIRS,
202                     library_dirs=LIBRARY_DIRS,
203                     libraries=LIBRARIES,
204                     define_macros=list(EXTRA_DEFINES.items()),
205                     extra_compile_args=conf["CXXFLAGS"],
206                     extra_link_args=conf["LDFLAGS"],
207                     ),
208                 NumpyExtension("_pvt_struct",
209                     [pvt_struct_source],
210                     extra_compile_args=conf["CXXFLAGS"],
211                     extra_link_args=conf["LDFLAGS"],
212                     ),
213                 ],
214
215             include_package_data=True,
216             package_data={
217                     "pycuda": [
218                         "cuda/*.hpp",
219                         ]
220                     },
221
222             zip_safe=False,
223
224             # 2to3 invocation
225             cmdclass={'build_py': build_py})
226
227
228 if __name__ == '__main__':
229     main()