macosx Darwin-Initialize.cmake leaks variables
A simple CMakeLists.txt on osx shows that variable "d" is set (but empty) by the system modules. this pollutes the user's variable namespace. Noticed it when I created a library target "d" and got unexpected policy CMP0054 warnings when trying to find_package(d CONFIG REQUIRED) (triggered from the generated "d-targets.cmake" ...)
% cat ../CMakeLists.txt
cmake_minimum_required(VERSION 3.17)
project(p C)
if(DEFINED d)
message("D is defined as ${d} ... but I did not define it")
endif()
% cmake ..
-- The C compiler identification is AppleClang 11.0.0.11000033
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
D is defined as ... but I did not define it
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/yo/b
% cmake --version
cmake version 3.17.3
CMake suite maintained and supported by Kitware (kitware.com/cmake).
%
The offending foreach loop is in Modules/Platform/Darwin-Initialize.cmake around line 66:
foreach(d Platforms/MacOSX.platform/Developer/SDKs SDKs)
file(GLOB _CMAKE_OSX_SDKS ${OSX_DEVELOPER_ROOT}/${d}/*)
if(_CMAKE_OSX_SDKS)
set(_CMAKE_OSX_SDKS_DIR ${OSX_DEVELOPER_ROOT}/${d})
break()
endif()
endforeach()
Maybe this file should prefix all its local variables with "_" (e.g. ${_d} instead of ${d})? A "grep foreach" shows that it does this for _SDK, but not "d" or "ver" "or "v" or "sdk" or "arch" ... what's the best practice here?
% fgrep -i foreach Darwin-Initialize.cmake
foreach(d Platforms/MacOSX.platform/Developer/SDKs SDKs)
endforeach()
foreach(_SDK ${_CMAKE_OSX_SDKS})
endforeach()
foreach(ver ${CMAKE_OSX_DEPLOYMENT_TARGET}
endforeach()
foreach(v CMAKE_OSX_SYSROOT _CMAKE_OSX_SYSROOT_DEFAULT)
endforeach()
foreach(sdk ${_sdks})
endforeach()
foreach(arch ${CMAKE_OSX_ARCHITECTURES})
foreach(sdk ${_sdks})
endforeach()
endforeach()