find_library: Only run link test on system dirs

Paths provided to us by the user or by pkg-config can be (and must be)
assumed to be usable since they might not be usable standalone.

Closes https://github.com/mesonbuild/meson/issues/3832
pull/3835/head
Nirbheek Chauhan 7 years ago committed by Nirbheek Chauhan
parent 602e58d398
commit f18af09189
  1. 27
      mesonbuild/compilers/c.py
  2. 5
      test cases/common/146 C and CPP link/foo.cpp
  3. 4
      test cases/common/146 C and CPP link/foobar.c

@ -851,31 +851,36 @@ class CCompiler(Compiler):
args = ['-l' + libname]
if self.links(code, env, extra_args=args):
return args
# Ensure that we won't modify the list that was passed to us
extra_dirs = extra_dirs[:]
# Search in the system libraries too
extra_dirs += self.get_library_dirs()
system_dirs = self.get_library_dirs()
# Not found or we want to use a specific libtype? Try to find the
# library file itself.
prefixes, suffixes = self.get_library_naming(env, libtype)
# Triply-nested loop!
# Triply-nested loops!
for d in extra_dirs:
for suffix in suffixes:
for prefix in prefixes:
trial = os.path.join(d, prefix + libname + '.' + suffix)
# as well as checking the path, we need to check compilation
# with link-whole, as static libs (.a) need to be checked
# to ensure they are the right architecture, e.g. 32bit or
# 64-bit. Just a normal test link won't work as the .a file
# doesn't seem to be checked by linker if there are no
# unresolved symbols from the main C file.
if os.path.isfile(trial):
return [trial]
for d in system_dirs:
for suffix in suffixes:
for prefix in prefixes:
trial = os.path.join(d, prefix + libname + '.' + suffix)
# When searching the system paths used by the compiler, we
# need to check linking with link-whole, as static libs
# (.a) need to be checked to ensure they are the right
# architecture, e.g. 32bit or 64-bit.
# Just a normal test link won't work as the .a file doesn't
# seem to be checked by linker if there are no unresolved
# symbols from the main C file.
extra_link_args = self.get_link_whole_for([trial])
extra_link_args = self.linker_to_compiler_args(extra_link_args)
if (os.path.isfile(trial) and
self.links(code, env,
extra_args=extra_link_args)):
return [trial]
# XXX: For OpenBSD and macOS we (may) need to search for libfoo.x.y.z.dylib
# XXX: For OpenBSD and macOS we (may) need to search for libfoo.x{,.y.z}.ext
return None
def find_library_impl(self, libname, env, extra_dirs, code, libtype):

@ -16,6 +16,9 @@
const int cnums[] = {0, 61};
/* Provided by foobar.c */
extern "C" int get_number_index (void);
template<typename T, int N>
std::vector<T> makeVector(const T (&data)[N])
{
@ -27,5 +30,5 @@ namespace {
}
extern "C" int six_one(void) {
return numbers[1];
return numbers[get_number_index ()];
}

@ -17,6 +17,10 @@
#include "foo.hpp"
#include "foobar.h"
int get_number_index (void) {
return 1;
}
void mynumbers(int nums[]) {
nums[0] = forty_two();
nums[1] = six_one();

Loading…
Cancel
Save