Commit 635747e6 authored by Davis King's avatar Davis King

Cleaned up setup.py and the python cmake script.

parent e975eeed
This diff is collapsed.
import os
import re
import sys
import shutil
import platform
import subprocess
from distutils import log
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from distutils.version import LooseVersion
def get_extra_cmake_options():
"""read --clean, --yes, --no, and --compiler-flag options from the command line and add them as cmake switches.
"""
_cmake_extra_options = []
_clean_build_folder = False
opt_key = None
argv = [arg for arg in sys.argv] # take a copy
# parse command line options and consume those we care about
for opt_idx, arg in enumerate(argv):
if opt_key == 'compiler-flags':
_cmake_extra_options.append('-DCMAKE_CXX_FLAGS={arg}'.format(arg=arg.strip()))
elif opt_key == 'yes':
_cmake_extra_options.append('-D{arg}=yes'.format(arg=arg.strip()))
elif opt_key == 'no':
_cmake_extra_options.append('-D{arg}=no'.format(arg=arg.strip()))
if opt_key:
sys.argv.remove(arg)
opt_key = None
continue
if arg == '--clean':
_clean_build_folder = True
sys.argv.remove(arg)
continue
if arg in ['--yes', '--no', '--compiler-flags']:
opt_key = arg[2:].lower()
sys.argv.remove(arg)
continue
return _cmake_extra_options, _clean_build_folder
cmake_extra_options,clean_build_folder = get_extra_cmake_options()
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=''):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
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
if os.path.exists(name):
log.info('Removing old directory {}'.format(name))
shutil.rmtree(name, ignore_errors=False, onerror=remove_read_only)
class CMakeBuild(build_ext):
def get_cmake_version(self):
try:
out = subprocess.check_output(['cmake', '--version'])
except OSError:
raise RuntimeError("CMake must be installed to build the following extensions: " +
", ".join(e.name for e in self.extensions))
return re.search(r'version\s*([\d.]+)', out.decode()).group(1)
def run(self):
if platform.system() == "Windows":
if LooseVersion(self.get_cmake_version()) < '3.1.0':
raise RuntimeError("CMake >= 3.1.0 is required on Windows")
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
'-DPYTHON_EXECUTABLE=' + sys.executable]
cmake_args += cmake_extra_options
cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]
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]
build_args += ['--', '-j2']
build_folder = os.path.abspath(self.build_temp)
if clean_build_folder:
rmtree(build_folder)
if not os.path.exists(build_folder):
os.makedirs(build_folder)
print("Invoking CMake: '{}'".format(['cmake', ext.sourcedir] + cmake_args))
subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=build_folder)
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=build_folder)
from setuptools.command.test import test as TestCommand
class PyTest(TestCommand):
user_options = [('pytest-args=', 'a', "Arguments to pass to pytest")]
def initialize_options(self):
TestCommand.initialize_options(self)
self.pytest_args = ''
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)
def read_version():
"""Read version information
"""
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
def readme(fname):
"""Read text out of a file relative to setup.py.
"""
return open(os.path.join(fname)).read()
setup(
name='dlib',
version=read_version(),
keywords=['dlib', 'Computer Vision', 'Machine Learning'],
description='A toolkit for making real world machine learning and data analysis applications',
long_description=readme('README.md'),
author='Davis King',
author_email='davis@dlib.net',
url='https://github.com/davisking/dlib',
license='Boost Software License',
packages=['dlib'],
ext_modules=[CMakeExtension('dlib','tools/python')],
cmdclass=dict(build_ext=CMakeBuild, test=PyTest),
zip_safe=False,
tests_require=['pytest'],
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',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Topic :: Scientific/Engineering',
'Topic :: Scientific/Engineering :: Artificial Intelligence',
'Topic :: Scientific/Engineering :: Image Recognition',
'Topic :: Software Development',
],
)
......@@ -5,7 +5,6 @@ set(USE_SSE4_INSTRUCTIONS ON CACHE BOOL "Use SSE4 instructions")
# Make DLIB_ASSERT statements not abort the python interpreter, but just return an error.
add_definitions(-DDLIB_NO_ABORT_ON_2ND_FATAL_ERROR)
option(PYTHON3 "Build a Python3 compatible library rather than Python2." OFF)
# Avoid cmake warnings about changes in behavior of some Mac OS X path
# variable we don't care about.
......@@ -13,23 +12,6 @@ if (POLICY CMP0042)
cmake_policy(SET CMP0042 NEW)
endif()
# Sometimes a computer will have multiple python verions installed. So in this
# block of code we find the one in the user's path and add its home folder into
# cmake's search path. That way it will use that version of python first.
if (PYTHON3)
find_program(PYTHON_EXECUTABLE python3)
endif()
if (NOT PYTHON_EXECUTABLE)
find_program(PYTHON_EXECUTABLE python)
endif()
# Resolve symbolic links, hopefully this will give us a path in the proper
# python home directory.
get_filename_component(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE} REALPATH)
# Pick out the parent directories
get_filename_component(PYTHON_PATH ${PYTHON_EXECUTABLE} PATH)
get_filename_component(PYTHON_PATH ${PYTHON_PATH} PATH)
list(APPEND CMAKE_PREFIX_PATH "${PYTHON_PATH}")
if (CMAKE_COMPILER_IS_GNUCXX)
# Just setting CMAKE_POSITION_INDEPENDENT_CODE should be enough to set
......@@ -50,16 +32,12 @@ if (MSVC)
endif()
add_subdirectory(../../dlib/external/pybind11 ./pybind11_build)
add_subdirectory(../../dlib ./dlib_build)
# include dlib so we can link against it
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../../dlib ./dlib_build)
if (USING_OLD_VISUAL_STUDIO_COMPILER)
message(FATAL_ERROR "You have to use a version of Visual Studio that supports C++11. As of December 2017, the only versions that have good enough C++11 support to compile the dlib Pyhton API is a fully updated Visual Studio 2015 or a fully updated Visual Studio 2017. Older versions of either of these compilers have bad C++11 support and will fail to compile the Python extension. ***SO UPDATE YOUR VISUAL STUDIO TO MAKE THIS ERROR GO AWAY***")
endif()
if (WIN32)
message(STATUS "USING PYTHON_LIBS: ${PYTHON_LIBRARIES}")
endif()
# Test for numpy
find_package(PythonInterp)
......@@ -115,12 +93,8 @@ if(NOT ${DLIB_NO_GUI_SUPPORT})
list(APPEND python_srcs src/gui.cpp)
endif()
pybind11_add_module(dlib_ ${python_srcs})
set_target_properties(dlib_
PROPERTIES OUTPUT_NAME dlib)
target_link_libraries(dlib_ PRIVATE dlib)
pybind11_add_module(dlib_python ${python_srcs})
target_link_libraries(dlib_python PRIVATE dlib::dlib)
# Set the output library name to dlib because that's what setup.py and distutils expects.
set_target_properties(dlib_python PROPERTIES OUTPUT_NAME dlib)
# When you run "make install" we will copy the compiled dlib.so (or dlib.pyd)
# library file to the python_examples and the test folder.
install(TARGETS dlib_ LIBRARY DESTINATION ${CMAKE_CURRENT_LIST_DIR}/../../python_examples)
install(TARGETS dlib_ LIBRARY DESTINATION ${CMAKE_CURRENT_LIST_DIR}/test)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment