|
1 | 1 | #! /usr/bin/env python3 |
2 | 2 |
|
3 | | -from __future__importprint_function |
4 | | -fromosimportsys,path |
| 3 | +importos |
| 4 | +importre |
| 5 | +importsys |
| 6 | +importsysconfig |
| 7 | +importplatform |
| 8 | +importsubprocess |
5 | 9 |
|
6 | | -try: |
7 | | -fromskbuildimportsetup |
8 | | -exceptImportError: |
9 | | -print('scikit-build is required to build from source.',file=sys.stderr) |
10 | | -print('Please run:',file=sys.stderr) |
11 | | -print('',file=sys.stderr) |
12 | | -print(' python -m pip install scikit-build') |
13 | | -sys.exit(1) |
| 10 | +fromdistutils.versionimportLooseVersion |
| 11 | +fromsetuptoolsimportsetup,Extension,find_packages |
| 12 | +fromsetuptools.command.build_extimportbuild_ext |
| 13 | +fromsetuptools.command.testimporttestasTestCommand |
| 14 | +fromshutilimportcopyfile,copymode |
14 | 15 |
|
15 | | -fromsetuptoolsimportfind_packages |
| 16 | + |
| 17 | +classCMakeExtension(Extension): |
| 18 | +def__init__(self,name,sourcedir=''): |
| 19 | +Extension.__init__(self,name,sources=[]) |
| 20 | +self.sourcedir=os.path.abspath(sourcedir) |
| 21 | + |
| 22 | + |
| 23 | +classCMakeBuild(build_ext): |
| 24 | +defrun(self): |
| 25 | +try: |
| 26 | +out=subprocess.check_output(['cmake','--version']) |
| 27 | +exceptOSError: |
| 28 | +raiseRuntimeError( |
| 29 | +"CMake must be installed to build the following extensions: "+ |
| 30 | +", ".join(e.nameforeinself.extensions)) |
| 31 | + |
| 32 | +ifplatform.system()=="Windows": |
| 33 | +cmake_version=LooseVersion(re.search(r'version\s*([\d.]+)', |
| 34 | +out.decode()).group(1)) |
| 35 | +ifcmake_version<'3.1.0': |
| 36 | +raiseRuntimeError("CMake >= 3.1.0 is required on Windows") |
| 37 | + |
| 38 | +forextinself.extensions: |
| 39 | +self.build_extension(ext) |
| 40 | + |
| 41 | +defbuild_extension(self,ext): |
| 42 | +extdir=os.path.abspath( |
| 43 | +os.path.dirname(self.get_ext_fullpath(ext.name))) |
| 44 | +cmake_args= ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY='+extdir, |
| 45 | +'-DPYTHON_EXECUTABLE='+sys.executable] |
| 46 | + |
| 47 | +cfg='Debug'ifself.debugelse'Release' |
| 48 | +build_args= ['--config',cfg] |
| 49 | + |
| 50 | +ifplatform.system()=="Windows": |
| 51 | +cmake_args+= ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format( |
| 52 | +cfg.upper(), |
| 53 | +extdir)] |
| 54 | +ifsys.maxsize>2**32: |
| 55 | +cmake_args+= ['-A','x64'] |
| 56 | +build_args+= ['--','/m'] |
| 57 | +else: |
| 58 | +cmake_args+= ['-DCMAKE_BUILD_TYPE='+cfg] |
| 59 | +build_args+= ['--','-j2'] |
| 60 | + |
| 61 | +env=os.environ.copy() |
| 62 | +env['CXXFLAGS']='{} -DVERSION_INFO=\\"{}\\"'.format( |
| 63 | +env.get('CXXFLAGS',''), |
| 64 | +self.distribution.get_version()) |
| 65 | +ifnotos.path.exists(self.build_temp): |
| 66 | +os.makedirs(self.build_temp) |
| 67 | +subprocess.check_call(['cmake',ext.sourcedir]+cmake_args, |
| 68 | +cwd=self.build_temp,env=env) |
| 69 | +subprocess.check_call(['cmake','--build','.']+build_args, |
| 70 | +cwd=self.build_temp) |
| 71 | +# Copy *_test file to tests directory |
| 72 | +test_bin=os.path.join(self.build_temp,'python_cpp_example_test') |
| 73 | +self.copy_test_file(test_bin) |
| 74 | +print()# Add an empty line for cleaner output |
| 75 | + |
| 76 | +defcopy_test_file(self,src_file): |
| 77 | +''' |
| 78 | + Copy ``src_file`` to ``dest_file`` ensuring parent directory exists. |
| 79 | + By default, message like `creating directory /path/to/package` and |
| 80 | + `copying directory /src/path/to/package -> path/to/package` are displayed on standard output. Adapted from scikit-build. |
| 81 | + ''' |
| 82 | +# Create directory if needed |
| 83 | +dest_dir=os.path.join(os.path.dirname( |
| 84 | +os.path.abspath(__file__)),'tests','bin') |
| 85 | +ifdest_dir!=""andnotos.path.exists(dest_dir): |
| 86 | +print("creating directory {}".format(dest_dir)) |
| 87 | +os.makedirs(dest_dir) |
| 88 | + |
| 89 | +# Copy file |
| 90 | +dest_file=os.path.join(dest_dir,os.path.basename(src_file)) |
| 91 | +print("copying {} -> {}".format(src_file,dest_file)) |
| 92 | +copyfile(src_file,dest_file) |
| 93 | +copymode(src_file,dest_file) |
16 | 94 |
|
17 | 95 | setup( |
18 | 96 | name='python_cpp_example', |
19 | | -version='0.3', |
| 97 | +version='0.2', |
20 | 98 | author='Benjamin Jack', |
21 | 99 | author_email='benjamin.r.jack@gmail.com', |
22 | 100 | description='A hybrid Python/C++ test project', |
23 | 101 | long_description='', |
24 | 102 | packages=find_packages('src'), |
25 | 103 | package_dir={'':'src'}, |
| 104 | +ext_modules=[CMakeExtension('python_cpp_example/python_cpp_example')], |
| 105 | +cmdclass=dict(build_ext=CMakeBuild), |
26 | 106 | test_suite='tests', |
27 | 107 | zip_safe=False, |
28 | 108 | ) |