CMake Tip: Find the path to Visual Studio

December 28, 2010 No Comments by Richard

Finding ATL headers

I needed to find the ATL headers that should be used with the currently selected generator in my CMake project (FireBreath). The reason for this is that if the user is running Visual Studio Express edition, the ATL headers won’t be there so we need to look for the Windows DDK (Driver Development Kit) and use the headers from that.  If that isn’t there, we should throw an error.

Turns out that finding the path to the VC directory isn’t really that hard, but you have to know how =]  I found the registry paths (as well as how to use them in CMake) in the CMake source code.  Here is the snippet in hopes it will help someone:

if (MSVC10)
    GET_FILENAME_COMPONENT(VS_DIR [HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\10.0\\Setup\\VS;ProductDir] REALPATH CACHE)
elseif (MSVC90)
    GET_FILENAME_COMPONENT(VS_DIR [HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\9.0\\Setup\\VS;ProductDir] REALPATH CACHE)
elseif (MSVC80)
    GET_FILENAME_COMPONENT(VS_DIR [HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\Setup\\VS;ProductDir] REALPATH CACHE)
endif()

This should work on Visual Studio 2005, 2008, and 2010. Other versions could be added if you care. FireBreath only supports 2005 on, so I didn’t bother. (Might actually work on older versions, but I haven’t tried). The path to the Visual Studio directory should end up in VS_DIR.