Skip to content

Commit

Permalink
Copy pdb to runtime/library output dir as fallback (#486)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Nopey and jschwe authored Feb 8, 2024
1 parent afb4e2f commit 5b0a786
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 17 deletions.
25 changes: 18 additions & 7 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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}]]
)
Expand Down
4 changes: 2 additions & 2 deletions cmake/CorrosionGenerator.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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})
Expand Down
7 changes: 0 additions & 7 deletions test/cbindgen/rust2cpp/rust/Cargo.lock

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

9 changes: 8 additions & 1 deletion test/output directory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")


14 changes: 14 additions & 0 deletions test/output directory/output directory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
7 changes: 7 additions & 0 deletions test/output directory/output directory/proj3/Cargo.lock

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

11 changes: 11 additions & 0 deletions test/output directory/output directory/proj3/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world from test rust binary");
}
4 changes: 4 additions & 0 deletions test/output directory/output directory/proj3/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[no_mangle]
pub extern "C" fn ret_12() -> u32 {
12
}

0 comments on commit 5b0a786

Please sign in to comment.