Skip to content

Commit

Permalink
Merge pull request #5 from rstoneback/windows-compat
Browse files Browse the repository at this point in the history
Disabled using system CDF library
  • Loading branch information
mddepew committed Mar 30, 2018
2 parents e027f91 + 097a5da commit 8375b97
Showing 1 changed file with 94 additions and 88 deletions.
182 changes: 94 additions & 88 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import os
import sys

#import setuptools
# import setuptools
from setuptools import setup
#from setuptools.command.install import install
# from setuptools.command.install import install

import numpy as np
import numpy.distutils
Expand All @@ -14,7 +14,7 @@
from numpy.distutils.command.build import build

from subprocess import call
#import setuptools
# import setuptools


# path to base CDF directory if CDF library already installed and you want to use it
Expand Down Expand Up @@ -43,15 +43,16 @@
# https://github.com/Turbo87/py-xcsoar/blob/master/setup.py

# code to find if a CDF library is already installed in default locations


def find_CDF_base(lib_name):

# try environment variables
cdf_base = os.getenv('CDF_BASE')
if cdf_base is not None:
# check
if os.path.exists(cdf_base):
return cdf_base

# that didn't work
# look in default locations
platform = sys.platform
Expand All @@ -62,22 +63,22 @@ def find_CDF_base(lib_name):
defaults = ['/usr/local/', user_dir]
elif platform == 'win32':
# Don't use NASA binaries on windows, not compatible
defaults = []
defaults = ['C:/CDF_Distribution/']
else:
raise ValueError('Unknown platform, set CDF library in setup.py.')

# collect a list of posible directories to search in
# grab all dirs, select those that start with cdf
search_dir = []
for default in defaults:
sub_dirs = os.listdir(default)
for sub in sub_dirs:
#print(sub)
# print(sub)
if sub[0:3].lower() == 'cdf':
search_dir.append(sub)
search_dir = np.sort(search_dir)
for test_dir in search_dir[::-1]:
test_path = os.path.join(default, test_dir, 'lib', lib_name)
test_path = os.path.join(default, test_dir, 'lib', lib_name)
if os.path.exists(test_path):
return os.path.join(default, test_dir)

Expand All @@ -91,117 +92,128 @@ def find_CDF_base(lib_name):
env_name = 'x86_64'
lib_name = 'libcdf.a'
# including shared lib in mac breaks things
shared_lib_name = None #'libcdf.dylib' #
extra_link_args = ['-lm', '-lc'] #, '-Wl,-undefined', '-Wl,dynamic_lookup'] #'-export_dynamic']
shared_lib_name = None # 'libcdf.dylib'
extra_link_args = ['-lm', '-lc'] # , '-Wl,-undefined', '-Wl,dynamic_lookup'] #'-export_dynamic']
elif (platform == 'linux') | (platform == 'linux2'):
os_name = 'linux'
env_name = 'gnu'
lib_name = 'libcdf.a'
shared_lib_name = None #'libcdf.so'
extra_link_args = ['-lm', '-lc'] #, '-Wl,-undefined', '-Wl,dynamic_lookup'] #'-export_dynamic']
shared_lib_name = None # 'libcdf.so'
extra_link_args = ['-lm', '-lc'] # , '-Wl,-undefined', '-Wl,dynamic_lookup'] #'-export_dynamic']
elif (platform == 'win32'):
os_name='mingw'
env_name='gnu'
os_name = 'mingw'
env_name = 'gnu'
lib_name = 'libcdf.a'
shared_lib_name = None
# extra_link_args = ['/nodefaultlib:libcd']
extra_link_args = [] #, '-Wl,-undefined', '-Wl,dynamic_lookup'] #'-export_dynamic']
extra_link_args = [] # , '-Wl,-undefined', '-Wl,dynamic_lookup'] #'-export_dynamic']
else:
if (lib_name is None) or ((base_cdf is None) and ((os_name is None)
or (env_name is None) or (extra_link_args is None)) ):
if (lib_name is None) or ((base_cdf is None) and ((os_name is None)
or (env_name is None) or (extra_link_args is None))):
raise ValueError('Unknown platform, please set setup.py parameters manually.')

# CDF directory hasn't been specified by user, see if an installation can be found
if base_cdf is None:
# Use of system CDF libraries disabled for compatibility reasons
# if base_cdf is None:
if False:
try:
base_cdf = find_CDF_base(lib_name)
print(' '.join(('Found CDF installation at', base_cdf)))
except ValueError:
print( 'Unable to find CDF installation in default location.')
print('Unable to find CDF installation in default location.')
base_cdf = None


if base_cdf is None:
# Use of system CDF libraries disabled for compatibility reasons
# if base_cdf is None:
if True:
build_cdf_flag = True
# library not provided, build library included with pysatCDF
else:
build_cdf_flag = False
#raise NotImplementedError
# raise NotImplementedError
if not os.path.isdir(base_cdf):
raise ValueError('CDF directory supplied is not an actual directory.')
if (lib_name is None):
raise ValueError('Attempting to use pre-installed CDF library as directed. Please set setup.py parameters manually.')

raise ValueError('Attempting to use pre-installed CDF library as directed.'
'Please set setup.py parameters manually.')


BASEPATH = os.path.dirname(os.path.abspath(__file__))
CDF_PATH = os.path.join(BASEPATH, 'cdf36_3-dist')


class CDFBuild(build):
def run(self):
CDF_build(self, self.build_temp)
build.run(self)
return



def CDF_build(self, ppath):

# build CDF Library
build_path = os.path.abspath(ppath)
if platform == 'win32':
# Replace backslashes with forward slashes to avoid path being mangled by escape sequences
build_path = build_path.replace('\\','/')
build_path = build_path.replace('\\', '/')
# print (' ')
# print ("In CDF_build ", build_path, CDF_PATH, ppath)
# print(' ')
# print (' ')

# check if library already exists
if (not os.path.isfile(os.path.join(self.build_lib, 'pysatCDF', 'lib', lib_name))) or self.force:
if (not os.path.isfile(os.path.join(self.build_lib,
'pysatCDF',
'lib',
lib_name))) or self.force:
cmd0 = ['make', 'clean']
cmd = ['make',
'OS=' + os_name,
'ENV=' + env_name,
'CURSES=no',
'SHARED=yes',
'UCOPTIONS=-Dsingle_underscore',
'INSTALLDIR='+build_path,# -undefined -Wl,dynamic_lookup',
'all',]
'OS=' + os_name,
'ENV=' + env_name,
'CURSES=no',
'SHARED=yes',
'UCOPTIONS=-Dsingle_underscore',
'INSTALLDIR=' + build_path, # -undefined -Wl,dynamic_lookup',
'all', ]
cmd2 = ['make',
# 'INSTALLDIR='+build_path,
'INSTALLDIR='+build_path,
'install',]
# 'INSTALLDIR='+build_path,
'INSTALLDIR=' + build_path,
'install', ]

# clean any previous attempts
def compile0():
call(cmd0, cwd=CDF_PATH)

# set compile options via makefile
def compile1():
call(cmd, cwd=CDF_PATH)

# do the installation
def compile2():
call(cmd2, cwd=CDF_PATH)
self.execute(compile0, [], 'Cleaning CDF')
self.execute(compile1, [], 'Configuring CDF')
self.execute(compile2, [], 'Compiling CDF')
self.execute(compile0, [], 'Cleaning CDF')

# copy resulting tool to library build folder
self.mkpath(os.path.join(ppath, 'pysatCDF'))

if not self.dry_run:
# print ("Not a dry run")
# print(" ")
self.copy_tree(os.path.join(ppath, 'include'),
os.path.join(self.build_lib, 'pysatCDF', 'include'))
self.copy_tree(os.path.join(ppath, 'include'),
os.path.join(self.build_lib, 'pysatCDF', 'include'))
self.mkpath(os.path.join(self.build_lib, 'pysatCDF', 'lib'))
self.copy_file(os.path.join(ppath, 'lib', lib_name),
os.path.join(self.build_lib, 'pysatCDF', 'lib', lib_name))
self.copy_file(os.path.join(ppath, 'lib', lib_name),
os.path.join(self.build_lib, 'pysatCDF', 'lib', lib_name))

if shared_lib_name is not None:
self.copy_file(os.path.join(ppath, 'lib', shared_lib_name),
os.path.join(self.build_lib, 'pysatCDF', 'lib', shared_lib_name))
self.copy_file(os.path.join(ppath, 'lib', shared_lib_name),
os.path.join(self.build_lib, 'pysatCDF', 'lib', shared_lib_name))

# run original build code
#build.run(self)
# build.run(self)
return


Expand All @@ -210,15 +222,17 @@ def run(self):

CDF_build(self, self.build_src)
# print 'yo yo yo'
#print (self.__dict__)
# print (self.__dict__)
lib_path = os.path.abspath(os.path.join(self.build_lib, 'pysatCDF'))
# set directories for the CDF library installed with pysatCDF
self.extensions[0].include_dirs = [os.path.join(lib_path, 'include')]
self.extensions[0].f2py_options = ['--include-paths', os.path.join(lib_path, 'include')]
self.extensions[0].extra_objects = [os.path.join(lib_path, 'lib', lib_name)]
# add shared library, if provided
if shared_lib_name is not None:
self.extensions[0].extra_link_args.append(os.path.join(lib_path, 'lib', shared_lib_name))
self.extensions[0].extra_link_args.append(os.path.join(lib_path,
'lib',
shared_lib_name))

build_src.run(self)
return
Expand All @@ -231,44 +245,41 @@ def run(self):
cmdclass = {}
else:
print ('Building CDF for pysatCDF.')
cmdclass={'build': CDFBuild,
'build_src': ExtensionBuild,
}
cmdclass = {'build': CDFBuild,
'build_src': ExtensionBuild, }
f2py_cdf_include_path = ''
f2py_cdf_lib_path = ''

# setup fortran extension
#---------------------------------------------------------------------------
# ---------------------------------------------------------------------------

ext1 = numpy.distutils.core.Extension(
name = 'pysatCDF.fortran_cdf',
sources = [os.path.join('pysatCDF', 'fortran_cdf.f')],
include_dirs = [f2py_cdf_include_path],
f2py_options = ['--include-paths', f2py_cdf_include_path],# '--Wall', 'n', '--Wno-tabs', 'n'],
extra_objects = [f2py_cdf_lib_path],
extra_link_args = extra_link_args,
)
name='pysatCDF.fortran_cdf',
sources=[os.path.join('pysatCDF', 'fortran_cdf.f')],
include_dirs=[f2py_cdf_include_path],
f2py_options=['--include-paths', f2py_cdf_include_path], # '--Wall', 'n', '--Wno-tabs', 'n'],
extra_objects=[f2py_cdf_lib_path],
extra_link_args=extra_link_args)


# call setup
#--------------------------------------------------------------------------
numpy.distutils.core.setup(

name = 'pysatCDF',
version = '0.2.8',
packages = ['pysatCDF'],
cmdclass = cmdclass,
ext_modules = [ext1, ],
description= 'Simple NASA Common Data Format (CDF) File reader.',
long_description = 'pysatCDF is a reader for CDF files and provides '+
'additional support for exporting to pysat data formats (not required). '+
'The NASA CDF library is included.',
url='http://github.com/rstoneback/pysatCDF',
# --------------------------------------------------------------------------
numpy.distutils.core.setup(
name='pysatCDF',
version='0.2.8',
packages=['pysatCDF'],
cmdclass=cmdclass,
ext_modules=[ext1, ],
description='Simple NASA Common Data Format (CDF) File reader.',
long_description=('pysatCDF is a reader for CDF files and provides '
'additional support for exporting to pysat data formats (not required). '
'The NASA CDF library is included.'),
url='http://github.com/rstoneback/pysatCDF',
# Author details
author='Russell Stoneback',
author_email='rstoneba@utdallas.edu',
#data_files = [('', ['cdf36_1-dist/CDF_copyright.txt'])],
# data_files = [('', ['cdf36_1-dist/CDF_copyright.txt'])],

# Choose your license
license='BSD',
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
Expand All @@ -284,7 +295,7 @@ def run(self):
'Topic :: Scientific/Engineering :: Astronomy',
'Topic :: Scientific/Engineering :: Physics',
'Topic :: Scientific/Engineering :: Atmospheric Science',

# Pick your license as you wish (should match "license" above)
'License :: OSI Approved :: BSD License',

Expand All @@ -295,10 +306,5 @@ def run(self):
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],
install_requires = ['numpy'],
)





install_requires=['numpy'],
)

0 comments on commit 8375b97

Please sign in to comment.