Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vcpkg-cmake-config] Merge INTERFACE_LINK_LIBRARIES configurations #22546

Merged
merged 13 commits into from
Feb 18, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ and applies a rather simply correction which in some cases will yield the wrong
## How it Works

1. Moves `/debug/<CONFIG_PATH>/*targets-debug.cmake` to `/share/${PACKAGE_NAME}`.
2. Removes `/debug/<CONFIG_PATH>/*config.cmake`.
3. Transform all references matching `/bin/*.exe` to `/tools/<port>/*.exe` on Windows.
4. Transform all references matching `/bin/*` to `/tools/<port>/*` on other platforms.
5. Fixes `${_IMPORT_PREFIX}` in auto generated targets.
6. Replace `${CURRENT_INSTALLED_DIR}` with `${_IMPORT_PREFIX}` in configs and targets.
2. Transforms all references matching `/bin/*.exe` to `/tools/<port>/*.exe` on Windows.
3. Transforms all references matching `/bin/*` to `/tools/<port>/*` on other platforms.
4. Fixes `${_IMPORT_PREFIX}` in auto generated targets.
5. Replaces `${CURRENT_INSTALLED_DIR}` with `${_IMPORT_PREFIX}` in configs.
6. Merges INTERFACE_LINK_LIBRARIES of release and debug configurations.
7. Replaces `${CURRENT_INSTALLED_DIR}` with `${VCPKG_IMPORT_PREFIX}` in targets.
8. Removes `/debug/<CONFIG_PATH>/*config.cmake`.

## Examples

Expand Down
2 changes: 0 additions & 2 deletions ports/tmx/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ vcpkg_cmake_configure(
vcpkg_cmake_install()
vcpkg_copy_pdbs()

file(RENAME "${CURRENT_PACKAGES_DIR}/lib/cmake/tmx/tmxExports.cmake" "${CURRENT_PACKAGES_DIR}/lib/cmake/tmx/tmxTargets.cmake")
vcpkg_cmake_config_fixup(CONFIG_PATH lib/cmake/tmx)
file(RENAME "${CURRENT_PACKAGES_DIR}/share/tmx/tmxTargets.cmake" "${CURRENT_PACKAGES_DIR}/share/tmx/tmxExports.cmake")

file(REMOVE_RECURSE
"${CURRENT_PACKAGES_DIR}/debug/include"
Expand Down
1 change: 1 addition & 0 deletions ports/tmx/vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "tmx",
"version": "1.2.0",
"port-version": 1,
"description": "A portable C library to load tiled maps in your games.",
"dependencies": [
"libxml2",
Expand Down
2 changes: 1 addition & 1 deletion ports/vcpkg-cmake-config/vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "vcpkg-cmake-config",
"version-date": "2021-12-28"
"version-date": "2022-01-15"
}
106 changes: 90 additions & 16 deletions ports/vcpkg-cmake-config/vcpkg_cmake_config_fixup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ and applies a rather simply correction which in some cases will yield the wrong
## How it Works

1. Moves `/debug/<CONFIG_PATH>/*targets-debug.cmake` to `/share/${PACKAGE_NAME}`.
2. Removes `/debug/<CONFIG_PATH>/*config.cmake`.
3. Transform all references matching `/bin/*.exe` to `/tools/<port>/*.exe` on Windows.
4. Transform all references matching `/bin/*` to `/tools/<port>/*` on other platforms.
5. Fixes `${_IMPORT_PREFIX}` in auto generated targets.
6. Replace `${CURRENT_INSTALLED_DIR}` with `${_IMPORT_PREFIX}` in configs and targets.
2. Transforms all references matching `/bin/*.exe` to `/tools/<port>/*.exe` on Windows.
3. Transforms all references matching `/bin/*` to `/tools/<port>/*` on other platforms.
4. Fixes `${_IMPORT_PREFIX}` in auto generated targets.
5. Replaces `${CURRENT_INSTALLED_DIR}` with `${_IMPORT_PREFIX}` in configs.
6. Merges INTERFACE_LINK_LIBRARIES of release and debug configurations.
7. Replaces `${CURRENT_INSTALLED_DIR}` with `${VCPKG_IMPORT_PREFIX}` in targets.
8. Removes `/debug/<CONFIG_PATH>/*config.cmake`.

## Examples

Expand Down Expand Up @@ -130,16 +132,6 @@ function(vcpkg_cmake_config_fixup)
endif()
endif()

file(GLOB_RECURSE unused_files
"${debug_share}/*[Tt]argets.cmake"
"${debug_share}/*[Cc]onfig.cmake"
"${debug_share}/*[Cc]onfigVersion.cmake"
"${debug_share}/*[Cc]onfig-version.cmake"
)
foreach(unused_file IN LISTS unused_files)
file(REMOVE "${unused_file}")
endforeach()

file(GLOB_RECURSE release_targets
"${release_share}/*-release.cmake"
)
Expand Down Expand Up @@ -169,8 +161,15 @@ function(vcpkg_cmake_config_fixup)
endif()

#Fix ${_IMPORT_PREFIX} and absolute paths in cmake generated targets and configs;
#Since those can be renamed we have to check in every *.cmake
#Since those can be renamed we have to check in every *.cmake, but only once.
file(GLOB_RECURSE main_cmakes "${release_share}/*.cmake")
if(DEFINED Z_VCPKG_CMAKE_CONFIG_FIXUP_IGNORE)
list(REMOVE_ITEM main_cmakes ${Z_VCPKG_CMAKE_CONFIG_FIXUP_IGNORE})
vcpkg_list(APPEND Z_VCPKG_CMAKE_CONFIG_FIXUP_IGNORE ${main_cmakes})
else()
vcpkg_list(SET Z_VCPKG_CMAKE_CONFIG_FIXUP_IGNORE ${main_cmakes})
endif()
set(Z_VCPKG_CMAKE_CONFIG_FIXUP_IGNORE "${Z_VCPKG_CMAKE_CONFIG_FIXUP_IGNORE}" CACHE INTERNAL "")
dg0yt marked this conversation as resolved.
Show resolved Hide resolved

foreach(main_cmake IN LISTS main_cmakes)
file(READ "${main_cmake}" contents)
Expand Down Expand Up @@ -201,6 +200,38 @@ get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)]]
contents "${contents}") # This is a meson-related workaround, see https://github.com/mesonbuild/meson/issues/6955
endif()

# Merge release and debug configurations of target property INTERFACE_LINK_LIBRARIES.
string(REPLACE "${release_share}/" "${debug_share}/" debug_cmake "${main_cmake}")
if(DEFINED VCPKG_BUILD_TYPE)
# Skip. Warning: A release-only port in a dual-config installation
# may pull release dependencies into the debug configuration.
elseif(NOT contents MATCHES "INTERFACE_LINK_LIBRARIES")
# Skip. No relevant properties.
elseif(NOT contents MATCHES "# Generated CMake target import file\\.")
# Skip. No safe assumptions about a matching debug import file.
elseif(NOT EXISTS "${debug_cmake}")
message(SEND_ERROR "Did not find a debug import file matching '${main_cmake}'")
else()
file(READ "${debug_cmake}" debug_contents)
while(contents MATCHES "set_target_properties\\(([^ \$]*) PROPERTIES[^)]*\\)")
set(matched_command "${CMAKE_MATCH_0}")
string(REPLACE "+" "\\+" target "${CMAKE_MATCH_1}")
if(NOT debug_contents MATCHES "set_target_properties\\(${target} PROPERTIES[^)]*\\)")
message(SEND_ERROR "Did not find a debug configuration for target '${target}'.")
endif()
set(debug_command "${CMAKE_MATCH_0}")
string(REGEX MATCH " INTERFACE_LINK_LIBRARIES \"([^\"]*)\"" release_line "${matched_command}")
set(release_libs "${CMAKE_MATCH_1}")
string(REGEX MATCH " INTERFACE_LINK_LIBRARIES \"([^\"]*)\"" debug_line "${debug_command}")
set(debug_libs "${CMAKE_MATCH_1}")
z_vcpkg_cmake_config_fixup_merge(merged_libs release_libs debug_libs)
string(REPLACE "${release_line}" " INTERFACE_LINK_LIBRARIES \"${merged_libs}\"" updated_command "${matched_command}")
string(REPLACE "set_target_properties" "set_target_properties::done" updated_command "${updated_command}") # Prevend 2nd match
string(REPLACE "${matched_command}" "${updated_command}" contents "${contents}")
endwhile()
string(REPLACE "set_target_properties::done" "set_target_properties" contents "${contents}") # Restore original command
endif()

#Fix absolute paths to installed dir with ones relative to ${CMAKE_CURRENT_LIST_DIR}
#This happens if vcpkg built libraries are directly linked to a target instead of using
#an imported target.
Expand All @@ -219,6 +250,16 @@ get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)]]
file(WRITE "${main_cmake}" "${contents}")
endforeach()

file(GLOB_RECURSE unused_files
"${debug_share}/*[Tt]argets.cmake"
"${debug_share}/*[Cc]onfig.cmake"
"${debug_share}/*[Cc]onfigVersion.cmake"
"${debug_share}/*[Cc]onfig-version.cmake"
)
foreach(unused_file IN LISTS unused_files)
file(REMOVE "${unused_file}")
endforeach()

# Remove /debug/<target_path>/ if it's empty.
file(GLOB_RECURSE remaining_files "${debug_share}/*")
if(remaining_files STREQUAL "")
Expand All @@ -231,3 +272,36 @@ get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)]]
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share")
endif()
endfunction()

# Merges link interface library lists for release and debug
# into a single expression which use generator expression as necessary.
function(z_vcpkg_cmake_config_fixup_merge out_var release_var debug_var)
set(release_libs "VCPKG;${${release_var}}")
string(REGEX REPLACE ";optimized;([^;]*)" ";\\1" release_libs "${release_libs}")
string(REGEX REPLACE ";debug;([^;]*)" ";" release_libs "${release_libs}")
list(REMOVE_AT release_libs 0)
list(FILTER release_libs EXCLUDE REGEX [[^\\[$]<\\[$]<CONFIG:DEBUG>:]])
list(TRANSFORM release_libs REPLACE [[^\\[$]<\\[$]<NOT:\\[$]<CONFIG:DEBUG>>:(.*)>$]] "\\1")

set(debug_libs "VCPKG;${${debug_var}}")
string(REGEX REPLACE ";optimized;([^;]*)" ";" debug_libs "${debug_libs}")
string(REGEX REPLACE ";debug;([^;]*)" ";\\1" debug_libs "${debug_libs}")
list(REMOVE_AT debug_libs 0)
list(FILTER debug_libs EXCLUDE REGEX [[^\\[$]<\\[$]<NOT:\\[$]<CONFIG:DEBUG>>:]])
list(TRANSFORM debug_libs REPLACE [[^\\[$]<\\[$]<CONFIG:DEBUG>:(.*)>$]] "\\1")

set(merged_libs "")
foreach(release_lib debug_lib IN ZIP_LISTS release_libs debug_libs)
if(release_lib STREQUAL debug_lib)
list(APPEND merged_libs "${release_lib}")
else()
if(release_lib)
list(APPEND merged_libs "\\\$<\\\$<NOT:\\\$<CONFIG:DEBUG>>:${release_lib}>")
endif()
if(debug_lib)
list(APPEND merged_libs "\\\$<\\\$<CONFIG:DEBUG>:${debug_lib}>")
endif()
endif()
endforeach()
set("${out_var}" "${merged_libs}" PARENT_SCOPE)
endfunction()
3 changes: 3 additions & 0 deletions scripts/test_ports/unit-test-cmake/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ endif()
if("function-arguments" IN_LIST FEATURES)
include("${CMAKE_CURRENT_LIST_DIR}/test-z_vcpkg_function_arguments.cmake")
endif()
if("merge-libs" IN_LIST FEATURES)
include("${CMAKE_CURRENT_LIST_DIR}/test-z_vcpkg_cmake_config_fixup_merge.cmake")
endif()

if(Z_VCPKG_UNIT_TEST_HAS_ERROR)
_message(FATAL_ERROR "At least one test failed")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# z_vcpkg_cmake_config_fixup_merge(<out_list> <release_list> <debug_list>)
set(release_libs namespace::C++_shared)
set(debug_libs namespace::C++_shared)
unit_test_check_variable_equal(
[[z_vcpkg_cmake_config_fixup_merge(merged release_libs debug_libs)]]
merged "namespace::C++_shared"
)

set(release_libs A)
set(debug_libs B)
unit_test_check_variable_equal(
[[z_vcpkg_cmake_config_fixup_merge(merged release_libs debug_libs)]]
merged [[\$<\$<NOT:\$<CONFIG:DEBUG>>:A>;\$<\$<CONFIG:DEBUG>:B>]]
)

set(release_libs A B)
set(debug_libs A )
unit_test_check_variable_equal(
[[z_vcpkg_cmake_config_fixup_merge(merged release_libs debug_libs)]]
merged [[A;\$<\$<NOT:\$<CONFIG:DEBUG>>:B>]]
)

set(release_libs A )
set(debug_libs A B)
unit_test_check_variable_equal(
[[z_vcpkg_cmake_config_fixup_merge(merged release_libs debug_libs)]]
merged [[A;\$<\$<CONFIG:DEBUG>:B>]]
)

set(release_libs A C)
set(debug_libs C)
unit_test_check_variable_equal(
[[z_vcpkg_cmake_config_fixup_merge(merged release_libs debug_libs)]]
merged [[\$<\$<NOT:\$<CONFIG:DEBUG>>:A>;\$<\$<CONFIG:DEBUG>:C>;\$<\$<NOT:\$<CONFIG:DEBUG>>:C>]]
)

set(release_libs [[\$<\$<NOT:\$<CONFIG:DEBUG>>:A>;\$<\$<CONFIG:DEBUG>:B>]])
set(debug_libs [[\$<\$<NOT:\$<CONFIG:DEBUG>>:A>;\$<\$<CONFIG:DEBUG>:B>]])
unit_test_check_variable_equal(
[[z_vcpkg_cmake_config_fixup_merge(merged release_libs debug_libs)]]
merged [[\$<\$<NOT:\$<CONFIG:DEBUG>>:A>;\$<\$<CONFIG:DEBUG>:B>]]
)

set(release_libs optimized o1 debug d1)
set(debug_libs optimized o2 debug d2)
unit_test_check_variable_equal(
[[z_vcpkg_cmake_config_fixup_merge(merged release_libs debug_libs)]]
merged [[\$<\$<NOT:\$<CONFIG:DEBUG>>:o1>;\$<\$<CONFIG:DEBUG>:d2>]]
)

set(release_libs debug d1 optimized o1)
set(debug_libs debug d2 optimized o2)
unit_test_check_variable_equal(
[[z_vcpkg_cmake_config_fixup_merge(merged release_libs debug_libs)]]
merged [[\$<\$<CONFIG:DEBUG>:d2>;\$<\$<NOT:\$<CONFIG:DEBUG>>:o1>]]
)
9 changes: 9 additions & 0 deletions scripts/test_ports/unit-test-cmake/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
"list": {
"description": "Test the vcpkg_list function"
},
"merge-libs": {
"description": "Test the z_vcpkg_cmake_config_fixup_merge_lists function",
"dependencies": [
{
"name": "vcpkg-cmake-config",
"host": true
}
]
},
"minimum-required": {
"description": "Test the vcpkg_minimum_required function"
}
Expand Down
4 changes: 2 additions & 2 deletions versions/baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -6918,7 +6918,7 @@
},
"tmx": {
"baseline": "1.2.0",
"port-version": 0
"port-version": 1
},
"tmxlite": {
"baseline": "1.3.0",
Expand Down Expand Up @@ -7157,7 +7157,7 @@
"port-version": 0
},
"vcpkg-cmake-config": {
"baseline": "2021-12-28",
"baseline": "2022-01-15",
"port-version": 0
},
"vcpkg-gfortran": {
Expand Down
5 changes: 5 additions & 0 deletions versions/t-/tmx.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"versions": [
{
"git-tree": "1b0ec7979f9a75aab71e9488940a4beff74bd745",
"version": "1.2.0",
"port-version": 1
},
{
"git-tree": "207601a8ff1891880a88013e3b36ea0f81979010",
"version": "1.2.0",
Expand Down
5 changes: 5 additions & 0 deletions versions/v-/vcpkg-cmake-config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"versions": [
{
"git-tree": "342ab2e2d183e3e962c7a52802085a47f0992d81",
"version-date": "2022-01-15",
"port-version": 0
},
{
"git-tree": "e33152002c946b93a0262931ba8bf54a2e6ab9ad",
"version-date": "2021-12-28",
Expand Down