Skip to content

Commit

Permalink
Add test for installing an executable
Browse files Browse the repository at this point in the history
Tests installing an executable to the default location
  • Loading branch information
jschwe committed Aug 10, 2024
1 parent 2efaede commit 3a18c4c
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
**/target/
**/*.rs.bk
build*/
install*/
.vscode
.idea
cmake-build-*
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ if(Rust_VERSION VERSION_GREATER_EQUAL "1.64.0")
endif()
add_subdirectory(custom_profiles)
add_subdirectory(cbindgen)
add_subdirectory(corrosion_install)
add_subdirectory(cxxbridge)
add_subdirectory(envvar)
add_subdirectory(features)
Expand Down
11 changes: 11 additions & 0 deletions test/corrosion_install/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
if(NOT (CMAKE_CROSSCOMPILING AND MSVC))
# When using MSVC the cmake build via ExternalProject seems to inherit the target architecture,
# which breaks the test. Since we practically don't care about this, and we just want to ensure
# that installing an executable works, skipping this test when cross-compiling is fine.
corrosion_tests_add_test(install_rust_bin "generated_from_installed_bin")

set_tests_properties("install_rust_bin_run_generated_from_installed_bin"
PROPERTIES PASS_REGULAR_EXPRESSION
"Hello World! I'm generated code"
)
endif()
32 changes: 32 additions & 0 deletions test/corrosion_install/install_rust_bin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 3.22)
project(test_project VERSION 0.1.0)

# Note: Corrosion supports `hostbuild`, so building a Rust binary in a subproject
# like this doesn't offer any benefit over using the hostbuild option.
# However, this is a reasonable way to test that installing Rust binaries via
# corrosion_install works as expected.
include(ExternalProject)

set(bin_suffix "")
if(CMAKE_HOST_WIN32)
set(bin_suffix ".exe")
endif()
set(generator_bin_path "${CMAKE_CURRENT_BINARY_DIR}/rust_bin/bin/my_rust_bin${bin_suffix}")

ExternalProject_Add(
rust_bin
PREFIX "${CMAKE_CURRENT_BINARY_DIR}/rust_bin"
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/rust_bin"
CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/rust_bin"
)

# This custom command is the main part of the test:
# We test that corrosion (in the CMake of the ExternalProject) properly installed
# a Rust executable to the location we specified by running the executable, which generates some cpp code.
add_custom_command(
OUTPUT generated_main.cpp
COMMAND "${generator_bin_path}" > "${CMAKE_CURRENT_BINARY_DIR}/generated_main.cpp"
DEPENDS rust_bin
)

add_executable(generated_from_installed_bin ${CMAKE_CURRENT_BINARY_DIR}/generated_main.cpp)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.22)
project(test_rust_bin VERSION 0.1.0)
include(../../../test_header.cmake)
include(GNUInstallDirs)

corrosion_import_crate(MANIFEST_PATH Cargo.toml)
corrosion_install(TARGETS my_rust_bin)
7 changes: 7 additions & 0 deletions test/corrosion_install/install_rust_bin/rust_bin/Cargo.lock

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

7 changes: 7 additions & 0 deletions test/corrosion_install/install_rust_bin/rust_bin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "my_rust_bin"
version = "0.1.0"
edition = "2018"
license = "MIT"

[dependencies]
9 changes: 9 additions & 0 deletions test/corrosion_install/install_rust_bin/rust_bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn main() {
println!(
"#include <iostream>
int main() {{
std::cout << \"Hello World! I'm generated code\";
return 0;
}}"
);
}

0 comments on commit 3a18c4c

Please sign in to comment.