Skip to content

Commit

Permalink
Fix output directory on Multi-Config Generators (#568)
Browse files Browse the repository at this point in the history
* CMake doesn't automatically append the config type to the output dir when using a custom command.
  • Loading branch information
obsgolem authored Dec 11, 2024
1 parent 934fee2 commit 4c0d9af
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
11 changes: 9 additions & 2 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ function(_corrosion_set_imported_location_deferred target_name base_property out
if(output_dir_curr_config)
set(curr_out_dir "${output_dir_curr_config}")
elseif(output_directory)
set(curr_out_dir "${output_directory}")
string(GENEX_STRIP "${output_directory}" output_dir_no_genex)
# Only add config dir if there is no genex in here. See
# https://cmake.org/cmake/help/latest/prop_tgt/RUNTIME_OUTPUT_DIRECTORY.html
if(output_directory STREQUAL output_dir_no_genex)
set(curr_out_dir "${output_directory}/${config_type}")
else()
set(curr_out_dir "${output_directory}")
endif()
else()
set(curr_out_dir "${CMAKE_CURRENT_BINARY_DIR}")
endif()
Expand Down Expand Up @@ -207,7 +214,7 @@ function(_corrosion_copy_byproduct_deferred target_name output_dir_prop_names ca
# Fallback to `output_dir` if specified
# Note: Multi-configuration generators append a per-configuration subdirectory to the
# specified directory unless a generator expression is used (from CMake documentation).
set(curr_out_dir "${output_dir}")
set(curr_out_dir "${output_dir}/${config_type}")
else()
# Fallback to the default directory. We do not append the configuration directory here
# and instead let CMake do this, since otherwise the resolving of dynamic library
Expand Down
20 changes: 14 additions & 6 deletions test/output directory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ if(CORROSION_TESTS_INSTALL_CORROSION)
set_tests_properties("output_directory_build" PROPERTIES FIXTURES_REQUIRED "fixture_corrosion_install")
endif()

get_cmake_property(IS_MULTI_CONFIG GENERATOR_IS_MULTI_CONFIG)

if (IS_MULTI_CONFIG)
set(config_path_str "$<CONFIG>/")
else()
set(config_path_str "")
endif()

foreach(output_approach targetprop var targetprop_pdb_fallback)
if(output_approach STREQUAL "targetprop")
set(rust_proj_suffix "1")
Expand All @@ -57,7 +65,7 @@ foreach(output_approach targetprop var targetprop_pdb_fallback)
COMMAND
"${CMAKE_COMMAND}"
-P "${CMAKE_CURRENT_SOURCE_DIR}/TestFileExists.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/build/custom_bin_${output_approach}/${bin_name}"
"${CMAKE_CURRENT_BINARY_DIR}/build/custom_bin_${output_approach}/${config_path_str}${bin_name}"
)
set_tests_properties("output_directory_bin_${output_approach}" PROPERTIES FIXTURES_REQUIRED "build_fixture_output_directory")

Expand All @@ -69,7 +77,7 @@ foreach(output_approach targetprop var targetprop_pdb_fallback)
COMMAND
"${CMAKE_COMMAND}"
-P "${CMAKE_CURRENT_SOURCE_DIR}/TestFileExists.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/build/custom_archive_${output_approach}/${static_lib_name}"
"${CMAKE_CURRENT_BINARY_DIR}/build/custom_archive_${output_approach}/${config_path_str}${static_lib_name}"
)
set_tests_properties("output_directory_staticlib_${output_approach}" PROPERTIES FIXTURES_REQUIRED "build_fixture_output_directory")

Expand All @@ -85,7 +93,7 @@ foreach(output_approach targetprop var targetprop_pdb_fallback)
COMMAND
"${CMAKE_COMMAND}"
-P "${CMAKE_CURRENT_SOURCE_DIR}/TestFileExists.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/build/custom_lib_${output_approach}/${dynamic_lib_name}"
"${CMAKE_CURRENT_BINARY_DIR}/build/custom_lib_${output_approach}/${config_path_str}${dynamic_lib_name}"
)
set_tests_properties("output_directory_cdylib_${output_approach}" PROPERTIES FIXTURES_REQUIRED "build_fixture_output_directory")

Expand All @@ -98,7 +106,7 @@ foreach(output_approach targetprop var targetprop_pdb_fallback)
-P "${CMAKE_CURRENT_SOURCE_DIR}/TestFileExists.cmake"
# Implib is an ARCHIVE artifact, see:
# https://cmake.org/cmake/help/v3.25/manual/cmake-buildsystem.7.html#archive-output-artifacts
"${CMAKE_CURRENT_BINARY_DIR}/build/custom_archive_${output_approach}/${implib_name}"
"${CMAKE_CURRENT_BINARY_DIR}/build/custom_archive_${output_approach}/${config_path_str}${implib_name}"
)
set_tests_properties("output_directory_implib_${output_approach}" PROPERTIES FIXTURES_REQUIRED "build_fixture_output_directory")

Expand All @@ -123,7 +131,7 @@ foreach(output_approach targetprop var targetprop_pdb_fallback)
COMMAND
"${CMAKE_COMMAND}"
-P "${CMAKE_CURRENT_SOURCE_DIR}/TestFileExists.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/build/${expected_lib_pdb_path}/${lib_pdb_name}"
"${CMAKE_CURRENT_BINARY_DIR}/build/${expected_lib_pdb_path}/${config_path_str}${lib_pdb_name}"
)
set_tests_properties("output_directory_cdylib_pdb_${output_approach}" PROPERTIES FIXTURES_REQUIRED "build_fixture_output_directory")

Expand All @@ -132,7 +140,7 @@ foreach(output_approach targetprop var targetprop_pdb_fallback)
COMMAND
"${CMAKE_COMMAND}"
-P "${CMAKE_CURRENT_SOURCE_DIR}/TestFileExists.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/build/${expected_bin_pdb_path}/${bin_pdb_name}"
"${CMAKE_CURRENT_BINARY_DIR}/build/${expected_bin_pdb_path}/${config_path_str}${bin_pdb_name}"
)
set_tests_properties("output_directory_bin_pdb_${output_approach}" PROPERTIES FIXTURES_REQUIRED "build_fixture_output_directory")
endif()
Expand Down
2 changes: 1 addition & 1 deletion test/output directory/output directory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ add_custom_command(TARGET cargo-build_rust_bin1 POST_BUILD
COMMAND
${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/another_dir"
COMMAND
${CMAKE_COMMAND} -E copy_if_different "$<TARGET_PROPERTY:rust_bin1,LOCATION>" "${CMAKE_CURRENT_BINARY_DIR}/another_dir/moved_bin"
${CMAKE_COMMAND} -E copy_if_different "$<TARGET_PROPERTY:rust_bin1,LOCATION_$<CONFIG>>" "${CMAKE_CURRENT_BINARY_DIR}/another_dir/moved_bin"
)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/custom_bin_var")
Expand Down

0 comments on commit 4c0d9af

Please sign in to comment.