1+ import os
2+ import re
3+ import sys
4+ import platform
5+ import subprocess
6+
7+ from setuptools import setup , Extension
8+ from setuptools .command .build_ext import build_ext
9+ from distutils .version import LooseVersion
10+
11+
12+ class CMakeExtension (Extension ):
13+ def __init__ (self , name , sourcedir = '' ):
14+ Extension .__init__ (self , name , sources = [])
15+ self .sourcedir = os .path .abspath (sourcedir )
16+
17+
18+ class CMakeBuild (build_ext ):
19+ def run (self ):
20+ try :
21+ out = subprocess .check_output (['cmake' , '--version' ])
22+ except OSError :
23+ raise RuntimeError ("CMake must be installed to build the following extensions: " +
24+ ", " .join (e .name for e in self .extensions ))
25+
26+ if platform .system () == "Windows" :
27+ cmake_version = LooseVersion (re .search (r'version\s*([\d.]+)' , out .decode ()).group (1 ))
28+ if cmake_version < '3.1.0' :
29+ raise RuntimeError ("CMake >= 3.1.0 is required on Windows" )
30+
31+ for ext in self .extensions :
32+ self .build_extension (ext )
33+
34+ def build_extension (self , ext ):
35+ extdir = os .path .abspath (os .path .dirname (self .get_ext_fullpath (ext .name )))
36+
37+ # required for auto-detection of auxiliary "native" libs
38+ if not extdir .endswith (os .path .sep ):
39+ extdir += os .path .sep
40+
41+ cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir ,
42+ '-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=' + extdir ,
43+ '-DPYTHON_EXECUTABLE=' + sys .executable ,
44+ '-DBUILD_PYTHON_MODULE=ON' ]
45+
46+ cfg = 'Debug' if self .debug else 'Release'
47+ build_args = ['--config' , cfg ]
48+
49+ if platform .system () == "Windows" :
50+ cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}' .format (cfg .upper (), extdir )]
51+ if sys .maxsize > 2 ** 32 :
52+ cmake_args += ['-A' , 'x64' ]
53+ build_args += ['--config' , 'Release' ]
54+ build_args += ['--parallel' , '2' ]
55+ else :
56+ cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg ]
57+ build_args += ['--parallel' , '2' ]
58+
59+ env = os .environ .copy ()
60+ env ['CXXFLAGS' ] = '{} -DVERSION_INFO=\\ "{}\\ "' .format (env .get ('CXXFLAGS' , '' ),
61+ self .distribution .get_version ())
62+ if not os .path .exists (self .build_temp ):
63+ os .makedirs (self .build_temp )
64+ subprocess .check_call (['cmake' , ext .sourcedir ] + cmake_args , cwd = self .build_temp , env = env )
65+ subprocess .check_call (['cmake' , '--build' , '.' ] + build_args , cwd = self .build_temp )
66+
67+ setup (
68+ name = 'pytinymesh' ,
69+ version = '0.1.0' ,
70+ author = 'Tatsuya Yatagawa' ,
71+ 72+ description = 'TinyMesh is a light-weight mesh processing library' ,
73+ long_description = '' ,
74+ ext_modules = [CMakeExtension ('pytinymesh' )],
75+ cmdclass = dict (build_ext = CMakeBuild )
76+ )
0 commit comments