Skip to content

Commit

Permalink
Create CMake targets earlier
Browse files Browse the repository at this point in the history
Create the main CMake targets earlier, so we can
set target properties on them early in the generator,
so we can use them later on during configuration.

We don't strictly need this right now, but it reduces the diff to
the stable/v0.5 branch and being able to set properties earlier is
something that seems like it will be useful.
  • Loading branch information
jschwe committed May 15, 2024
1 parent 4da0f42 commit e404d22
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
23 changes: 16 additions & 7 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,6 @@ function(_corrosion_add_library_target)
endif()
set("${CALT_OUT_ARCHIVE_OUTPUT_BYPRODUCTS}" "${archive_output_byproducts}" PARENT_SCOPE)

add_library(${target_name} INTERFACE)

if(has_staticlib)
add_library(${target_name}-static STATIC IMPORTED GLOBAL)
add_dependencies(${target_name}-static cargo-build_${target_name})
Expand Down Expand Up @@ -491,11 +489,6 @@ function(_corrosion_add_bin_target workspace_manifest_path bin_name out_bin_bypr
# target property
set(bin_filename "${bin_name}")
set(${out_bin_byproduct} "${bin_filename}" PARENT_SCOPE)


# Todo: This is compatible with the way corrosion previously exposed the bin name,
# but maybe we want to prefix the exposed name with the package name?
add_executable(${bin_name} IMPORTED GLOBAL)
add_dependencies(${bin_name} cargo-build_${bin_name})

if(Rust_CARGO_TARGET_OS STREQUAL "darwin")
Expand Down Expand Up @@ -1753,6 +1746,22 @@ function(corrosion_parse_package_version package_manifest_path out_package_versi
endif()
endfunction()

function(_corrosion_initialize_properties target_name)
# Initialize the `<XYZ>_OUTPUT_DIRECTORY` properties based on `CMAKE_<XYZ>_OUTPUT_DIRECTORY`.
foreach(output_var RUNTIME_OUTPUT_DIRECTORY ARCHIVE_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY PDB_OUTPUT_DIRECTORY)
if (DEFINED "CMAKE_${output_var}")
set_property(TARGET ${target_name} PROPERTY "${output_var}" "${CMAKE_${output_var}}")
endif()

foreach(config_type ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER "${config_type}" config_type_upper)
if (DEFINED "CMAKE_${output_var}_${config_type_upper}")
set_property(TARGET ${target_name} PROPERTY "${output_var}_${config_type_upper}" "${CMAKE_${output_var}_${config_type_upper}}")
endif()
endforeach()
endforeach()
endfunction()

# Helper macro to pass through an optional `OPTION` argument parsed via `cmake_parse_arguments`
# to another function that takes the same OPTION.
# If the option was set, then the variable <var_name> will be set to the same option name again,
Expand Down
21 changes: 4 additions & 17 deletions cmake/CorrosionGenerator.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ function(_generator_add_package_targets)
set(shared_lib_byproduct "")
set(pdb_byproduct "")

add_library(${target_name} INTERFACE)
_corrosion_initialize_properties(${target_name})
_corrosion_add_library_target(
WORKSPACE_MANIFEST_PATH "${workspace_manifest_path}"
TARGET_NAME "${target_name}"
Expand Down Expand Up @@ -161,6 +163,8 @@ function(_generator_add_package_targets)
elseif("bin" IN_LIST kinds)
set(bin_byproduct "")
set(pdb_byproduct "")
add_executable(${target_name} IMPORTED GLOBAL)
_corrosion_initialize_properties(${target_name})
_corrosion_add_bin_target("${workspace_manifest_path}" "${target_name}"
"bin_byproduct" "pdb_byproduct"
)
Expand Down Expand Up @@ -306,21 +310,4 @@ function(_generator_add_cargo_targets)
if(GGC_IMPORTED_CRATES)
set(${GGC_IMPORTED_CRATES} "${created_targets}" PARENT_SCOPE)
endif()

foreach(target_name ${created_targets})
foreach(output_var RUNTIME_OUTPUT_DIRECTORY ARCHIVE_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY PDB_OUTPUT_DIRECTORY)
get_target_property(output_dir ${target_name} "${output_var}")
if (NOT output_dir AND DEFINED "CMAKE_${output_var}")
set_property(TARGET ${target_name} PROPERTY ${output_var} "${CMAKE_${output_var}}")
endif()

foreach(config_type ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER "${config_type}" config_type_upper)
get_target_property(output_dir ${target_name} "${output_var}_${config_type_upper}")
if (NOT output_dir AND DEFINED "CMAKE_${output_var}_${config_type_upper}")
set_property(TARGET ${target_name} PROPERTY "${output_var}_${config_type_upper}" "${CMAKE_${output_var}_${config_type_upper}}")
endif()
endforeach()
endforeach()
endforeach()
endfunction()

0 comments on commit e404d22

Please sign in to comment.