Skip to content

Commit

Permalink
Make setup.py more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
dean0x7d committed Apr 28, 2017
1 parent 775fc63 commit 5e5923a
Showing 1 changed file with 52 additions and 32 deletions.
84 changes: 52 additions & 32 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import sys
import shutil
import platform
import subprocess

from subprocess import check_call, check_output, CalledProcessError
from distutils.version import LooseVersion

from setuptools import setup, find_packages, Extension
from setuptools.command.build_ext import build_ext
Expand All @@ -16,45 +18,63 @@


class CMakeExtension(Extension):
def __init__(self, name, sourcedir=''):
def __init__(self, name, sourcedir=""):
super().__init__(name, sources=[])
self.sourcedir = sourcedir
self.sourcedir = os.path.abspath(sourcedir)


class CMakeBuild(build_ext):
def run(self):
if shutil.which('cmake') is None:
print("CMake 3.1 or newer is required to build pybinding")
sys.exit(-1)
try:
out = check_output(["cmake", "--version"])
except OSError:
raise RuntimeError("CMake not found. Version 3.1 or newer is required")

cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)', out.decode()).group(1))
if cmake_version < "3.1.0":
raise RuntimeError("CMake 3.1 or newer is required")

for ext in self.extensions:
build_dir = os.path.join(os.path.dirname(__file__), 'build', 'cmake')
os.makedirs(build_dir, exist_ok=True)
cmake_dir = os.path.abspath(ext.sourcedir)

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,
'-DPB_WERROR=' + os.environ.get("PB_WERROR", "OFF"),
'-DPB_TESTS=' + os.environ.get("PB_TESTS", "OFF"),
'-DPB_NATIVE_SIMD=' + os.environ.get("PB_NATIVE_SIMD", "ON"),
'-DPB_MKL=' + os.environ.get("PB_MKL", "OFF"),
'-DPB_CUDA=' + os.environ.get("PB_CUDA", "OFF")]
build_args = ['--config', os.environ.get("PB_BUILD_TYPE", "Release")]

if platform.system() == "Windows":
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE=' + extfulldir]
if sys.maxsize > 2**32:
cmake_args += ['-A', 'x64']
build_args += ['--', '/v:m', '/m']
else:
cmake_args += ['-DCMAKE_BUILD_TYPE=' + os.environ.get("PB_BUILD_TYPE", "Release")]
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 += ["-DPB_WERROR=" + os.environ.get("PB_WERROR", "OFF"),
"-DPB_TESTS=" + os.environ.get("PB_TESTS", "OFF"),
"-DPB_NATIVE_SIMD=" + os.environ.get("PB_NATIVE_SIMD", "ON"),
"-DPB_MKL=" + os.environ.get("PB_MKL", "OFF"),
"-DPB_CUDA=" + os.environ.get("PB_CUDA", "OFF")]

cfg = os.environ.get("PB_BUILD_TYPE", "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 += ["--", "/v:m", "/m"]
else:
cmake_args += ["-DCMAKE_BUILD_TYPE=" + cfg]
if "-j" not in os.environ.get("MAKEFLAGS", ""):
parallel_jobs = 2 if not os.environ.get("READTHEDOCS") else 1
build_args += ['--', '-j{}'.format(parallel_jobs)]

subprocess.check_call(['cmake', cmake_dir] + cmake_args, cwd=build_dir)
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=build_dir)
build_args += ["--", "-j{}".format(parallel_jobs)]

env = os.environ.copy()
env["CXXFLAGS"] = '{} -DCPB_VERSION=\\"{}\\"'.format(env.get("CXXFLAGS", ""),
self.distribution.get_version())

def build():
os.makedirs(self.build_temp, exist_ok=True)
check_call(["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
check_call(["cmake", "--build", "."] + build_args, cwd=self.build_temp)

try:
build()
except CalledProcessError: # possible CMake error if the build cache has been copied
shutil.rmtree(self.build_temp) # delete build cache and try again
build()


def about(package):
Expand Down

0 comments on commit 5e5923a

Please sign in to comment.