Merge pull request #109 from greenstick/patch-1
[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     default_lib_dirs = [
40         "${CUDA_ROOT}/lib", "${CUDA_ROOT}/lib64",
41         # https://github.com/inducer/pycuda/issues/98
42         "${CUDA_ROOT}/lib/stubs", "${CUDA_ROOT}/lib64/stubs",
43         ]
44
45     return ConfigSchema(make_boost_base_options() + [
46         Switch("USE_SHIPPED_BOOST", True, "Use included Boost library"),
47
48         BoostLibraries("python"),
49         BoostLibraries("thread"),
50
51         Switch("CUDA_TRACE", False, "Enable CUDA API tracing"),
52         Option("CUDA_ROOT", default=cuda_root_default,
53             help="Path to the CUDA toolkit"),
54         Option("CUDA_PRETEND_VERSION",
55             help="Assumed CUDA version, in the form 3010 for 3.1."),
56         IncludeDir("CUDA", None),
57
58         Switch("CUDA_ENABLE_GL", False, "Enable CUDA GL interoperability"),
59         Switch("CUDA_ENABLE_CURAND", True, "Enable CURAND library"),
60
61         LibraryDir("CUDADRV", default_lib_dirs),
62         Libraries("CUDADRV", ["cuda"]),
63
64         LibraryDir("CUDART", default_lib_dirs),
65         Libraries("CUDART", ["cudart"]),
66
67         LibraryDir("CURAND", default_lib_dirs),
68         Libraries("CURAND", ["curand"]),
69
70         StringListOption("CXXFLAGS", [],
71             help="Any extra C++ compiler options to include"),
72         StringListOption("LDFLAGS", [],
73             help="Any extra linker options to include"),
74         ])
75
76
77 def main():
78     import sys
79
80     from aksetup_helper import (hack_distutils, get_config, setup,
81             NumpyExtension, set_up_shipped_boost_if_requested,
82             check_git_submodules)
83
84     check_git_submodules()
85
86     hack_distutils()
87     conf = get_config(get_config_schema())
88
89     EXTRA_SOURCES, EXTRA_DEFINES = set_up_shipped_boost_if_requested("pycuda", conf)
90
91     EXTRA_DEFINES["PYGPU_PACKAGE"] = "pycuda"
92     EXTRA_DEFINES["PYGPU_PYCUDA"] = "1"
93
94     LIBRARY_DIRS = conf["BOOST_LIB_DIR"] + conf["CUDADRV_LIB_DIR"]
95     LIBRARIES = (conf["BOOST_PYTHON_LIBNAME"] + conf["BOOST_THREAD_LIBNAME"]
96             + conf["CUDADRV_LIBNAME"])
97
98     if not conf["CUDA_INC_DIR"] and conf["CUDA_ROOT"]:
99         conf["CUDA_INC_DIR"] = [join(conf["CUDA_ROOT"], "include")]
100
101     if conf["CUDA_TRACE"]:
102         EXTRA_DEFINES["CUDAPP_TRACE_CUDA"] = 1
103
104     if conf["CUDA_PRETEND_VERSION"]:
105         EXTRA_DEFINES["CUDAPP_PRETEND_CUDA_VERSION"] = conf["CUDA_PRETEND_VERSION"]
106
107     INCLUDE_DIRS = ['src/cpp'] + conf["BOOST_INC_DIR"]
108     if conf["CUDA_INC_DIR"]:
109         INCLUDE_DIRS += conf["CUDA_INC_DIR"]
110
111     conf["USE_CUDA"] = True
112
113     if 'darwin' in sys.platform and sys.maxsize == 2147483647:
114         # The Python interpreter is running in 32 bit mode on OS X
115         if "-arch" not in conf["CXXFLAGS"]:
116             conf["CXXFLAGS"].extend(['-arch', 'i386', '-m32'])
117         if "-arch" not in conf["LDFLAGS"]:
118             conf["LDFLAGS"].extend(['-arch', 'i386', '-m32'])
119
120     if 'darwin' in sys.platform:
121         # set path to Cuda dynamic libraries,
122         # as a safe substitute for DYLD_LIBRARY_PATH
123         for lib_dir in conf["CUDADRV_LIB_DIR"]:
124             conf["LDFLAGS"].extend(["-Xlinker", "-rpath", "-Xlinker", lib_dir])
125
126     if conf["CUDA_ENABLE_GL"]:
127         EXTRA_SOURCES.append("src/wrapper/wrap_cudagl.cpp")
128         EXTRA_DEFINES["HAVE_GL"] = 1
129
130     if conf["CUDA_ENABLE_CURAND"]:
131         EXTRA_DEFINES["HAVE_CURAND"] = 1
132         EXTRA_SOURCES.extend([
133             "src/wrapper/wrap_curand.cpp"
134             ])
135         LIBRARIES.extend(conf["CURAND_LIBNAME"])
136         LIBRARY_DIRS.extend(conf["CURAND_LIB_DIR"])
137
138     ver_dic = {}
139     exec(compile(open("pycuda/__init__.py").read(), "pycuda/__init__.py", 'exec'),
140             ver_dic)
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
225 if __name__ == '__main__':
226     main()