From 3a18c4cc551601595d33f3b4392c9eba08b12002 Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Sat, 10 Aug 2024 13:12:16 +0800 Subject: [PATCH] Add test for installing an executable Tests installing an executable to the default location --- .gitignore | 1 - test/CMakeLists.txt | 1 + test/corrosion_install/CMakeLists.txt | 11 +++++++ .../install_rust_bin/CMakeLists.txt | 32 +++++++++++++++++++ .../install_rust_bin/rust_bin/CMakeLists.txt | 7 ++++ .../install_rust_bin/rust_bin/Cargo.lock | 7 ++++ .../install_rust_bin/rust_bin/Cargo.toml | 7 ++++ .../install_rust_bin/rust_bin/src/main.rs | 9 ++++++ 8 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 test/corrosion_install/CMakeLists.txt create mode 100644 test/corrosion_install/install_rust_bin/CMakeLists.txt create mode 100644 test/corrosion_install/install_rust_bin/rust_bin/CMakeLists.txt create mode 100644 test/corrosion_install/install_rust_bin/rust_bin/Cargo.lock create mode 100644 test/corrosion_install/install_rust_bin/rust_bin/Cargo.toml create mode 100644 test/corrosion_install/install_rust_bin/rust_bin/src/main.rs diff --git a/.gitignore b/.gitignore index c7125b0a..3a618f37 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ **/target/ **/*.rs.bk build*/ -install*/ .vscode .idea cmake-build-* diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b6a525c2..17e9059c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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) diff --git a/test/corrosion_install/CMakeLists.txt b/test/corrosion_install/CMakeLists.txt new file mode 100644 index 00000000..0ebe4589 --- /dev/null +++ b/test/corrosion_install/CMakeLists.txt @@ -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() diff --git a/test/corrosion_install/install_rust_bin/CMakeLists.txt b/test/corrosion_install/install_rust_bin/CMakeLists.txt new file mode 100644 index 00000000..275a00fd --- /dev/null +++ b/test/corrosion_install/install_rust_bin/CMakeLists.txt @@ -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) diff --git a/test/corrosion_install/install_rust_bin/rust_bin/CMakeLists.txt b/test/corrosion_install/install_rust_bin/rust_bin/CMakeLists.txt new file mode 100644 index 00000000..4061560f --- /dev/null +++ b/test/corrosion_install/install_rust_bin/rust_bin/CMakeLists.txt @@ -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) diff --git a/test/corrosion_install/install_rust_bin/rust_bin/Cargo.lock b/test/corrosion_install/install_rust_bin/rust_bin/Cargo.lock new file mode 100644 index 00000000..492a67d8 --- /dev/null +++ b/test/corrosion_install/install_rust_bin/rust_bin/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "my_rust_bin" +version = "0.1.0" diff --git a/test/corrosion_install/install_rust_bin/rust_bin/Cargo.toml b/test/corrosion_install/install_rust_bin/rust_bin/Cargo.toml new file mode 100644 index 00000000..5442fb26 --- /dev/null +++ b/test/corrosion_install/install_rust_bin/rust_bin/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "my_rust_bin" +version = "0.1.0" +edition = "2018" +license = "MIT" + +[dependencies] diff --git a/test/corrosion_install/install_rust_bin/rust_bin/src/main.rs b/test/corrosion_install/install_rust_bin/rust_bin/src/main.rs new file mode 100644 index 00000000..08ce8be6 --- /dev/null +++ b/test/corrosion_install/install_rust_bin/rust_bin/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + println!( +"#include +int main() {{ + std::cout << \"Hello World! I'm generated code\"; + return 0; +}}" + ); +}