2017-05-03 11:34:11 +08:00
function ( vcpkg_fixup_cmake_targets )
2021-03-01 05:17:19 +08:00
if ( Z_VCPKG_CMAKE_CONFIG_FIXUP_GUARD )
message ( FATAL_ERROR "The ${PORT} port already depends on vcpkg-cmake-config; using both vcpkg-cmake-config and vcpkg_fixup_cmake_targets in the same port is unsupported." )
endif ( )
2017-05-03 11:34:11 +08:00
2021-11-09 13:29:35 +08:00
cmake_parse_arguments ( PARSE_ARGV 0 arg "DO_NOT_DELETE_PARENT_CONFIG_PATH;NO_PREFIX_CORRECTION" "CONFIG_PATH;TARGET_PATH;TOOLS_PATH" "" )
2021-03-01 05:17:19 +08:00
if ( arg_UNPARSED_ARGUMENTS )
message ( FATAL_ERROR "vcpkg_fixup_cmake_targets was passed extra arguments: ${arg_UNPARSED_ARGUMENTS}" )
2017-06-21 07:19:36 +08:00
endif ( )
2021-03-01 05:17:19 +08:00
if ( NOT arg_TARGET_PATH )
set ( arg_TARGET_PATH share/ ${ PORT } )
2018-01-04 12:11:35 +08:00
endif ( )
2021-04-01 03:47:35 +08:00
if ( NOT arg_TOOLS_PATH )
set ( arg_TOOLS_PATH tools/ ${ PORT } )
endif ( )
2018-01-04 12:11:35 +08:00
2020-08-01 02:13:45 +08:00
string ( REPLACE "." "\\." EXECUTABLE_SUFFIX "${VCPKG_TARGET_EXECUTABLE_SUFFIX}" )
2019-06-25 14:21:30 +08:00
2021-03-01 05:17:19 +08:00
set ( DEBUG_SHARE ${ CURRENT_PACKAGES_DIR } /debug/ ${ arg_TARGET_PATH } )
set ( RELEASE_SHARE ${ CURRENT_PACKAGES_DIR } / ${ arg_TARGET_PATH } )
2017-05-03 11:34:11 +08:00
2021-03-01 05:17:19 +08:00
if ( arg_CONFIG_PATH AND NOT RELEASE_SHARE STREQUAL "${CURRENT_PACKAGES_DIR}/${arg_CONFIG_PATH}" )
if ( arg_CONFIG_PATH STREQUAL "share" )
2018-04-18 07:11:18 +08:00
file ( RENAME ${ CURRENT_PACKAGES_DIR } /debug/share ${ CURRENT_PACKAGES_DIR } /debug/share2 )
file ( RENAME ${ CURRENT_PACKAGES_DIR } /share ${ CURRENT_PACKAGES_DIR } /share2 )
2021-03-01 05:17:19 +08:00
set ( arg_CONFIG_PATH share2 )
2018-04-18 07:11:18 +08:00
endif ( )
2021-03-01 05:17:19 +08:00
set ( DEBUG_CONFIG ${ CURRENT_PACKAGES_DIR } /debug/ ${ arg_CONFIG_PATH } )
set ( RELEASE_CONFIG ${ CURRENT_PACKAGES_DIR } / ${ arg_CONFIG_PATH } )
2017-11-26 07:25:18 +08:00
if ( NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug" )
if ( NOT EXISTS ${ DEBUG_CONFIG } )
message ( FATAL_ERROR "'${DEBUG_CONFIG}' does not exist." )
endif ( )
2017-05-11 06:08:52 +08:00
2018-04-18 07:11:18 +08:00
# This roundabout handling enables CONFIG_PATH share
2017-11-26 07:25:18 +08:00
file ( MAKE_DIRECTORY ${ DEBUG_SHARE } )
file ( GLOB FILES ${ DEBUG_CONFIG } /* )
file ( COPY ${ FILES } DESTINATION ${ DEBUG_SHARE } )
file ( REMOVE_RECURSE ${ DEBUG_CONFIG } )
endif ( )
2017-10-27 13:34:54 +08:00
file ( GLOB FILES ${ RELEASE_CONFIG } /* )
file ( COPY ${ FILES } DESTINATION ${ RELEASE_SHARE } )
file ( REMOVE_RECURSE ${ RELEASE_CONFIG } )
2017-05-11 06:08:52 +08:00
2017-11-26 07:25:18 +08:00
if ( NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug" )
get_filename_component ( DEBUG_CONFIG_DIR_NAME ${ DEBUG_CONFIG } NAME )
2017-05-11 06:08:52 +08:00
string ( TOLOWER "${DEBUG_CONFIG_DIR_NAME}" DEBUG_CONFIG_DIR_NAME )
2021-03-01 05:17:19 +08:00
if ( DEBUG_CONFIG_DIR_NAME STREQUAL "cmake" AND NOT arg_DO_NOT_DELETE_PARENT_CONFIG_PATH )
2017-11-26 07:25:18 +08:00
file ( REMOVE_RECURSE ${ DEBUG_CONFIG } )
else ( )
get_filename_component ( DEBUG_CONFIG_PARENT_DIR ${ DEBUG_CONFIG } DIRECTORY )
get_filename_component ( DEBUG_CONFIG_DIR_NAME ${ DEBUG_CONFIG_PARENT_DIR } NAME )
string ( TOLOWER "${DEBUG_CONFIG_DIR_NAME}" DEBUG_CONFIG_DIR_NAME )
2021-03-01 05:17:19 +08:00
if ( DEBUG_CONFIG_DIR_NAME STREQUAL "cmake" AND NOT arg_DO_NOT_DELETE_PARENT_CONFIG_PATH )
2017-11-26 07:25:18 +08:00
file ( REMOVE_RECURSE ${ DEBUG_CONFIG_PARENT_DIR } )
endif ( )
2017-05-11 06:08:52 +08:00
endif ( )
endif ( )
get_filename_component ( RELEASE_CONFIG_DIR_NAME ${ RELEASE_CONFIG } NAME )
string ( TOLOWER "${RELEASE_CONFIG_DIR_NAME}" RELEASE_CONFIG_DIR_NAME )
2021-03-01 05:17:19 +08:00
if ( RELEASE_CONFIG_DIR_NAME STREQUAL "cmake" AND NOT arg_DO_NOT_DELETE_PARENT_CONFIG_PATH )
2017-05-11 06:08:52 +08:00
file ( REMOVE_RECURSE ${ RELEASE_CONFIG } )
else ( )
get_filename_component ( RELEASE_CONFIG_PARENT_DIR ${ RELEASE_CONFIG } DIRECTORY )
get_filename_component ( RELEASE_CONFIG_DIR_NAME ${ RELEASE_CONFIG_PARENT_DIR } NAME )
string ( TOLOWER "${RELEASE_CONFIG_DIR_NAME}" RELEASE_CONFIG_DIR_NAME )
2021-03-01 05:17:19 +08:00
if ( RELEASE_CONFIG_DIR_NAME STREQUAL "cmake" AND NOT arg_DO_NOT_DELETE_PARENT_CONFIG_PATH )
2017-05-11 06:08:52 +08:00
file ( REMOVE_RECURSE ${ RELEASE_CONFIG_PARENT_DIR } )
endif ( )
endif ( )
endif ( )
2017-11-26 07:25:18 +08:00
if ( NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug" )
2018-04-18 07:11:18 +08:00
if ( NOT EXISTS "${DEBUG_SHARE}" )
2017-11-26 07:25:18 +08:00
message ( FATAL_ERROR "'${DEBUG_SHARE}' does not exist." )
endif ( )
2017-05-03 11:34:11 +08:00
endif ( )
2018-03-25 04:08:06 +08:00
file ( GLOB_RECURSE UNUSED_FILES
2017-05-23 03:10:20 +08:00
" $ { D E B U G _ S H A R E } / * [ T t ] a r g e t s . c m a k e "
" $ { D E B U G _ S H A R E } / * [ C c ] o n f i g . c m a k e "
" $ { D E B U G _ S H A R E } / * [ C c ] o n f i g V e r s i o n . c m a k e "
" $ { D E B U G _ S H A R E } / * [ C c ] o n f i g - v e r s i o n . c m a k e "
)
2017-06-28 02:24:31 +08:00
if ( UNUSED_FILES )
file ( REMOVE ${ UNUSED_FILES } )
endif ( )
2017-05-03 11:34:11 +08:00
2018-03-25 04:08:06 +08:00
file ( GLOB_RECURSE RELEASE_TARGETS
2018-01-18 17:28:56 +08:00
" $ { R E L E A S E _ S H A R E } / * - r e l e a s e . c m a k e "
2017-05-23 03:10:20 +08:00
)
2018-03-25 04:08:06 +08:00
foreach ( RELEASE_TARGET IN LISTS RELEASE_TARGETS )
2017-05-07 14:02:25 +08:00
file ( READ ${ RELEASE_TARGET } _contents )
2017-10-19 23:53:59 +08:00
string ( REPLACE "${CURRENT_INSTALLED_DIR}" "\${_IMPORT_PREFIX}" _contents "${_contents}" )
2021-04-01 03:47:35 +08:00
string ( REGEX REPLACE "\\\${_IMPORT_PREFIX}/bin/([^ \" ]+ ${ EXECUTABLE_SUFFIX } ) " " \ $ { _ I M P O R T _ P R E F I X } / $ { a r g _ T O O L S _ P A T H } / \ \ 1 " _ c o n t e n t s " $ { _ c o n t e n t s } " )
2017-05-07 14:02:25 +08:00
file ( WRITE ${ RELEASE_TARGET } "${_contents}" )
endforeach ( )
2017-11-26 07:25:18 +08:00
if ( NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug" )
2018-03-25 04:08:06 +08:00
file ( GLOB_RECURSE DEBUG_TARGETS
2018-01-18 17:28:56 +08:00
" $ { D E B U G _ S H A R E } / * - d e b u g . c m a k e "
2017-11-26 07:25:18 +08:00
)
2018-03-25 04:08:06 +08:00
foreach ( DEBUG_TARGET IN LISTS DEBUG_TARGETS )
file ( RELATIVE_PATH DEBUG_TARGET_REL "${DEBUG_SHARE}" "${DEBUG_TARGET}" )
2017-11-26 07:25:18 +08:00
file ( READ ${ DEBUG_TARGET } _contents )
string ( REPLACE "${CURRENT_INSTALLED_DIR}" "\${_IMPORT_PREFIX}" _contents "${_contents}" )
2021-04-01 03:47:35 +08:00
string ( REGEX REPLACE "\\\${_IMPORT_PREFIX}/bin/([^ \" ;]+ ${ EXECUTABLE_SUFFIX } ) " " \ $ { _ I M P O R T _ P R E F I X } / $ { a r g _ T O O L S _ P A T H } / \ \ 1 " _ c o n t e n t s " $ { _ c o n t e n t s } " )
2017-11-26 07:25:18 +08:00
string ( REPLACE "\${_IMPORT_PREFIX}/lib" "\${_IMPORT_PREFIX}/debug/lib" _contents "${_contents}" )
string ( REPLACE "\${_IMPORT_PREFIX}/bin" "\${_IMPORT_PREFIX}/debug/bin" _contents "${_contents}" )
2018-03-25 04:08:06 +08:00
file ( WRITE ${ RELEASE_SHARE } / ${ DEBUG_TARGET_REL } "${_contents}" )
2017-11-26 07:25:18 +08:00
file ( REMOVE ${ DEBUG_TARGET } )
endforeach ( )
endif ( )
2017-05-03 11:34:11 +08:00
2021-03-01 05:17:19 +08:00
#Fix ${_IMPORT_PREFIX} in cmake generated targets and configs;
2019-03-01 15:53:42 +08:00
#Since those can be renamed we have to check in every *.cmake
file ( GLOB_RECURSE MAIN_CMAKES "${RELEASE_SHARE}/*.cmake" )
2020-11-26 04:06:25 +08:00
2019-03-01 15:53:42 +08:00
foreach ( MAIN_CMAKE IN LISTS MAIN_CMAKES )
file ( READ ${ MAIN_CMAKE } _contents )
2020-11-26 04:06:25 +08:00
#This correction is not correct for all cases. To make it correct for all cases it needs to consider
2021-03-01 05:17:19 +08:00
#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
2020-11-26 04:06:25 +08:00
#this requirement is only true for the *Config.cmake. The targets are not required to be in the same
#folder as the *Config.cmake!
2021-03-01 05:17:19 +08:00
if ( NOT arg_NO_PREFIX_CORRECTION )
2020-11-26 04:06:25 +08:00
string ( REGEX REPLACE
" g e t _ f i l e n a m e _ c o m p o n e n t \ \ ( _ I M P O R T _ P R E F I X \ " \ \ \ $ { C M A K E _ C U R R E N T _ L I S T _ F I L E } \ " P A T H \ \ ) ( \ n g e t _ f i l e n a m e _ c o m p o n e n t \ \ ( _ I M P O R T _ P R E F I X \ " \ \ \ $ { _ I M P O R T _ P R E F I X } \ " P A T H \ \ ) ) * "
" 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 ) "
_ c o n t e n t s " $ { _ c o n t e n t s } " ) # see #1044 for details why this replacement is necessary. See #4782 why it must be a regex.
string ( REGEX REPLACE
" g e t _ f i l e n a m e _ c o m p o n e n t \ \ ( P A C K A G E _ P R E F I X _ D I R \ " \ \ \ $ { C M A K E _ C U R R E N T _ L I S T _ D I R } / \ \ . \ \ . / ( \ \ . \ \ . / ) * \ " A B S O L U T E \ \ ) "
" get_filename_component ( PACKAGE_PREFIX_DIR \"\${CMAKE_CURRENT_LIST_DIR}/../../\" ABSOLUTE ) "
_ c o n t e n t s " $ { _ c o n t e n t s } " )
string ( REGEX REPLACE
" g e t _ f i l e n a m e _ c o m p o n e n t \ \ ( P A C K A G E _ P R E F I X _ D I R \ " \ \ \ $ { C M A K E _ C U R R E N T _ L I S T _ D I R } / \ \ . \ \ . ( ( \ \ \ \ | / ) \ \ . \ \ . ) * \ " A B S O L U T E \ \ ) "
" get_filename_component ( PACKAGE_PREFIX_DIR \"\${CMAKE_CURRENT_LIST_DIR}/../../\" ABSOLUTE ) "
_ c o n t e n t s " $ { _ c o n t e n t s } " ) # This is a meson-related workaround, see https://github.com/mesonbuild/meson/issues/6955
endif ( )
2019-03-01 15:53:42 +08:00
#Fix wrongly absolute paths to install dir with the correct dir using ${_IMPORT_PREFIX}
2020-11-26 04:06:25 +08:00
#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
2021-03-01 05:17:19 +08:00
#least >:${CURRENT_INSTALLED_DIR}.
2020-11-26 04:06:25 +08:00
#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
2019-03-01 15:53:42 +08:00
string ( REPLACE "${CURRENT_INSTALLED_DIR}" [[ ${ _IMPORT_PREFIX } ]] _contents "${_contents}" )
file ( WRITE ${ MAIN_CMAKE } "${_contents}" )
2017-06-21 06:20:23 +08:00
endforeach ( )
2018-01-04 12:11:35 +08:00
# Remove /debug/<target_path>/ if it's empty.
2017-05-03 11:34:11 +08:00
file ( GLOB_RECURSE REMAINING_FILES "${DEBUG_SHARE}/*" )
if ( NOT REMAINING_FILES )
file ( REMOVE_RECURSE ${ DEBUG_SHARE } )
endif ( )
# Remove /debug/share/ if it's empty.
file ( GLOB_RECURSE REMAINING_FILES "${CURRENT_PACKAGES_DIR}/debug/share/*" )
if ( NOT REMAINING_FILES )
file ( REMOVE_RECURSE ${ CURRENT_PACKAGES_DIR } /debug/share )
endif ( )
2018-03-12 14:04:52 +08:00
# Patch out any remaining absolute references
file ( TO_CMAKE_PATH "${CURRENT_PACKAGES_DIR}" CMAKE_CURRENT_PACKAGES_DIR )
file ( GLOB CMAKE_FILES ${ RELEASE_SHARE } /*.cmake )
foreach ( CMAKE_FILE IN LISTS CMAKE_FILES )
file ( READ ${ CMAKE_FILE } _contents )
string ( REPLACE "${CMAKE_CURRENT_PACKAGES_DIR}" "\${CMAKE_CURRENT_LIST_DIR}/../.." _contents "${_contents}" )
file ( WRITE ${ CMAKE_FILE } "${_contents}" )
endforeach ( )
2017-05-03 11:34:11 +08:00
endfunction ( )
2021-08-21 05:46:02 +08:00