From 1f8a5265f637d6fd1554c7ca7d6fe8261d21c1de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Thu, 15 Aug 2019 16:52:52 +0200 Subject: [PATCH] Add CMake example --- rust-to-cmake/Cargo.toml | 11 +++++++++++ rust-to-cmake/build.rs | 11 +++++++++++ rust-to-cmake/libdouble/CMakeLists.txt | 7 +++++++ rust-to-cmake/libdouble/double.c | 3 +++ rust-to-cmake/src/main.rs | 11 +++++++++++ 5 files changed, 43 insertions(+) create mode 100644 rust-to-cmake/Cargo.toml create mode 100644 rust-to-cmake/build.rs create mode 100644 rust-to-cmake/libdouble/CMakeLists.txt create mode 100644 rust-to-cmake/libdouble/double.c create mode 100644 rust-to-cmake/src/main.rs diff --git a/rust-to-cmake/Cargo.toml b/rust-to-cmake/Cargo.toml new file mode 100644 index 0000000..d1929a9 --- /dev/null +++ b/rust-to-cmake/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "rust-to-cmake" +version = "0.1.0" +authors = ["Alex Crichton "] +build = "build.rs" + +[dependencies] +libc = "0.2" + +[build-dependencies] +cmake = "0.1.41" diff --git a/rust-to-cmake/build.rs b/rust-to-cmake/build.rs new file mode 100644 index 0000000..8b1da15 --- /dev/null +++ b/rust-to-cmake/build.rs @@ -0,0 +1,11 @@ +extern crate cmake; + +fn main() { + // Builds the project in the directory located in `libfoo`, installing it + // into $OUT_DIR + let dst = cmake::build("libdouble"); + + println!("cargo:rustc-link-search=native={}", dst.display()); + println!("cargo:rustc-link-lib=static=double"); +} + diff --git a/rust-to-cmake/libdouble/CMakeLists.txt b/rust-to-cmake/libdouble/CMakeLists.txt new file mode 100644 index 0000000..20c98a8 --- /dev/null +++ b/rust-to-cmake/libdouble/CMakeLists.txt @@ -0,0 +1,7 @@ +add_library ( + double + STATIC + double.c +) + +install (TARGETS double DESTINATION .) \ No newline at end of file diff --git a/rust-to-cmake/libdouble/double.c b/rust-to-cmake/libdouble/double.c new file mode 100644 index 0000000..9fd3c60 --- /dev/null +++ b/rust-to-cmake/libdouble/double.c @@ -0,0 +1,3 @@ +int double_input(int input) { + return input * 2; +} diff --git a/rust-to-cmake/src/main.rs b/rust-to-cmake/src/main.rs new file mode 100644 index 0000000..2104230 --- /dev/null +++ b/rust-to-cmake/src/main.rs @@ -0,0 +1,11 @@ +extern crate libc; + +extern { + fn double_input(input: libc::c_int) -> libc::c_int; +} + +fn main() { + let input = 4; + let output = unsafe { double_input(input) }; + println!("{} * 2 = {}", input, output); +}