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