NeoPZ
setup.py
Go to the documentation of this file.
1 # This file is not used during generation of NEOPZ python module (.so) through CMake.
2 #
3 # It will be kept here for future use (NEOPZ module installation under Python3 tree)
4 # Installation is not mandatory, as NEOPZ module can be imported directly into Python3, without installation,
5 # under Python command interpreter by doing:
6 # >>> from NEOPZ import *
7 # >>>
8 #
9 # Should one decide to install this module under Python3 tree, just do as follows under a shell terminal (i.e., bash, etc) :
10 # python3 setup.py develop
11 #
12 
13 import os
14 import re
15 import sys
16 import platform
17 import subprocess
18 
19 from setuptools import setup, Extension
20 from setuptools.command.build_ext import build_ext
21 from distutils.version import LooseVersion
22 
23 
24 class CMakeExtension(Extension):
25  def __init__(self, name, sourcedir='..'):
26  Extension.__init__(self, name, sources=[])
27  self.sourcedir = os.path.abspath(sourcedir)
28 
29 
30 class CMakeBuild(build_ext):
31  def run(self):
32  try:
33  out = subprocess.check_output(['cmake', '--version'])
34  except OSError:
35  raise RuntimeError("CMake must be installed to build the following extensions: " +
36  ", ".join(e.name for e in self.extensions))
37 
38  if platform.system() == "Windows":
39  cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)', out.decode()).group(1))
40  if cmake_version < '3.1.0':
41  raise RuntimeError("CMake >= 3.1.0 is required on Windows")
42 
43  for ext in self.extensions:
44  self.build_extension(ext)
45 
46  def build_extension(self, ext):
47  extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
48  cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
49  '-DPYTHON_EXECUTABLE=' + sys.executable,
50  '-DBUILD_ESTIMATION_PROJECTS=OFF',
51  '-DBUILD_OPENCV_PROJECTS=OFF',
52  '-DBUILD_PYTHON_BINDINGS=ON']
53 
54  cfg = 'Debug' if self.debug else 'Release'
55  build_args = ['--config', cfg]
56 
57  if platform.system() == "Windows":
58  cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)]
59  if sys.maxsize > 2**32:
60  cmake_args += ['-A', 'x64']
61  build_args += ['--', '/m']
62  else:
63  cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
64  build_args += ['--', '-j2']
65 
66  env = os.environ.copy()
67  env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''),
68  self.distribution.get_version())
69  if not os.path.exists(self.build_temp):
70  os.makedirs(self.build_temp)
71  subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
72  subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)
73 
74 setup(
75  name='NEOPZ',
76  version='0.0.1',
77  author='Luiz Marques',
78  author_email='lccmarques@gmail.com',
79  description='A test project using pybind11 and CMake',
80  long_description='',
81  ext_modules=[CMakeExtension('NEOPZ')],
82  cmdclass=dict(build_ext=CMakeBuild),
83  zip_safe=False,
84 )
Definition: setup.py:1
def __init__(self, name, sourcedir='..')
Definition: setup.py:25
def run(self)
Definition: setup.py:31
def build_extension(self, ext)
Definition: setup.py:46