git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFH] CMake: detect if being run via Visual Studio, independent of build generator?

From
Philip Oakley <philipoakley@iee.email>
Date
May 29, 2021, 20:31 UTC
Message-ID
<7ac2c0f4-e8ed-5676-1f81-3446e33def9c@iee.email>
In-Reply-To
<CAKiG+9U70wXm7MtTLMUpPC_aHMp58bTtJBbP=NgoAcQQmCPSuQ@mail.gmail.com>
On 29/05/2021 19:33, Sibi Siddharthan wrote:
Show 18 quoted lines
> On Sat, May 29, 2021 at 9:55 PM Philip Oakley <philipoakley@iee.email> wrote:
>> 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.

Ahh, so it (MSVC) would be unset at that point no matter what at that early point in the code, yes?

Show 5 quoted lines
>
> 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})

I'd seen this one recommended on a few StackOverflow answers but it no longer works (for a new install of Visual Studio) because CMAKE_GENERATOR is now set to "Ninja" as default (sigh).

Simply dropping the MSVC test may be one option - we are already guarded by the earlier WIN32 test so were aren't on another OS, though I expect there could be some who want to not use VS, and already have options..

> and
> add CMakeSettings.json to force Visual Studio to use MSBuild.

I was trying to avoid requiring VS users do any extra set up steps. Too many steps often puts off new users, and forcing a change could be annoying for established users - hence the caution.

> Please see https://docs.microsoft.com/en-us/cpp/build/cmakesettings-reference?view=msvc-160
I'll have a look.
Thanks.
Previous: Sibi SiddharthanNext: Sibi Siddharthan
Message 5 of 14 in “[RFH] CMake: detect if being run via Visual Studio, independent of build generator?”
  1. Philip OakleyMay 29, 2021
  2. Matt RogersMay 29, 2021
  3. Philip OakleyMay 29, 2021
  4. Sibi SiddharthanMay 29, 2021
  5. Philip OakleyMay 29, 2021
  6. Sibi SiddharthanMay 29, 2021
  7. Matt RogersMay 30, 2021
  8. Philip OakleyMay 30, 2021
  9. Matt RogersMay 30, 2021
  10. Sibi SiddharthanMay 30, 2021
  11. Philip OakleyMay 30, 2021
  12. Philip OakleyMay 30, 2021
  13. Matt RogersMay 31, 2021
  14. Philip OakleyMay 31, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.