Skip to content

Commit f310fce

Browse files
authored
Merge pull request #541 from matplotlib/bugfix-539
Fix setup.py to identify GEOS dylib on MacOS
2 parents f9c9dab + 02340ee commit f310fce

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

packages/basemap/setup.py

+22-14
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,33 @@ def get_geos_install_prefix():
3737
candidates = [os.path.expanduser("~/local"), os.path.expanduser("~"),
3838
"/usr/local", "/usr", "/opt/local", "/opt", "/sw"]
3939

40+
# Prepare filename pattern to find the GEOS library.
41+
extensions = {"win32": "dll", "cygwin": "dll", "darwin": "dylib"}
42+
libext = extensions.get(sys.platform, "so*")
43+
libname = "*geos_c*.{0}".format(libext)
44+
libdirs = ["bin", "lib", "lib/x86_64-linux-gnu", "lib64"]
45+
4046
for prefix in candidates:
4147
libfiles = []
42-
libdirs = ["bin", "lib", "lib/x86_64-linux-gnu", "lib64"]
43-
libext = "dll" if os.name == "nt" else "so"
44-
libcode = "{0}geos_c".format("" if os.name == "nt" else "lib")
45-
libname = "{0}*.{1}*".format(libcode, libext)
4648
for libdir in libdirs:
4749
libfiles.extend(glob.glob(os.path.join(prefix, libdir, libname)))
4850
hfile = os.path.join(prefix, "include", "geos_c.h")
4951
if os.path.isfile(hfile) and libfiles:
5052
return prefix
5153

52-
warnings.warn(" ".join([
53-
"Cannot find GEOS library and/or headers in standard locations",
54-
"('{0}'). Please install the corresponding packages using your",
55-
"software management system or set the environment variable",
56-
"GEOS_DIR to point to the location where GEOS is installed",
57-
"(for example, if 'geos_c.h' is in '/usr/local/include'",
58-
"and 'libgeos_c' is in '/usr/local/lib', then you need to",
59-
"set GEOS_DIR to '/usr/local'",
60-
]).format("', '".join(candidates)), RuntimeWarning)
54+
# At this point, the GEOS library was not found, so we throw a warning if
55+
# the user is trying to build the library.
56+
build_cmds = ("bdist_wheel", "build", "install")
57+
if any(cmd in sys.argv[1:] for cmd in build_cmds):
58+
warnings.warn(" ".join([
59+
"Cannot find GEOS library and/or headers in standard locations",
60+
"('{0}'). Please install the corresponding packages using your",
61+
"software management system or set the environment variable",
62+
"GEOS_DIR to point to the location where GEOS is installed",
63+
"(for example, if 'geos_c.h' is in '/usr/local/include'",
64+
"and 'libgeos_c' is in '/usr/local/lib', then you need to",
65+
"set GEOS_DIR to '/usr/local'",
66+
]).format("', '".join(candidates)), RuntimeWarning)
6167
return None
6268

6369

@@ -94,7 +100,9 @@ def run(self):
94100
import numpy
95101
include_dirs.append(numpy.get_include())
96102
except ImportError as err:
97-
warnings.warn("unable to locate NumPy headers", RuntimeWarning)
103+
build_cmds = ("bdist_wheel", "build", "install")
104+
if any(cmd in sys.argv[1:] for cmd in build_cmds):
105+
warnings.warn("unable to locate NumPy headers", RuntimeWarning)
98106

99107
# Define GEOS include, library and runtime dirs.
100108
geos_install_prefix = get_geos_install_prefix()

packages/basemap/utils/GeosLibrary.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,24 @@ def extract(self, overwrite=True):
147147
for line in lines:
148148
fd.write(line.replace(oldtext, newtext).encode())
149149

150-
# The SVN revision file is not created on the fly before 3.6.0.
151-
svn_hfile = os.path.join(zipfold, "geos_svn_revision.h")
152-
if self.version_tuple < (3, 6, 0) and not os.path.exists(svn_hfile):
153-
with io.open(svn_hfile, "wb") as fd:
154-
text = "#define GEOS_SVN_REVISION 0"
155-
fd.write(text.encode())
150+
# Apply specific patches for GEOS < 3.6.0.
151+
if self.version_tuple < (3, 6, 0):
152+
# The SVN revision file is not created on the fly before 3.6.0.
153+
svn_hfile = os.path.join(zipfold, "geos_svn_revision.h")
154+
if not os.path.exists(svn_hfile):
155+
with io.open(svn_hfile, "wb") as fd:
156+
text = "#define GEOS_SVN_REVISION 0"
157+
fd.write(text.encode())
158+
# Reduce warnings when compiling with `nmake` on Windows.
159+
cmakefile = os.path.join(zipfold, "CMakeLists.txt")
160+
if os.path.exists(cmakefile):
161+
with io.open(cmakefile, "r", encoding="utf-8") as fd:
162+
lines = fd.readlines()
163+
with io.open(cmakefile, "wb") as fd:
164+
oldtext = 'string(REGEX REPLACE "/W[0-9]" "/W4"'
165+
newtext = oldtext.replace("W4", "W1")
166+
for line in lines:
167+
fd.write(line.replace(oldtext, newtext).encode())
156168

157169
def build(self, installdir=None, njobs=1):
158170
"""Build and install GEOS from source."""
@@ -191,7 +203,7 @@ def build(self, installdir=None, njobs=1):
191203
build_opts.extend([
192204
"--",
193205
"WIN64={0}".format("YES" if win64 else "NO"),
194-
"BUILD_BATCH={0}".format("YES" if njobs > 1 else "NO")
206+
"BUILD_BATCH={0}".format("YES" if njobs > 1 else "NO"),
195207
])
196208
else:
197209
build_opts = ["-j", "{0:d}".format(njobs)] + build_opts

0 commit comments

Comments
 (0)