Skip to content

Commit

Permalink
[vcpkg_fixup_cmake_targets] Add NO_PREFIX_CORRECTION (#12215)
Browse files Browse the repository at this point in the history
* [vcpkg/scripts/fixup_cmake] add option NO_PREFIX_CORRECTION to not apply the

* add a bit of explanation why the prefix correction might be incorrect.

* apply the if only around the import prefix changes

* Update scripts/cmake/vcpkg_fixup_cmake_targets.cmake

Co-authored-by: Robert Schumacher <[email protected]>

Co-authored-by: Robert Schumacher <[email protected]>
Co-authored-by: Billy Robert O'Neal III <[email protected]>
  • Loading branch information
3 people authored Nov 25, 2020
1 parent 54c6351 commit e2ec3b2
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions scripts/cmake/vcpkg_fixup_cmake_targets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
## Passing this option disable such behavior, as it is convenient for ports that install
## more than one CMake package configuration file.
##
## ### NO_PREFIX_CORRECTION
## Disables the correction of_IMPORT_PREFIX done by vcpkg due to moving the targets.
## Currently the correction does not take into account how the files are moved and applies
## I rather simply correction which in some cases will yield the wrong results.
##
## ## Notes
## Transform all `/debug/<CONFIG_PATH>/*targets-debug.cmake` files and move them to `/<TARGET_PATH>`.
## Removes all `/debug/<CONFIG_PATH>/*targets.cmake` and `/debug/<CONFIG_PATH>/*config.cmake`.
Expand All @@ -44,7 +49,7 @@
## * [nlohmann-json](https:/Microsoft/vcpkg/blob/master/ports/nlohmann-json/portfile.cmake)
function(vcpkg_fixup_cmake_targets)
# parse parameters such that semicolons in options arguments to COMMAND don't get erased
cmake_parse_arguments(PARSE_ARGV 0 _vfct "DO_NOT_DELETE_PARENT_CONFIG_PATH" "CONFIG_PATH;TARGET_PATH" "")
cmake_parse_arguments(PARSE_ARGV 0 _vfct "DO_NOT_DELETE_PARENT_CONFIG_PATH" "CONFIG_PATH;TARGET_PATH;NO_PREFIX_CORRECTION" "")

if(_vfct_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "vcpkg_fixup_cmake_targets was passed extra arguments: ${_vfct_UNPARSED_ARGUMENTS}")
Expand Down Expand Up @@ -160,21 +165,40 @@ function(vcpkg_fixup_cmake_targets)
#Fix ${_IMPORT_PREFIX} in cmake generated targets and configs;
#Since those can be renamed we have to check in every *.cmake
file(GLOB_RECURSE MAIN_CMAKES "${RELEASE_SHARE}/*.cmake")

foreach(MAIN_CMAKE IN LISTS MAIN_CMAKES)
file(READ ${MAIN_CMAKE} _contents)
string(REGEX REPLACE
"get_filename_component\\(_IMPORT_PREFIX \"\\\${CMAKE_CURRENT_LIST_FILE}\" PATH\\)(\nget_filename_component\\(_IMPORT_PREFIX \"\\\${_IMPORT_PREFIX}\" PATH\\))*"
"get_filename_component(_IMPORT_PREFIX \"\${CMAKE_CURRENT_LIST_FILE}\" PATH)\nget_filename_component(_IMPORT_PREFIX \"\${_IMPORT_PREFIX}\" PATH)\nget_filename_component(_IMPORT_PREFIX \"\${_IMPORT_PREFIX}\" PATH)"
_contents "${_contents}") # see #1044 for details why this replacement is necessary. See #4782 why it must be a regex.
string(REGEX REPLACE
"get_filename_component\\(PACKAGE_PREFIX_DIR \"\\\${CMAKE_CURRENT_LIST_DIR}/\\.\\./(\\.\\./)*\" ABSOLUTE\\)"
"get_filename_component(PACKAGE_PREFIX_DIR \"\${CMAKE_CURRENT_LIST_DIR}/../../\" ABSOLUTE)"
_contents "${_contents}")
string(REGEX REPLACE
"get_filename_component\\(PACKAGE_PREFIX_DIR \"\\\${CMAKE_CURRENT_LIST_DIR}/\\.\\.((\\\\|/)\\.\\.)*\" ABSOLUTE\\)"
"get_filename_component(PACKAGE_PREFIX_DIR \"\${CMAKE_CURRENT_LIST_DIR}/../../\" ABSOLUTE)"
_contents "${_contents}") # This is a meson-related workaround, see https:/mesonbuild/meson/issues/6955
#This correction is not correct for all cases. To make it correct for all cases it needs to consider
#original folder deepness to CURRENT_PACKAGES_DIR in comparison to the moved to folder deepness which
#is always at least (>=) 2, e.g. share/${PORT}. Currently the code assumes it is always 2 although
#this requirement is only true for the *Config.cmake. The targets are not required to be in the same
#folder as the *Config.cmake!
if(NOT _vfct_NO_PREFIX_CORRECTION)
string(REGEX REPLACE
"get_filename_component\\(_IMPORT_PREFIX \"\\\${CMAKE_CURRENT_LIST_FILE}\" PATH\\)(\nget_filename_component\\(_IMPORT_PREFIX \"\\\${_IMPORT_PREFIX}\" PATH\\))*"
"get_filename_component(_IMPORT_PREFIX \"\${CMAKE_CURRENT_LIST_FILE}\" PATH)\nget_filename_component(_IMPORT_PREFIX \"\${_IMPORT_PREFIX}\" PATH)\nget_filename_component(_IMPORT_PREFIX \"\${_IMPORT_PREFIX}\" PATH)"
_contents "${_contents}") # see #1044 for details why this replacement is necessary. See #4782 why it must be a regex.
string(REGEX REPLACE
"get_filename_component\\(PACKAGE_PREFIX_DIR \"\\\${CMAKE_CURRENT_LIST_DIR}/\\.\\./(\\.\\./)*\" ABSOLUTE\\)"
"get_filename_component(PACKAGE_PREFIX_DIR \"\${CMAKE_CURRENT_LIST_DIR}/../../\" ABSOLUTE)"
_contents "${_contents}")
string(REGEX REPLACE
"get_filename_component\\(PACKAGE_PREFIX_DIR \"\\\${CMAKE_CURRENT_LIST_DIR}/\\.\\.((\\\\|/)\\.\\.)*\" ABSOLUTE\\)"
"get_filename_component(PACKAGE_PREFIX_DIR \"\${CMAKE_CURRENT_LIST_DIR}/../../\" ABSOLUTE)"
_contents "${_contents}") # This is a meson-related workaround, see https:/mesonbuild/meson/issues/6955
endif()

#Fix wrongly absolute paths to install dir with the correct dir using ${_IMPORT_PREFIX}
#This happens if vcpkg built libraries are directly linked to a target instead of using
#an imported target for it. We could add more logic here to identify defect target files.
#Since the replacement here in a multi config build always requires a generator expression
#in front of the absoulte path to ${CURRENT_INSTALLED_DIR}. So the match should always be at
#least >:${CURRENT_INSTALLED_DIR}.
#In general the following generator expressions should be there:
#\$<\$<CONFIG:DEBUG>:${CURRENT_INSTALLED_DIR}/debug/lib/somelib>
#and/or
#\$<\$<NOT:\$<CONFIG:DEBUG>>:${CURRENT_INSTALLED_DIR}/lib/somelib>
#with ${CURRENT_INSTALLED_DIR} being fully expanded
string(REPLACE "${CURRENT_INSTALLED_DIR}" [[${_IMPORT_PREFIX}]] _contents "${_contents}")
file(WRITE ${MAIN_CMAKE} "${_contents}")
endforeach()
Expand Down

0 comments on commit e2ec3b2

Please sign in to comment.