Skip to content

Commit

Permalink
tests: Add test for installing cbindgen header
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwe committed Jan 12, 2025
1 parent 4833136 commit 0c1c611
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
41 changes: 40 additions & 1 deletion test/cbindgen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
corrosion_tests_add_test(cbindgen_rust2cpp_auto "cpp-exe" TEST_SRC_DIR auto)
corrosion_tests_add_test(cbindgen_manual "cpp-exe" TEST_SRC_DIR manual)


set_tests_properties(cbindgen_rust2cpp_auto_run_cpp-exe cbindgen_manual_run_cpp-exe
PROPERTIES PASS_REGULAR_EXPRESSION
"^add_point Result: Point { x: 100, y: 100 }\r?\n$"
)

add_test(NAME "cbindgen_install_configure"
COMMAND
${CMAKE_COMMAND}
-S "${CMAKE_CURRENT_SOURCE_DIR}/install_lib"
-B "${CMAKE_CURRENT_BINARY_DIR}/build_install_lib"
"-G${CMAKE_GENERATOR}"
--install-prefix "${CMAKE_CURRENT_BINARY_DIR}/build_install_lib/test_install_lib_install_dir"

COMMAND_EXPAND_LISTS
)

add_test(NAME "cbindgen_install"
COMMAND
${CMAKE_COMMAND}
--build "${CMAKE_CURRENT_BINARY_DIR}/build_install_lib"
--target install
--config Debug
)

add_test(NAME cbindgen_install_check_header_installed
COMMAND
"${CMAKE_COMMAND}"
-P "${CMAKE_CURRENT_SOURCE_DIR}/../TestFileExists.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/build_install_lib/test_install_lib_install_dir/include/rust-lib.h"
)

add_test(NAME cbindgen_install_clean
COMMAND
"${CMAKE_COMMAND}"
-E remove_directory "${CMAKE_CURRENT_BINARY_DIR}/build_install_lib/"
)
set_tests_properties("cbindgen_install_configure" PROPERTIES FIXTURES_SETUP "configure_fixture_cbindgen_install")
set_tests_properties("cbindgen_install" PROPERTIES FIXTURES_REQUIRED "configure_fixture_cbindgen_install")

set_tests_properties("cbindgen_install" PROPERTIES FIXTURES_SETUP "install_fixture_cbindgen_install")
set_tests_properties("cbindgen_install_check_header_installed" PROPERTIES FIXTURES_REQUIRED "install_fixture_cbindgen_install")
set_tests_properties("cbindgen_install_check_header_installed" PROPERTIES FIXTURES_SETUP "fx_check_header_installed")
set_tests_properties(cbindgen_install_clean PROPERTIES FIXTURES_CLEANUP "configure_fixture_cbindgen_install;install_fixture_cbindgen_install;fx_check_header_installed")

# Todo: We also should add a cpp2rust test with the following setup:
# - A rust lib that is used by a rust executable
# - cbindgen creates bindings for the rust-lib
Expand Down
20 changes: 20 additions & 0 deletions test/cbindgen/install_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.15)
project(test_project VERSION 0.1.0)

set(CORROSION_TOOLS_RUST_TOOLCHAIN "stable")
include(../../test_header.cmake)

corrosion_import_crate(MANIFEST_PATH rust_lib/Cargo.toml)

if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
corrosion_add_target_local_rustflags(rust_lib "-Clink-arg=-Wl,-soname,librust_lib.so")
set_target_properties(rust_lib-shared PROPERTIES IMPORTED_SONAME librust_lib.so)
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
corrosion_add_target_local_rustflags(rust_lib -Clink-arg=-Wl,-install_name,@rpath/librust_lib.dylib,-current_version,1.0,-compatibility_version,1.0)
set_target_properties(rust_lib-shared PROPERTIES IMPORTED_NO_SONAME 0)
set_target_properties(rust_lib-shared PROPERTIES IMPORTED_SONAME librust_lib.dylib)
endif()

corrosion_experimental_cbindgen(TARGET rust_lib HEADER_NAME "rust-lib.h")

corrosion_install(TARGETS rust_lib LIBRARY PUBLIC_HEADER)
9 changes: 9 additions & 0 deletions test/cbindgen/install_lib/rust_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "rust_lib"
version = "0.1.0"
edition = "2018"

[dependencies]

[lib]
crate-type = ["staticlib", "cdylib"]
5 changes: 5 additions & 0 deletions test/cbindgen/install_lib/rust_lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

#[no_mangle]
pub extern "C" fn add(left: u64, right: u64) -> u64 {
left + right
}

0 comments on commit 0c1c611

Please sign in to comment.