From 22ec305dd51aee877aa82dfc7d8a7170d46bda8a Mon Sep 17 00:00:00 2001 From: Mark Mikofski Date: Fri, 12 Feb 2016 00:08:17 -0800 Subject: [PATCH] build windows dlls in setup using setuptools --- .gitignore | 7 +++- setup.py | 63 ++++++++++++++++++++++++++------- solar_utils/solar_utils.py | 6 ++-- solar_utils/tests/test_cdlls.py | 5 ++- 4 files changed, 62 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index fba0d88..979b39c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,16 @@ # build build/ dist/ -solar_utils.egg.info/ +solar_utils.egg-info/ *.pyc *.dylib *.so *.dll +*.exp +*.obj +*.lib +*.a +*.dll.manifest # IDE .idea/ diff --git a/setup.py b/setup.py index 6ce5d3c..939e0e6 100644 --- a/setup.py +++ b/setup.py @@ -9,24 +9,63 @@ except ImportError: sys.exit('setuptools was not detected - please install setuptools and pip') from solar_utils import __version__ as VERSION, __name__ as NAME +import logging + +logging.basicConfig(level=logging.DEBUG) +logger = logging.getLogger(__name__) PLATFORM = sys.platform if PLATFORM == 'win32': - SRCDIR = 'win32' + SRC_DIR = 'win32' elif PLATFORM == 'darwin': - SRCDIR = 'darwin' + SRC_DIR = 'darwin' elif PLATFORM == 'linux2': - SRCDIR = 'linux' + SRC_DIR = 'linux' else: sys.exit('unknown platform - expected "win32", "darwin" or "linux2"') -CC = distutils.ccompiler.new_compiler() -CC.set_include_dirs('path\\to\\srcdir') -CC.compile(['win32\\solposAM.c','win32\\solpos.c']) -CC.link_shared_lib(['win32\\solposAM.obj','win32\\solpos.obj'], 'solposAM') +PKG_DATA = [os.path.join(SRC_DIR, '*.*'), os.path.join(SRC_DIR, 'src', '*.*')] +LIB_DIR = os.path.join(NAME, SRC_DIR) +SRC_DIR = os.path.join(LIB_DIR, 'src') +logger.debug(PKG_DATA) +TESTS = '%s.tests' % NAME +TEST_DATA = ['test_spectrl2_data.json'] +SOLPOS = 'solpos.c' +SOLPOSAM = 'solposAM.c' +SOLPOSAM_LIB = 'solposAM' +SPECTRL2 = 'spectrl2.c' +SPECTRL2_2 = 'spectrl2_2.c' +SPECTRL2_LIB = 'spectrl2' +SOLPOS = os.path.join(SRC_DIR, SOLPOS) +SOLPOSAM = os.path.join(SRC_DIR, SOLPOSAM) +SPECTRL2 = os.path.join(SRC_DIR, SPECTRL2) +SPECTRL2_2 = os.path.join(SRC_DIR, SPECTRL2_2) +LIB_FILES = ['%s.dll', '%s.lib', '%s.exp', 'lib%s.so', 'lib%s.dylib', 'lib%s.a'] +if 'clean' in sys.argv or 'distclean' in sys.argv: + for lib_file in LIB_FILES: + try: + os.remove(os.path.join(LIB_DIR, lib_file % SOLPOSAM_LIB)) + except OSError as err: + sys.stderr.write(err.message) + try: + os.remove(os.path.join(LIB_DIR, lib_file % SPECTRL2_LIB)) + except OSError as err: + sys.stderr.write(err.message) +else: + # compile NREL source code + CC = distutils.ccompiler.new_compiler() # initialize compiler object + CC.set_include_dirs([SRC_DIR]) # set includes directory + OBJS = CC.compile([SOLPOS, SOLPOSAM]) # compile solpos and solposAM objects + # link objects and make shared library in library directory + CC.link_shared_lib(OBJS, SOLPOSAM_LIB, output_dir=LIB_DIR) + OBJS = CC.compile([SPECTRL2, SPECTRL2_2, SOLPOS]) # compile spectrl2 objects + CC.set_libraries([SOLPOSAM_LIB]) # set linked libraries + CC.set_library_dirs([LIB_DIR]) # set library directories + # link objects and make shared library in library directory + CC.link_shared_lib(OBJS, SPECTRL2_LIB, output_dir=LIB_DIR) -setup(name = NAME, - version = VERSION, - packages = [NAME], - package_data = {NAME: PKG_DATA}, - description = 'Python wrapper around NREL SOLPOS and SPECTRL2') +setup(name=NAME, + version=VERSION, + packages=[NAME, TESTS], + package_data={NAME: PKG_DATA, TESTS: TEST_DATA}, + description='Python wrapper around NREL SOLPOS and SPECTRL2') diff --git a/solar_utils/solar_utils.py b/solar_utils/solar_utils.py index fa06dd2..436b6fd 100644 --- a/solar_utils/solar_utils.py +++ b/solar_utils/solar_utils.py @@ -63,7 +63,7 @@ def solposAM(location, datetime, weather): :type weather: list of floats :returns: angles, airmass :rtype: float - :raises: :exc:`~pvsimlife.solar_utils.solar_utils_exceptions.SOLPOS_Error` + :raises: :exc:`~solar_utils.solar_utils_exceptions.SOLPOS_Error` """ # load the DLL solposAMdll = ctypes.cdll.LoadLibrary(SOLPOSAMDLL) @@ -120,8 +120,8 @@ def spectrl2(units, location, datetime, weather, orientation, :type albedo: list of lists of floats :returns: specdif, specdir, specetr, specglo and specx :rtype: float - :raises: :exc:`~pvsimlife.solar_utils.solar_utils_exceptions.SPECTRL2_Error`, - :exc:`~pvsimlife.solar_utils.solar_utils_exceptions.SOLPOS_Error` + :raises: :exc:`~solar_utils.solar_utils_exceptions.SPECTRL2_Error`, + :exc:`~solar_utils.solar_utils_exceptions.SOLPOS_Error` .. seealso:: :func:`solposAM` diff --git a/solar_utils/tests/test_cdlls.py b/solar_utils/tests/test_cdlls.py index b69c0b9..bc554a5 100644 --- a/solar_utils/tests/test_cdlls.py +++ b/solar_utils/tests/test_cdlls.py @@ -19,9 +19,8 @@ import os from nose.tools import ok_ -from pvsimlife.solar_utils import solposAM, spectrl2 -from pvsimlife.solar_utils.solar_utils_exceptions import SOLPOS_Error, \ - SPECTRL2_Error +from solar_utils import * +from solar_utils.solar_utils_exceptions import SOLPOS_Error, SPECTRL2_Error _DIRNAME = os.path.dirname(__file__) TOL = 1E-3