Find and use external libraries.

pull/15/head
Jussi Pakkanen 12 years ago
parent 68b50bd732
commit 347c263431
  1. 4
      backends.py
  2. 1
      coredata.py
  3. 22
      environment.py
  4. 41
      interpreter.py
  5. 5
      test cases/linuxlike/2 external library/meson.build
  6. 8
      test cases/linuxlike/2 external library/prog.c

@ -143,6 +143,7 @@ class Backend():
if isinstance(target, interpreter.SharedLibrary): if isinstance(target, interpreter.SharedLibrary):
commands += compiler.get_pic_flags() commands += compiler.get_pic_flags()
for dep in target.get_external_deps(): for dep in target.get_external_deps():
if isinstance(dep, environment.Dependency):
commands += dep.get_compile_flags() commands += dep.get_compile_flags()
return commands return commands
@ -537,7 +538,10 @@ class NinjaBackend(Backend):
else: else:
raise RuntimeError('Unknown build target type.') raise RuntimeError('Unknown build target type.')
for dep in target.get_external_deps(): for dep in target.get_external_deps():
if isinstance(dep, environment.Dependency):
commands += dep.get_link_flags() commands += dep.get_link_flags()
else:
commands.append(dep.get_filename())
dependencies = target.get_dependencies() dependencies = target.get_dependencies()
commands += self.build_target_link_arguments(dependencies) commands += self.build_target_link_arguments(dependencies)
if self.environment.coredata.coverage: if self.environment.coredata.coverage:

@ -40,6 +40,7 @@ class CoreData():
self.compilers = {} self.compilers = {}
self.deps = {} self.deps = {}
self.ext_progs = {} self.ext_progs = {}
self.ext_libs = {}
def load(filename): def load(filename):
obj = pickle.load(open(filename, 'rb')) obj = pickle.load(open(filename, 'rb'))

@ -16,6 +16,7 @@
import subprocess, os.path, platform import subprocess, os.path, platform
import coredata import coredata
from glob import glob
build_filename = 'meson.build' build_filename = 'meson.build'
@ -416,6 +417,27 @@ class Environment():
def get_datadir(self): def get_datadir(self):
return self.coredata.datadir return self.coredata.datadir
def find_library(self, libname):
dirs = self.get_library_dirs()
suffixes = [self.get_shared_lib_suffix(), self.get_static_lib_suffix()]
prefix = self.get_shared_lib_prefix()
for d in dirs:
for suffix in suffixes:
trial = os.path.join(d, prefix + libname + '.' + suffix)
if os.path.isfile(trial):
return trial
def get_library_dirs(self):
if is_windows():
return ['C:/mingw/lib'] # Fixme
if is_osx():
return ['/usr/lib'] # Fix me as well.
unixdirs = ['/usr/lib', '/lib']
plat = subprocess.check_output(['uname', '-m']).decode().strip()
unixdirs += glob('/usr/lib/' + plat + '*')
unixdirs.append('/usr/local/lib')
return unixdirs
class Dependency(): class Dependency():
def __init__(self): def __init__(self):
pass pass

@ -40,11 +40,11 @@ class InterpreterObject():
raise InvalidCode('Unknown method "%s" in object.' % method_name) raise InvalidCode('Unknown method "%s" in object.' % method_name)
class ExternalProgram(InterpreterObject): class ExternalProgram(InterpreterObject):
def __init__(self, name, fullpath=None): def __init__(self, name, fullpath=None):
InterpreterObject.__init__(self) InterpreterObject.__init__(self)
self.name = name self.name = name
self.fullpath = fullpath self.fullpath = fullpath
#self.methods.update({'found': self.found_method})
def found(self): def found(self):
return self.fullpath is not None return self.fullpath is not None
@ -58,6 +58,25 @@ class ExternalProgram(InterpreterObject):
def get_name(self): def get_name(self):
return self.name return self.name
class ExternalLibrary(InterpreterObject):
def __init__(self, name, fullpath=None):
InterpreterObject.__init__(self)
self.name = name
self.fullpath = fullpath
#self.methods.update({'found': self.found_method})
def found(self):
return self.fullpath is not None
def found_method(self, args, kwargs):
return self.found()
def get_filename(self):
return self.fullpath
def get_name(self):
return self.name
class Generator(InterpreterObject): class Generator(InterpreterObject):
def __init__(self, args, kwargs): def __init__(self, args, kwargs):
@ -349,7 +368,8 @@ class BuildTarget(InterpreterObject):
def add_external_deps(self, deps): def add_external_deps(self, deps):
for dep in deps: for dep in deps:
if not isinstance(dep, environment.Dependency): if not isinstance(dep, environment.Dependency) and\
not isinstance(dep, ExternalLibrary):
raise InvalidArguments('Argument is not an external dependency') raise InvalidArguments('Argument is not an external dependency')
self.external_deps.append(dep) self.external_deps.append(dep)
@ -496,6 +516,7 @@ class Interpreter():
'include_directories' : self.func_include_directories, 'include_directories' : self.func_include_directories,
'add_global_arguments' : self.func_add_global_arguments, 'add_global_arguments' : self.func_add_global_arguments,
'find_program' : self.func_find_program, 'find_program' : self.func_find_program,
'find_library' : self.func_find_library,
} }
def get_build_def_files(self): def get_build_def_files(self):
@ -625,6 +646,22 @@ class Interpreter():
raise InvalidArguments('Line %d: program "%s" not found.' % (node.lineno(), exename)) raise InvalidArguments('Line %d: program "%s" not found.' % (node.lineno(), exename))
return progobj return progobj
def func_find_library(self, node, args, kwargs):
self.validate_arguments(args, 1, [str])
required = kwargs.get('required', False)
if not isinstance(required, bool):
raise InvalidArguments('Line %d: "required" argument must be a boolean.' % node.lineno())
libname = args[0]
if libname in self.coredata.ext_libs and\
self.coredata.ext_libs[libname].found():
return self.coredata.ext_progs[libname]
result = self.environment.find_library(libname)
libobj = ExternalLibrary(libname, result)
self.coredata.ext_libs[libname] = libobj
if required and not libobj.found():
raise InvalidArguments('Line %d: external library "%s" not found.' % (node.lineno(), libname))
return libobj
def func_find_dep(self, node, args, kwargs): def func_find_dep(self, node, args, kwargs):
self.validate_arguments(args, 1, [str]) self.validate_arguments(args, 1, [str])
name = args[0] name = args[0]

@ -0,0 +1,5 @@
project('external library', 'c')
zlib = find_library('z', required : true)
e = executable('zprog', 'prog.c', deps : zlib)
add_test('libtest', e)

@ -0,0 +1,8 @@
#include<zlib.h>
int main(int argc, char **argv) {
void * something = deflate;
if(something != 0)
return 0;
return 1;
}
Loading…
Cancel
Save