Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to determine generator for cmake project

Tags:

cmake

I'm in a cmake build directory and want to build the project, but don't know if cmake was run with -G Unix\ Makefile or -G Ninja.

Now I know I can just be ignorant of that and use cmake --build ., but when I want to provide additional options cmake --build . -- SOMETHING I should know if I should provide gnumake or ninja options.

As possible solutions I found that I can just check the presence of a Makefile or build.ninja file. Or grep in the CMakeCache.txt for CMAKE_GENERATOR.

EDIT:

In a similar question here it is explained that values passed to cmake with -D can be queried with cmake -LA -N. But this doesn't list the -G parameter.

I am wondering if there is a more proper way (just like cmake -LA -N instead of grepping for variable values).

like image 589
pseyfert Avatar asked Sep 10 '16 16:09

pseyfert


1 Answers

You've cited two possible solutions:

  • Check presence of build system files. Not a general solution, as it only works for some generators, but it may work in your case. This would not work for Visual Studio (for example, unless you parse the Visual Studio files themselves), as each generator version creates the same filenames.

  • Grep CMakeCache.txt for CMAKE_GENERATOR. Relies on platform specific tools (grep), and the location of the CMakeCache.txt. Both likely not a problem in most situations.

A slight modification to the second option, which makes it more portable, is to cache the CMAKE_GENERATOR in another variable:

set(USED_CMAKE_GENERATOR "${CMAKE_GENERATOR}" CACHE STRING "Expose CMAKE_GENERATOR" FORCE)

Then, when you use cmake -L, USED_CMAKE_GENERATOR will show up.

like image 77
MuertoExcobito Avatar answered Oct 22 '22 06:10

MuertoExcobito