Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake: Selecting a generator within CMakeLists.txt

Tags:

cmake

I would like to force CMake to use the "Unix Makefiles" generator from within CMakeLists.txt.

This is the command I use now.

cmake -G "Unix Makefiles" . 

I would like it to be this.

cmake . 

When running on windows with VC installed and a custom tool-chain.

I would expect to be-able to set the generator in the CMakeLists.txt file.

Maybe something like this.

set(CMAKE_GENERATOR "Unix Makefiles") 
like image 423
Ted Avatar asked Jun 29 '12 22:06

Ted


People also ask

How do I choose a CMake generator?

Use its -G option to specify the generator for a new build tree. The cmake-gui(1) offers interactive selection of a generator when creating a new build tree.

How do I change the generator in my CMake gui?

For those seeking the CMake GUI answer. Go to File->Delete Cache and then click Configure again. A scenario where changing the generator is needed is that you are keeping the CMake GUI open and reusing the same directory (source and CMakeList.

How do I use CMakeLists txt?

When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory. To open a project, you can point CLion to the top-level CMakeLists. txt and choose Open as Project.

How do you pass command line arguments in CMake?

If you create the cache variable in the CMakeLists. txt file and then pass the argument via calling cmake, won't the CMakeList. txt file keep overwriting the argument value? No, the cache is populated on the first run with either the default value, or the value supplied on the command line if it is provided.


2 Answers

Here is what worked for me - create a file called PreLoad.cmake in your project dir containing this:

set (CMAKE_GENERATOR "Unix Makefiles" CACHE INTERNAL "" FORCE)

like image 184
Ivan Avatar answered Sep 24 '22 13:09

Ivan


It seems to me that the variable CMAKE_GENERATOR is set too late if set in the CMakeLists.txt. If you use (even at the beginning of CMakeLists.txt)

set(CMAKE_GENERATOR "Ninja") message("generator is set to ${CMAKE_GENERATOR}") 

you can see in the output something like

% cmake ../source -- The C compiler identification is GNU 4.9.2 ... -- Detecting CXX compile features - done generator is set to Ninja -- Configuring done -- Generating done -- Build files have been written to: /tmp/build 

So the variable is only set at the very end of the generation procedure. If you use something like

set(CMAKE_GENERATOR "Ninja" CACHE INTERNAL "" FORCE) 

in CMakeLists.txt, then in the very first run of cmake ../source (without -G) the default generator is used. The variable CMAKE_GENERATOR is stored in the cache, though. So if you rerun cmake ../source afterwards, it will use the generator as specified in the CMAKE_GENERATOR variable in the cache.

This is surely not the most elegant solution, though ;-) Maybe use a batch file that will actually execute the cmake -G generator for the user...

like image 22
Michael Kopp Avatar answered Sep 23 '22 13:09

Michael Kopp