1227d34b9527b11e05d5defde610314c64de9025
[ealt-edge.git] / example-apps / PDD / pcb-defect-detection / libs / box_utils / cython_utils / setup.py
1 # --------------------------------------------------------
2 # Fast R-CNN
3 # Copyright (c) 2015 Microsoft
4 # Licensed under The MIT License [see LICENSE for details]
5 # Written by Ross Girshick
6 # --------------------------------------------------------
7
8 import os
9 from os.path import join as pjoin
10 import numpy as np
11 from distutils.core import setup
12 from distutils.extension import Extension
13 from Cython.Distutils import build_ext
14
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)
22     return None
23
24 def locate_cuda():
25     """Locate the CUDA environment on the system
26
27     Returns a dict with keys 'home', 'nvcc', 'include', and 'lib64'
28     and values giving the absolute path to each directory.
29
30     Starts by looking for the CUDAHOME env variable. If not found, everything
31     is based on finding 'nvcc' in the PATH.
32     """
33
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')
38     else:
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)
42         if nvcc is None:
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))
46
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))
53
54     return cudaconfig
55 CUDA = locate_cuda()
56
57 # Obtain the numpy include directory.  This logic works across numpy versions.
58 try:
59     numpy_include = np.get_include()
60 except AttributeError:
61     numpy_include = np.get_numpy_include()
62
63 def customize_compiler_for_nvcc(self):
64     """inject deep into distutils to customize how the dispatch
65     to gcc/nvcc works.
66
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."""
72
73     # tell the compiler it can processes .cu
74     self.src_extensions.append('.cu')
75
76     # save references to the default compiler_so and _comple methods
77     default_compiler_so = self.compiler_so
78     super = self._compile
79
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):
84         print(extra_postargs)
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']
91         else:
92             postargs = extra_postargs['gcc']
93
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
97
98     # inject our redefined _compile method into the class
99     self._compile = _compile
100
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)
106
107 ext_modules = [
108     Extension(
109         "cython_bbox",
110         ["bbox.pyx"],
111         extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
112         include_dirs = [numpy_include]
113     ),
114     Extension(
115         "cython_nms",
116         ["nms.pyx"],
117         extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
118         include_dirs = [numpy_include]
119     )
120     # Extension(
121     #     "cpu_nms",
122     #     ["cpu_nms.pyx"],
123     #     extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
124     #     include_dirs = [numpy_include]
125     # )
126 ]
127
128 setup(
129     name='tf_faster_rcnn',
130     ext_modules=ext_modules,
131     # inject our custom trigger
132     cmdclass={'build_ext': custom_build_ext},
133 )