I do not really know cmake, but I am trying to setup some sort of an option in CMake - a single line that can be commented or uncommented - and depending on its existence, an additional file should be included in the set of source files.
So, based on:
... I came up with following example CMakeFiles.txt:
project(MyApp) option(ADD_EXTRA_FILE TRUE) set(MyApp_sources main.c file1.c file2.c ) if(NOT ADD_EXTRA_FILE) set(MyApp_sources ${MyApp_sources} "extra_file.c") endif(NOT ADD_EXTRA_FILE) message(STATUS "ADD_EXTRA_FILE: ${ADD_EXTRA_FILE}") message(STATUS "MyApp_sources: ${MyApp_sources}") When I run this, I would expect ADD_EXTRA_FILE to be TRUE, and therefore if(NOT ADD_EXTRA_FILE) should evaluate to FALSE, and therefore the extra file should not be added; however:
$ cmake . -- ADD_EXTRA_FILE: OFF -- MyApp_sources: main.c;file1.c;file2.c;extra_file.c -- Configuring done -- Generating done -- Build files have been written to: /tmp ... it turns out, ADD_EXTRA_FILE is OFF (?), and apparently if(NOT ADD_EXTRA_FILE) evaluates to TRUE, and the extra file is added anyways.
Is it possible to achieve what I want in Cmake - and if so, how?
target_sourcesfor adding sources to a given target. I'd preferlist(APPEND MyApp_sources extra_file.c)to add a new element to the source list btw, should you decide to keep using it.