Skip to content

Commit 311b42d

Browse files
committed
Prefer custom HDF5 over pkg-config/fallback paths
When specifying a custom HDF5 location, alternative locations for the library from system pkg-config files and the hardcoded fallback paths can interfere with proper operation of the specified library. If a location is specified, avoid invoking pkg-config and the fallback paths to honor the user intent. This addresses GitHub issue h5py#946.
1 parent 309c474 commit 311b42d

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

setup_build.py

+23-16
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ def localpath(*args):
3535
localpath("lzf/lzf/lzf_c.c"),
3636
localpath("lzf/lzf/lzf_d.c")]}
3737

38+
FALLBACK_PATHS = {
39+
'include_dirs': [],
40+
'library_dirs': []
41+
}
42+
3843
COMPILER_SETTINGS = {
3944
'libraries' : ['hdf5', 'hdf5_hl'],
4045
'include_dirs' : [localpath('lzf')],
@@ -49,8 +54,8 @@ def localpath(*args):
4954
('H5_BUILT_AS_DYNAMIC_LIB', None)
5055
])
5156
else:
52-
COMPILER_SETTINGS['include_dirs'].extend(['/opt/local/include', '/usr/local/include'])
53-
COMPILER_SETTINGS['library_dirs'].extend(['/opt/local/include', '/usr/local/include'])
57+
FALLBACK_PATHS['include_dirs'].extend(['/opt/local/include', '/usr/local/include'])
58+
FALLBACK_PATHS['library_dirs'].extend(['/opt/local/lib', '/usr/local/lib'])
5459

5560

5661
class h5py_build_ext(build_ext):
@@ -76,14 +81,22 @@ def _make_extensions(config):
7681

7782
settings = COMPILER_SETTINGS.copy()
7883

79-
try:
80-
if pkgconfig.exists('hdf5'):
81-
pkgcfg = pkgconfig.parse("hdf5")
82-
settings['include_dirs'].extend(pkgcfg['include_dirs'])
83-
settings['library_dirs'].extend(pkgcfg['library_dirs'])
84-
settings['define_macros'].extend(pkgcfg['define_macros'])
85-
except EnvironmentError:
86-
pass
84+
# Ensure that if a custom HDF5 location is specified, prevent
85+
# pkg-config and fallback locations from appearing in the settings
86+
if config.hdf5 is not None:
87+
settings['include_dirs'].insert(0, op.join(config.hdf5, 'include'))
88+
settings['library_dirs'].insert(0, op.join(config.hdf5, 'lib'))
89+
else:
90+
try:
91+
if pkgconfig.exists('hdf5'):
92+
pkgcfg = pkgconfig.parse("hdf5")
93+
settings['include_dirs'].extend(pkgcfg['include_dirs'])
94+
settings['library_dirs'].extend(pkgcfg['library_dirs'])
95+
settings['define_macros'].extend(pkgcfg['define_macros'])
96+
except EnvironmentError:
97+
pass
98+
settings['include_dirs'].extend(FALLBACK_PATHS['include_dirs'])
99+
settings['library_dirs'].extend(FALLBACK_PATHS['library_dirs'])
87100

88101
try:
89102
numpy_includes = numpy.get_include()
@@ -97,12 +110,6 @@ def _make_extensions(config):
97110
import mpi4py
98111
settings['include_dirs'] += [mpi4py.get_include()]
99112

100-
# Ensure a custom location appears first, so we don't get a copy of
101-
# HDF5 from some default location in COMPILER_SETTINGS
102-
if config.hdf5 is not None:
103-
settings['include_dirs'].insert(0, op.join(config.hdf5, 'include'))
104-
settings['library_dirs'].insert(0, op.join(config.hdf5, 'lib'))
105-
106113
# TODO: should this only be done on UNIX?
107114
if os.name != 'nt':
108115
settings['runtime_library_dirs'] = settings['library_dirs']

0 commit comments

Comments
 (0)