From 5b0a78661f41b6d3b706ce1efb7f430d9258de52 Mon Sep 17 00:00:00 2001 From: Magnus Larsen Date: Thu, 8 Feb 2024 08:46:29 -0800 Subject: [PATCH] Copy pdb to runtime/library output dir as fallback (#486) This PR adds fallback logic that triggers when PDB_OUTPUT_DIRECTORY is not set on a corrosion target-- if the target is an executable, it uses the RUNTIME_OUTPUT_DIRECTORY instead (and LIBRARY_OUTPUT for shared libraries). This matches CMake behavior for C/C++ targets. --------- Co-authored-by: Jonathan Schwender --- cmake/Corrosion.cmake | 25 +++++++++++++------ cmake/CorrosionGenerator.cmake | 4 +-- test/cbindgen/rust2cpp/rust/Cargo.lock | 7 ------ test/output directory/CMakeLists.txt | 9 ++++++- .../output directory/CMakeLists.txt | 14 +++++++++++ .../output directory/proj3/Cargo.lock | 7 ++++++ .../output directory/proj3/Cargo.toml | 11 ++++++++ .../proj3/src/bin/rust_bin3.rs | 3 +++ .../output directory/proj3/src/lib.rs | 4 +++ 9 files changed, 67 insertions(+), 17 deletions(-) create mode 100644 test/output directory/output directory/proj3/Cargo.lock create mode 100644 test/output directory/output directory/proj3/Cargo.toml create mode 100644 test/output directory/output directory/proj3/src/bin/rust_bin3.rs create mode 100644 test/output directory/output directory/proj3/src/lib.rs diff --git a/cmake/Corrosion.cmake b/cmake/Corrosion.cmake index 3fe667d2..682ba5a7 100644 --- a/cmake/Corrosion.cmake +++ b/cmake/Corrosion.cmake @@ -198,18 +198,29 @@ function(_corrosion_set_imported_location target_name base_property output_direc ") endfunction() -function(_corrosion_copy_byproduct_deferred target_name output_dir_prop_name cargo_build_dir file_names) +function(_corrosion_copy_byproduct_deferred target_name output_dir_prop_names cargo_build_dir file_names) if(ARGN) message(FATAL_ERROR "Unexpected additional arguments") endif() - get_target_property(output_dir ${target_name} "${output_dir_prop_name}") + + foreach(output_dir_prop_name ${output_dir_prop_names}) + get_target_property(output_dir ${target_name} "${output_dir_prop_name}") + if(output_dir) + break() + endif() + endforeach() # A Genex expanding to the output directory depending on the configuration. set(multiconfig_out_dir_genex "") foreach(config_type ${CMAKE_CONFIGURATION_TYPES}) string(TOUPPER "${config_type}" config_type_upper) - get_target_property(output_dir_curr_config ${target_name} "${output_dir_prop_name}_${config_type_upper}") + foreach(output_dir_prop_name ${output_dir_prop_names}) + get_target_property(output_dir_curr_config ${target_name} "${output_dir_prop_name}_${config_type_upper}") + if(output_dir_curr_config) + break() + endif() + endforeach() if(output_dir_curr_config) set(curr_out_dir "${output_dir_curr_config}") @@ -275,17 +286,17 @@ endfunction() # # Parameters: # - target_name: The name of the Rust target -# - output_dir_prop_name: The property name controlling the destination (e.g. -# `RUNTIME_OUTPUT_DIRECTORY`) +# - output_dir_prop_names: The property name(s) controlling the destination (e.g. +# `LIBRARY_OUTPUT_DIRECTORY` or `PDB_OUTPUT_DIRECTORY;RUNTIME_OUTPUT_DIRECTORY`) # - cargo_build_dir: the directory cargo build places it's output artifacts in. # - filenames: the file names of any output artifacts as a list. -function(_corrosion_copy_byproducts target_name output_dir_prop_name cargo_build_dir file_names) +function(_corrosion_copy_byproducts target_name output_dir_prop_names cargo_build_dir file_names) cmake_language(EVAL CODE " cmake_language(DEFER CALL _corrosion_copy_byproduct_deferred [[${target_name}]] - [[${output_dir_prop_name}]] + [[${output_dir_prop_names}]] [[${cargo_build_dir}]] [[${file_names}]] ) diff --git a/cmake/CorrosionGenerator.cmake b/cmake/CorrosionGenerator.cmake index 2f717a89..e387e76a 100644 --- a/cmake/CorrosionGenerator.cmake +++ b/cmake/CorrosionGenerator.cmake @@ -146,7 +146,7 @@ function(_generator_add_package_targets) endif() if(pdb_byproduct) _corrosion_copy_byproducts( - ${target_name} PDB_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${pdb_byproduct}" + ${target_name} "PDB_OUTPUT_DIRECTORY;LIBRARY_OUTPUT_DIRECTORY" "${cargo_build_out_dir}" "${pdb_byproduct}" ) endif() list(APPEND corrosion_targets ${target_name}) @@ -178,7 +178,7 @@ function(_generator_add_package_targets) ) if(pdb_byproduct) _corrosion_copy_byproducts( - ${target_name} PDB_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${pdb_byproduct}" + ${target_name} "PDB_OUTPUT_DIRECTORY;RUNTIME_OUTPUT_DIRECTORY" "${cargo_build_out_dir}" "${pdb_byproduct}" ) endif() list(APPEND corrosion_targets ${target_name}) diff --git a/test/cbindgen/rust2cpp/rust/Cargo.lock b/test/cbindgen/rust2cpp/rust/Cargo.lock index c7d8782b..0d2c5eac 100644 --- a/test/cbindgen/rust2cpp/rust/Cargo.lock +++ b/test/cbindgen/rust2cpp/rust/Cargo.lock @@ -2,13 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "other-crate" -version = "0.1.0" - [[package]] name = "rust-lib" version = "0.1.0" -dependencies = [ - "other-crate", -] diff --git a/test/output directory/CMakeLists.txt b/test/output directory/CMakeLists.txt index b3961118..0f5cf723 100644 --- a/test/output directory/CMakeLists.txt +++ b/test/output directory/CMakeLists.txt @@ -40,11 +40,13 @@ if(CORROSION_TESTS_INSTALL_CORROSION) set_tests_properties("output_directory_build" PROPERTIES FIXTURES_REQUIRED "fixture_corrosion_install") endif() -foreach(output_approach targetprop var) +foreach(output_approach targetprop var targetprop_pdb_fallback) if(output_approach STREQUAL "targetprop") set(rust_proj_suffix "1") elseif(output_approach STREQUAL "var") set(rust_proj_suffix "2") + elseif(output_approach STREQUAL "targetprop_pdb_fallback") + set(rust_proj_suffix "3") else() message(FATAL_ERROR "specify rust project suffix for new output approach ${output_approach}") endif() @@ -109,6 +111,9 @@ foreach(output_approach targetprop var) # end up in the same directory. set(expected_lib_pdb_path "custom_binlib_pdb_var") set(expected_bin_pdb_path "custom_binlib_pdb_var") + elseif(output_approach STREQUAL "targetprop_pdb_fallback") + set(expected_lib_pdb_path "custom_lib_targetprop_pdb_fallback") + set(expected_bin_pdb_path "custom_bin_targetprop_pdb_fallback") else() message(FATAL_ERROR "specify rust project suffix for new output approach ${output_approach}") endif() @@ -145,3 +150,5 @@ set_tests_properties("postbuild_custom_command" PROPERTIES FIXTURES_REQUIRED "bu add_test(NAME "output_directory_cleanup" COMMAND "${CMAKE_COMMAND}" -E remove_directory "${CMAKE_CURRENT_BINARY_DIR}/build") set_tests_properties("output_directory_cleanup" PROPERTIES FIXTURES_CLEANUP "build_fixture_output_directory") + + diff --git a/test/output directory/output directory/CMakeLists.txt b/test/output directory/output directory/CMakeLists.txt index 5a0f2cf8..9fbe6415 100644 --- a/test/output directory/output directory/CMakeLists.txt +++ b/test/output directory/output directory/CMakeLists.txt @@ -42,3 +42,17 @@ add_executable(consumer consumer.cpp) add_dependencies(consumer cargo-build_rust_lib1 cargo-build_rust_lib2) target_link_libraries(consumer rust_lib1 rust_lib2) + + +corrosion_import_crate(MANIFEST_PATH proj3/Cargo.toml) + +# Note: The output directories defined here must be manually kept in sync with the expected test location. +set_target_properties(rust_bin3 + PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/custom_bin_targetprop_pdb_fallback" +) +set_target_properties(rust_lib3 + PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/custom_archive_targetprop_pdb_fallback" + LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/custom_lib_targetprop_pdb_fallback" +) diff --git a/test/output directory/output directory/proj3/Cargo.lock b/test/output directory/output directory/proj3/Cargo.lock new file mode 100644 index 00000000..b7a53967 --- /dev/null +++ b/test/output directory/output directory/proj3/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "rust_package3" +version = "0.1.0" diff --git a/test/output directory/output directory/proj3/Cargo.toml b/test/output directory/output directory/proj3/Cargo.toml new file mode 100644 index 00000000..79509fc9 --- /dev/null +++ b/test/output directory/output directory/proj3/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "rust_package3" +version = "0.1.0" +edition = "2018" + +[lib] +name = "rust_lib3" +crate-type=["staticlib", "cdylib"] + +[[bin]] +name = "rust_bin3" diff --git a/test/output directory/output directory/proj3/src/bin/rust_bin3.rs b/test/output directory/output directory/proj3/src/bin/rust_bin3.rs new file mode 100644 index 00000000..11c0291c --- /dev/null +++ b/test/output directory/output directory/proj3/src/bin/rust_bin3.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world from test rust binary"); +} diff --git a/test/output directory/output directory/proj3/src/lib.rs b/test/output directory/output directory/proj3/src/lib.rs new file mode 100644 index 00000000..cfadd0f9 --- /dev/null +++ b/test/output directory/output directory/proj3/src/lib.rs @@ -0,0 +1,4 @@ +#[no_mangle] +pub extern "C" fn ret_12() -> u32 { + 12 +}