Skip to content

Commit 22ec305

Browse files
committed
build windows dlls in setup using setuptools
1 parent 1f2c9fd commit 22ec305

4 files changed

Lines changed: 62 additions & 19 deletions

File tree

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# build
22
build/
33
dist/
4-
solar_utils.egg.info/
4+
solar_utils.egg-info/
55
*.pyc
66
*.dylib
77
*.so
88
*.dll
9+
*.exp
10+
*.obj
11+
*.lib
12+
*.a
13+
*.dll.manifest
914

1015
# IDE
1116
.idea/

setup.py

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,63 @@
99
except ImportError:
1010
sys.exit('setuptools was not detected - please install setuptools and pip')
1111
from solar_utils import __version__ as VERSION, __name__ as NAME
12+
import logging
13+
14+
logging.basicConfig(level=logging.DEBUG)
15+
logger = logging.getLogger(__name__)
1216

1317
PLATFORM = sys.platform
1418
if PLATFORM == 'win32':
15-
SRCDIR = 'win32'
19+
SRC_DIR = 'win32'
1620
elif PLATFORM == 'darwin':
17-
SRCDIR = 'darwin'
21+
SRC_DIR = 'darwin'
1822
elif PLATFORM == 'linux2':
19-
SRCDIR = 'linux'
23+
SRC_DIR = 'linux'
2024
else:
2125
sys.exit('unknown platform - expected "win32", "darwin" or "linux2"')
2226

23-
CC = distutils.ccompiler.new_compiler()
24-
CC.set_include_dirs('path\\to\\srcdir')
25-
CC.compile(['win32\\solposAM.c','win32\\solpos.c'])
26-
CC.link_shared_lib(['win32\\solposAM.obj','win32\\solpos.obj'], 'solposAM')
27+
PKG_DATA = [os.path.join(SRC_DIR, '*.*'), os.path.join(SRC_DIR, 'src', '*.*')]
28+
LIB_DIR = os.path.join(NAME, SRC_DIR)
29+
SRC_DIR = os.path.join(LIB_DIR, 'src')
30+
logger.debug(PKG_DATA)
31+
TESTS = '%s.tests' % NAME
32+
TEST_DATA = ['test_spectrl2_data.json']
33+
SOLPOS = 'solpos.c'
34+
SOLPOSAM = 'solposAM.c'
35+
SOLPOSAM_LIB = 'solposAM'
36+
SPECTRL2 = 'spectrl2.c'
37+
SPECTRL2_2 = 'spectrl2_2.c'
38+
SPECTRL2_LIB = 'spectrl2'
39+
SOLPOS = os.path.join(SRC_DIR, SOLPOS)
40+
SOLPOSAM = os.path.join(SRC_DIR, SOLPOSAM)
41+
SPECTRL2 = os.path.join(SRC_DIR, SPECTRL2)
42+
SPECTRL2_2 = os.path.join(SRC_DIR, SPECTRL2_2)
43+
LIB_FILES = ['%s.dll', '%s.lib', '%s.exp', 'lib%s.so', 'lib%s.dylib', 'lib%s.a']
44+
if 'clean' in sys.argv or 'distclean' in sys.argv:
45+
for lib_file in LIB_FILES:
46+
try:
47+
os.remove(os.path.join(LIB_DIR, lib_file % SOLPOSAM_LIB))
48+
except OSError as err:
49+
sys.stderr.write(err.message)
50+
try:
51+
os.remove(os.path.join(LIB_DIR, lib_file % SPECTRL2_LIB))
52+
except OSError as err:
53+
sys.stderr.write(err.message)
54+
else:
55+
# compile NREL source code
56+
CC = distutils.ccompiler.new_compiler() # initialize compiler object
57+
CC.set_include_dirs([SRC_DIR]) # set includes directory
58+
OBJS = CC.compile([SOLPOS, SOLPOSAM]) # compile solpos and solposAM objects
59+
# link objects and make shared library in library directory
60+
CC.link_shared_lib(OBJS, SOLPOSAM_LIB, output_dir=LIB_DIR)
61+
OBJS = CC.compile([SPECTRL2, SPECTRL2_2, SOLPOS]) # compile spectrl2 objects
62+
CC.set_libraries([SOLPOSAM_LIB]) # set linked libraries
63+
CC.set_library_dirs([LIB_DIR]) # set library directories
64+
# link objects and make shared library in library directory
65+
CC.link_shared_lib(OBJS, SPECTRL2_LIB, output_dir=LIB_DIR)
2766

28-
setup(name = NAME,
29-
version = VERSION,
30-
packages = [NAME],
31-
package_data = {NAME: PKG_DATA},
32-
description = 'Python wrapper around NREL SOLPOS and SPECTRL2')
67+
setup(name=NAME,
68+
version=VERSION,
69+
packages=[NAME, TESTS],
70+
package_data={NAME: PKG_DATA, TESTS: TEST_DATA},
71+
description='Python wrapper around NREL SOLPOS and SPECTRL2')

solar_utils/solar_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def solposAM(location, datetime, weather):
6363
:type weather: list of floats
6464
:returns: angles, airmass
6565
:rtype: float
66-
:raises: :exc:`~pvsimlife.solar_utils.solar_utils_exceptions.SOLPOS_Error`
66+
:raises: :exc:`~solar_utils.solar_utils_exceptions.SOLPOS_Error`
6767
"""
6868
# load the DLL
6969
solposAMdll = ctypes.cdll.LoadLibrary(SOLPOSAMDLL)
@@ -120,8 +120,8 @@ def spectrl2(units, location, datetime, weather, orientation,
120120
:type albedo: list of lists of floats
121121
:returns: specdif, specdir, specetr, specglo and specx
122122
:rtype: float
123-
:raises: :exc:`~pvsimlife.solar_utils.solar_utils_exceptions.SPECTRL2_Error`,
124-
:exc:`~pvsimlife.solar_utils.solar_utils_exceptions.SOLPOS_Error`
123+
:raises: :exc:`~solar_utils.solar_utils_exceptions.SPECTRL2_Error`,
124+
:exc:`~solar_utils.solar_utils_exceptions.SOLPOS_Error`
125125
126126
.. seealso::
127127
:func:`solposAM`

solar_utils/tests/test_cdlls.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
import os
2020
from nose.tools import ok_
2121

22-
from pvsimlife.solar_utils import solposAM, spectrl2
23-
from pvsimlife.solar_utils.solar_utils_exceptions import SOLPOS_Error, \
24-
SPECTRL2_Error
22+
from solar_utils import *
23+
from solar_utils.solar_utils_exceptions import SOLPOS_Error, SPECTRL2_Error
2524

2625
_DIRNAME = os.path.dirname(__file__)
2726
TOL = 1E-3

0 commit comments

Comments
 (0)