Skip to content

Commit

Permalink
Added find_library method and deprecated the standalone version. Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
jpakkane committed Mar 12, 2016
1 parent 0d5eaa2 commit 6b548a1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
13 changes: 13 additions & 0 deletions mesonbuild/compilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ def unix_link_flags_to_native(self, args):
def unix_compile_flags_to_native(self, args):
return args

def find_library(self, libname):
raise EnvironmentException('Language {} does not support library finding.'.format(self.language))

class CCompiler(Compiler):
def __init__(self, exelist, version, is_cross, exe_wrapper=None):
super().__init__(exelist, version)
Expand Down Expand Up @@ -563,6 +566,16 @@ def has_type(self, typename, prefix, extra_args):
'''
return self.compiles(templ % (prefix, typename), extra_args)

def find_library(self, libname):
code = '''int main(int argc, char **argv) {
return 0;
}
'''
linkarg = '-l' + libname
if self.links(code, extra_args=[linkarg]):
return linkarg
return None

def thread_flags(self):
return ['-pthread']

Expand Down
11 changes: 9 additions & 2 deletions mesonbuild/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,14 @@ class ExternalLibrary(Dependency):
def __init__(self, name, fullpath=None, silent=False):
super().__init__()
self.name = name
self.fullpath = fullpath
# Rename fullpath to link_args once standalone find_library() gets removed.
if fullpath is not None:
if isinstance(fullpath, list):
self.fullpath = fullpath
else:
self.fullpath = [fullpath]
else:
self.fullpath = fullpath
if not silent:
if self.found():
mlog.log('Library', mlog.bold(name), 'found:', mlog.green('YES'),
Expand All @@ -401,7 +408,7 @@ def found(self):

def get_link_args(self):
if self.found():
return [self.fullpath]
return self.fullpath
return []

class BoostDependency(Dependency):
Expand Down
20 changes: 20 additions & 0 deletions mesonbuild/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import importlib

find_lib_deprecation_printed = False

class InterpreterException(coredata.MesonException):
pass

Expand Down Expand Up @@ -581,6 +583,7 @@ def __init__(self, compiler, env):
'alignment' : self.alignment_method,
'version' : self.version_method,
'cmd_array' : self.cmd_array_method,
'find_library': self.find_library_method,
})

def version_method(self, args, kwargs):
Expand Down Expand Up @@ -750,6 +753,19 @@ def has_header_method(self, args, kwargs):
mlog.log('Has header "%s":' % string, h)
return haz

def find_library_method(self, args, kwargs):
if len(args) != 1:
raise InterpreterException('find_library method takes one argument.')
libname = args[0]
if not isinstance(libname, str):
raise InterpreterException('Library name not a string.')
required = kwargs.get('required', True)
if not isinstance(required, bool):
raise InterpreterException('required must be boolean.')
linkarg = self.compiler.find_library(libname)
lib = dependencies.ExternalLibrary(libname, linkarg)
return ExternalLibraryHolder(lib)

class ModuleState:
pass

Expand Down Expand Up @@ -1526,6 +1542,10 @@ def func_find_program(self, node, args, kwargs):
return progobj

def func_find_library(self, node, args, kwargs):
global find_lib_deprecation_printed
if not find_lib_deprecation_printed:
find_lib_deprecation_printed = True
mlog.log(mlog.red('DEPRECATION:'), 'find_library() is deprecated, use the corresponding method in compiler object instead.')
self.validate_arguments(args, 1, [str])
required = kwargs.get('required', True)
if not isinstance(required, bool):
Expand Down
9 changes: 7 additions & 2 deletions test cases/linuxlike/2 external library/meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
project('external library', 'c')

zlib = find_library('z')
cc = meson.get_compiler('c')
zlib = find_library('z') # DEPRECATED
zlib2 = cc.find_library('z') # The modern way.

# Verify that link testing works.
linkcode = '''#include<zlib.h>
Expand All @@ -16,13 +18,16 @@ int main(int argc, char **argv) {
return ptr == 0;
}
'''
cc = meson.get_compiler('c')

assert(cc.links(linkcode, args : '-lz', name : 'Test link against zlib'), 'Linking test failed.')
assert(not cc.links(nolinkcode, name : 'Failing link'), 'Linking succeeded when it should have failed.')

e = executable('zprog', 'prog.c', dependencies : zlib)
test('libtest', e)

e2 = executable('zprog_alt', 'prog.c', dependencies : zlib2)
test('libtest_alt', e2)

# Test that ext deps work via an internal dep.
intdep = declare_dependency(dependencies : zlib)
exe2 = executable('zprog2', 'prog.c', dependencies : intdep)
Expand Down

0 comments on commit 6b548a1

Please sign in to comment.