Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. import os
  2. import pathlib
  3. from setuptools import setup, Extension
  4. from setuptools.command.build_ext import build_ext as build_ext_orig
  5.  
  6.  
  7. class CMakeExtension(Extension):
  8.     def __init__(self, name, cmake_lists_dir='.', **kwa):
  9.         Extension.__init__(self, name, sources=[], **kwa)
  10.         self.cmake_lists_dir = os.path.abspath(cmake_lists_dir)
  11.  
  12.  
  13. class build_ext(build_ext_orig):
  14.     def run(self):
  15.         for ext in self.extensions:
  16.             self.build_cmake(ext)
  17.         super().run()
  18.  
  19.     def build_cmake(self, ext):
  20.         cwd = pathlib.Path().absolute()
  21.  
  22.         # these dirs will be created in build_py, so if you don't have
  23.         # any python sources to bundle, the dirs will be missing
  24.         build_temp = pathlib.Path(self.build_temp)
  25.         build_temp.mkdir(parents=True, exist_ok=True)
  26.         extdir = pathlib.Path(self.get_ext_fullpath(ext.name))
  27.         extdir.mkdir(parents=True, exist_ok=True)
  28.  
  29.         # example of cmake args
  30.         config = 'Debug' if self.debug else 'Release'
  31.         cmake_args = [
  32.             '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + str(extdir.parent.absolute()),
  33.             '-DCMAKE_BUILD_TYPE=' + config
  34.         ]
  35.  
  36.         # example of build args
  37.         build_args = [
  38.             '--config', config,
  39.             '--', '-j4'
  40.         ]
  41.  
  42.         os.chdir(str(build_temp))
  43.         self.spawn(['cmake', str(cwd)] + cmake_args)
  44.         if not self.dry_run:
  45.             self.spawn(['cmake', '--build', '.'] + build_args)
  46.         os.chdir(str(cwd))
  47.  
  48.  
  49. def main():
  50.     setup(name="spexi",
  51.           version="1.0.0",
  52.           description="Python interface for the Spexi Geospatial functions",
  53.           author="Colin Basnett",
  54.           author_email="colin@spexigeo.com",
  55.           ext_modules=[CMakeExtension('spexi')],
  56.           cmdclass={
  57.               'build_ext': build_ext,
  58.           })
  59.  
  60.  
  61. if __name__ == "__main__":
  62.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement