Re: [RFH] CMake: detect if being run via Visual Studio, independent of build generator?
- From
Sibi Siddharthan <sibisiddharthan.github@gmail.com>
- Date
- May 29, 2021, 18:33 UTC
- Message-ID
- <CAKiG+9U70wXm7MtTLMUpPC_aHMp58bTtJBbP=NgoAcQQmCPSuQ@mail.gmail.com>
- In-Reply-To
- <7aadc622-ad4f-1d7e-a956-57ab74f18096@iee.email>
On Sat, May 29, 2021 at 9:55 PM Philip Oakley <philipoakley@iee.email> wrote:
Show 17 quoted lines
>
> On 29/05/2021 16:49, Matt Rogers wrote:
> > I have some experience at my job with CMake, but some quick testing
> > has found that adding a check like:
> >
> > message("MSVC = ${MSVC} , WIN32 = ${WIN32})
> >
> > shows that the MSVC is uninitialized and WIN322 is initialized. so the
> > issue is that the MSVC variable isn't being set which is causing
> > vcpkg_install.bat to not run, rather than the WIN32 variable.
>
> Thanks for confirming what I'm seeing. It's good to have.
> >
> > The msvc variable is intended to be set whenever the compiler is a Visual C/C++
> > compiler [1]. And it seems like visual studio should be setting that itself
> > either via a toolchain or some other mechanism.
>CMake sets this variable. Please see {CMAKE_INSTALLATION}/share/cmake-<version>/modules/Platform/Windows-MSVC.cmake. This happens after CMake is required to find a compiler. This happens in line:93 where we enable the C language.
To fix this I would suggest to change line:53
- if(MSVC AND NOT EXISTS ${VCPKG_DIR})
+ if(CMAKE_GENERATOR MATCHES "Visual Studio" AND NOT EXISTS ${VCPKG_DIR})and add CMakeSettings.json to force Visual Studio to use MSBuild. Please see https://docs.microsoft.com/en-us/cpp/build/cmakesettings-reference?view=msvc-160
Thank You, Sibi Siddharthan