1 # --------------------------------------------------------
3 # Copyright (c) 2015 Microsoft
4 # Licensed under The MIT License [see LICENSE for details]
5 # Written by Ross Girshick
6 # --------------------------------------------------------
9 from os.path import join as pjoin
11 from distutils.core import setup
12 from distutils.extension import Extension
13 from Cython.Distutils import build_ext
15 def find_in_path(name, path):
16 "Find a file in a search path"
17 #adapted fom http://code.activestate.com/recipes/52224-find-a-file-given-a-search-path/
18 for dir in path.split(os.pathsep):
19 binpath = pjoin(dir, name)
20 if os.path.exists(binpath):
21 return os.path.abspath(binpath)
25 """Locate the CUDA environment on the system
27 Returns a dict with keys 'home', 'nvcc', 'include', and 'lib64'
28 and values giving the absolute path to each directory.
30 Starts by looking for the CUDAHOME env variable. If not found, everything
31 is based on finding 'nvcc' in the PATH.
34 # first check if the CUDAHOME env variable is in use
35 if 'CUDAHOME' in os.environ:
36 home = os.environ['CUDAHOME']
37 nvcc = pjoin(home, 'bin', 'nvcc')
39 # otherwise, search the PATH for NVCC
40 default_path = pjoin(os.sep, 'usr', 'local', 'cuda', 'bin')
41 nvcc = find_in_path('nvcc', os.environ['PATH'] + os.pathsep + default_path)
43 raise EnvironmentError('The nvcc binary could not be '
44 'located in your $PATH. Either add it to your path, or set $CUDAHOME')
45 home = os.path.dirname(os.path.dirname(nvcc))
47 cudaconfig = {'home':home, 'nvcc':nvcc,
48 'include': pjoin(home, 'include'),
49 'lib64': pjoin(home, 'lib64')}
50 for k, v in cudaconfig.items():
51 if not os.path.exists(v):
52 raise EnvironmentError('The CUDA %s path could not be located in %s' % (k, v))
57 # Obtain the numpy include directory. This logic works across numpy versions.
59 numpy_include = np.get_include()
60 except AttributeError:
61 numpy_include = np.get_numpy_include()
63 def customize_compiler_for_nvcc(self):
64 """inject deep into distutils to customize how the dispatch
67 If you subclass UnixCCompiler, it's not trivial to get your subclass
68 injected in, and still have the right customizations (i.e.
69 distutils.sysconfig.customize_compiler) run on it. So instead of going
70 the OO route, I have this. Note, it's kindof like a wierd functional
71 subclassing going on."""
73 # tell the compiler it can processes .cu
74 self.src_extensions.append('.cu')
76 # save references to the default compiler_so and _comple methods
77 default_compiler_so = self.compiler_so
80 # now redefine the _compile method. This gets executed for each
81 # object but distutils doesn't have the ability to change compilers
82 # based on source extension: we add it.
83 def _compile(obj, src, ext, cc_args, extra_postargs, pp_opts):
85 if os.path.splitext(src)[1] == '.cu':
86 # use the cuda for .cu files
87 self.set_executable('compiler_so', CUDA['nvcc'])
88 # use only a subset of the extra_postargs, which are 1-1 translated
89 # from the extra_compile_args in the Extension class
90 postargs = extra_postargs['nvcc']
92 postargs = extra_postargs['gcc']
94 super(obj, src, ext, cc_args, postargs, pp_opts)
95 # reset the default compiler_so, which we might have changed for cuda
96 self.compiler_so = default_compiler_so
98 # inject our redefined _compile method into the class
99 self._compile = _compile
101 # run the customize_compiler
102 class custom_build_ext(build_ext):
103 def build_extensions(self):
104 customize_compiler_for_nvcc(self.compiler)
105 build_ext.build_extensions(self)
111 extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
112 include_dirs = [numpy_include]
117 extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
118 include_dirs = [numpy_include]
123 # extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
124 # include_dirs = [numpy_include]
129 name='tf_faster_rcnn',
130 ext_modules=ext_modules,
131 # inject our custom trigger
132 cmdclass={'build_ext': custom_build_ext},