adding gdal as dependency on windows for msys2 compilers and msvc

I tried that:

if(NOT(DEFINED ENV{GDAL_DIR}))
	# On windows with 2 installs: an msvc-compiled and a gcc-compiled one.
	if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
		# MSVC
		if(DEFINED ENV{GDAL_DIR_msvc})
			set(ENV{GDAL_DIR} $ENV{GDAL_DIR_msvc})
		endif() 
	elseif(DEFINED ENV{GDAL_DIR_msys2})
		# gcc with msys2
		set(ENV{GDAL_DIR} $ENV{GDAL_DIR_msys2})
	else()
		message(WARNING "GDAL NOT FOUND OR NOT PROPERLY INSTALL")
	endif()
endif()

And I set two user environment variables:

GDAL_DIR_msvc=C:/OSGeo4W/include
GDAL_DIR_msys2=C:/Users/<userlogin>/<pathtomsys2>/ucrt64/lib/cmake/gdal

GDAL_DIR_msvc is pointing to the directory containing gdal.h.

Yet, when using msvc, $ENV{GDAL_DIR} is C:/OSGeo4W/include as expected but msys2 is still detected thus compilation fails due to ABI discrepancies.

I don’t understand how find_package could find again the msys2 version.

[EDIT] looking in debug mode, the issue is that find_package is looking for a gdal-config.cmake. In C:/OSGeo4W/ and its sub-directories there are no such files but FindGDAL module should be able to pick gdal.h in the includes folder. Yet, I’ve got C:/Users/<userlogin>/<pathtomsys2>/ucrt64/lib in my user Path, and find_package starts by looking inside Path and finds a gdal-config.cmake.

How can I prevent, only for this library, when using MSVC find_package to look for gdal there. The solution must not change find_package behavior for other dependencies. For instance, I would have hope that having set GDAL_ENV or GDAL_ROOT would have give priority to this directory whatever the mode used (config or module). Is there a Package/CMake variable to tell wich mode to use?