1

I see a number of articles that suggest you check for compiler and add flags as appropriate, eg.

if (CMAKE_COMPILER_IS_GNUCC) ... endif() if (MSVC) ... endif()

This is a deeply undesirable situation though.

It relies on you having, for every project, to add specific support for each compiler that you support, one at a time.

Other things, like C++11 features and debug flags are automatically generated by cmake for each of the compilers it supports.

Is there no equivalent solution for adding the equivalent of -Wall / /W3 to the compile simply via a cmake setting?

Community
  • 1
  • 1
Doug
  • 32,844
  • 38
  • 166
  • 222

2 Answers2

1

It relies on you having, for every project, to add specific support for each >compiler that you support, one at a time.

At now you can only have something like compiler.cmake, where you configure suitable flags for each compiler, and share compiler.cmake among projects.

Is there no equivalent solution for adding the equivalent of -Wall / /W3 to the >compile simply via a cmake setting?

No, now there is only disscussion about similar feature and it's possible implementation, see

https://cmake.org/pipermail/cmake-developers/2016-March/028107.html

fghj
  • 8,898
  • 4
  • 28
  • 56
1

For anyone else who finds this...

There is a reasonably robust implementation of this which can be found here, as a 3rd party addition:

https://github.com/ruslo/sugar/wiki/Cross-platform-warning-suppression

You use it like this:

## Project
cmake_minimum_required(VERSION 3.1)
project(npp)

# Dependencies
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/npp)
... whatever ...

# Clone entire sugar repo to source folder and import
include(${CMAKE_CURRENT_SOURCE_DIR}/sugar/cmake/Sugar)
include(sugar_generate_warning_flags)

# Generate flags, included excluded flags, etc.
# see: https://github.com/ruslo/leathers/wiki/List
sugar_generate_warning_flags(
  flags
  properties
  ENABLE ALL
  DISABLE c++98-compat padded
  TREAT_AS_ERROR ALL
  CLEAR_GLOBAL)

# Library / executable if any
file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/npp/*.cpp)
add_library(npp STATIC ${SOURCES})

# Set flags
set_target_properties(npp PROPERTIES ${properties} COMPILE_OPTIONS "${flags}")

# Local tests
enable_testing()
add_executable(tests "${CMAKE_CURRENT_SOURCE_DIR}/tests/tests.cpp")

# Set flags
set_target_properties(tests PROPERTIES ${properties} COMPILE_OPTIONS "${flags}")
target_link_libraries(tests npp)
add_test(tests tests)

Obviously this is far from ideal, as it's quite irritating to have to clone a set of modules, but it's practical for the moment.

Doug
  • 32,844
  • 38
  • 166
  • 222