Skip to content

Commit

Permalink
Merge pull request mesonbuild#6421 from dcbaker/zlib-system-dep
Browse files Browse the repository at this point in the history
Add a "system" dependency for zlib
  • Loading branch information
jpakkane authored Feb 7, 2020
2 parents 3689e49 + b0c219b commit 31d89a4
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
11 changes: 11 additions & 0 deletions docs/markdown/Dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,17 @@ that it is not possible to obtain the shaderc version using this method.

`method` may be `auto`, `pkg-config` or `system`.

## Zlib

Zlib ships with pkg-config and cmake support, but on some operating systems
(windows, macOs, FreeBSD, dragonflybsd), it is provided as part of the base
operating system without pkg-config support. The new System finder can be used
on these OSes to link with the bundled version.

`method` may be `auto`, `pkg-config`, `cmake`, or `system`.

*New in 0.54.0* the `system` method.

<hr>
<a name="footnote1">1</a>: They may appear to be case-insensitive, if the
underlying file system happens to be case-insensitive.
8 changes: 8 additions & 0 deletions docs/markdown/snippets/zlib_system_dependency.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Add a system type dependency for zlib

This allows zlib to be detected on macOS and FreeBSD without the use of
pkg-config or cmake, neither of which are part of the base install on those
OSes (but zlib is).

A side effect of this change is that `dependency('zlib')` also works with
cmake instead of requiring `dependency('ZLIB')`.
3 changes: 2 additions & 1 deletion mesonbuild/dependencies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
ExternalDependency, NotFoundDependency, ExternalLibrary, ExtraFrameworkDependency, InternalDependency,
PkgConfigDependency, CMakeDependency, find_external_dependency, get_dep_identifier, packages, _packages_accept_language,
DependencyFactory)
from .dev import ValgrindDependency, gmock_factory, gtest_factory, llvm_factory
from .dev import ValgrindDependency, gmock_factory, gtest_factory, llvm_factory, zlib_factory
from .coarrays import coarray_factory
from .mpi import MPIDependency
from .scalapack import ScalapackDependency
Expand All @@ -44,6 +44,7 @@
'gmock': gmock_factory,
'llvm': llvm_factory,
'valgrind': ValgrindDependency,
'zlib': zlib_factory,

'boost': BoostDependency,
'cuda': CudaDependency,
Expand Down
53 changes: 53 additions & 0 deletions mesonbuild/dependencies/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
strip_system_libdirs, ConfigToolDependency, CMakeDependency, DependencyFactory,
)
from .misc import threads_factory
from ..compilers.c import AppleClangCCompiler
from ..compilers.cpp import AppleClangCPPCompiler

if T.TYPE_CHECKING:
from .. environment import Environment
Expand Down Expand Up @@ -449,6 +451,50 @@ def get_link_args(self, **kwargs):
return []


class ZlibSystemDependency(ExternalDependency):

def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.Any]):
super().__init__(name, environment, kwargs)

m = self.env.machines[self.for_machine]

# I'm not sure this is entirely correct. What if we're cross compiling
# from something to macOS?
if ((m.is_darwin() and isinstance(self.clib_compiler, (AppleClangCCompiler, AppleClangCPPCompiler))) or
m.is_freebsd() or m.is_dragonflybsd()):
self.is_found = True
self.link_args = ['-lz']

# No need to set includes,
# on macos xcode/clang will do that for us.
# on freebsd zlib.h is in /usr/include
elif m.is_windows():
if self.clib_compiler.get_argument_syntax() == 'msvc':
libs = ['zlib1' 'zlib']
else:
libs = ['z']
for lib in libs:
l = self.clib_compiler.find_library(lib, environment, [])
h = self.clib_compiler.has_header('zlib.h', '', environment, dependencies=[self])
if l and h:
self.is_found = True
self.link_args = l
break
else:
return
else:
mlog.debug('Unsupported OS {}'.format(m.system))
return

v, _ = self.clib_compiler.get_define('ZLIB_VERSION', '#include <zlib.h>', self.env, [], [self])
self.version = v.strip('"')


@staticmethod
def get_methods():
return [DependencyMethods.SYSTEM]


llvm_factory = DependencyFactory(
'LLVM',
[DependencyMethods.CMAKE, DependencyMethods.CONFIG_TOOL],
Expand All @@ -469,3 +515,10 @@ def get_link_args(self, **kwargs):
pkgconfig_class=GMockDependencyPC,
system_class=GMockDependencySystem,
)

zlib_factory = DependencyFactory(
'zlib',
[DependencyMethods.PKGCONFIG, DependencyMethods.CMAKE, DependencyMethods.SYSTEM],
cmake_name='ZLIB',
system_class=ZlibSystemDependency,
)
23 changes: 23 additions & 0 deletions test cases/common/228 zlib/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
project('zlib system dependency', 'c')

if not ['darwin', 'freebsd', 'dragonfly', 'windows'].contains(host_machine.system())
error('MESON_SKIP_TEST only applicable on macOS, FreeBSD, DragonflyBSD, and Windows.')
endif

cc = meson.get_compiler('c')

if host_machine.system() == 'darwin' and cc.get_id() != 'clang'
# this will only work on mac if using Apple's clang compiler, but there is no
# way in the meson source level to differentiate apple clang and llvm clang
# In the meson CI only apple clang is tested
error('MESON_SKIP_TEST on macOS only clang is supported.')
endif

if not (cc.find_library('z', required: false).found() or
cc.find_library('zlib', required : false).found() or
cc.find_library('zlib1', required : false).found())
error('MESON_SKIP_TEST Cannot seem to find zlib via find_library, this test will probably fail.')
endif

z = dependency('zlib', method : 'system')
assert(z.version().version_compare('>= 1.2'), 'Version does not seem to have been detected correctly.')

0 comments on commit 31d89a4

Please sign in to comment.