diff options
Diffstat (limited to 'test cases/rust/15 polyglot sharedlib')
-rw-r--r-- | test cases/rust/15 polyglot sharedlib/adder.c | 18 | ||||
-rw-r--r-- | test cases/rust/15 polyglot sharedlib/adder.h | 34 | ||||
-rw-r--r-- | test cases/rust/15 polyglot sharedlib/adder.rs | 9 | ||||
-rw-r--r-- | test cases/rust/15 polyglot sharedlib/addertest.c | 12 | ||||
-rw-r--r-- | test cases/rust/15 polyglot sharedlib/meson.build | 20 |
5 files changed, 93 insertions, 0 deletions
diff --git a/test cases/rust/15 polyglot sharedlib/adder.c b/test cases/rust/15 polyglot sharedlib/adder.c new file mode 100644 index 0000000..66613ed --- /dev/null +++ b/test cases/rust/15 polyglot sharedlib/adder.c @@ -0,0 +1,18 @@ +#include<adder.h> +#include<stdlib.h> + +struct _Adder { + int number; +}; + +adder* adder_create(int number) { + adder *a = malloc(sizeof(struct _Adder)); + a->number = number; + return a; +} + +// adder_add is implemented in the Rust file. + +void adder_destroy(adder *a) { + free(a); +} diff --git a/test cases/rust/15 polyglot sharedlib/adder.h b/test cases/rust/15 polyglot sharedlib/adder.h new file mode 100644 index 0000000..fb2105e --- /dev/null +++ b/test cases/rust/15 polyglot sharedlib/adder.h @@ -0,0 +1,34 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#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 + +typedef struct _Adder adder; + +DLL_PUBLIC extern adder* adder_create(int number); +DLL_PUBLIC extern int adder_add(adder *a, int number); +DLL_PUBLIC extern void adder_destroy(adder*); + +#ifdef __cplusplus +} +#endif diff --git a/test cases/rust/15 polyglot sharedlib/adder.rs b/test cases/rust/15 polyglot sharedlib/adder.rs new file mode 100644 index 0000000..9095350 --- /dev/null +++ b/test cases/rust/15 polyglot sharedlib/adder.rs @@ -0,0 +1,9 @@ +#[repr(C)] +pub struct Adder { + pub number: i32 +} + +#[no_mangle] +pub extern fn adder_add(a: &Adder, number: i32) -> i32 { + return a.number + number; +} diff --git a/test cases/rust/15 polyglot sharedlib/addertest.c b/test cases/rust/15 polyglot sharedlib/addertest.c new file mode 100644 index 0000000..87e45b6 --- /dev/null +++ b/test cases/rust/15 polyglot sharedlib/addertest.c @@ -0,0 +1,12 @@ +#include<stdlib.h> +#include<adder.h> + +int main(int argc, char **argv) { + adder *a = adder_create(3); + int result = adder_add(a, 4); + if(result != 7) { + return 1; + } + adder_destroy(a); + return 0; +} diff --git a/test cases/rust/15 polyglot sharedlib/meson.build b/test cases/rust/15 polyglot sharedlib/meson.build new file mode 100644 index 0000000..13fc8fd --- /dev/null +++ b/test cases/rust/15 polyglot sharedlib/meson.build @@ -0,0 +1,20 @@ +project('adder', 'c', 'rust', version: '1.0.0') + +if build_machine.system() != 'linux' + error('MESON_SKIP_TEST, this test only works on Linux. Patches welcome.') +endif + +thread_dep = dependency('threads') +dl_dep = meson.get_compiler('c').find_library('dl', required: false) +m_dep = meson.get_compiler('c').find_library('m', required: false) + +rl = static_library('radder', 'adder.rs', rust_crate_type: 'staticlib') + +l = shared_library('adder', 'adder.c', + c_args: '-DBUILDING_ADDER', + link_with: rl, + version: '1.0.0', + soversion: '1', + link_args: '-Wl,-u,adder_add', # Ensure that Rust code is not removed as unused. + dependencies: [thread_dep, dl_dep, m_dep]) +test('adder', executable('addertest', 'addertest.c', link_with: l)) |