aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJussi Pakkanen <jpakkane@gmail.com>2021-05-18 23:29:34 +0300
committerGitHub <noreply@github.com>2021-05-18 23:29:34 +0300
commit2a0c2e51373e3e8aa1d4b7eb7f707f826f26f1ef (patch)
tree43a941be3555f9d563ab2bbc6328b240fb04db46
parentd7cb58e57dc3017f77f47a44efc17a4b2d28a49c (diff)
parente7e04c814ba8f513deb7b67d298277e083cd1514 (diff)
downloadmeson-2a0c2e51373e3e8aa1d4b7eb7f707f826f26f1ef.zip
meson-2a0c2e51373e3e8aa1d4b7eb7f707f826f26f1ef.tar.gz
meson-2a0c2e51373e3e8aa1d4b7eb7f707f826f26f1ef.tar.bz2
Merge pull request #8403 from dcbaker/submit/rust-fix-shared-internal-linking
Submit/rust fix shared internal linking
-rw-r--r--mesonbuild/backend/ninjabackend.py17
-rw-r--r--test cases/rust/16 internal c dependencies/lib.c6
-rw-r--r--test cases/rust/16 internal c dependencies/lib.h22
-rw-r--r--test cases/rust/16 internal c dependencies/main.rs9
-rw-r--r--test cases/rust/16 internal c dependencies/meson.build14
-rwxr-xr-xtest cases/rust/16 internal c dependencies/test.py25
6 files changed, 88 insertions, 5 deletions
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py
index 05427d3..3749ff7 100644
--- a/mesonbuild/backend/ninjabackend.py
+++ b/mesonbuild/backend/ninjabackend.py
@@ -1608,12 +1608,19 @@ int dummy;
# dependency, so that collisions with libraries in rustc's
# sysroot don't cause ambiguity
args += ['--extern', '{}={}'.format(d.name, os.path.join(d.subdir, d.filename))]
+ elif d.typename == 'static library':
+ # Rustc doesn't follow Meson's convention that static libraries
+ # are called .a, and implementation libraries are .lib, so we
+ # have to manually handle that.
+ if rustc.linker.id == 'link':
+ args += ['-C', f'link-arg={self.get_target_filename_for_linking(d)}']
+ else:
+ args += ['-l', f'static={d.name}']
+ external_deps.extend(d.external_deps)
else:
- # 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)
+ # Rust uses -l for non rust dependencies, but we still need to
+ # add dylib=foo
+ args += ['-l', f'dylib={d.name}']
for e in external_deps:
for a in e.get_link_args():
if a.endswith(('.dll', '.so', '.dylib')):
diff --git a/test cases/rust/16 internal c dependencies/lib.c b/test cases/rust/16 internal c dependencies/lib.c
new file mode 100644
index 0000000..e852de6
--- /dev/null
+++ b/test cases/rust/16 internal c dependencies/lib.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+#include "lib.h"
+
+void c_func(void) {
+ printf("This is a " MODE " C library\n");
+}
diff --git a/test cases/rust/16 internal c dependencies/lib.h b/test cases/rust/16 internal c dependencies/lib.h
new file mode 100644
index 0000000..847bd16
--- /dev/null
+++ b/test cases/rust/16 internal c dependencies/lib.h
@@ -0,0 +1,22 @@
+#pragma once
+
+#if defined _WIN32 || defined __CYGWIN__
+ #if defined BUILDING_ADDER
+ #define DLL_PUBLIC __declspec(dllexport)
+ #else
+ #define DLL_PUBLIC __declspec(dllimport)
+ #endif
+#else
+ #if defined __GNUC__
+ #if defined BUILDING_ADDER
+ #define DLL_PUBLIC __attribute__ ((visibility("default")))
+ #else
+ #define DLL_PUBLIC
+ #endif
+ #else
+ #pragma message("Compiler does not support symbol visibility.")
+ #define DLL_PUBLIC
+ #endif
+#endif
+
+DLL_PUBLIC void c_func(void);
diff --git a/test cases/rust/16 internal c dependencies/main.rs b/test cases/rust/16 internal c dependencies/main.rs
new file mode 100644
index 0000000..5383599
--- /dev/null
+++ b/test cases/rust/16 internal c dependencies/main.rs
@@ -0,0 +1,9 @@
+extern "C" {
+ fn c_func();
+}
+
+fn main() {
+ unsafe {
+ c_func();
+ }
+}
diff --git a/test cases/rust/16 internal c dependencies/meson.build b/test cases/rust/16 internal c dependencies/meson.build
new file mode 100644
index 0000000..c7476d7
--- /dev/null
+++ b/test cases/rust/16 internal c dependencies/meson.build
@@ -0,0 +1,14 @@
+project('internal dependencies', 'c', 'rust')
+
+test_prog = find_program('test.py')
+
+static = static_library('static', 'lib.c', c_args : '-DMODE="static"')
+exe = executable('static', 'main.rs', link_with : static)
+test('static linkage', test_prog, args : [exe, 'This is a static C library'])
+
+# Shared linkage with rust doesn't work on macOS with meson, yet
+if host_machine.system() != 'darwin'
+ shared = shared_library('shared', 'lib.c', c_args : '-DMODE="shared"')
+ exe = executable('shared', 'main.rs', link_with : shared)
+ test('shared linkage', test_prog, args : [exe, 'This is a shared C library'])
+endif
diff --git a/test cases/rust/16 internal c dependencies/test.py b/test cases/rust/16 internal c dependencies/test.py
new file mode 100755
index 0000000..dacec12
--- /dev/null
+++ b/test cases/rust/16 internal c dependencies/test.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python3
+
+import argparse
+import subprocess
+import sys
+
+
+def main() -> None:
+ parser = argparse.ArgumentParser()
+ parser.add_argument('command')
+ parser.add_argument('expected')
+ args = parser.parse_args()
+
+ out = subprocess.run(args.command, stdout=subprocess.PIPE)
+ actual = out.stdout.decode().strip()
+
+ if args.expected != actual:
+ print('expected:', args.expected, file=sys.stderr)
+ print('actual: ', actual, file=sys.stderr)
+ sys.exit(1)
+ sys.exit(0)
+
+
+if __name__ == "__main__":
+ main()