Merge pull request #8404 from dcbaker/submit/rust-c-dependencies

Fix linking Rust with C dependencies
pull/8423/head
Jussi Pakkanen 4 years ago committed by GitHub
commit 542dea2571
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      mesonbuild/backend/ninjabackend.py
  2. 10
      test cases/rust/13 external c dependencies/c_accessing_zlib.c
  3. 23
      test cases/rust/13 external c dependencies/meson.build
  4. 2
      test cases/rust/13 external c dependencies/meson_options.txt
  5. 9
      test cases/rust/13 external c dependencies/prog.rs
  6. 18
      test cases/rust/13 external c dependencies/test.json

@ -1598,6 +1598,7 @@ int dummy;
args += rustc.get_output_args(os.path.join(target.subdir, target.get_filename()))
args += self.environment.coredata.get_external_args(target.for_machine, rustc.language)
linkdirs = mesonlib.OrderedSet()
external_deps = target.external_deps.copy()
for d in target.link_targets:
linkdirs.add(d.subdir)
if d.uses_rust():
@ -1609,14 +1610,27 @@ int dummy;
# Rust uses -l for non rust dependencies, but we still need to add (shared|static)=foo
_type = 'static' if d.typename == 'static library' else 'shared'
args += ['-l', f'{_type}={d.name}']
if d.typename == 'static library':
external_deps.extend(d.external_deps)
for e in external_deps:
for a in e.get_link_args():
if a.endswith(('.dll', '.so', '.dylib')):
dir_, lib = os.path.split(a)
linkdirs.add(dir_)
lib, ext = os.path.splitext(lib)
if lib.startswith('lib'):
lib = lib[3:]
args.extend(['-l', f'dylib={lib}'])
elif a.startswith('-L'):
args.append(a)
elif a.startswith('-l'):
# This should always be a static lib, I think
args.extend(['-l', f'static={a[2:]}'])
for d in linkdirs:
if d == '':
d = '.'
args += ['-L', d]
has_shared_deps = False
for dep in target.get_dependencies():
if isinstance(dep, build.SharedLibrary):
has_shared_deps = True
has_shared_deps = any(isinstance(dep, build.SharedLibrary) for dep in target.get_dependencies())
if isinstance(target, build.SharedLibrary) or has_shared_deps:
# add prefer-dynamic if any of the Rust libraries we link
# against are dynamic, otherwise we'll end up with

@ -0,0 +1,10 @@
#include <stdio.h>
#include <string.h>
#include <zlib.h>
void c_accessing_zlib(void) {
struct z_stream_s zstream;
printf("Hello from C!\n");
memset(&zstream, 0, sizeof(zstream));
inflateInit(&zstream);
}

@ -0,0 +1,23 @@
project('rust linking to c using dependency', 'c', 'rust')
if host_machine.system() == 'darwin'
error('MESON_SKIP_TEST: doesnt work right on macos, please fix!')
endif
dep_zlib = dependency('zlib', static : get_option('static'), method : get_option('method'), required : false)
if not dep_zlib.found()
error('MESON_SKIP_TEST: Could not find a @0@ zlib'.format(get_option('static') ? 'static' : 'shared'))
endif
l = static_library(
'c_accessing_zlib',
'c_accessing_zlib.c',
dependencies: [dep_zlib],
)
e = executable(
'prog', 'prog.rs',
link_with : l,
)
test('cdepstest', e)

@ -0,0 +1,2 @@
option('static', type : 'boolean')
option('method', type : 'string')

@ -0,0 +1,9 @@
extern "C" {
fn c_accessing_zlib();
}
fn main() {
unsafe {
c_accessing_zlib();
}
}

@ -0,0 +1,18 @@
{
"matrix": {
"options": {
"static": [
{ "val": true },
{ "val": false }
],
"method": [
{ "val": "pkg-config" },
{ "val": "cmake" },
{ "val": "system" }
]
},
"exclude": [
{ "static": true, "method": "pkg-config" }
]
}
}
Loading…
Cancel
Save