Skip to content

Commit

Permalink
build windows dlls in setup using setuptools
Browse files Browse the repository at this point in the history
  • Loading branch information
mikofski committed Feb 12, 2016
1 parent 1f2c9fd commit 22ec305
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 19 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -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/
Expand Down
63 changes: 51 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
6 changes: 3 additions & 3 deletions solar_utils/solar_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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`
Expand Down
5 changes: 2 additions & 3 deletions solar_utils/tests/test_cdlls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 22ec305

Please sign in to comment.