From f6fc1751e9c2f650b2f0ce434851e8784e659fa9 Mon Sep 17 00:00:00 2001 From: Erik Boasson Date: Wed, 5 Jun 2019 12:13:10 +0200 Subject: [PATCH] Treat warnings as errors in CI builds The CMake files now add "-Werror"/"/WX" if the "WERROR" CMake variable is true. By default it is not; the CI builds set this. Signed-off-by: Erik Boasson --- .travis.yml | 1 + appveyor.yml | 2 +- src/CMakeLists.txt | 37 +++++++++++++++++++++++++++---------- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 70b0781..6738669 100644 --- a/.travis.yml +++ b/.travis.yml @@ -164,6 +164,7 @@ script: -DCMAKE_INSTALL_PREFIX=$(pwd)/install -DUSE_SANITIZER=${ASAN} -DBUILD_TESTING=on + -DWERROR=on -G "${GENERATOR}" ../src - ${SCAN_BUILD} cmake --build . --config ${BUILD_TYPE} --target install - CYCLONEDDS_URI='all' ctest -T test -C ${BUILD_TYPE} diff --git a/appveyor.yml b/appveyor.yml index 0c96ec1..363272b 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -36,7 +36,7 @@ build_script: - mkdir build - cd build - conan install -s arch=%ARCH% -s build_type=%CONFIGURATION% .. - - cmake -DBUILD_TESTING=on -DCMAKE_BUILD_TYPE=%CONFIGURATION% -DCMAKE_INSTALL_PREFIX=%CD%/install -G "%GENERATOR%" ../src + - cmake -DBUILD_TESTING=on -DWERROR=ON -DCMAKE_BUILD_TYPE=%CONFIGURATION% -DCMAKE_INSTALL_PREFIX=%CD%/install -G "%GENERATOR%" ../src - cmake --build . --config %CONFIGURATION% --target install -- /maxcpucount - cd install/share/CycloneDDS/examples/helloworld - mkdir build diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d5f906d..e5beb7a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,6 +22,11 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) "Debug" "Release" "MinSizeRel" "RelWithDebInfo") endif() +# By default don't treat warnings as errors, else anyone building it with a different compiler that +# just happens to generate a warning, as well as anyone adding or modifying something and making a +# small mistake would run into errors. CI builds can be configured differently. +option(WERROR "Treat compiler warnings as errors" OFF) + FUNCTION(PREPEND var prefix) SET(listVar "") FOREACH(f ${ARGN}) @@ -86,18 +91,30 @@ endif() # Set reasonably strict warning options for clang, gcc, msvc # Enable coloured ouput if Ninja is used for building -if(${CMAKE_C_COMPILER_ID} STREQUAL "Clang" OR ${CMAKE_C_COMPILER_ID} STREQUAL "AppleClang") - add_definitions(-Wall -Wextra -Wconversion -Wunused -Wmissing-prototypes) - if(${CMAKE_GENERATOR} STREQUAL "Ninja") - add_definitions(-Xclang -fcolor-diagnostics) +if("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang") + #message(STATUS clang) + add_compile_options(-Wall -Wextra -Wconversion -Wunused -Wmissing-prototypes) + if(${WERROR}) + add_compile_options(-Werror) endif() -elseif(${CMAKE_C_COMPILER_ID} STREQUAL "GNU") - add_definitions(-Wall -Wextra -Wconversion -Wmissing-prototypes) - if(${CMAKE_GENERATOR} STREQUAL "Ninja") - add_definitions(-fdiagnostics-color=always) + if("${CMAKE_GENERATOR}" STREQUAL "Ninja") + add_compile_options(-Xclang -fcolor-diagnostics) + endif() +elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") + #message(STATUS gcc) + add_compile_options(-Wall -Wextra -Wconversion -Wmissing-prototypes) + if(${WERROR}) + add_compile_options(-Werror) + endif() + if("${CMAKE_GENERATOR}" STREQUAL "Ninja") + add_compile_options(-fdiagnostics-color=always) + endif() +elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") + #message(STATUS msvc) + add_compile_options(/W3) + if(${WERROR}) + add_compile_options(/WX) endif() -elseif(${CMAKE_C_COMPILER_ID} STREQUAL "MSVC") - add_definitions(/W3) endif() # I don't know how to enable warnings properly so that it ends up in Xcode projects as well