I feel a little stupid right now. After recently converting a few smaller projects to use CMake, I decided to also get rid of a few "Platform_Config.h" files. These files contain a few preprocessing directives like #define USE_NEW_CACHE
and control compilation.
How would I 'convert' these defines to be controlled with CMake? Ideally by using these "cache" variables the user can easily edit.
In case you want to set defines per target: Since 2.8.11 you can use
target_compile_definitions
.In earlier versions you probably don't want to use
set_target_properties
as is, since it overwrites any defines you set previously. Callget_target_property
first instead, then merge with previous values. Seeadd_target_definitions
here.option command might provide what you are looking for.
use it with the COMPILE DEFINITIONS property on the target and i think you are done. To set the property on the target, use the command set target properties
edit:
The option i wrote in the example shows up as a checkbox in the CMake GUI.
There are two options. You can use the add_definitions method to pass defines as compiler flags: E.g. somewhere in your projects cmakelists.txt:
CMake will make sure the -D prefix is converted to the right flag for your compiler (/D for msvc and -D for gcc).
Alternatively, check out configure_file. It is more complex, but may be better suited to your original approach with a Platform_Config file.
You can create an input-file, similar to your original Platform_Config.h and add "#cmakedefine" lines to it.
Let's call in Platform_Config.h.in:
When then running
it will generate a new Platform_Config file in your build-dir. Those variables in cmake which are also a cmakedefine will be present in the generated file, the other ones will be commented out or undefed.
Of course, you should make sure the actual, generated file is then correctly found when including it in your source files.