Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake: Working with multiple output configurations

I'm busy porting my build process from msbuild to cmake, to better be able to deal with the gcc toolchain (which generates much faster code for some of the numeric stuff I'm doing).

Now, I'd like cmake to generate several versions of the output, stuff like one version with sse2, another with x64, and so on. However, cmake seems to work most naturally if you simply have a bunch of flags (say, "sse2_enable", and "platform") and then generate one output based on those platforms.

What's the best way to work with multiple output configurations like this? Intuitively, I'd like to iterate over a large number of flag combinations and rerun the same CMakeLists.txt files for each combination - but of course, you can't express that within the CMakeLists.txt files (AFAIK).

like image 961
Eamon Nerbonne Avatar asked Feb 19 '10 15:02

Eamon Nerbonne


2 Answers

The recommended way to do this is to simply have multiple build directories. From each one you simply call cmake with the required settings.

For example you could do, starting in the base source directory (using Linux shell syntax but the idea is the same):

mkdir build-sse2 && cd build-sse2
cmake .. -DENABLE_SSE2 # or whatever to enable it in your CMakeLists.txt
make

cd ..

mkdir build-x64 && cd build-x64
cmake .. -DENABLE_X64 # or whatever again...
make

This way, each build directory is completely separated from each other.

This allows you to have one directory for Debug, another for Release and another for cross-compiling.

like image 153
Milliams Avatar answered Sep 28 '22 11:09

Milliams


There hasn't been much activity here, so I've come up with a workable solution myself. It's probably not ideal, so if you have a better idea, please do add it!

Now, it's hard to iterate over build configs in cmake because cmake's crucial variables don't live in function scope - so, for instance, that means if you do include_directories(X) the X directory will remain in the include list even after the function exits.

Directories do have scope - and while normally each input directory corresponds to one output directory, you can have multiple output directories.

So, my solution looks like this:

project(FooAllConfigs)

set(FooVar 2)
set(FooAnotherVar b)
add_subdirectory("project_dir" "out-2b")
set(FooVar 5)
set(FooAnotherVar c)
add_subdirectory("project_dir" "out-5c")
set(FooVar 3)
set(FooAnotherVar b)
add_subdirectory("project_dir" "out-3b")
set(FooVar 3)
set(FooAnotherVar c)
add_subdirectory("project_dir" "out-3c")

The normal project dir then contains a CMakeLists.txt file with code to set up the appropriate includes and compiler options given the global variables set in the FooAllConfigs project, and it also determines a build suffix that's appended to all build outputs - any even indirectly included output (e.g. as generated by add_executable) must have a unique name.

This works fine for me.

like image 22
Eamon Nerbonne Avatar answered Sep 28 '22 09:09

Eamon Nerbonne