# How to know the generator previously used ?

**URL:** <https://discourse.cmake.org/t/how-to-know-the-generator-previously-used/12748>\
**Category:** Code\
**Created:** [October 10, 2024, 11:28am UTC](https://discourse.cmake.org/t/how-to-know-the-generator-previously-used/12748 "2024-10-10T11:28:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)\
**Post date:** [October 10, 2024, 11:28am UTC](https://discourse.cmake.org/t/how-to-know-the-generator-previously-used/12748/1 "2024-10-10T11:28:44Z")

</div>

**CMake version: 3.29.3**

Hello,

I develop on Windows, and use a batch file to tun the build. This script calls the CMake command :

```batch
REM positionning in the CMake bin directory
cd "%BIN_DIR%"

REM remove the previous CMake items (platform/mode may have changed)
if exist CMakeFiles rmdir /s /q CMakeFiles
if exist CMakeCache.txt del CMakeCache.txt

cmake^
 -S "<current_directory"^
 -B .^
 -D ...
 -D PLATFORM=%PLATFORM%^
 -D MODE=%MODE%
 -D VS_GENERATOR=%VS_GENERATOR%^
 -D ...

```

Note I gave to the batch script an argument about the generator (VS2017, VS2019, VS2022).

In the current directory, there is a CMakeLists.txt which calls `ExternalProject_Add()` :

```auto
ExternalProject_Add(...
      SOURCE_DIR ...
      BINARY_DIR ...
      DOWNLOAD_COMMAND ""
      CMAKE_GENERATOR ${cmake_generator_param}
      CMAKE_GENERATOR_PLATFORM ${cmake_generator_platform_param}
      CMAKE_ARGS
          -D...
          -D...
          -D...

```

`cmake_generator_param` is a correct value deduced from the argument given to the batch.

All the code is used to build several projects, with potentially differents versions of Visual Studio and architecture. That’s why I delete the cache at the beginning of the batch. _But maybe I’m wrong._

With CMake 3.29.3, when I switch among Visual Studio generators, I get the message :

```batch
Does not match the generator used previously: Visual Studio 17 2022
Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.

```

I so should add the `--fresh` option when I switch version. _But my script has to know when it needs to apply this option_ (I need some automatization for continuous integration).

I think I have two solutions :

- Saving a cache value in the upper CMake execution, but so I can’t delete the cache at the beginning of the batch script. What could the side effects. The cache deletion instructions are legacy, but I guess the comment is obsolete, because we hard set the platform and mode in the CMake command.
- **Request a cache value for the lower CMake execution.** But how to do that ?

---

<div class="post-metadata">

**Author:** ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)\
**Post date:** [October 10, 2024, 8:03pm UTC](https://discourse.cmake.org/t/how-to-know-the-generator-previously-used/12748/2 "2024-10-10T20:03:04Z")

</div>

Can you parse CMakeCache.txt for `CMAKE_GENERATOR:INTERNAL=Ninja` and similar? This is how I’ve done things like this from Python say.

---

<div class="post-metadata">

**Author:** ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)\
**Post date:** [October 10, 2024, 8:23pm UTC](https://discourse.cmake.org/t/how-to-know-the-generator-previously-used/12748/3 "2024-10-10T20:23:01Z")

</div>

I thought it would be possible to do `cmake -Bbuild -LA | grep CMAKE_GENERATOR` but that one variable is omitted from the output…

---

<div class="post-metadata">

**Author:** ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)\
**Post date:** [October 11, 2024, 6:58am UTC](https://discourse.cmake.org/t/how-to-know-the-generator-previously-used/12748/5 "2024-10-11T06:58:18Z")

</div>

> [@scivision](#):
>
> I thought it would be possible to do `cmake -Bbuild -LA | grep CMAKE_GENERATOR` but that one variable is omitted from the output…

No, I can’t use Ninja.

---

<div class="post-metadata">

**Author:** ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)\
**Post date:** [October 11, 2024, 6:59am UTC](https://discourse.cmake.org/t/how-to-know-the-generator-previously-used/12748/6 "2024-10-11T06:59:14Z")

</div>

> [@scivision](#):
>
> I thought it would be possible to do `cmake -Bbuild -LA | grep CMAKE_GENERATOR` but that one variable is omitted from the output…

`cmake -L` is only for variables set with the option `-D` (from what I understand from the documentation).

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [October 11, 2024, 7:05am UTC](https://discourse.cmake.org/t/how-to-know-the-generator-previously-used/12748/7 "2024-10-11T07:05:23Z")

</div>

Looks like parsing the `CMakeCache.txt` to extract `CMAKE_GENERATOR` value is the only way, as it was mentioned in the [first reply](https://discourse.cmake.org/t/how-to-know-the-generator-previously-used/12748/2). Don’t get me wrong, your question seems to be valid to me, but I can’t think of any other way.

What you could also do is set [CMAKE\_GENERATOR](https://cmake.org/cmake/help/latest/envvar/CMAKE_GENERATOR.html#envvar:CMAKE_GENERATOR) environment variable - that way all the projects in the system will have that generator as the default one, unless a different generator is provided with `-G` or with this `${cmake_generator_param}` thing from your example.

---

<div class="post-metadata">

**Author:** ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)\
**Post date:** [October 11, 2024, 7:17am UTC](https://discourse.cmake.org/t/how-to-know-the-generator-previously-used/12748/8 "2024-10-11T07:17:36Z")

</div>

> [@retif](#):
>
> Looks like parsing the `CMakeCache.txt` to extract `CMAKE_GENERATOR` value is the only way, as it was mentioned in the [first reply](https://discourse.cmake.org/t/how-to-know-the-generator-previously-used/12748/2). Don’t get me wrong, your question seems to be valid to me, but I can’t think of any other way.

Actually, I am working on this solution.  
I coded in a new .cmake file a function to retrieve the value, but I can’t manage to include it: it can’t be found, although it is in caller’s directory! Note the caller is also an included .cmake…  
I don’t know if it possible to get the path of an included cmake file from inside this file, without relying to waht was given as path to include it. It seems that `CMAKE_CURRENT_LIST_DIR` is supposed to return this value, but i doesn’t. I’m going to ask an other question…

---

<div class="post-metadata">

**Author:** ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)\
**Post date:** [October 11, 2024, 2:52pm UTC](https://discourse.cmake.org/t/how-to-know-the-generator-previously-used/12748/9 "2024-10-11T14:52:42Z")

</div>

Try making a script “detect\_gen.cmake”:

```cmake
cmake_minimum_required(VERSION 3.21)

file(REAL_PATH ${bindir} bindir EXPAND_TILDE)

file(READ "${bindir}/CMakeCache.txt" _cache)

if(_cache MATCHES "CMAKE_GENERATOR:INTERNAL=([^ \n]+)")
  set(CMAKE_GENERATOR ${CMAKE_MATCH_1})
  message(STATUS "Detected CMake generator: ${CMAKE_GENERATOR}")
else()
  message(FATAL_ERROR "Failed to detect CMake generator")
endif()

```

This script is used like:

```sh
cmake -Dbindir=/path/to/build -P detect_gen.cmake

```

[reference](http://scivision.dev/cmake-detect-generator-cache/)

---

<div class="post-metadata">

**Author:** ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)\
**Post date:** [October 16, 2024, 9:13am UTC](https://discourse.cmake.org/t/how-to-know-the-generator-previously-used/12748/10 "2024-10-16T09:13:31Z")

</div>

I made a similar function, but as I didn’t find the optimal regular expression, my function is longer !  
I’ll try yours.

Thanks!
