2

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"?

chw21
  • 7,970
  • 1
  • 16
  • 31

2 Answers2

4

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.

rocambille
  • 15,398
  • 12
  • 50
  • 68
  • 1
    Comming soon is a little bit optimistic. ruslo announced on the mailing list, that he wants to implement this feature in May 2016. It has not yet appeared in CMake master. – usr1234567 Sep 13 '16 at 11:14
1

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?

usr1234567
  • 21,601
  • 16
  • 108
  • 128
  • I know that all offer a switch **like** `-Wall` -- I was just hoping that there would be a simpler way to invoke it. Just between `GNU` and `Intel` and `Unix` and `Win32` there are 4 different versions of `-Wall`. – chw21 Sep 13 '16 at 06:28