Skip to content

Commit

Permalink
Add basic test for installing libraries.
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwe committed Aug 11, 2024
1 parent e9bf4cd commit a94c5e6
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 0 deletions.
16 changes: 16 additions & 0 deletions test/corrosion_install/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,19 @@ if(NOT (CMAKE_CROSSCOMPILING AND MSVC))
"Hello World! I'm generated code"
)
endif()

# Todo: Fix and re-enable tests on Windows
if(NOT CMAKE_CROSSCOMPILING AND NOT WIN32)
corrosion_tests_add_test(install_lib "main-static;main-shared")

set_tests_properties("install_lib_run_main-static" "install_lib_run_main-shared"
PROPERTIES PASS_REGULAR_EXPRESSION
"The sum is 11"
)
endif()

# Further tests we should add:
# - Test installing a Rust executable, that requires a (C/C++) shared library at runtime.
# Note: We should delete the build directory of the subproject
# before running the installed rust executable, to insure the shared library is loaded from the
# installed location and not from the build dir.
44 changes: 44 additions & 0 deletions test/corrosion_install/install_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
cmake_minimum_required(VERSION 3.15)
project(test_project VERSION 0.1.0)
include(ExternalProject)

add_library(static_lib STATIC IMPORTED)
add_library(shared_lib SHARED IMPORTED)
set(install_prefix "${CMAKE_CURRENT_BINARY_DIR}/rust_lib")
set(static_lib_install_path "${install_prefix}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}rust_lib${CMAKE_STATIC_LIBRARY_SUFFIX}")
set(shared_lib_install_path "${install_prefix}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}rust_lib${CMAKE_SHARED_LIBRARY_SUFFIX}")


set_target_properties(static_lib PROPERTIES
IMPORTED_LOCATION
"${static_lib_install_path}")

set_target_properties(shared_lib PROPERTIES
IMPORTED_LOCATION
"${shared_lib_install_path}")

add_executable(main-static main.cpp)
target_link_libraries(main-static PRIVATE static_lib)

ExternalProject_Add(
rust_lib
PREFIX "${CMAKE_CURRENT_BINARY_DIR}/rust_lib"
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/rust_lib"
CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${install_prefix}"
# INSTALL_BYPRODUCTS "${static_lib_install_path}"
)

# Dummy target since INSTALL_BYPRODUCTS requires CMake 3.26
add_custom_target(build_rust_project_dummy
COMMAND echo dummy
BYPRODUCTS "${static_lib_install_path}" "${shared_lib_install_path}"
DEPENDS rust_lib)

add_dependencies(main-static build_rust_project_dummy)

set(CMAKE_BUILD_RPATH ${install_prefix}/lib)
add_executable(main-shared main.cpp)
target_link_libraries(main-shared
PUBLIC shared_lib)

add_dependencies(main-shared build_rust_project_dummy)
11 changes: 11 additions & 0 deletions test/corrosion_install/install_lib/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdint.h>
#include <assert.h>
#include <iostream>

extern "C" uint64_t add(uint64_t a, uint64_t b);

int main(int argc, char **argv) {
uint64_t sum = add(5, 6);
assert(sum == 11);
std::cout << "The sum is " << sum << std::endl;
}
17 changes: 17 additions & 0 deletions test/corrosion_install/install_lib/rust_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.15)
project(test_project VERSION 0.1.0)
include(../../../test_header.cmake)

corrosion_import_crate(MANIFEST_PATH 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_install(TARGETS rust_lib)
7 changes: 7 additions & 0 deletions test/corrosion_install/install_lib/rust_lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/corrosion_install/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/corrosion_install/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 a94c5e6

Please sign in to comment.