| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2013-2019 The Meson development team |
| 3 |
|
| 4 |
# This file contains the detection logic for miscellaneous external dependencies. |
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import os |
| 8 |
import re |
| 9 |
from pathlib import Path |
| 10 |
|
| 11 |
from ..mesonlib import OrderedSet, join_args |
| 12 |
from .base import DependencyCandidate, DependencyException, DependencyMethods |
| 13 |
from .configtool import ConfigToolDependency |
| 14 |
from .detect import packages |
| 15 |
from .pkgconfig import PkgConfigDependency, PkgConfigInterface |
| 16 |
from .factory import factory_methods |
| 17 |
import typing as T |
| 18 |
|
| 19 |
if T.TYPE_CHECKING: |
| 20 |
from .factory import DependencyGenerator |
| 21 |
from ..environment import Environment |
| 22 |
from .base import DependencyObjectKWs |
| 23 |
|
| 24 |
|
| 25 |
class HDF5PkgConfigDependency(PkgConfigDependency): |
| 26 |
|
| 27 |
"""Handle brokenness in the HDF5 pkg-config files.""" |
| 28 |
|
| 29 |
def __init__(self, name: str, environment: 'Environment', kwargs: DependencyObjectKWs) -> None: |
| 30 |
language = kwargs.get('language') or 'c' |
| 31 |
if language not in {'c', 'cpp', 'fortran'}: |
| 32 |
raise DependencyException(f'Language {language} is not supported with HDF5.') |
| 33 |
|
| 34 |
super().__init__(name, environment, kwargs) |
| 35 |
if not self.is_found: |
| 36 |
return |
| 37 |
|
| 38 |
# some broken pkgconfig don't actually list the full path to the needed includes |
| 39 |
newinc: T.List[str] = [] |
| 40 |
for arg in self.compile_args: |
| 41 |
if arg.startswith('-I'): |
| 42 |
stem = 'static' if self.static else 'shared' |
| 43 |
if (Path(arg[2:]) / stem).is_dir(): |
| 44 |
newinc.append('-I' + str(Path(arg[2:]) / stem)) |
| 45 |
self.compile_args += newinc |
| 46 |
|
| 47 |
link_args: T.List[str] = [] |
| 48 |
for larg in self.get_link_args(): |
| 49 |
lpath = Path(larg) |
| 50 |
# some pkg-config hdf5.pc (e.g. Ubuntu) don't include the commonly-used HL HDF5 libraries, |
| 51 |
# so let's add them if they exist |
| 52 |
# additionally, some pkgconfig HDF5 HL files are malformed so let's be sure to find HL anyway |
| 53 |
if lpath.is_file(): |
| 54 |
hl = [] |
| 55 |
if language == 'cpp': |
| 56 |
hl += ['_hl_cpp', '_cpp'] |
| 57 |
elif language == 'fortran': |
| 58 |
hl += ['_hl_fortran', 'hl_fortran', '_fortran'] |
| 59 |
hl += ['_hl'] # C HL library, always needed |
| 60 |
|
| 61 |
suffix = '.' + lpath.name.split('.', 1)[1] # in case of .dll.a |
| 62 |
for h in hl: |
| 63 |
hlfn = lpath.parent / (lpath.name.split('.', 1)[0] + h + suffix) |
| 64 |
if hlfn.is_file(): |
| 65 |
link_args.append(str(hlfn)) |
| 66 |
# HDF5 C libs are required by other HDF5 languages |
| 67 |
link_args.append(larg) |
| 68 |
else: |
| 69 |
link_args.append(larg) |
| 70 |
|
| 71 |
self.link_args = link_args |
| 72 |
|
| 73 |
|
| 74 |
class HDF5ConfigToolDependency(ConfigToolDependency): |
| 75 |
|
| 76 |
"""Wrapper around hdf5 binary config tools.""" |
| 77 |
|
| 78 |
version_arg = '-showconfig' |
| 79 |
|
| 80 |
def __init__(self, name: str, environment: 'Environment', kwargs: DependencyObjectKWs) -> None: |
| 81 |
language = kwargs.get('language') or 'c' |
| 82 |
if language not in {'c', 'cpp', 'fortran'}: |
| 83 |
raise DependencyException(f'Language {language} is not supported with HDF5.') |
| 84 |
|
| 85 |
if language == 'c': |
| 86 |
cenv = 'CC' |
| 87 |
lenv = 'C' |
| 88 |
tools = ['h5cc', 'h5pcc'] |
| 89 |
elif language == 'cpp': |
| 90 |
cenv = 'CXX' |
| 91 |
lenv = 'CXX' |
| 92 |
tools = ['h5c++', 'h5pc++'] |
| 93 |
elif language == 'fortran': |
| 94 |
cenv = 'FC' |
| 95 |
lenv = 'F' |
| 96 |
tools = ['h5fc', 'h5pfc'] |
| 97 |
else: |
| 98 |
raise DependencyException('How did you get here?') |
| 99 |
|
| 100 |
nkwargs = kwargs.copy() |
| 101 |
nkwargs['tools'] = tools |
| 102 |
|
| 103 |
# Override the compiler that the config tools are going to use by |
| 104 |
# setting the environment variables that they use for the compiler and |
| 105 |
# linkers. |
| 106 |
|
| 107 |
for_machine = kwargs['native'] |
| 108 |
compiler = environment.coredata.compilers[for_machine][language] |
| 109 |
try: |
| 110 |
os.environ[f'HDF5_{cenv}'] = join_args(compiler.get_exelist()) |
| 111 |
os.environ[f'HDF5_{lenv}LINKER'] = join_args(compiler.get_linker_exelist()) |
| 112 |
super().__init__(name, environment, nkwargs) |
| 113 |
finally: |
| 114 |
del os.environ[f'HDF5_{cenv}'] |
| 115 |
del os.environ[f'HDF5_{lenv}LINKER'] |
| 116 |
if not self.is_found: |
| 117 |
return |
| 118 |
|
| 119 |
# We first need to call the tool with -c to get the compile arguments |
| 120 |
# and then without -c to get the link arguments. |
| 121 |
args = self.get_config_value(['-show', '-c'], 'args')[1:] |
| 122 |
args += self.get_config_value(['-show', '-noshlib' if self.static else '-shlib'], 'args')[1:] |
| 123 |
found = False |
| 124 |
for arg in args: |
| 125 |
if arg.startswith(('-I', '-f', '-D')) or arg == '-pthread': |
| 126 |
self.compile_args.append(arg) |
| 127 |
elif arg.startswith(('-L', '-l', '-Wl')): |
| 128 |
self.link_args.append(arg) |
| 129 |
found = True |
| 130 |
elif Path(arg).is_file(): |
| 131 |
self.link_args.append(arg) |
| 132 |
found = True |
| 133 |
|
| 134 |
# cmake h5cc is broken |
| 135 |
if not found: |
| 136 |
raise DependencyException('HDF5 was built with cmake instead of autotools, and h5cc is broken.') |
| 137 |
|
| 138 |
def _sanitize_version(self, ver: str) -> str: |
| 139 |
v = re.search(r'\s*HDF5 Version: (\d+\.\d+\.\d+)', ver) |
| 140 |
return v.group(1) |
| 141 |
|
| 142 |
|
| 143 |
@factory_methods({DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL}) |
| 144 |
def hdf5_factory(env: 'Environment', kwargs: DependencyObjectKWs, |
| 145 |
methods: T.List[DependencyMethods]) -> T.List['DependencyGenerator']: |
| 146 |
candidates: T.List['DependencyGenerator'] = [] |
| 147 |
for_machine = kwargs['native'] |
| 148 |
|
| 149 |
if DependencyMethods.PKGCONFIG in methods: |
| 150 |
# Use an ordered set so that these remain the first tried pkg-config files |
| 151 |
pkgconfig_files = OrderedSet(['hdf5', 'hdf5-serial']) |
| 152 |
pkg = PkgConfigInterface.instance(env, for_machine, silent=False) |
| 153 |
if pkg: |
| 154 |
try: |
| 155 |
# old hdf5 versions put version number in .pc filename, e.g., hdf5-1.2.3.pc. |
| 156 |
for mod in pkg.list_all(): |
| 157 |
if mod.startswith('hdf5'): |
| 158 |
pkgconfig_files.add(mod) |
| 159 |
except DependencyException: |
| 160 |
# use just the standard files if pkg-config --list-all fails |
| 161 |
pass |
| 162 |
for mod in pkgconfig_files: |
| 163 |
candidates.append(DependencyCandidate.from_dependency( |
| 164 |
mod, HDF5PkgConfigDependency, (env, kwargs))) |
| 165 |
|
| 166 |
if DependencyMethods.CONFIG_TOOL in methods: |
| 167 |
candidates.append(DependencyCandidate.from_dependency( |
| 168 |
'hdf5', HDF5ConfigToolDependency, (env, kwargs))) |
| 169 |
|
| 170 |
return candidates |
| 171 |
|
| 172 |
packages['hdf5'] = hdf5_factory |