diff options
Diffstat (limited to 'meson/test cases/rust')
70 files changed, 834 insertions, 0 deletions
diff --git a/meson/test cases/rust/1 basic/meson.build b/meson/test cases/rust/1 basic/meson.build new file mode 100644 index 000000000..3919279a9 --- /dev/null +++ b/meson/test cases/rust/1 basic/meson.build @@ -0,0 +1,9 @@ +project('rustprog', 'rust') + +e = executable('program', 'prog.rs', + rust_args : ['-C', 'lto'], # Just a test + install : true +) +test('rusttest', e) + +subdir('subdir') diff --git a/meson/test cases/rust/1 basic/prog.rs b/meson/test cases/rust/1 basic/prog.rs new file mode 100644 index 000000000..b171a80c2 --- /dev/null +++ b/meson/test cases/rust/1 basic/prog.rs @@ -0,0 +1,3 @@ +fn main() { + println!("rust compiler is working"); +} diff --git a/meson/test cases/rust/1 basic/subdir/meson.build b/meson/test cases/rust/1 basic/subdir/meson.build new file mode 100644 index 000000000..51b385b40 --- /dev/null +++ b/meson/test cases/rust/1 basic/subdir/meson.build @@ -0,0 +1,2 @@ +e = executable('program2', 'prog.rs', install : true) +test('rusttest2', e) diff --git a/meson/test cases/rust/1 basic/subdir/prog.rs b/meson/test cases/rust/1 basic/subdir/prog.rs new file mode 100644 index 000000000..b171a80c2 --- /dev/null +++ b/meson/test cases/rust/1 basic/subdir/prog.rs @@ -0,0 +1,3 @@ +fn main() { + println!("rust compiler is working"); +} diff --git a/meson/test cases/rust/1 basic/test.json b/meson/test cases/rust/1 basic/test.json new file mode 100644 index 000000000..94c995b3f --- /dev/null +++ b/meson/test cases/rust/1 basic/test.json @@ -0,0 +1,8 @@ +{ + "installed": [ + {"type": "exe", "file": "usr/bin/program"}, + {"type": "pdb", "file": "usr/bin/program"}, + {"type": "exe", "file": "usr/bin/program2"}, + {"type": "pdb", "file": "usr/bin/program2"} + ] +} diff --git a/meson/test cases/rust/10 language stds/2015.rs b/meson/test cases/rust/10 language stds/2015.rs new file mode 100644 index 000000000..4d28c57c9 --- /dev/null +++ b/meson/test cases/rust/10 language stds/2015.rs @@ -0,0 +1,3 @@ +trait Foo { + fn foo(&self, Box<dyn Foo>); +} diff --git a/meson/test cases/rust/10 language stds/2018.rs b/meson/test cases/rust/10 language stds/2018.rs new file mode 100644 index 000000000..400915463 --- /dev/null +++ b/meson/test cases/rust/10 language stds/2018.rs @@ -0,0 +1,9 @@ +const fn foo(x: i32) -> i32 { + return x + 1; +} + +const VALUE: i32 = foo(-1); + +pub fn main() { + std::process::exit(VALUE); +} diff --git a/meson/test cases/rust/10 language stds/meson.build b/meson/test cases/rust/10 language stds/meson.build new file mode 100644 index 000000000..994433968 --- /dev/null +++ b/meson/test cases/rust/10 language stds/meson.build @@ -0,0 +1,18 @@ +project('rust std options', 'rust') + +# this only works in 2018 +new = executable( + 'new', + '2018.rs', + override_options : ['rust_std=2018'], +) + +# this only works in 2015 +old = static_library( + 'old', + '2015.rs', + override_options : ['rust_std=2015'], +) + + +test('2018 std', new) diff --git a/meson/test cases/rust/11 generated main/gen.py b/meson/test cases/rust/11 generated main/gen.py new file mode 100644 index 000000000..c8cfe76ec --- /dev/null +++ b/meson/test cases/rust/11 generated main/gen.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 + +import argparse + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument('out') + parser.add_argument('--mode', choices=['main', 'lib'], default='main') + args = parser.parse_args() + + with open(args.out, 'w') as f: + if args.mode == 'main': + f.write('fn main() { println!("I prefer tarnish, actually.") }') + elif args.mode == 'lib': + f.write('pub fn libfun() { println!("I prefer tarnish, actually.") }') + + +if __name__ == "__main__": + main() diff --git a/meson/test cases/rust/11 generated main/generated_lib_main.rs b/meson/test cases/rust/11 generated main/generated_lib_main.rs new file mode 100644 index 000000000..d9b373e41 --- /dev/null +++ b/meson/test cases/rust/11 generated main/generated_lib_main.rs @@ -0,0 +1,5 @@ +extern crate static_lib_generated as lib; + +fn main() { + lib::libfun(); +} diff --git a/meson/test cases/rust/11 generated main/meson.build b/meson/test cases/rust/11 generated main/meson.build new file mode 100644 index 000000000..695124cd2 --- /dev/null +++ b/meson/test cases/rust/11 generated main/meson.build @@ -0,0 +1,21 @@ +project('generated rust main', 'rust') + +gen = find_program('gen.py') + +c = custom_target( + 'custom_target', + command : [gen, '@OUTPUT@'], + output : ['main.rs'], +) + +executable('custom_target_main', c) +executable('custom_target_index_main', c[0]) + +gener = generator(gen, arguments : ['@OUTPUT@'], output : '@BASENAME@.rs') +# Doesn't actually use gen.py as input, just a limitation of generators +executable('generator_main', gener.process(['gen.py'])) + +subdir('sub') +executable('custom_target_subdir_main', s) + +executable('link_with_generated_lib', 'generated_lib_main.rs', link_with : lib) diff --git a/meson/test cases/rust/11 generated main/sub/meson.build b/meson/test cases/rust/11 generated main/sub/meson.build new file mode 100644 index 000000000..3ce96e510 --- /dev/null +++ b/meson/test cases/rust/11 generated main/sub/meson.build @@ -0,0 +1,13 @@ +s = custom_target( + 'subdir_target', + command : [gen, '@OUTPUT@'], + output : ['main.rs'], +) + +l = custom_target( + 'lib_target', + command : [gen, '@OUTPUT@', '--mode', 'lib'], + output : ['lib.rs'], +) + +lib = static_library('static_lib_generated', l) diff --git a/meson/test cases/rust/12 bindgen/include/other.h b/meson/test cases/rust/12 bindgen/include/other.h new file mode 100644 index 000000000..3f715f169 --- /dev/null +++ b/meson/test cases/rust/12 bindgen/include/other.h @@ -0,0 +1,6 @@ +// SPDX-license-identifer: Apache-2.0 +// Copyright © 2021 Intel Corporation + +# pragma once + +#include <stdint.h> diff --git a/meson/test cases/rust/12 bindgen/meson.build b/meson/test cases/rust/12 bindgen/meson.build new file mode 100644 index 000000000..7844884e2 --- /dev/null +++ b/meson/test cases/rust/12 bindgen/meson.build @@ -0,0 +1,82 @@ +# SPDX-license-identifer: Apache-2.0 +# Copyright © 2021 Intel Corporation + +project('rustmod bindgen', ['c', 'rust']) + +prog_bindgen = find_program('bindgen', required : false) +if not prog_bindgen.found() + error('MESON_SKIP_TEST bindgen not found') +endif + +# This seems to happen on windows when libclang.dll is not in path or is not +# valid. We must try to process a header file for this to work. +# +# See https://github.com/rust-lang/rust-bindgen/issues/1797 +result = run_command(prog_bindgen, 'include/other.h') +if result.returncode() != 0 + error('MESON_SKIP_TEST bindgen does not seem to work') +endif + +# This is to test the include_directories argument to bindgen +inc = include_directories('include') + +c_lib = static_library('clib', 'src/source.c', include_directories : inc) + +rust = import('unstable-rust') + +gen = rust.bindgen( + input : 'src/header.h', + output : 'header.rs', + include_directories : inc, +) + +# see: https://github.com/mesonbuild/meson/issues/8160 +f = configure_file( + input : 'src/main.rs', + output : 'main.rs', + copy : true, +) + +rust_bin = executable( + 'rust_bin', + [f, gen], + link_with : c_lib, +) + +test('main', rust_bin) + +# Test a generated header +gen_h = custom_target( + 'gen.h', + command : [find_program('src/gen_header.py'), '@INPUT@', '@OUTPUT@'], + output : 'gen.h', + input : 'src/header.h' +) + +gen2_h = custom_target( + 'other.h', + command : [find_program('src/gen_header.py'), '@INPUT@', '@OUTPUT@'], + output : 'other.h', + input : 'include/other.h' +) + +gen_rs = rust.bindgen( + input : [gen_h, gen2_h], + output : 'gen.rs', +) + +f = configure_file( + input : 'src/main2.rs', + output : 'main2.rs', + copy : true, +) + +rust_bin2 = executable( + 'rust_bin2', + [f, gen_rs], + link_with : c_lib, +) + +test('generated header', rust_bin2) + +subdir('sub') diff --git a/meson/test cases/rust/12 bindgen/src/gen_header.py b/meson/test cases/rust/12 bindgen/src/gen_header.py new file mode 100644 index 000000000..099e4ec74 --- /dev/null +++ b/meson/test cases/rust/12 bindgen/src/gen_header.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +# SPDX-license-identifer: Apache-2.0 +# Copyright © 2021 Intel Corporation + +import argparse +import shutil + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument('input') + parser.add_argument('output') + args = parser.parse_args() + + shutil.copy2(args.input, args.output) + + +if __name__ == "__main__": + main() diff --git a/meson/test cases/rust/12 bindgen/src/header.h b/meson/test cases/rust/12 bindgen/src/header.h new file mode 100644 index 000000000..2c03f3384 --- /dev/null +++ b/meson/test cases/rust/12 bindgen/src/header.h @@ -0,0 +1,8 @@ +// SPDX-license-identifer: Apache-2.0 +// Copyright © 2021 Intel Corporation + +#pragma once + +#include "other.h" + +int32_t add(const int32_t, const int32_t); diff --git a/meson/test cases/rust/12 bindgen/src/main.rs b/meson/test cases/rust/12 bindgen/src/main.rs new file mode 100644 index 000000000..3f85e96be --- /dev/null +++ b/meson/test cases/rust/12 bindgen/src/main.rs @@ -0,0 +1,14 @@ +// SPDX-license-identifer: Apache-2.0 +// Copyright © 2021 Intel Corporation + +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] + +include!("header.rs"); + +fn main() { + unsafe { + std::process::exit(add(0, 0)); + }; +} diff --git a/meson/test cases/rust/12 bindgen/src/main2.rs b/meson/test cases/rust/12 bindgen/src/main2.rs new file mode 100644 index 000000000..a3c28d78b --- /dev/null +++ b/meson/test cases/rust/12 bindgen/src/main2.rs @@ -0,0 +1,15 @@ + +// SPDX-license-identifer: Apache-2.0 +// Copyright © 2021 Intel Corporation + +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] + +include!("gen.rs"); + +fn main() { + unsafe { + std::process::exit(add(0, 0)); + }; +} diff --git a/meson/test cases/rust/12 bindgen/src/source.c b/meson/test cases/rust/12 bindgen/src/source.c new file mode 100644 index 000000000..d652d28b9 --- /dev/null +++ b/meson/test cases/rust/12 bindgen/src/source.c @@ -0,0 +1,8 @@ +// SPDX-license-identifer: Apache-2.0 +// Copyright © 2021 Intel Corporation + +#include "header.h" + +int32_t add(const int32_t first, const int32_t second) { + return first + second; +} diff --git a/meson/test cases/rust/12 bindgen/sub/meson.build b/meson/test cases/rust/12 bindgen/sub/meson.build new file mode 100644 index 000000000..1da360ebd --- /dev/null +++ b/meson/test cases/rust/12 bindgen/sub/meson.build @@ -0,0 +1,39 @@ +gen_rs3 = rust.bindgen( + input : [gen_h, gen2_h], + output : 'gen.rs', +) + +f3 = configure_file( + input : '../src/main2.rs', + output : 'main3.rs', + copy : true, +) + +rust_bin3 = executable( + 'rust_bin3', + [f3, gen_rs3], + link_with : c_lib, +) + +test('generated header (subdir)', rust_bin3) + +gen4 = rust.bindgen( + input : '../src/header.h', + output : 'header.rs', + include_directories : inc, +) + +# see: https://github.com/mesonbuild/meson/issues/8160 +f4 = configure_file( + input : '../src/main.rs', + output : 'main.rs', + copy : true, +) + +rust_bin4 = executable( + 'rust_bin_subdir', + [f4, gen4], + link_with : c_lib, +) + +test('static header (subdir)', rust_bin4) diff --git a/meson/test cases/rust/13 external c dependencies/c_accessing_zlib.c b/meson/test cases/rust/13 external c dependencies/c_accessing_zlib.c new file mode 100644 index 000000000..358b98998 --- /dev/null +++ b/meson/test cases/rust/13 external c dependencies/c_accessing_zlib.c @@ -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); +} diff --git a/meson/test cases/rust/13 external c dependencies/meson.build b/meson/test cases/rust/13 external c dependencies/meson.build new file mode 100644 index 000000000..e5caf70cf --- /dev/null +++ b/meson/test cases/rust/13 external c dependencies/meson.build @@ -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) diff --git a/meson/test cases/rust/13 external c dependencies/meson_options.txt b/meson/test cases/rust/13 external c dependencies/meson_options.txt new file mode 100644 index 000000000..f501348b6 --- /dev/null +++ b/meson/test cases/rust/13 external c dependencies/meson_options.txt @@ -0,0 +1,2 @@ +option('static', type : 'boolean') +option('method', type : 'string') diff --git a/meson/test cases/rust/13 external c dependencies/prog.rs b/meson/test cases/rust/13 external c dependencies/prog.rs new file mode 100644 index 000000000..b30ec24fb --- /dev/null +++ b/meson/test cases/rust/13 external c dependencies/prog.rs @@ -0,0 +1,9 @@ +extern "C" { + fn c_accessing_zlib(); +} + +fn main() { + unsafe { + c_accessing_zlib(); + } +} diff --git a/meson/test cases/rust/13 external c dependencies/test.json b/meson/test cases/rust/13 external c dependencies/test.json new file mode 100644 index 000000000..423581ff6 --- /dev/null +++ b/meson/test cases/rust/13 external c dependencies/test.json @@ -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" } + ] + } +} diff --git a/meson/test cases/rust/14 external libm/meson.build b/meson/test cases/rust/14 external libm/meson.build new file mode 100644 index 000000000..20af83c87 --- /dev/null +++ b/meson/test cases/rust/14 external libm/meson.build @@ -0,0 +1,24 @@ +project('rust linking to libm', 'c', 'rust') + +if host_machine.system() == 'darwin' + error('MESON_SKIP_TEST: doesnt work right on macos, please fix!') +endif + +cc = meson.get_compiler('c') +dep_m = cc.find_library('m', required : false, static : get_option('static')) +if not dep_m.found() + error('MESON_SKIP_TEST: Could not find a @0@ libm'.format(get_option('static') ? 'static' : 'shared')) +endif + +librs_math = static_library( + 'rs_math', + 'rs_math.rs', + dependencies : [dep_m], +) + +e = executable( + 'prog', 'prog.rs', + link_with : [librs_math], +) + +test('cdepstest', e) diff --git a/meson/test cases/rust/14 external libm/meson_options.txt b/meson/test cases/rust/14 external libm/meson_options.txt new file mode 100644 index 000000000..f501348b6 --- /dev/null +++ b/meson/test cases/rust/14 external libm/meson_options.txt @@ -0,0 +1,2 @@ +option('static', type : 'boolean') +option('method', type : 'string') diff --git a/meson/test cases/rust/14 external libm/prog.rs b/meson/test cases/rust/14 external libm/prog.rs new file mode 100644 index 000000000..674597785 --- /dev/null +++ b/meson/test cases/rust/14 external libm/prog.rs @@ -0,0 +1,5 @@ +extern crate rs_math; + +fn main() { + assert_eq!(rs_math::rs_log2(8.0), 3.0); +} diff --git a/meson/test cases/rust/14 external libm/rs_math.rs b/meson/test cases/rust/14 external libm/rs_math.rs new file mode 100644 index 000000000..ef53a5c3e --- /dev/null +++ b/meson/test cases/rust/14 external libm/rs_math.rs @@ -0,0 +1,12 @@ +#![crate_name = "rs_math"] + +use std::os::raw::c_double; + +extern "C" { + fn log2(n: c_double) -> c_double; +} + +#[no_mangle] +pub extern fn rs_log2(n: c_double) -> c_double { + unsafe { log2(n) } +} diff --git a/meson/test cases/rust/14 external libm/test.json b/meson/test cases/rust/14 external libm/test.json new file mode 100644 index 000000000..42d8fe95e --- /dev/null +++ b/meson/test cases/rust/14 external libm/test.json @@ -0,0 +1,10 @@ +{ + "matrix": { + "options": { + "static": [ + { "val": true }, + { "val": false } + ] + } + } +} diff --git a/meson/test cases/rust/15 polyglot sharedlib/adder.c b/meson/test cases/rust/15 polyglot sharedlib/adder.c new file mode 100644 index 000000000..66613edf5 --- /dev/null +++ b/meson/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/meson/test cases/rust/15 polyglot sharedlib/adder.h b/meson/test cases/rust/15 polyglot sharedlib/adder.h new file mode 100644 index 000000000..fb2105ed6 --- /dev/null +++ b/meson/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/meson/test cases/rust/15 polyglot sharedlib/adder.rs b/meson/test cases/rust/15 polyglot sharedlib/adder.rs new file mode 100644 index 000000000..909535035 --- /dev/null +++ b/meson/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/meson/test cases/rust/15 polyglot sharedlib/addertest.c b/meson/test cases/rust/15 polyglot sharedlib/addertest.c new file mode 100644 index 000000000..87e45b64f --- /dev/null +++ b/meson/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/meson/test cases/rust/15 polyglot sharedlib/meson.build b/meson/test cases/rust/15 polyglot sharedlib/meson.build new file mode 100644 index 000000000..13fc8fd47 --- /dev/null +++ b/meson/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)) diff --git a/meson/test cases/rust/16 internal c dependencies/lib.c b/meson/test cases/rust/16 internal c dependencies/lib.c new file mode 100644 index 000000000..e852de6c5 --- /dev/null +++ b/meson/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/meson/test cases/rust/16 internal c dependencies/lib.h b/meson/test cases/rust/16 internal c dependencies/lib.h new file mode 100644 index 000000000..847bd1664 --- /dev/null +++ b/meson/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/meson/test cases/rust/16 internal c dependencies/main.rs b/meson/test cases/rust/16 internal c dependencies/main.rs new file mode 100644 index 000000000..538359943 --- /dev/null +++ b/meson/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/meson/test cases/rust/16 internal c dependencies/meson.build b/meson/test cases/rust/16 internal c dependencies/meson.build new file mode 100644 index 000000000..c7476d750 --- /dev/null +++ b/meson/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/meson/test cases/rust/16 internal c dependencies/test.py b/meson/test cases/rust/16 internal c dependencies/test.py new file mode 100755 index 000000000..dacec120b --- /dev/null +++ b/meson/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() diff --git a/meson/test cases/rust/2 sharedlib/meson.build b/meson/test cases/rust/2 sharedlib/meson.build new file mode 100644 index 000000000..aac296810 --- /dev/null +++ b/meson/test cases/rust/2 sharedlib/meson.build @@ -0,0 +1,16 @@ +project('rust shared library', 'rust') + +if host_machine.system() == 'darwin' + error('MESON_SKIP_TEST: doesnt work right on macos, please fix!') +endif + +l = shared_library('stuff', 'stuff.rs', install : true) +e = executable('prog', 'prog.rs', link_with : l, install : true) + +if build_machine.system() == 'windows' + rustup = find_program('rustup') + test('linktest', rustup, + args: ['run', 'stable', e]) +else + test('linktest', e) +endif diff --git a/meson/test cases/rust/2 sharedlib/prog.rs b/meson/test cases/rust/2 sharedlib/prog.rs new file mode 100644 index 000000000..fbf318159 --- /dev/null +++ b/meson/test cases/rust/2 sharedlib/prog.rs @@ -0,0 +1,3 @@ +extern crate stuff; + +fn main() { println!("printing: {}", stuff::explore()); } diff --git a/meson/test cases/rust/2 sharedlib/stuff.rs b/meson/test cases/rust/2 sharedlib/stuff.rs new file mode 100644 index 000000000..8cabc6257 --- /dev/null +++ b/meson/test cases/rust/2 sharedlib/stuff.rs @@ -0,0 +1,3 @@ +#![crate_name = "stuff"] + +pub fn explore() -> &'static str { "librarystring" } diff --git a/meson/test cases/rust/2 sharedlib/test.json b/meson/test cases/rust/2 sharedlib/test.json new file mode 100644 index 000000000..585fdeb69 --- /dev/null +++ b/meson/test cases/rust/2 sharedlib/test.json @@ -0,0 +1,10 @@ +{ + "installed": [ + {"type": "exe", "file": "usr/bin/prog"}, + {"type": "pdb", "file": "usr/bin/prog"}, + {"type": "file", "platform": "gcc", "file": "usr/lib/libstuff.so"}, + {"type": "file", "platform": "msvc", "file": "usr/bin/stuff.dll"}, + {"type": "pdb", "file": "usr/bin/stuff"}, + {"type": "file", "platform": "msvc", "file": "usr/lib/stuff.dll.lib"} + ] +} diff --git a/meson/test cases/rust/3 staticlib/meson.build b/meson/test cases/rust/3 staticlib/meson.build new file mode 100644 index 000000000..676956463 --- /dev/null +++ b/meson/test cases/rust/3 staticlib/meson.build @@ -0,0 +1,5 @@ +project('rust static library', 'rust') + +l = static_library('stuff', 'stuff.rs', install : true) +e = executable('prog', 'prog.rs', link_with : l, install : true) +test('linktest', e) diff --git a/meson/test cases/rust/3 staticlib/prog.rs b/meson/test cases/rust/3 staticlib/prog.rs new file mode 100644 index 000000000..fbf318159 --- /dev/null +++ b/meson/test cases/rust/3 staticlib/prog.rs @@ -0,0 +1,3 @@ +extern crate stuff; + +fn main() { println!("printing: {}", stuff::explore()); } diff --git a/meson/test cases/rust/3 staticlib/stuff.rs b/meson/test cases/rust/3 staticlib/stuff.rs new file mode 100644 index 000000000..8cabc6257 --- /dev/null +++ b/meson/test cases/rust/3 staticlib/stuff.rs @@ -0,0 +1,3 @@ +#![crate_name = "stuff"] + +pub fn explore() -> &'static str { "librarystring" } diff --git a/meson/test cases/rust/3 staticlib/test.json b/meson/test cases/rust/3 staticlib/test.json new file mode 100644 index 000000000..ca292254e --- /dev/null +++ b/meson/test cases/rust/3 staticlib/test.json @@ -0,0 +1,7 @@ +{ + "installed": [ + {"type": "exe", "file": "usr/bin/prog"}, + {"type": "pdb", "file": "usr/bin/prog"}, + {"type": "file", "file": "usr/lib/libstuff.rlib"} + ] +} diff --git a/meson/test cases/rust/4 polyglot/meson.build b/meson/test cases/rust/4 polyglot/meson.build new file mode 100644 index 000000000..4e96679f6 --- /dev/null +++ b/meson/test cases/rust/4 polyglot/meson.build @@ -0,0 +1,9 @@ +project('rust and c polyglot executable', 'c', 'rust') + +if host_machine.system() == 'darwin' + error('MESON_SKIP_TEST: doesnt work right on macos, please fix!') +endif + +l = shared_library('stuff', 'stuff.rs', rust_crate_type: 'cdylib', install : true) +e = executable('prog', 'prog.c', link_with : l, install : true) +test('polyglottest', e) diff --git a/meson/test cases/rust/4 polyglot/prog.c b/meson/test cases/rust/4 polyglot/prog.c new file mode 100644 index 000000000..dbbd8800a --- /dev/null +++ b/meson/test cases/rust/4 polyglot/prog.c @@ -0,0 +1,8 @@ +#include <stdio.h> + +void f(); + +int main(void) { + printf("Hello from C!\n"); + f(); +} diff --git a/meson/test cases/rust/4 polyglot/stuff.rs b/meson/test cases/rust/4 polyglot/stuff.rs new file mode 100644 index 000000000..ecf623c64 --- /dev/null +++ b/meson/test cases/rust/4 polyglot/stuff.rs @@ -0,0 +1,6 @@ +#![crate_name = "stuff"] + +#[no_mangle] +pub extern fn f() { + println!("Hello from Rust!"); +} diff --git a/meson/test cases/rust/4 polyglot/test.json b/meson/test cases/rust/4 polyglot/test.json new file mode 100644 index 000000000..a8837a195 --- /dev/null +++ b/meson/test cases/rust/4 polyglot/test.json @@ -0,0 +1,10 @@ +{ + "installed": [ + {"type": "exe", "file": "usr/bin/prog"}, + {"type": "pdb", "file": "usr/bin/prog"}, + {"type": "file", "platform": "gcc", "file": "usr/lib/libstuff.so"}, + {"type": "file", "platform": "msvc", "file": "usr/bin/stuff.dll"}, + {"type": "file", "platform": "msvc", "file": "usr/lib/stuff.dll.lib"}, + {"type": "pdb", "file": "usr/bin/stuff"} + ] +} diff --git a/meson/test cases/rust/5 polyglot static/meson.build b/meson/test cases/rust/5 polyglot static/meson.build new file mode 100644 index 000000000..a6e9550b6 --- /dev/null +++ b/meson/test cases/rust/5 polyglot static/meson.build @@ -0,0 +1,17 @@ +project('static rust and c polyglot executable', 'c', 'rust') + +deps = [ + meson.get_compiler('c').find_library('dl', required: false), + meson.get_compiler('c').find_library('m', required: false), + dependency('threads'), +] + +extra_winlibs = meson.get_compiler('c').get_id() in ['msvc', 'clang-cl'] ? ['userenv.lib', 'ws2_32.lib'] : [] + +l = static_library('stuff', 'stuff.rs', rust_crate_type : 'staticlib', install : true) +e = executable('prog', 'prog.c', + dependencies: deps, + link_with : l, + link_args: extra_winlibs, + install : true) +test('polyglottest', e) diff --git a/meson/test cases/rust/5 polyglot static/prog.c b/meson/test cases/rust/5 polyglot static/prog.c new file mode 100644 index 000000000..dbbd8800a --- /dev/null +++ b/meson/test cases/rust/5 polyglot static/prog.c @@ -0,0 +1,8 @@ +#include <stdio.h> + +void f(); + +int main(void) { + printf("Hello from C!\n"); + f(); +} diff --git a/meson/test cases/rust/5 polyglot static/stuff.rs b/meson/test cases/rust/5 polyglot static/stuff.rs new file mode 100644 index 000000000..ecf623c64 --- /dev/null +++ b/meson/test cases/rust/5 polyglot static/stuff.rs @@ -0,0 +1,6 @@ +#![crate_name = "stuff"] + +#[no_mangle] +pub extern fn f() { + println!("Hello from Rust!"); +} diff --git a/meson/test cases/rust/5 polyglot static/test.json b/meson/test cases/rust/5 polyglot static/test.json new file mode 100644 index 000000000..1d4eff4be --- /dev/null +++ b/meson/test cases/rust/5 polyglot static/test.json @@ -0,0 +1,7 @@ +{ + "installed": [ + {"type": "exe", "file": "usr/bin/prog"}, + {"type": "pdb", "file": "usr/bin/prog"}, + {"type": "file", "file": "usr/lib/libstuff.a"} + ] +} diff --git a/meson/test cases/rust/6 named staticlib/meson.build b/meson/test cases/rust/6 named staticlib/meson.build new file mode 100644 index 000000000..f91b7a10a --- /dev/null +++ b/meson/test cases/rust/6 named staticlib/meson.build @@ -0,0 +1,5 @@ +project('rust static library', 'rust') + +l = static_library('named_stuff', 'stuff.rs', install : true) +e = executable('prog', 'prog.rs', link_with : l, install : true) +test('linktest', e) diff --git a/meson/test cases/rust/6 named staticlib/prog.rs b/meson/test cases/rust/6 named staticlib/prog.rs new file mode 100644 index 000000000..856c4b7d1 --- /dev/null +++ b/meson/test cases/rust/6 named staticlib/prog.rs @@ -0,0 +1,3 @@ +extern crate named_stuff; + +fn main() { println!("printing: {}", named_stuff::explore()); } diff --git a/meson/test cases/rust/6 named staticlib/stuff.rs b/meson/test cases/rust/6 named staticlib/stuff.rs new file mode 100644 index 000000000..9b36e57a1 --- /dev/null +++ b/meson/test cases/rust/6 named staticlib/stuff.rs @@ -0,0 +1 @@ +pub fn explore() -> &'static str { "librarystring" } diff --git a/meson/test cases/rust/6 named staticlib/test.json b/meson/test cases/rust/6 named staticlib/test.json new file mode 100644 index 000000000..3ac3e0b9c --- /dev/null +++ b/meson/test cases/rust/6 named staticlib/test.json @@ -0,0 +1,7 @@ +{ + "installed": [ + {"type": "exe", "file": "usr/bin/prog"}, + {"type": "pdb", "file": "usr/bin/prog"}, + {"type": "file", "file": "usr/lib/libnamed_stuff.rlib"} + ] +} diff --git a/meson/test cases/rust/7 private crate collision/meson.build b/meson/test cases/rust/7 private crate collision/meson.build new file mode 100644 index 000000000..81b6aabfa --- /dev/null +++ b/meson/test cases/rust/7 private crate collision/meson.build @@ -0,0 +1,5 @@ +project('rust private crate collision', 'rust') + +l = static_library('rand', 'rand.rs', install : true) +e = executable('prog', 'prog.rs', link_with : l, install : true) +test('linktest', e) diff --git a/meson/test cases/rust/7 private crate collision/prog.rs b/meson/test cases/rust/7 private crate collision/prog.rs new file mode 100644 index 000000000..b9a30f180 --- /dev/null +++ b/meson/test cases/rust/7 private crate collision/prog.rs @@ -0,0 +1,3 @@ +extern crate rand; + +fn main() { println!("printing: {}", rand::explore()); } diff --git a/meson/test cases/rust/7 private crate collision/rand.rs b/meson/test cases/rust/7 private crate collision/rand.rs new file mode 100644 index 000000000..8a3d427b2 --- /dev/null +++ b/meson/test cases/rust/7 private crate collision/rand.rs @@ -0,0 +1,4 @@ +// use a name that collides with one of the rustc_private libraries +#![crate_name = "rand"] + +pub fn explore() -> &'static str { "librarystring" } diff --git a/meson/test cases/rust/7 private crate collision/test.json b/meson/test cases/rust/7 private crate collision/test.json new file mode 100644 index 000000000..e35f6ff73 --- /dev/null +++ b/meson/test cases/rust/7 private crate collision/test.json @@ -0,0 +1,7 @@ +{ + "installed": [ + {"type": "exe", "file": "usr/bin/prog"}, + {"type": "pdb", "file": "usr/bin/prog"}, + {"type": "file", "file": "usr/lib/librand.rlib"} + ] +} diff --git a/meson/test cases/rust/8 many files/foo.rs b/meson/test cases/rust/8 many files/foo.rs new file mode 100644 index 000000000..0899dc01b --- /dev/null +++ b/meson/test cases/rust/8 many files/foo.rs @@ -0,0 +1,3 @@ +pub fn bar() -> () { + println!("Hello, world!"); +} diff --git a/meson/test cases/rust/8 many files/main.rs b/meson/test cases/rust/8 many files/main.rs new file mode 100644 index 000000000..3ffeee2be --- /dev/null +++ b/meson/test cases/rust/8 many files/main.rs @@ -0,0 +1,5 @@ +mod foo; + +fn main() { + foo::bar(); +} diff --git a/meson/test cases/rust/8 many files/meson.build b/meson/test cases/rust/8 many files/meson.build new file mode 100644 index 000000000..1d906b372 --- /dev/null +++ b/meson/test cases/rust/8 many files/meson.build @@ -0,0 +1,3 @@ +project('manyfiles', 'rust') + +test('manyfiles', executable('manyfiles', 'main.rs', 'foo.rs')) diff --git a/meson/test cases/rust/9 unit tests/meson.build b/meson/test cases/rust/9 unit tests/meson.build new file mode 100644 index 000000000..b649abb85 --- /dev/null +++ b/meson/test cases/rust/9 unit tests/meson.build @@ -0,0 +1,43 @@ +project('rust unit tests', 'rust') + +t = executable( + 'rust_test', + ['test.rs'], + rust_args : ['--test'], +) + +test( + 'rust test (should fail)', + t, + protocol : 'rust', + suite : ['foo'], + should_fail : true, +) + +test( + 'rust test (should pass)', + t, + args : ['--skip', 'test_add_intentional_fail'], + protocol : 'rust', + suite : ['foo'], +) + + +test( + 'rust test (should skip)', + t, + args : ['--skip', 'test_add'], + protocol : 'rust', + suite : ['foo'], +) + +exe = executable('rust_exe', ['test2.rs', 'test.rs']) + +rust = import('unstable-rust') +rust.test('rust_test_from_exe', exe, should_fail : true) + +lib = static_library('rust_static', ['test.rs']) +rust.test('rust_test_from_static', lib, args: ['--skip', 'test_add_intentional_fail']) + +lib = shared_library('rust_shared', ['test.rs']) +rust.test('rust_test_from_shared', lib, args: ['--skip', 'test_add_intentional_fail']) diff --git a/meson/test cases/rust/9 unit tests/test.rs b/meson/test cases/rust/9 unit tests/test.rs new file mode 100644 index 000000000..0225be55b --- /dev/null +++ b/meson/test cases/rust/9 unit tests/test.rs @@ -0,0 +1,24 @@ +pub fn add(a: i32, b: i32) -> i32 { + return a + b; +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_add() { + assert_eq!(add(1, 2), 3); + } + + #[test] + fn test_add_intentional_fail() { + assert_eq!(add(1, 2), 5); + } + + #[test] + #[ignore] + fn test_add_intentional_fail2() { + assert_eq!(add(1, 7), 5); + } +} diff --git a/meson/test cases/rust/9 unit tests/test2.rs b/meson/test cases/rust/9 unit tests/test2.rs new file mode 100644 index 000000000..9623c7c24 --- /dev/null +++ b/meson/test cases/rust/9 unit tests/test2.rs @@ -0,0 +1,11 @@ +mod test; +use std::env; + +fn main() { + let args: Vec<String> = env::args().collect(); + let first = args[1].parse::<i32>().expect("Invliad value for first argument."); + let second = args[2].parse::<i32>().expect("Invliad value for second argument."); + + let new = test::add(first, second); + println!("New value: {}", new); +} |