CMake additional Debug Options

2019-07-12 18:36发布

问题:

When compiling code with debug symbols, I usually also want to enable certain compile-time and run-time checks, like so:

gfortran -c -o hello.o -g -O0 -Wall -fcheck-all -fbacktrace hello.f90

(Yes, it's Fortran, but I assume that it'd work with other languages just the same.)

If I wanted to compile the same with the Intel Fortran Compiler, the command would look like this:

ifort -c -o hello.o -g -O0 -warn all -check all -traceback hello.f90

The only way to compile like this in CMake that I have found so far is something like this:

IF(${CMAKE_Fortran_COMPILER_ID} MATCHES "GNU")
    set(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} -Wall -fcheck=all -fbacktrace")
ELSEIF(${CMAKE_Fortran_COMPILER_ID} MATCHES "Intel")
    set(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} -warn all -check all -traceback")
ELSE()
    message(WARNING "Unable to determine Compiler ID: ${CMAKE_Fortran_COMPILER_ID}")
ENDIF(${CMAKE_Fortran_COMPILER_ID} MATCHES "GNU")

But this introduces all the hardcoded flags that I wanted to avoid when starting to use CMake. Is there a way to add compiler switches based on what they do, like "Enable all Compile time Warnings" or "Enable all Runtime Checks"?

回答1:

CMake warning API: coming soon.

The discussion proposes commands like add_compile_warnings or target_compile_warnings, following the model of add_compile_options and target_compile_definitions, with warning levels such as:

  • all (compiler specific "all", e.g. /Wall or -Wall);
  • default;
  • none;
  • everything (all possible warnings for compiler, if there is no such option use maximum level plus some warnings explicitly).

You may study the proposed API and perhaps expose your unsatisfied needs in the discussion.



回答2:

No, CMakes (in the current version 3.6) does not offer such sets of compiler switches. You have to define them by yourself.
Actually, all compilers offer such a switch like Wall. You are not satisfied with the choice of the compiler programmers, how could the CMake programmers do better?



标签: cmake