Skip to content

Commit cb26ef2

Browse files
committed
Add a Rust project skeleton that uses an exposed version string from the API
1 parent d0ab60c commit cb26ef2

File tree

9 files changed

+265
-2
lines changed

9 files changed

+265
-2
lines changed

CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.8)
1+
cmake_minimum_required(VERSION 3.15)
22

33
# Compile with /usr/bin/clang on MacOS
44
# See https://github.com/diffblue/cbmc/issues/4956
@@ -252,4 +252,14 @@ if(WITH_JBMC)
252252
add_subdirectory(jbmc)
253253
endif()
254254

255+
option(WITH_RUST_API "Build with the CPROVER Rust API" ON)
256+
if(WITH_RUST_API)
257+
find_package(Corrosion REQUIRED)
258+
corrosion_import_crate(MANIFEST_PATH src/libcprover-rust/Cargo.toml)
259+
target_link_libraries(cprover-api-cpp cprover-api-rust)
260+
install(TARGETS cprover-api-rust RUNTIME DESTINATION lib)
261+
# TODO: cargo builds the artifact with underscores - we need to change to hyphens
262+
# to bring to same naming convention as the rest of CBMC
263+
endif()
264+
255265
include(cmake/packaging.cmake)

regression/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ add_subdirectory(goto-interpreter)
8585
add_subdirectory(cbmc-sequentialization)
8686
add_subdirectory(cpp-linter)
8787
add_subdirectory(catch-framework)
88-
add_subdirectory(libcprover-cpp)
88+
# add_subdirectory(libcprover-cpp)
8989

9090
if(WITH_MEMORY_ANALYZER)
9191
add_subdirectory(snapshot-harness)

src/libcprover-cpp/api.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,15 @@ void api_sessiont::load_model_from_files(const std::vector<std::string> &files)
5252
model = util_make_unique<goto_modelt>(
5353
initialize_goto_model(files, *message_handler, *options));
5454
}
55+
56+
/////////////////////////////
57+
58+
std::unique_ptr<api_sessiont> new_api_session()
59+
{
60+
return std::unique_ptr<api_sessiont>(new api_sessiont());
61+
}
62+
63+
std::unique_ptr<std::string> get_api_version(std::unique_ptr<api_sessiont> api_handle)
64+
{
65+
return util_make_unique<std::string>(api_handle->get_api_version());
66+
}

src/libcprover-cpp/api.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,18 @@ struct api_sessiont
2727
/// \param files: A vector<string> containing the filenames to be loaded
2828
void load_model_from_files(const std::vector<std::string> &files);
2929

30+
// A simple API version information function.
31+
// TODO: Investigate best way to store and expose version string.
32+
std::string get_api_version() const { return std::string{"0.1"}; }
33+
3034
private:
3135
std::unique_ptr<goto_modelt> model;
3236
std::unique_ptr<message_handlert> message_handler;
3337
std::unique_ptr<optionst> options;
3438
};
3539

40+
// TODO: Investigate best way to define this
41+
std::unique_ptr<api_sessiont> new_api_session();
42+
std::unique_ptr<std::string> get_api_version(std::unique_ptr<api_sessiont> api_handle);
43+
3644
#endif

src/libcprover-rust/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore build artefacts folder
2+
target/

src/libcprover-rust/Cargo.lock

Lines changed: 173 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/libcprover-rust/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "cprover-api-rust"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
cxx = "1.0"
10+
11+
[build-dependencies]
12+
cxx-build = "1.0"
13+
14+
[lib]
15+
crate-type = ["staticlib"]

src/libcprover-rust/build.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::path::Path;
2+
3+
fn main() {
4+
let cpp_api_path = Path::new("../libcprover-cpp");
5+
println!("{}", cpp_api_path.display());
6+
let _build = cxx_build::bridge("src/lib.rs")
7+
.include(cpp_api_path)
8+
.flag_if_supported("-std=c++11")
9+
.compile("cprover-rust-api");
10+
11+
println!("cargo:rustc-link-search=native=/Users/fotiskoutoulakis/Devel/cbmc/build/lib/");
12+
println!("cargo:rustc-link-lib=static=goto-programs");
13+
println!("cargo:rustc-link-lib=static=util");
14+
println!("cargo:rustc-link-lib=static=langapi");
15+
println!("cargo:rustc-link-lib=static=ansi-c");
16+
println!("cargo:rustc-link-lib=static=big-int");
17+
println!("cargo:rustc-link-lib=static=linking");
18+
println!("cargo:rustc-link-lib=static=cprover-api-cpp");
19+
}

src/libcprover-rust/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#[cxx::bridge]
2+
pub mod ffi {
3+
unsafe extern "C++" {
4+
include!("api.h");
5+
type api_sessiont;
6+
7+
fn new_api_session() -> UniquePtr<api_sessiont>;
8+
fn get_api_version(api_handle: UniquePtr<api_sessiont>) -> UniquePtr<CxxString>;
9+
}
10+
}
11+
12+
// To test run "cargo test -- --nocapture"
13+
#[cfg(test)]
14+
mod tests {
15+
use super::*;
16+
17+
#[test]
18+
fn it_works() {
19+
let client = ffi::new_api_session();
20+
let result = ffi::get_api_version(client);
21+
println!("{}", *result);
22+
// assert_eq!(*result, "0.1");
23+
}
24+
}

0 commit comments

Comments
 (0)