setup.py 20.3 KB
Newer Older
1
"""setup for the dlib project
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
2
3
 Copyright (C) 2015  Ehsan Azar (dashesy@linux.com)
 License: Boost Software License   See LICENSE.txt for the full license.
4

5
6
7
8
This file basically just uses CMake to compile the dlib python bindings project
located in the tools/python folder and then puts the outputs into standard
python packages.

9
10
11
12
13
14
15
16
17
18
To build the dlib:
    python setup.py build
To build and install:
    python setup.py install
To package the wheel:
    python setup.py bdist_wheel
To repackage the previously built package as wheel (bypassing build):
    python setup.py bdist_wheel --repackage
To install a develop version (egg with symbolic link):
    python setup.py develop
19
20
21
22
To exclude/include certain options in the cmake config use --yes and --no:
    for example:
    --yes DLIB_NO_GUI_SUPPORT: will set -DDLIB_NO_GUI_SUPPORT=yes
    --no DLIB_NO_GUI_SUPPORT: will set -DDLIB_NO_GUI_SUPPORT=no
23
24
25
Additional options:
    --debug: makes a debug build
    --cmake: path to specific cmake executable
26
    --G or -G: name of a build system generator (equivalent of passing -G "name" to cmake)
27
28
29
30
31
32
33
34
35
36
37
"""

from __future__ import print_function
import shutil
import stat
import errno

from setuptools.command.bdist_egg import bdist_egg as _bdist_egg
from setuptools.command.develop import develop as _develop
from distutils.command.build_ext import build_ext as _build_ext
from distutils.command.build import build as _build
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
38
from distutils.errors import DistutilsSetupError
39
from distutils.spawn import find_executable
jimreesman's avatar
jimreesman committed
40
from distutils.sysconfig import get_python_inc, get_python_version, get_config_var
41
42
43
44
45
46
47
48
49
from distutils import log
import os
import sys
from setuptools import Extension, setup
import platform
from subprocess import Popen, PIPE, STDOUT
import signal
from threading import Thread
import time
50
import re
51
52
53
54
55
56
57
58
59
60
61
62


# change directory to this module path
try:
    this_file = __file__
except NameError:
    this_file = sys.argv[0]
this_file = os.path.abspath(this_file)
if os.path.dirname(this_file):
    os.chdir(os.path.dirname(this_file))
script_dir = os.getcwd()

63

Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
64
65
66
67
68
69
def _get_options():
    """read arguments and creates options
    """
    _cmake_path = find_executable("cmake")
    _cmake_extra = []
    _cmake_config = 'Release'
70

Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
71
72
    _options = []
    opt_key = None
73
    _generator_set = False  # if a build generator is set
74

75
    argv = [arg for arg in sys.argv]  # take a copy
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
76
    # parse commandline options and consume those we care about
77
    for opt_idx, arg in enumerate(argv):
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
78
        if opt_key == 'cmake':
79
            _cmake_path = arg
80
        elif opt_key == 'yes':
81
            _cmake_extra.append('-D{arg}=yes'.format(arg=arg.strip()))
82
        elif opt_key == 'no':
83
            _cmake_extra.append('-D{arg}=no'.format(arg=arg.strip()))
84
        elif opt_key == 'G':
85
            _cmake_extra += ['-G', arg.strip()]
86
            _generator_set = True
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
87
88
89

        if opt_key:
            sys.argv.remove(arg)
90
            opt_key = None
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
91
            continue
92

93
94
95
96
97
98
        # Keep -G to resemble cmake's
        if arg == '-G' or arg.lower() == '--g':
            opt_key = 'G'
            sys.argv.remove(arg)
            continue

99
100
        if not arg.startswith('--'):
            continue
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
101

102
        opt = arg[2:].lower()
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
103
104
105
106
107
        if opt == 'cmake':
            _cmake_path = None
            opt_key = opt
            sys.argv.remove(arg)
            continue
108
109
110
111
        elif opt in ['yes', 'no']:
            opt_key = opt
            sys.argv.remove(arg)
            continue
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
112
113
114
115
116
117

        custom_arg = True
        if opt == 'debug':
            _cmake_config = 'Debug'
        elif opt == 'release':
            _cmake_config = 'Release'
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
118
        elif opt in ['repackage']:
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
119
120
121
122
123
            _options.append(opt)
        else:
            custom_arg = False
        if custom_arg:
            sys.argv.remove(arg)
124

125
    return _options, _cmake_config, _cmake_path, _cmake_extra, _generator_set
126

127
options, cmake_config, cmake_path, cmake_extra, generator_set = _get_options()
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
179
180
181
182
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

def reg_value(rk, rname):
    """enumerate the subkeys in a registry key
    :param rk: root key in registry
    :param rname: name of the value we are interested in
    """
    try:
        import _winreg as winreg
    except ImportError:
        # noinspection PyUnresolvedReferences
        import winreg

    count = 0
    try:
        while True:
            name, value, _ = winreg.EnumValue(rk, count)
            if rname == name:
                return value
            count += 1
    except OSError:
        pass

    return None


def enum_reg_key(rk):
    """enumerate the subkeys in a registry key
    :param rk: root key in registry
    """
    try:
        import _winreg as winreg
    except ImportError:
        # noinspection PyUnresolvedReferences
        import winreg

    sub_keys = []
    count = 0
    try:
        while True:
            name = winreg.EnumKey(rk, count)
            sub_keys.append(name)
            count += 1
    except OSError:
        pass

    return sub_keys


def get_msvc_win64_generator():
    """find the default MSVC generator but Win64
    This logic closely matches cmake's resolution for default build generator.
    Only we select the Win64 version of it.
    """
    try:
        import _winreg as winreg
    except ImportError:
        # noinspection PyUnresolvedReferences
        import winreg

    known_vs = {
        "6.0": "Visual Studio 6",
        "7.0": "Visual Studio 7",
        "7.1": "Visual Studio 7 .NET 2003",
        "8.0": "Visual Studio 8 2005",
        "9.0": "Visual Studio 9 2008",
        "10.0": "Visual Studio 10 2010",
        "11.0": "Visual Studio 11 2012",
        "12.0": "Visual Studio 12 2013",
        "14.0": "Visual Studio 14 2015",
    }

    newest_vs = None
    newest_ver = 0

    platform_arch = platform.architecture()[0]
    sam = winreg.KEY_WOW64_32KEY + winreg.KEY_READ if '64' in platform_arch else winreg.KEY_READ
    for vs in ['VisualStudio\\', 'VCExpress\\', 'WDExpress\\']:
        vs_key = "SOFTWARE\\Microsoft\\{vs}\\".format(vs=vs)
        try:
            root_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, vs_key, 0, sam)
        except OSError:
            continue
        try:
            sub_keys = enum_reg_key(root_key)
        except OSError:
            sub_keys = []
        winreg.CloseKey(root_key)
        if not sub_keys:
            continue

        # look to see if we have InstallDir
        for sub_key in sub_keys:
            try:
                root_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, vs_key + sub_key, 0, sam)
            except OSError:
                continue
            ins_dir = reg_value(root_key, 'InstallDir')
            winreg.CloseKey(root_key)

            if not ins_dir:
                continue

            gen_name = known_vs.get(sub_key)
            if gen_name is None:
                # if it looks like a version number
                try:
                    ver = float(sub_key)
                except ValueError:
                    continue
                gen_name = 'Visual Studio %d' % int(ver)
            else:
                ver = float(sub_key)

            if ver > newest_ver:
                newest_vs = gen_name
244
                newest_ver = ver
245
246
247
248
249

    if newest_vs:
        return ['-G', newest_vs + ' Win64']
    return []

250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
try:
    from Queue import Queue, Empty
except ImportError:
    # noinspection PyUnresolvedReferences
    from queue import Queue, Empty  # python 3.x


_ON_POSIX = 'posix' in sys.builtin_module_names


def enqueue_output(out, queue):
    for line in iter(out.readline, b''):
        queue.put(line)


def _log_buf(buf):
    if not buf:
        return
268
269
    if sys.stdout.encoding:
        buf = buf.decode(sys.stdout.encoding)
270
271
272
273
274
275
    buf = buf.rstrip()
    lines = buf.splitlines()
    for line in lines:
        log.info(line)


276
277
def run_process(cmds, timeout=None):
    """run a process asynchronously
278
    :param cmds: list of commands to invoke on a shell e.g. ['make', 'install']
279
    :param timeout: timeout in seconds (optional)
280
281
    """

282
    # open process as its own session, and with no stdout buffering
283
284
285
    p = Popen(cmds,
              stdout=PIPE, stderr=STDOUT,
              bufsize=1,
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
286
              close_fds=_ON_POSIX, preexec_fn=os.setsid if _ON_POSIX else None)
287

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
    q = Queue()
    t = Thread(target=enqueue_output, args=(p.stdout, q))
    t.daemon = True  # thread dies with the program
    t.start()

    _time = time.time()
    e = None
    try:
        while t.isAlive():
            try:
                buf = q.get(timeout=.1)
            except Empty:
                buf = b''
            _log_buf(buf)
            elapsed = time.time() - _time
            if timeout and elapsed > timeout:
                break
        # Make sure we print all the output from the process.
        if p.stdout:
            for line in p.stdout:
                _log_buf(line)
            p.wait()
    except (KeyboardInterrupt, SystemExit) as e:
        # if user interrupted
        pass

    # noinspection PyBroadException
    try:
        os.kill(p.pid, signal.SIGINT)
    except (KeyboardInterrupt, SystemExit) as e:
        pass
    except:
        pass

    # noinspection PyBroadException
    try:
        if e:
            os.kill(p.pid, signal.SIGKILL)
        else:
            p.wait()
    except (KeyboardInterrupt, SystemExit) as e:
        # noinspection PyBroadException
        try:
            os.kill(p.pid, signal.SIGKILL)
        except:
            pass
    except:
        pass

    t.join(timeout=0.1)
    if e:
        raise e
340
341
342
343
344
345
346
347
348
349

    return p.returncode


def readme(fname):
    """Read text out of a file relative to setup.py.
    """
    return open(os.path.join(script_dir, fname)).read()


Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
350
351
352
def read_version():
    """Read version information
    """
353
354
355
356
    major = re.findall("set\(CPACK_PACKAGE_VERSION_MAJOR.*\"(.*)\"", open('dlib/CMakeLists.txt').read())[0]
    minor = re.findall("set\(CPACK_PACKAGE_VERSION_MINOR.*\"(.*)\"", open('dlib/CMakeLists.txt').read())[0]
    patch = re.findall("set\(CPACK_PACKAGE_VERSION_PATCH.*\"(.*)\"", open('dlib/CMakeLists.txt').read())[0]
    return major + '.' + minor + '.' + patch 
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
357

Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
358

359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
def rmtree(name):
    """remove a directory and its subdirectories.
    """
    def remove_read_only(func, path, exc):
        excvalue = exc[1]
        if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
            os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
            func(path)
        else:
            raise
    shutil.rmtree(name, ignore_errors=False, onerror=remove_read_only)


def copy_file(src, dst):
    """copy a single file and log
    """
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
375
    log.info("Copying file %s -> %s." % (src, dst))
376
377
378
    shutil.copy2(src, dst)


379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
def clean_dist():
    """re-create the dist folder
    """
    dist_dir = os.path.join(script_dir, "./dist")
    if os.path.exists(dist_dir):
        log.info('Removing distribution directory %s' % dist_dir)
        rmtree(dist_dir)

    dist_dir = os.path.join(script_dir, "./dist/dlib")
    try:
        os.makedirs(dist_dir)
    except OSError:
        pass


# always start with a clean slate
clean_dist()


398
399
400
401
402
403
404
# noinspection PyPep8Naming
class build(_build):
    def run(self):
        repackage = 'repackage' in options
        if not repackage:
            self.build_dlib()

405
406
        # this is where the extension examples go
        dist_dir_examples = os.path.join(script_dir, "./dist/dlib/examples")
407
408
409
410
411
        try:
            os.makedirs(dist_dir_examples)
        except OSError:
            pass

412
413
        # this is where the extension goes
        dist_dir = os.path.join(script_dir, "./dist/dlib")
414
        log.info('Populating the distribution directory %s ...' % dist_dir)
415
416

        # create the module init files
417
418
419
420
        with open(os.path.join(dist_dir, '__init__.py'), 'w') as f:
            # just so that we can `import dlib` and not `from dlib import dlib`
            f.write('from .dlib import *\n')
            # add version here
Davis King's avatar
Davis King committed
421
            f.write('__version__ = "{ver}"\n'.format(ver=read_version()))
422
423
424
425
        with open(os.path.join(dist_dir_examples, '__init__.py'), 'w'):
            pass

        # this is where the extension and Python examples are located
426
        out_dir = os.path.join(script_dir, "./python_examples")
427

428
429
430
431
432
        # these are the created artifacts we want to package
        dll_ext = ['.so']
        if sys.platform == "win32":
            dll_ext = ['.pyd', '.dll']

433
434
435
436
437
438
439
        ext_found = False
        # manually copy everything to distribution folder with package hierarchy in mind
        names = os.listdir(out_dir)
        for name in names:
            srcname = os.path.join(out_dir, name)
            dstname = os.path.join(dist_dir, name)
            dstextname = os.path.join(dist_dir_examples, name)
440
441
442

            name, extension = os.path.splitext(name.lower())
            if extension in ['.py', '.txt']:
443
                copy_file(srcname, dstextname)
444
            elif extension in dll_ext:
445
446
447
448
449
                if name.startswith('dlib'):
                    ext_found = True
                copy_file(srcname, dstname)

        if not ext_found:
450
            raise DistutilsSetupError("Cannot find built dlib extension module.")
451
452
453
454
455
456
457

        return _build.run(self)

    @staticmethod
    def build_dlib():
        """use cmake to build and install the extension
        """
458
        if cmake_path is None:
459
460
461
462
463
464
465
466
467
468
469
470
471
472
            cmake_install_url = "https://cmake.org/install/"
            message = ("You can install cmake using the instructions at " +
                       cmake_install_url)
            msg_pkgmanager = ("You can install cmake on {0} using "
                              "`sudo {1} install cmake`.")
            if sys.platform == "darwin":
                pkgmanagers = ('brew', 'port')
                for manager in pkgmanagers:
                    if find_executable(manager) is not None:
                        message = msg_pkgmanager.format('OSX', manager)
                        break
            elif sys.platform.startswith('linux'):
                try:
                    import distro
473
                except ImportError as err:
474
                    import pip
475
                    pip_exit = pip.main(['install', '-q', 'distro'])
476
                    if pip_exit > 0:
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
                        log.debug("Unable to install `distro` to identify "
                                  "the recommended command. Falling back "
                                  "to default error message.")
                        distro = err
                    else:
                        import distro
                if not isinstance(distro, ImportError):
                    distname = distro.id()
                    if distname in ('debian', 'ubuntu'):
                        message = msg_pkgmanager.format(
                            distname.title(), 'apt-get')
                    elif distname in ('fedora', 'centos', 'redhat'):
                        pkgmanagers = ("dnf", "yum")
                        for manager in pkgmanagers:
                            if find_executable(manager) is not None:
                                message = msg_pkgmanager.format(
                                    distname.title(), manager)
                                break
495
496
497
498
            raise DistutilsSetupError(
                "Cannot find cmake, ensure it is installed and in the path.\n"
                + message + "\n"
                "You can also specify its path with --cmake parameter.")
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
499

500
501
502
        platform_arch = platform.architecture()[0]
        log.info("Detected Python architecture: %s" % platform_arch)

503
504
        # make sure build artifacts are generated for the version of Python currently running
        cmake_extra_arch = []
505
506
507
        if sys.version_info >= (3, 0):
            cmake_extra_arch += ['-DPYTHON3=yes']

508
509
510
511
512
513
514
515
516
517
518
519
520
        log.info("Detected platform: %s" % sys.platform)
        if sys.platform == "darwin":
            # build on OS X
            inc_dir = get_python_inc()
            cmake_extra_arch += ['-DPYTHON_INCLUDE_DIR={inc}'.format(inc=inc_dir)]

            # by default, cmake will choose the system python lib in /usr/lib
            # this checks the sysconfig and will correctly pick up a brewed python lib
            # e.g. in /usr/local/Cellar
            py_ver = get_python_version()
            py_lib = os.path.join(get_config_var('LIBDIR'), 'libpython'+py_ver+'.dylib')
            cmake_extra_arch += ['-DPYTHON_LIBRARY={lib}'.format(lib=py_lib)]

521
        if platform_arch == '64bit' and sys.platform == "win32":
522
523
524
525
526
527
            # 64bit build on Windows

            if not generator_set:
                # see if we can deduce the 64bit default generator
                cmake_extra_arch += get_msvc_win64_generator()

528
529
530
531
532
533
534
535
536
537
538
539
            # help cmake to find Python library in 64bit Python in Windows
            #  because cmake is 32bit and cannot find PYTHON_LIBRARY from registry.
            inc_dir = get_python_inc()
            cmake_extra_arch += ['-DPYTHON_INCLUDE_DIR={inc}'.format(inc=inc_dir)]

            # this imitates cmake in path resolution
            py_ver = get_python_version()
            for ext in [py_ver.replace(".", "") + '.lib', py_ver + 'mu.lib', py_ver + 'm.lib', py_ver + 'u.lib']:
                py_lib = os.path.abspath(os.path.join(inc_dir, '../libs/', 'python' + ext))
                if os.path.exists(py_lib):
                    cmake_extra_arch += ['-DPYTHON_LIBRARY={lib}'.format(lib=py_lib)]
                    break
540

541
        build_dir = os.path.join(script_dir, "./tools/python/build")
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
        if os.path.exists(build_dir):
            log.info('Removing build directory %s' % build_dir)
            rmtree(build_dir)

        try:
            os.makedirs(build_dir)
        except OSError:
            pass

        # cd build
        os.chdir(build_dir)
        log.info('Configuring cmake ...')
        cmake_cmd = [
            cmake_path,
            "..",
557
        ] + cmake_extra + cmake_extra_arch
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
        if run_process(cmake_cmd):
            raise DistutilsSetupError("cmake configuration failed!")

        log.info('Build using cmake ...')

        cmake_cmd = [
            cmake_path,
            "--build", ".",
            "--config", cmake_config,
            "--target", "install",
        ]

        if run_process(cmake_cmd):
            raise DistutilsSetupError("cmake build failed!")

573
        # cd back where setup awaits
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
        os.chdir(script_dir)


# noinspection PyPep8Naming
class develop(_develop):

    def __init__(self, *args, **kwargs):
        _develop.__init__(self, *args, **kwargs)

    def run(self):
        self.run_command("build")
        return _develop.run(self)


# noinspection PyPep8Naming
class bdist_egg(_bdist_egg):
    def __init__(self, *args, **kwargs):
        _bdist_egg.__init__(self, *args, **kwargs)

    def run(self):
        self.run_command("build")
        return _bdist_egg.run(self)


# noinspection PyPep8Naming
class build_ext(_build_ext):
    def __init__(self, *args, **kwargs):
        _build_ext.__init__(self, *args, **kwargs)

    def run(self):
        # cmake will do the heavy lifting, just pick up the fruits of its labour
        pass

setup(
    name='dlib',
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
609
    version=read_version(),
610
611
    keywords=['dlib', 'Computer Vision', 'Machine Learning'],
    description='A toolkit for making real world machine learning and data analysis applications',
Davis King's avatar
Davis King committed
612
    long_description=readme('README.txt'),
613
614
615
616
    author='Davis King',
    author_email='davis@dlib.net',
    url='https://github.com/davisking/dlib',
    license='Boost Software License',
617
    packages=['dlib'],
618
619
620
621
622
623
624
625
626
627
628
    package_dir={'': 'dist'},
    include_package_data=True,
    cmdclass={
        'build': build,
        'build_ext': build_ext,
        'bdist_egg': bdist_egg,
        'develop': develop,
    },
    zip_safe=False,
    ext_modules=[Extension('dlib', [])],
    ext_package='dlib',
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Intended Audience :: Science/Research',
        'Intended Audience :: Developers',
        'Operating System :: MacOS :: MacOS X',
        'Operating System :: POSIX',
        'Operating System :: POSIX :: Linux',
        'Operating System :: Microsoft',
        'Operating System :: Microsoft :: Windows',
        'Programming Language :: C++',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
643
644
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.4',
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
645
        'Topic :: Scientific/Engineering',
Davis King's avatar
Davis King committed
646
        'Topic :: Scientific/Engineering :: Artificial Intelligence',
Ehsan Azarnasab's avatar
Ehsan Azarnasab committed
647
648
649
        'Topic :: Scientific/Engineering :: Image Recognition',
        'Topic :: Software Development',
    ],
650
)