views:

283

answers:

2

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).

A: 

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.

Eamon Nerbonne
+1  A: 

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.

Milliams
There are several problems with that approach, however. First of all, it requires multiple invocations to cmake
Eamon Nerbonne
Another disadvantage: a secondary use-case I had in mind was multiple builds for different CPU's, and then selecting the best one at run time - and then there really is a need for the final result to depend on multiple builds of the same sub-project.
Eamon Nerbonne
Anyhow, that's why I chose the other solution - but yours is definitely more canonical and works (with the aforementioned drawbacks) more generally - thanks for contributing!
Eamon Nerbonne