Improve CMake extension builder in setup.py

- Better conform to build_ext directory structure
- Select build type according to build_ext '-g' flag
- Check for CMake >= 3.1 on Windows
This commit is contained in:
Dean Moldovan 2016-06-04 18:31:09 +02:00
parent 0a8d583cd0
commit 1b97821856
3 changed files with 35 additions and 25 deletions

View file

@ -10,6 +10,6 @@ install:
- cmd: git submodule update -q --init --recursive
build_script:
- cmd: python setup.py sdist
- cmd: pip install dist\cmake_example-0.0.1.zip
- cmd: pip install --verbose dist\cmake_example-0.0.1.zip
test_script:
- cmd: python tests\test.py

View file

@ -24,6 +24,6 @@ before_install:
- source venv/bin/activate
install:
- python setup.py sdist
- pip install dist/*.tar.gz
- pip install --verbose dist/*.tar.gz
script:
- python tests/test.py

View file

@ -1,47 +1,57 @@
import os
import re
import sys
import platform
import subprocess
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from distutils.spawn import find_executable
from distutils.version import LooseVersion
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=''):
Extension.__init__(self, name, sources=[])
self.sourcedir = sourcedir
self.sourcedir = os.path.abspath(sourcedir)
class CMakeBuild(build_ext):
def run(self):
if find_executable('cmake') is None:
print("CMake must be installed to build this extension")
sys.exit(-1)
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))
if platform.system() == "Windows":
cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)', out.decode()).group(1))
if cmake_version < '3.1.0':
raise RuntimeError("CMake >= 3.1.0 is required on Windows")
for ext in self.extensions:
build_dir = os.path.join(os.path.dirname(__file__), 'build', 'cmake')
if not os.path.exists(build_dir):
os.makedirs(build_dir)
cmake_dir = os.path.abspath(ext.sourcedir)
self.build_extension(ext)
extpath = self.get_ext_fullpath(ext.name)
extfulldir = os.path.abspath(os.path.dirname(extpath))
cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extfulldir,
'-DPYTHON_EXECUTABLE=' + sys.executable]
build_args = ['--config', 'Release']
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]
if platform.system() == "Windows":
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE=' + extfulldir]
if sys.maxsize > 2**32:
cmake_args += ['-A', 'x64']
build_args += ['--', '/m']
else:
build_args += ['--', '-j2']
cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]
subprocess.check_call(['cmake', cmake_dir] + cmake_args, cwd=build_dir)
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=build_dir)
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']
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp)
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)
setup(
name='cmake_example',