summaryrefslogtreecommitdiff
path: root/test/c-from-rust
diff options
context:
space:
mode:
authorAlberto Sartori <alberto.sartori@huawei.com>2024-06-24 15:49:36 +0200
committerKlaus Aehlig <klaus.aehlig@huawei.com>2024-06-24 15:49:36 +0200
commitf87ad41f72ca4465a0c5b4ba9fd36a7b09e4d4f4 (patch)
tree24d4a3a5975df2046714c6bc114d5d050bdc61fc /test/c-from-rust
downloadrules-rust-f87ad41f72ca4465a0c5b4ba9fd36a7b09e4d4f4.tar.gz
Initial commit
Co-authored-by: Klaus Aehlig <klaus.aehlig@huawei.com>
Diffstat (limited to 'test/c-from-rust')
-rw-r--r--test/c-from-rust/TARGETS13
-rw-r--r--test/c-from-rust/check_ints.sh5
-rw-r--r--test/c-from-rust/clib/TARGETS19
-rw-r--r--test/c-from-rust/clib/bar.c5
-rw-r--r--test/c-from-rust/clib/bar.h3
-rw-r--r--test/c-from-rust/clib/foo.c5
-rw-r--r--test/c-from-rust/clib/foo.h3
-rw-r--r--test/c-from-rust/main.rs20
8 files changed, 73 insertions, 0 deletions
diff --git a/test/c-from-rust/TARGETS b/test/c-from-rust/TARGETS
new file mode 100644
index 0000000..7da2055
--- /dev/null
+++ b/test/c-from-rust/TARGETS
@@ -0,0 +1,13 @@
+{ "main":
+ { "type": ["rules/rust", "binary"]
+ , "name": ["main"]
+ , "crate_root": ["main.rs"]
+ , "deps": [["./", "clib", "foo"]]
+ }
+, "c-from-rust":
+ { "type": ["@", "rules-cc", "shell/test", "script"]
+ , "name": ["c-from-rust"]
+ , "test": ["check_ints.sh"]
+ , "deps": ["main"]
+ }
+}
diff --git a/test/c-from-rust/check_ints.sh b/test/c-from-rust/check_ints.sh
new file mode 100644
index 0000000..00780c3
--- /dev/null
+++ b/test/c-from-rust/check_ints.sh
@@ -0,0 +1,5 @@
+set -e
+
+for i in `seq 1 42`; do
+ ./main -$i | grep " -$i is $i"
+done
diff --git a/test/c-from-rust/clib/TARGETS b/test/c-from-rust/clib/TARGETS
new file mode 100644
index 0000000..c72e90b
--- /dev/null
+++ b/test/c-from-rust/clib/TARGETS
@@ -0,0 +1,19 @@
+{ "foo":
+ { "type": ["@", "rules-cc", "CC", "library"]
+ , "pure C": ["true"]
+ , "name": ["foo"]
+ , "srcs": ["foo.c"]
+ , "hdrs": ["foo.h"]
+ , "stage": ["foo"]
+ , "deps": ["bar"]
+ }
+, "bar":
+ { "type": ["@", "rules-cc", "CC", "library"]
+ , "pure C": ["true"]
+ , "name": ["bar"]
+ , "srcs": ["bar.c"]
+ , "hdrs": ["bar.h"]
+ , "ldflags": ["-lm"]
+ , "stage": ["bar"]
+ }
+}
diff --git a/test/c-from-rust/clib/bar.c b/test/c-from-rust/clib/bar.c
new file mode 100644
index 0000000..5ad792c
--- /dev/null
+++ b/test/c-from-rust/clib/bar.c
@@ -0,0 +1,5 @@
+#include <math.h>
+
+int bar(int x){
+ return sqrt(x);
+}
diff --git a/test/c-from-rust/clib/bar.h b/test/c-from-rust/clib/bar.h
new file mode 100644
index 0000000..1c88e77
--- /dev/null
+++ b/test/c-from-rust/clib/bar.h
@@ -0,0 +1,3 @@
+#pragma once
+
+int bar(int);
diff --git a/test/c-from-rust/clib/foo.c b/test/c-from-rust/clib/foo.c
new file mode 100644
index 0000000..5306a9c
--- /dev/null
+++ b/test/c-from-rust/clib/foo.c
@@ -0,0 +1,5 @@
+#include "bar/bar.h"
+
+int c_func(int x){
+ return bar(x*x);
+}
diff --git a/test/c-from-rust/clib/foo.h b/test/c-from-rust/clib/foo.h
new file mode 100644
index 0000000..6ce2745
--- /dev/null
+++ b/test/c-from-rust/clib/foo.h
@@ -0,0 +1,3 @@
+#pragma once
+int c_func(int);
+
diff --git a/test/c-from-rust/main.rs b/test/c-from-rust/main.rs
new file mode 100644
index 0000000..961150a
--- /dev/null
+++ b/test/c-from-rust/main.rs
@@ -0,0 +1,20 @@
+use std::env;
+
+extern "C" {
+ fn c_func(input: i32) -> i32;
+}
+
+fn c_call(i:i32) -> i32{
+ unsafe {
+ return c_func(i);
+ }
+}
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+ match args[1].parse::<i32>() {
+ Ok(i) => println!("Absolute value of {} is {}",i, c_call(i)),
+ Err(..) => println!("Wrong argument {}",args[1]),
+ };
+
+}