2015-08-20 07:43:42 +08:00
|
|
|
"""setup for the dlib project
|
2015-08-20 08:43:15 +08:00
|
|
|
Copyright (C) 2015 Ehsan Azar (dashesy@linux.com)
|
|
|
|
License: Boost Software License See LICENSE.txt for the full license.
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2015-10-27 20:22:33 +08:00
|
|
|
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.
|
|
|
|
|
2015-08-20 07:43:42 +08:00
|
|
|
To build the dlib:
|
|
|
|
python setup.py build
|
|
|
|
To build and install:
|
|
|
|
python setup.py install
|
2016-09-30 09:32:49 +08:00
|
|
|
To package the wheel (after pip installing twine and wheel):
|
2015-08-20 07:43:42 +08:00
|
|
|
python setup.py bdist_wheel
|
2017-08-28 08:04:31 +08:00
|
|
|
To upload the binary wheel to PyPi
|
2016-09-30 09:32:49 +08:00
|
|
|
twine upload dist/*.whl
|
2017-08-28 08:04:31 +08:00
|
|
|
To upload the source distribution to PyPi
|
2018-05-20 08:53:40 +08:00
|
|
|
python setup.py sdist
|
|
|
|
twine upload dist/dlib-*.tar.gz
|
2015-08-22 04:54:49 +08:00
|
|
|
To exclude/include certain options in the cmake config use --yes and --no:
|
|
|
|
for example:
|
2018-01-16 20:24:47 +08:00
|
|
|
--yes USE_AVX_INSTRUCTIONS: will set -DUSE_AVX_INSTRUCTIONS=yes
|
|
|
|
--no USE_AVX_INSTRUCTIONS: will set -DUSE_AVX_INSTRUCTIONS=no
|
2015-08-20 07:43:42 +08:00
|
|
|
Additional options:
|
2018-02-12 20:34:07 +08:00
|
|
|
--compiler-flags: pass flags onto the compiler, e.g. --compiler-flags "-Os -Wall" passes -Os -Wall onto GCC.
|
|
|
|
-G: Set the CMake generator. E.g. -G "Visual Studio 14 2015"
|
2018-01-16 20:24:47 +08:00
|
|
|
--clean: delete any previous build folders and rebuild. You should do this if you change any build options
|
|
|
|
by setting --compiler-flags or --yes or --no since last time you ran a build to make sure the changes
|
|
|
|
take effect.
|
2018-03-10 08:14:12 +08:00
|
|
|
--set: set arbitrary options e.g. --set CUDA_HOST_COMPILER=/usr/bin/gcc-6.4.0
|
2015-08-20 07:43:42 +08:00
|
|
|
"""
|
|
|
|
import os
|
2018-01-16 20:24:47 +08:00
|
|
|
import re
|
2015-08-20 07:43:42 +08:00
|
|
|
import sys
|
2018-01-16 20:24:47 +08:00
|
|
|
import shutil
|
2015-08-20 07:43:42 +08:00
|
|
|
import platform
|
2018-01-16 20:24:47 +08:00
|
|
|
import subprocess
|
2018-01-17 08:41:17 +08:00
|
|
|
import multiprocessing
|
2018-01-16 20:24:47 +08:00
|
|
|
from distutils import log
|
2018-01-17 21:02:43 +08:00
|
|
|
from math import ceil,floor
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
from setuptools import setup, Extension
|
|
|
|
from setuptools.command.build_ext import build_ext
|
|
|
|
from distutils.version import LooseVersion
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2015-08-20 07:49:45 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
def get_extra_cmake_options():
|
2018-03-10 08:14:12 +08:00
|
|
|
"""read --clean, --yes, --no, --set, --compiler-flags, and -G options from the command line and add them as cmake switches.
|
2015-08-21 00:38:57 +08:00
|
|
|
"""
|
2018-01-16 20:24:47 +08:00
|
|
|
_cmake_extra_options = []
|
|
|
|
_clean_build_folder = False
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2015-08-21 00:38:57 +08:00
|
|
|
opt_key = None
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2015-08-25 00:11:55 +08:00
|
|
|
argv = [arg for arg in sys.argv] # take a copy
|
2018-01-16 20:24:47 +08:00
|
|
|
# parse command line options and consume those we care about
|
2018-02-12 20:34:07 +08:00
|
|
|
for arg in argv:
|
2018-01-16 20:24:47 +08:00
|
|
|
if opt_key == 'compiler-flags':
|
|
|
|
_cmake_extra_options.append('-DCMAKE_CXX_FLAGS={arg}'.format(arg=arg.strip()))
|
2018-02-12 20:34:07 +08:00
|
|
|
elif opt_key == 'G':
|
|
|
|
_cmake_extra_options += ['-G', arg.strip()]
|
2015-08-22 04:54:49 +08:00
|
|
|
elif opt_key == 'yes':
|
2018-01-16 20:24:47 +08:00
|
|
|
_cmake_extra_options.append('-D{arg}=yes'.format(arg=arg.strip()))
|
2015-08-22 04:54:49 +08:00
|
|
|
elif opt_key == 'no':
|
2018-01-16 20:24:47 +08:00
|
|
|
_cmake_extra_options.append('-D{arg}=no'.format(arg=arg.strip()))
|
2018-03-10 08:14:12 +08:00
|
|
|
elif opt_key == 'set':
|
|
|
|
_cmake_extra_options.append('-D{arg}'.format(arg=arg.strip()))
|
2015-08-21 00:38:57 +08:00
|
|
|
|
|
|
|
if opt_key:
|
|
|
|
sys.argv.remove(arg)
|
2015-08-21 05:52:42 +08:00
|
|
|
opt_key = None
|
2015-08-21 00:38:57 +08:00
|
|
|
continue
|
2015-08-21 05:33:45 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
if arg == '--clean':
|
|
|
|
_clean_build_folder = True
|
2015-08-24 01:02:20 +08:00
|
|
|
sys.argv.remove(arg)
|
|
|
|
continue
|
|
|
|
|
2018-03-10 08:14:12 +08:00
|
|
|
if arg in ['--yes', '--no', '--set', '--compiler-flags']:
|
2018-01-16 20:24:47 +08:00
|
|
|
opt_key = arg[2:].lower()
|
2015-08-21 00:38:57 +08:00
|
|
|
sys.argv.remove(arg)
|
|
|
|
continue
|
2018-02-12 20:34:07 +08:00
|
|
|
if arg in ['-G']:
|
|
|
|
opt_key = arg[1:]
|
|
|
|
sys.argv.remove(arg)
|
|
|
|
continue
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
return _cmake_extra_options, _clean_build_folder
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
cmake_extra_options,clean_build_folder = get_extra_cmake_options()
|
2015-08-20 07:43:42 +08:00
|
|
|
|
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
class CMakeExtension(Extension):
|
|
|
|
def __init__(self, name, sourcedir=''):
|
|
|
|
Extension.__init__(self, name, sources=[])
|
|
|
|
self.sourcedir = os.path.abspath(sourcedir)
|
2015-08-20 08:43:15 +08:00
|
|
|
|
2015-08-20 07:43:42 +08:00
|
|
|
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
|
2015-08-21 09:03:18 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
if os.path.exists(name):
|
|
|
|
log.info('Removing old directory {}'.format(name))
|
|
|
|
shutil.rmtree(name, ignore_errors=False, onerror=remove_read_only)
|
2015-08-21 09:03:18 +08:00
|
|
|
|
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
class CMakeBuild(build_ext):
|
2015-08-21 09:03:18 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
def get_cmake_version(self):
|
2015-08-20 07:43:42 +08:00
|
|
|
try:
|
2018-01-16 20:24:47 +08:00
|
|
|
out = subprocess.check_output(['cmake', '--version'])
|
2015-08-20 07:43:42 +08:00
|
|
|
except OSError:
|
2018-05-24 01:00:27 +08:00
|
|
|
raise RuntimeError("\n*******************************************************************\n" +
|
|
|
|
" CMake must be installed to build the following extensions: " +
|
|
|
|
", ".join(e.name for e in self.extensions) +
|
|
|
|
"\n*******************************************************************\n")
|
2018-01-16 20:24:47 +08:00
|
|
|
return re.search(r'version\s*([\d.]+)', out.decode()).group(1)
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
def run(self):
|
2018-05-24 01:00:27 +08:00
|
|
|
cmake_version = self.get_cmake_version()
|
2018-01-16 20:24:47 +08:00
|
|
|
if platform.system() == "Windows":
|
2018-05-24 01:00:27 +08:00
|
|
|
if LooseVersion(cmake_version) < '3.1.0':
|
2018-01-16 20:24:47 +08:00
|
|
|
raise RuntimeError("CMake >= 3.1.0 is required on Windows")
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
for ext in self.extensions:
|
|
|
|
self.build_extension(ext)
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
def build_extension(self, ext):
|
|
|
|
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
|
|
|
|
'-DPYTHON_EXECUTABLE=' + sys.executable]
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
cmake_args += cmake_extra_options
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
cfg = 'Debug' if self.debug else 'Release'
|
|
|
|
build_args = ['--config', cfg]
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
if platform.system() == "Windows":
|
|
|
|
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)]
|
|
|
|
if sys.maxsize > 2**32:
|
|
|
|
cmake_args += ['-A', 'x64']
|
|
|
|
build_args += ['--', '/m']
|
|
|
|
else:
|
|
|
|
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
|
2018-01-17 08:41:17 +08:00
|
|
|
# Do a parallel build
|
2018-01-24 21:02:33 +08:00
|
|
|
build_args += ['--', '-j'+str(num_available_cpu_cores(2))]
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
build_folder = os.path.abspath(self.build_temp)
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
if clean_build_folder:
|
|
|
|
rmtree(build_folder)
|
|
|
|
if not os.path.exists(build_folder):
|
|
|
|
os.makedirs(build_folder)
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-18 07:21:48 +08:00
|
|
|
cmake_setup = ['cmake', ext.sourcedir] + cmake_args
|
|
|
|
cmake_build = ['cmake', '--build', '.'] + build_args
|
|
|
|
|
2018-05-07 02:40:09 +08:00
|
|
|
print("Building extension for Python {}".format(sys.version.split('\n',1)[0]))
|
2018-01-18 07:21:48 +08:00
|
|
|
print("Invoking CMake setup: '{}'".format(' '.join(cmake_setup)))
|
2018-05-07 03:19:38 +08:00
|
|
|
sys.stdout.flush()
|
2018-01-18 07:21:48 +08:00
|
|
|
subprocess.check_call(cmake_setup, cwd=build_folder)
|
|
|
|
print("Invoking CMake build: '{}'".format(' '.join(cmake_build)))
|
2018-05-07 03:19:38 +08:00
|
|
|
sys.stdout.flush()
|
2018-01-18 07:21:48 +08:00
|
|
|
subprocess.check_call(cmake_build, cwd=build_folder)
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-17 21:02:43 +08:00
|
|
|
def num_available_cpu_cores(ram_per_build_process_in_gb):
|
2018-01-18 08:12:01 +08:00
|
|
|
if 'TRAVIS' in os.environ and os.environ['TRAVIS']=='true':
|
2018-01-18 07:21:48 +08:00
|
|
|
# When building on travis-ci, just use 2 cores since travis-ci limits
|
|
|
|
# you to that regardless of what the hardware might suggest.
|
|
|
|
return 2
|
2018-01-17 21:02:43 +08:00
|
|
|
try:
|
|
|
|
mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')
|
|
|
|
mem_gib = mem_bytes/(1024.**3)
|
2018-01-24 21:02:33 +08:00
|
|
|
num_cores = multiprocessing.cpu_count()
|
|
|
|
# make sure we have enough ram for each build process.
|
2018-01-17 21:02:43 +08:00
|
|
|
mem_cores = int(floor(mem_gib/float(ram_per_build_process_in_gb)+0.5));
|
|
|
|
# We are limited either by RAM or CPU cores. So pick the limiting amount
|
|
|
|
# and return that.
|
|
|
|
return max(min(num_cores, mem_cores), 1)
|
|
|
|
except ValueError:
|
|
|
|
return 2 # just assume 2 if we can't get the os to tell us the right answer.
|
|
|
|
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
from setuptools.command.test import test as TestCommand
|
|
|
|
class PyTest(TestCommand):
|
|
|
|
user_options = [('pytest-args=', 'a', "Arguments to pass to pytest")]
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
def initialize_options(self):
|
|
|
|
TestCommand.initialize_options(self)
|
2018-02-16 22:29:15 +08:00
|
|
|
self.pytest_args = '--ignore docs --ignore dlib'
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
def run_tests(self):
|
|
|
|
import shlex
|
|
|
|
#import here, cause outside the eggs aren't loaded
|
|
|
|
import pytest
|
|
|
|
errno = pytest.main(shlex.split(self.pytest_args))
|
|
|
|
sys.exit(errno)
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
def read_version_from_cmakelists(cmake_file):
|
|
|
|
"""Read version information
|
|
|
|
"""
|
|
|
|
major = re.findall("set\(CPACK_PACKAGE_VERSION_MAJOR.*\"(.*)\"", open(cmake_file).read())[0]
|
|
|
|
minor = re.findall("set\(CPACK_PACKAGE_VERSION_MINOR.*\"(.*)\"", open(cmake_file).read())[0]
|
|
|
|
patch = re.findall("set\(CPACK_PACKAGE_VERSION_PATCH.*\"(.*)\"", open(cmake_file).read())[0]
|
|
|
|
return major + '.' + minor + '.' + patch
|
2015-08-20 07:43:42 +08:00
|
|
|
|
2018-01-16 20:24:47 +08:00
|
|
|
def read_entire_file(fname):
|
|
|
|
"""Read text out of a file relative to setup.py.
|
|
|
|
"""
|
|
|
|
return open(os.path.join(fname)).read()
|
2017-12-08 22:59:27 +08:00
|
|
|
|
2015-08-20 07:43:42 +08:00
|
|
|
setup(
|
|
|
|
name='dlib',
|
2018-01-16 20:24:47 +08:00
|
|
|
version=read_version_from_cmakelists('dlib/CMakeLists.txt'),
|
2015-08-20 07:43:42 +08:00
|
|
|
description='A toolkit for making real world machine learning and data analysis applications',
|
2018-05-27 01:30:04 +08:00
|
|
|
long_description='See http://dlib.net for documentation.',
|
2015-08-20 07:43:42 +08:00
|
|
|
author='Davis King',
|
|
|
|
author_email='davis@dlib.net',
|
|
|
|
url='https://github.com/davisking/dlib',
|
|
|
|
license='Boost Software License',
|
2018-01-16 20:24:47 +08:00
|
|
|
ext_modules=[CMakeExtension('dlib','tools/python')],
|
|
|
|
cmdclass=dict(build_ext=CMakeBuild, test=PyTest),
|
2015-08-20 07:43:42 +08:00
|
|
|
zip_safe=False,
|
2018-01-16 20:24:47 +08:00
|
|
|
tests_require=['pytest'],
|
|
|
|
packages=['dlib'],
|
|
|
|
keywords=['dlib', 'Computer Vision', 'Machine Learning'],
|
2015-08-21 01:51:07 +08:00
|
|
|
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',
|
2015-08-28 10:07:28 +08:00
|
|
|
'Programming Language :: Python :: 3',
|
|
|
|
'Programming Language :: Python :: 3.4',
|
2018-01-23 08:56:46 +08:00
|
|
|
'Programming Language :: Python :: 3.5',
|
|
|
|
'Programming Language :: Python :: 3.6',
|
2015-08-21 01:51:07 +08:00
|
|
|
'Topic :: Scientific/Engineering',
|
2015-09-27 23:59:58 +08:00
|
|
|
'Topic :: Scientific/Engineering :: Artificial Intelligence',
|
2015-08-21 01:51:07 +08:00
|
|
|
'Topic :: Scientific/Engineering :: Image Recognition',
|
|
|
|
'Topic :: Software Development',
|
|
|
|
],
|
2015-08-20 07:43:42 +08:00
|
|
|
)
|