setup.py 13.7 KB
Newer Older
1
# No 3rd-party modules here, see "3rd-party" note below
2 3 4 5 6 7 8 9
import io
import os
import os.path
import sys
import runpy
import subprocess
import re
import sysconfig
10

11

12 13
def main():
    os.chdir(os.path.dirname(os.path.abspath(__file__)))
14

15 16 17
    # These are neede for source fetching
    cmake_source_dir = "opencv"
    build_contrib = get_build_contrib()
18

19 20 21 22 23 24
    # Only import 3rd-party modules after having installed all the build dependencies:
    # any of them, or their dependencies, can be updated during that process,
    # leading to version conflicts
    numpy_version = get_or_install("numpy", "1.11.3" if sys.version_info[:2] >= (3, 6) else "1.11.1")
    get_or_install("scikit-build")
    import skbuild
25

26
    if os.path.exists('.git'):
27

28 29 30
        import pip.vcs.git
        g = pip.vcs.git.Git()
        use_depth = g.get_git_version() >= type(g.get_git_version())("1.8.4")
31 32 33 34 35

        g.run_command(["submodule", "update", "--init", "--recursive"] +
                      (["--depth=1"] if use_depth else []) +
                      [cmake_source_dir])

36
        if build_contrib:
37 38 39
            g.run_command(["submodule", "update", "--init", "--recursive"] +
                          (["--depth=1"] if use_depth else []) +
                          ["opencv_contrib"])
O
Olli-Pekka Heinisuo 已提交
40

41
        del use_depth, g, pip
O
Olli-Pekka Heinisuo 已提交
42

43
    # https://stackoverflow.com/questions/1405913/python-32bit-or-64bit-mode
44
    x64 = sys.maxsize > 2**32
45

46 47 48
    package_name = "opencv-contrib-python" if build_contrib else "opencv-python"
    long_description = io.open('README_CONTRIB.rst' if build_contrib else 'README.rst', encoding="utf-8").read()
    package_version = get_opencv_version()
O
Olli-Pekka Heinisuo 已提交
49

50
    packages = ['cv2', 'cv2.data']
51 52 53 54
    package_data = {
        'cv2':
            ['*%s' % sysconfig.get_config_var('SO')] +
            ['*.dll'] if os.name == 'nt' else [] +
55
            ["LICENSE.txt", "LICENSE-3RD-PARTY.txt"],
56
        'cv2.data':
57
            ["*.xml"]
58
    }
59 60 61

    # Files from CMake output to copy to package.
    # Path regexes with forward slashes relative to CMake install dir.
62 63
    rearrange_cmake_output_data = {
        'cv2': [r'bin/opencv_ffmpeg\d{3}%s\.dll' % ('_64' if x64 else '')] if os.name == 'nt' else [] +
64 65 66
            # In Windows, in python/X.Y/<arch>/; in Linux, in just python/X.Y/.
            # Naming conventions vary so widely between versions and OSes
            # had to give up on checking them.
67 68 69 70 71 72 73
            ['python/([^/]+/){1,2}cv2[^/]*%(ext)s' % {'ext': re.escape(sysconfig.get_config_var('SO'))}],
        'cv2.data': [  # OPENCV_OTHER_INSTALL_PATH
            ('etc' if os.name == 'nt' else 'share/OpenCV') +
            r'/haarcascades/.*\.xml'
        ]
    }

74 75
    # Files in sourcetree outside package dir that should be copied to package.
    # Raw paths relative to sourcetree root.
76 77 78
    files_outside_package_dir = {
        'cv2': ['LICENSE.txt', 'LICENSE-3RD-PARTY.txt']
    }
79 80 81 82

    cmake_args = ([
        "-G", "Visual Studio 14" + (" Win64" if x64 else '')
    ] if os.name == 'nt' else [
83 84
        "-G", "Unix Makefiles"  # don't make CMake try (and fail) Ninja first
    ]) +
85 86 87 88 89 90 91 92 93 94 95 96 97
    [
        # skbuild inserts PYTHON_* vars. That doesn't satisfy opencv build scripts in case of Py3
        "-DPYTHON%d_EXECUTABLE=%s" % (sys.version_info[0], sys.executable),
        "-DBUILD_opencv_python%d=ON" % sys.version_info[0],
        # Otherwise, opencv scripts would want to install `.pyd' right into site-packages,
        # and skbuild bails out on seeing that
        "-DINSTALL_CREATE_DISTRIB=ON",
        # See opencv/CMakeLists.txt for options and defaults
        "-DBUILD_opencv_apps=OFF",
        "-DBUILD_SHARED_LIBS=OFF",
        "-DBUILD_TESTS=OFF",
        "-DBUILD_PERF_TESTS=OFF",
        "-DBUILD_DOCS=OFF"
98 99 100
    ] +
    (["-DOPENCV_EXTRA_MODULES_PATH=" + os.path.abspath("opencv_contrib/modules")] if build_contrib else [])

101 102
    # OS-specific components
    if sys.platform == 'darwin' or sys.platform.startswith('linux'):
103 104
        cmake_args.append("-DWITH_QT=4")

105
    if sys.platform.startswith('linux'):
106 107
        cmake_args.append("-DWITH_V4L=ON")

108 109
        if all(v in os.environ for v in ('JPEG_INCLUDE_DIR', 'JPEG_LIBRARY')):
            cmake_args += [
110 111 112
                "-DBUILD_JPEG=OFF",
                "-DJPEG_INCLUDE_DIR=%s" % os.environ['JPEG_INCLUDE_DIR'],
                "-DJPEG_LIBRARY=%s" % os.environ['JPEG_LIBRARY']
113
            ]
114

115 116 117 118 119
    # Turn off broken components
    if sys.platform == 'darwin':
        cmake_args.append("-DWITH_LAPACK=OFF")  # Some OSX LAPACK fns are incompatible, see
                                                # https://github.com/skvark/opencv-python/issues/21
    if sys.platform.startswith('linux'):
120
        cmake_args.append("-DWITH_IPP=OFF")   # https://github.com/opencv/opencv/issues/10411
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

    # ABI config variables are introduced in PEP 425
    if sys.version_info[:2] < (3, 2):
        import warnings
        warnings.filterwarnings('ignore', r"Config variable '[^']+' is unset, "
                                          r"Python ABI tag may be incorrect",
                                category=RuntimeWarning)
        del warnings

    # works via side effect
    RearrangeCMakeOutput(rearrange_cmake_output_data,
                         files_outside_package_dir,
                         package_data.keys())

    skbuild.setup(
        name=package_name,
        version=package_version,
        url='https://github.com/skvark/opencv-python',
        license='MIT',
        description='Wrapper package for OpenCV python bindings.',
        long_description=long_description,
        packages=packages,
        package_data=package_data,
        maintainer="Olli-Pekka Heinisuo",
        include_package_data=True,
        ext_modules=EmptyListWithLength(),
        install_requires="numpy>=%s" % numpy_version,
        classifiers=[
          'Development Status :: 5 - Production/Stable',
          'Environment :: Console',
          'Intended Audience :: Developers',
          'Intended Audience :: Education',
          'Intended Audience :: Information Technology',
          'Intended Audience :: Science/Research',
          'License :: OSI Approved :: MIT License',
          'Operating System :: MacOS',
          'Operating System :: Microsoft :: Windows',
          'Operating System :: POSIX',
          'Operating System :: Unix',
          'Programming Language :: Python',
          'Programming Language :: Python :: 2',
          'Programming Language :: Python :: 2.7',
          'Programming Language :: Python :: 3',
          'Programming Language :: Python :: 3.4',
          'Programming Language :: Python :: 3.5',
          'Programming Language :: Python :: 3.6',
          'Programming Language :: C++',
          'Programming Language :: Python :: Implementation :: CPython',
          'Topic :: Scientific/Engineering',
          'Topic :: Scientific/Engineering :: Image Recognition',
          'Topic :: Software Development',
        ],
        cmake_args=cmake_args,
        cmake_source_dir=cmake_source_dir,
          )


class RearrangeCMakeOutput(object):
179 180 181 182
    """
        Patch SKBuild logic to only take files related to the Python package
        and construct a file hierarchy that SKBuild expects (see below)
    """
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    _setuptools_wrap = None

    # Have to wrap a function reference, or it's converted
    # into an instance method on attr assignment
    import argparse
    wraps = argparse.Namespace(
        _classify_files=None)
    del argparse

    package_paths_re = None
    packages = None
    files_outside_package = None

    def __init__(self, package_paths_re, files_outside_package, packages):
        cls = self.__class__
        assert not cls.wraps._classify_files, "Singleton object"
        import skbuild.setuptools_wrap

        cls._setuptools_wrap = skbuild.setuptools_wrap
        cls.wraps._classify_files = cls._setuptools_wrap._classify_files
        cls._setuptools_wrap._classify_files = self._classify_files_override

        cls.package_paths_re = package_paths_re
        cls.files_outside_package = files_outside_package
        cls.packages = packages
208

209 210 211 212 213 214 215 216 217 218 219 220 221
    def __del__(self):
        cls = self.__class__
        cls._setuptools_wrap._classify_files = cls.wraps._classify_files
        cls.wraps._classify_files = None
        cls._setuptools_wrap = None

    def _classify_files_override(self, install_paths,
            package_data, package_prefixes,
            py_modules, new_py_modules,
            scripts, new_scripts,
            data_files,
            cmake_source_dir, cmake_install_reldir):
        """
222 223 224 225 226 227 228 229 230
            From all CMake output, we're only interested in a few files
            and must place them into CMake install dir according
            to Python conventions for SKBuild to find them:
                package\
                    file
                    subpackage\
                        etc.
        """

231
        cls = self.__class__
232

233 234 235 236 237 238 239 240 241 242
        # 'relpath'/'reldir' = relative to CMAKE_INSTALL_DIR/cmake_install_dir
        # 'path'/'dir' = relative to sourcetree root
        cmake_install_dir = os.path.join(cls._setuptools_wrap.CMAKE_INSTALL_DIR,
                                         cmake_install_reldir)
        install_relpaths = [os.path.relpath(p, cmake_install_dir) for p in install_paths]
        fslash_install_relpaths = [p.replace(os.path.sep, '/') for p in install_relpaths]
        relpaths_zip = list(zip(fslash_install_relpaths, install_relpaths))
        del install_relpaths, fslash_install_relpaths

        final_install_relpaths = []
O
Olli-Pekka Heinisuo 已提交
243

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        print("Copying files from CMake output")
        for package_name, relpaths_re in cls.package_paths_re.items():
            package_dest_reldir = package_name.replace('.', os.path.sep)
            for relpath_re in relpaths_re:
                found = False
                r = re.compile(relpath_re+'$')
                for fslash_relpath, relpath in relpaths_zip:
                    m = r.match(fslash_relpath)
                    if not m: continue
                    found = True
                    new_install_relpath = os.path.join(
                        package_dest_reldir,
                        os.path.basename(relpath))
                    cls._setuptools_wrap._copy_file(
                        os.path.join(cmake_install_dir, relpath),
                        os.path.join(cmake_install_dir, new_install_relpath),
                        hide_listing=False)
                    final_install_relpaths.append(new_install_relpath)
                    del m, fslash_relpath, new_install_relpath
                else:
                    if not found: raise Exception("Not found: '%s'" % relpath_re)
                del r, found
O
Olli-Pekka Heinisuo 已提交
266

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
        del relpaths_zip

        print("Copying files from non-default sourcetree locations")
        for package_name, paths in cls.files_outside_package.items():
            package_dest_reldir = package_name.replace('.', os.path.sep)
            for path in paths:
                new_install_relpath = os.path.join(
                        package_dest_reldir,
                        # Don't yet have a need to copy
                        # to subdirectories of package dir
                        os.path.basename(path))
                cls._setuptools_wrap._copy_file(
                    path, os.path.join(cmake_install_dir, new_install_relpath),
                    hide_listing = False
                )
                final_install_relpaths.append(new_install_relpath)


        final_install_paths = [os.path.join(cmake_install_dir, p) for p in final_install_relpaths]

        return (cls.wraps._classify_files)(
            final_install_paths,
            package_data, package_prefixes,
            py_modules, new_py_modules,
            scripts, new_scripts,
            data_files,
293 294 295 296
            # To get around a check that prepends source dir to paths and breaks package detection code.
            cmake_source_dir='',
            cmake_install_dir=cmake_install_reldir
        )
297 298 299 300


def install_packages(*requirements):
    # No more convenient way until PEP 518 is implemented; setuptools only handles eggs
301
    subprocess.check_call([sys.executable, "-m", "pip", "install"] + list(requirements))
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325


def get_opencv_version():
    # cv_version.py should be generated by running find_version.py
    runpy.run_path("find_version.py")
    from cv_version import opencv_version
    return opencv_version


def get_build_contrib():
    build_contrib = False
    try:
        build_contrib = bool(int(os.getenv('ENABLE_CONTRIB', None)))
    except Exception:
        pass

    if not build_contrib:
        try:
            build_contrib = bool(int(open("contrib.enabled").read(1)))
        except Exception:
            pass
    return build_contrib


326
def get_or_install(name, version=None):
327 328 329 330
    """If a package is already installed, build against it. If not, install"""
    # Do not import 3rd-party modules into the current process
    import json
    js_packages = json.loads(
331 332
        subprocess.check_output([sys.executable, "-m", "pip", "list", "--format=json"])
        .decode('ascii'))  # valid names & versions are ASCII as per PEP 440
333 334 335 336
    try:
        [package] = (package for package in js_packages
                     if package['name'] == name)
    except ValueError:
337
        install_packages("%s==%s" % (name, version) if version else name)
338 339 340 341 342 343 344
        return version
    else:
        return package['version']


# This creates a list which is empty but returns a length of 1.
# Should make the wheel a binary distribution and platlib compliant.
O
Olli-Pekka Heinisuo 已提交
345 346 347 348
class EmptyListWithLength(list):
    def __len__(self):
        return 1

349

350 351
if __name__ == '__main__':
    main()