From 47219d7ef4c98f0a29658077d5bf4e78646722d3 Mon Sep 17 00:00:00 2001 From: Nikhar Abbas Date: Fri, 6 Nov 2020 16:53:42 -0700 Subject: [PATCH] Restore ROSCO submodule, build it in setup.py --- .gitmodules | 3 ++ ROSCO | 1 + setup.py | 108 ++++++++++++++++++++++++++++++++++------------------ 3 files changed, 75 insertions(+), 37 deletions(-) create mode 160000 ROSCO diff --git a/.gitmodules b/.gitmodules index e69de29bb..49ebf7787 100644 --- a/.gitmodules +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "ROSCO"] + path = ROSCO + url = https://github.com/NREL/ROSCO diff --git a/ROSCO b/ROSCO new file mode 160000 index 000000000..cffa23422 --- /dev/null +++ b/ROSCO @@ -0,0 +1 @@ +Subproject commit cffa2342288858f165255fc13126813f50f3df92 diff --git a/setup.py b/setup.py index 525fbfc2d..f4290791d 100644 --- a/setup.py +++ b/setup.py @@ -24,6 +24,15 @@ import glob import platform from setuptools import find_packages, setup, Command +from numpy.distutils.command.build_ext import build_ext +from numpy.distutils.core import setup, Extension + +import multiprocessing +from distutils.core import run_setup +from setuptools import find_packages +from numpy.distutils.command.build_ext import build_ext +from numpy.distutils.core import setup, Extension +from io import open # Package meta-data. NAME = 'rosco-toolbox' @@ -54,6 +63,48 @@ } +# For the CMake Extensions +this_directory = os.path.abspath(os.path.dirname(__file__)) + +class CMakeExtension(Extension): + + def __init__(self, name, sourcedir='', **kwa): + Extension.__init__(self, name, sources=[], **kwa) + self.sourcedir = os.path.abspath(sourcedir) + +class CMakeBuildExt(build_ext): + + def copy_extensions_to_source(self): + newext = [] + for ext in self.extensions: + if isinstance(ext, CMakeExtension): continue + newext.append( ext ) + self.extensions = newext + super().copy_extensions_to_source() + + def build_extension(self, ext): + if isinstance(ext, CMakeExtension): + # Ensure that CMake is present and working + try: + self.spawn(['cmake', '--version']) + except OSError: + raise RuntimeError('Cannot find CMake executable') + + # Refresh build directory + localdir = os.path.join(this_directory, 'ROSCO','build') + os.makedirs(localdir, exist_ok=True) + + # Build + self.spawn(['cmake', '-S', ext.sourcedir, '-B', localdir]) + self.spawn(['cmake', '--build', localdir]) + + else: + super().build_extension(ext) + + +# All of the extensions +roscoExt = CMakeExtension('rosco','ROSCO') + # The rest you shouldn't have to touch too much :) # ------------------------------------------------ # Except, perhaps the License and Trove Classifiers! @@ -115,42 +166,25 @@ def run(self): sys.exit() -# Where the magic happens: -setup( - name=NAME, - version=about['__version__'], - description=DESCRIPTION, - long_description=long_description, - long_description_content_type='text/markdown', - author=AUTHOR, - author_email=EMAIL, - python_requires=REQUIRES_PYTHON, - url=URL, - packages=find_packages(exclude=["tests", "*.tests", "*.tests.*", "tests.*"]), - install_requires=REQUIRED, - extras_require=EXTRAS, - include_package_data=True, - license='Apache License, Version 2.0', - - cmdclass={ - 'upload': UploadCommand, - }, -) - -## Move ROSCO -conda_env = os.environ['CONDA_PREFIX'] -if platform.system() == 'Windows': - rosco_path = os.path.join(conda_env, 'Library','Lib') -elif platform.system() == 'Darwin': - rosco_path = os.path.join(conda_env, 'lib') +metadata = dict( + name = 'NAME', + version = about['__version__'], + description = DESCRIPTION, + long_description = long_description, + long_description_content_type = 'text/markdown', + author = AUTHOR, + author_email = EMAIL, + url = URL, + install_requires = REQUIRED, + python_requires = REQUIRES_PYTHON, + extras_require = EXTRAS, + include_package_date = True, + packages = find_packages(exclude=["tests", "*.tests", "*.tests.*", "tests.*"]), + license = 'Apache License, Version 2.0', + ext_modules = [roscoExt], + cmdclass = {'build_ext': CMakeBuildExt, 'upload': UploadCommand}, + zip_safe = False, +) -try: - os.makedirs(os.path.join(os.path.dirname(os.path.abspath(__file__)),'local'), exist_ok=True) - copy(glob.glob(os.path.join(rosco_path, 'libdiscon.*'))[0], - os.path.join(os.path.dirname(os.path.abspath(__file__)),'local')) -except: - print('----------------------------------------------------------------------------- \n', - ' Unable to find a conda-forge distribution of ROSCO. \n', - ' Standard install instructions can be found at https://github.com/NREL/ROSCO \n', - '-----------------------------------------------------------------------------') \ No newline at end of file +setup(**metadata)