Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cbindgen: ensure header is built with target #604

Merged
merged 3 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2119,6 +2119,9 @@ function(corrosion_experimental_cbindgen)
)
add_dependencies("_corrosion_cbindgen_${cbindgen_bindings_target}_bindings" "_corrosion_cbindgen_${cbindgen_bindings_target}_bindings_${header_identifier}")
add_dependencies(${cbindgen_bindings_target} "_corrosion_cbindgen_${cbindgen_bindings_target}_bindings")
if(TARGET "${CCN_TARGET}")
add_dependencies(cargo-build_${CCN_TARGET} "_corrosion_cbindgen_${cbindgen_bindings_target}_bindings")
endif()
endfunction()

# Parse the version of a Rust package from it's package manifest (Cargo.toml)
Expand Down
16 changes: 15 additions & 1 deletion test/TestFileExists.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,19 @@ endif()
set(FILE_PATH "${CMAKE_ARGV3}")

if(NOT ( EXISTS "${FILE_PATH}" ))
message(FATAL_ERROR "Test failed: File `${FILE_PATH}` does not exist.")
set(error_details "File `${FILE_PATH}` does not exist!\n")
set(PARENT_TREE "${FILE_PATH}")
cmake_path(HAS_PARENT_PATH PARENT_TREE has_parent)
while(has_parent)
cmake_path(GET PARENT_TREE PARENT_PATH PARENT_TREE)
cmake_path(HAS_PARENT_PATH PARENT_TREE has_parent)
if(EXISTS "${PARENT_TREE}")
file(GLOB dir_contents LIST_DIRECTORIES true "${PARENT_TREE}/*")
list(APPEND error_details "Found Parent directory `${PARENT_TREE}` exists and contains:\n" ${dir_contents})
break()
else()
list(APPEND error_details "Parent directory `${PARENT_TREE}` also does not exist!")
endif()
endwhile()
message(FATAL_ERROR "Test failed: ${error_details}")
endif()
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
}
Loading