vcpkg/ports/vcpkg-qmake/z_vcpkg_qmake_fix_makefiles.cmake
Kai Pastor 47633daa65
[vcpkg-scripts] Catch use of ambiguous cmake vars (#34546)
Avoid wrong use of popular but ambiguous variables (`WIN32` etc.) in
scripts and portfiles by detection in CI.

Sometimes the variables are used wrongly, and sometimes this isn't
caught in PR review. This PR tries to catch those variables (when in the
active code path in script mode). [This can happen to every
contributor](https://github.com/microsoft/vcpkg/pull/34356#discussion_r1360074122),
even if he/she knows the rules ... vcpkg is raising the bar higher than
usual, having to deal with targets and hosts even in script mode.

(`b2-options.cmake` (boost fragment) shows where we get if we don't pay
attention: the same code being used in script mode (`WIN32` meaning
host) and in project mode (`WIN32` meaning target).)

The new check doesn't break any user builds because it relies on command
line options. However it changes the ABI hashes.
2024-06-14 11:40:01 -07:00

28 lines
1.2 KiB
CMake

include_guard(GLOBAL)
function(z_vcpkg_qmake_fix_makefiles BUILD_DIR)
#Fix the installation location
file(TO_NATIVE_PATH "${CURRENT_INSTALLED_DIR}" NATIVE_INSTALLED_DIR)
file(TO_NATIVE_PATH "${CURRENT_PACKAGES_DIR}" NATIVE_PACKAGES_DIR)
if(CMAKE_HOST_WIN32)
string(SUBSTRING "${NATIVE_INSTALLED_DIR}" 2 -1 INSTALLED_DIR_WITHOUT_DRIVE)
string(SUBSTRING "${NATIVE_PACKAGES_DIR}" 2 -1 PACKAGES_DIR_WITHOUT_DRIVE)
string(SUBSTRING "${NATIVE_INSTALLED_DIR}" 0 2 INSTALLED_DRIVE)
string(SUBSTRING "${NATIVE_PACKAGES_DIR}" 0 2 PACKAGES_DRIVE)
else()
set(INSTALLED_DRIVE "")
set(PACKAGES_DRIVE "")
set(INSTALLED_DIR_WITHOUT_DRIVE "${NATIVE_INSTALLED_DIR}")
set(PACKAGES_DIR_WITHOUT_DRIVE "${NATIVE_PACKAGES_DIR}")
endif()
file(GLOB_RECURSE MAKEFILES "${BUILD_DIR}/**Makefile**")
foreach(MAKEFILE ${MAKEFILES})
#Set the correct install directory to packages
vcpkg_replace_string("${MAKEFILE}"
"${INSTALLED_DRIVE}$(INSTALL_ROOT)${INSTALLED_DIR_WITHOUT_DRIVE}"
"${PACKAGES_DRIVE}$(INSTALL_ROOT)${PACKAGES_DIR_WITHOUT_DRIVE}")
endforeach()
endfunction()