Change the default value for CMAKE_CXX_FLAGS_DEBUG and friends in CMake

I would like to change the default values ​​for CMAKE_CXX_FLAGS_RELEASE or CMAKE_CXX_FLAGS_DEBUG in CMake. Basically, I have some default options that are slightly different from CMake (for example, for release), and I don’t need to ask myself: “Oh, does -O3 or our -O2 make them priority when adding using add_compile_options”.

Now I know how to set these values, but I don’t know how to make them editable by users in two usual ways: using - DCMAKE_CXX_FLAGS_DEBUG=yourflags on the command line or setting it using ccmake or CMakeSetup.

The problem is that CMAKE sets and caches its own default values ​​for them, and if you try to overwrite variables without using FORCE, the default values ​​will never be changed. If I use FORCE in my set: set(CMAKE_CXX_FLAGS_DEBUG blah CACHE STRING "" FORCE) , it will overwrite it every time the script is run, which excludes the possibility for the user to change it if he wishes.

I managed to hack it to work with CCMAKE by doing the following, but it still does not work with cmake -DCMAKE_CXX_FLAGS_DEBUG as it overwrites the user change AFTER he did it:

 set(DEFAULTS_SET FALSE CACHE BOOL "") set(CMAKE_CXX_FLAGS_DEBUG "-this -that" CACHE STRING "" FORCE) set(DEFAULTS_SET TRUE CACHE BOOL "" FORCE) 

Obviously, this is an unpleasant hack and does not fully work (in the case of cmake -Dwhatever = thisorthat). I could add other types of assembly, but I really don't understand why this is necessary to change a few simple things.

Edit March 1, 2015:

I created a solution that works, although I'm still not impressed with what I need to do. I saw other comments that solve the problem of setting CMAKE_CXX_FLAGS_DEBUG and friends without crossing them, but this initially did not work for me because I also tried to select them based on the compiler used. However, the compiler is not defined until it already populates the variables for me. The trick I used is as follows. Before the project team, you must set the flags variables in front of something special.

 set(CMAKE_CXX_FLAGS_DEBUG "_UNSET" CACHE STRING "") project(your_project C CXX) if(${CMAKE_CXX_FLAGS_DEBUG} STREQUAL "_UNSET") # Do some compiler switching here and then set your flags with FORCE. set(CMAKE_CXX_FLAGS_DEBUG "-ggdb3 -O0" CACHE STRING "" FORCE) endif() 

Now this allows me to select default values ​​that are completely overridden via the command line with -D or in cmake-gui.

+4
c ++ compiler-optimization cmake
Feb 26 '15 at
source share
1 answer

I just wanted to add four features that I see:

  • Having your own toolchain files containing presets for each of your compilers that you support, for example:

    GNUToolchain.cmake

     set(CMAKE_CXX_FLAGS_DEBUG "-ggdb3 -O0" CACHE STRING "") 

    And then use it with

     cmake -DCMAKE_TOOLCHAIN_FILE:string=GNUToolchain.cmake ... 
  • You can try to determine the compiler by checking CMAKE_GENERATOR (which is valid before project() ):

    CMakeLists.txt

     if("${CMAKE_GENERATOR}" MATCHES "Makefiles" OR ("${CMAKE_GENERATOR}" MATCHES "Ninja" AND NOT WIN32)) set(CMAKE_CXX_FLAGS_DEBUG "-ggdb3 -O0" CACHE STRING "") endif() project(your_project C CXX) 
  • You can use CMAKE_USER_MAKE_RULES_OVERRIDE to give the script your own values ..._INIT :

    It loads after the built-in CMakes compilers and platform information modules have been loaded, but before using the information. The file can set platform information variables to override the default values ​​for CMakes.

    MyInitFlags.cmake

     # Overwrite the init values choosen by CMake if (CMAKE_COMPILER_IS_GNUCXX) set(CMAKE_CXX_FLAGS_DEBUG_INIT "-ggdb3 -O0") endif() 

    CMakeLists.txt

     set(CMAKE_USER_MAKE_RULES_OVERRIDE "MyInitFlags.cmake") project(your_project C CXX) 
  • You can simplify your decision from March 1 by checking the options ..._INIT of the compiler flag variables:

    CMakeLists.txt

     project(your_project C CXX) if (DEFINED CMAKE_CXX_FLAGS_DEBUG_INIT AND "${CMAKE_CXX_FLAGS_DEBUG_INIT}" STREQUAL "${CMAKE_CXX_FLAGS_DEBUG}") # Overwrite the init values choosen by CMake if (CMAKE_COMPILER_IS_GNUCXX) set(CMAKE_CXX_FLAGS_DEBUG "-ggdb3 -O0" CACHE STRING "" FORCE) endif() endif() 

Comments:

I prefer and use the toolchain option. But I admit that it has the disadvantage of having to manually provide the toolchain file (unless you invoke cmake through the script / batch file).

Literature:

  • CMake: in what order are the files processed (Cache, Toolchain, ...)?
  • cmake - setting the global linker flag (for all purposes in the directory)
  • Switch between GCC and Clang / LLVM with CMake
+3
Oct 05 '15 at 19:52
source share



All Articles