Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow one to easily configure a build with commonly useful warnings #1740

Merged
merged 2 commits into from Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Expand Up @@ -105,6 +105,9 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
include(pkg-config)
find_package(OpenCV REQUIRED)

# configure flags regarding warnings
include(warnings)

# build and install tools/libraries in sub directories
add_subdirectory(debugviewer)
add_subdirectory(snd2png)
Expand Down
48 changes: 48 additions & 0 deletions cmake/warnings.cmake
@@ -0,0 +1,48 @@
# enable useful warnings and explicitly disable not useful ones and treat warnings them as errors
option(ENABLE_WARNINGS "adds additional compiler flags to enable useful warnings" "${ENABLE_DEVEL_DEFAULTS}")
set(CLANG_WARNINGS
-Wall
-Wextra # reasonable and standard
-Wshadow=local # warn the user if a variable declaration shadows one from a parent context
-Wnon-virtual-dtor # warn the user if a class with virtual functions has a non-virtual destructor. This helps catch hard
# to track down memory errors
-Wold-style-cast # warn for c-style casts
-Wcast-align # warn for potential performance problem casts
-Wunused # warn on anything being unused
-Woverloaded-virtual # warn if you overload (not override) a virtual function
-Wconversion # warn on type conversions that may lose data
-Wsign-conversion # warn on sign conversions
-Wnull-dereference # warn if a null dereference is detected
-Wdouble-promotion # warn if float is implicit promoted to double
-Wformat=2 # warn on security issues around functions that format output (ie printf)
-Wno-pedantic # warn NOT if non-standard C++ is used (some vendor extensions are very useful)
-Wno-missing-field-initializers # warn NOT about missing field initializers
-Wno-useless-cast # warn NOT about useless cases (this is sometimes very useful in templates)
-Wno-unused-const-variable # warn NOT about unused constants (usually used in other compile unit)
-Wno-unknown-warning-option # warn NOT about unknown warning options (depending on compiler/version not all are
# available)
)
set(GCC_WARNINGS
${CLANG_WARNINGS}
-Wmisleading-indentation # warn if indentation implies blocks where blocks do not exist
-Wduplicated-cond # warn if if / else chain has duplicated conditions
-Wduplicated-branches # warn if if / else branches have duplicated code
-Wlogical-op # warn about logical operations being used where bitwise were probably wanted
)
if (ENABLE_WARNINGS)
if (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
list(APPEND PRIVATE_COMPILE_OPTIONS ${CLANG_WARNINGS})
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
list(APPEND PRIVATE_COMPILE_OPTIONS ${GCC_WARNINGS})
else ()
message(AUTHOR_WARNING "Enabling warnings is not supported for compiler '${CMAKE_CXX_COMPILER_ID}'.")
endif ()
endif ()
option(TREAT_WARNINGS_AS_ERRORS "adds additional compiler flag to treat warnings as errors" "${ENABLE_DEVEL_DEFAULTS}")
if (TREAT_WARNINGS_AS_ERRORS)
if (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
list(APPEND PRIVATE_COMPILE_OPTIONS -Werror)
else ()
message(AUTHOR_WARNING "Treating warnings as errors is not supported for compiler '${CMAKE_CXX_COMPILER_ID}'.")
endif ()
endif ()
1 change: 1 addition & 0 deletions debugviewer/CMakeLists.txt
Expand Up @@ -3,4 +3,5 @@ cmake_minimum_required(VERSION 3.3.0)

add_executable(${PROJECT_NAME} ${PROJECT_NAME}.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE opencv_core opencv_imgcodecs opencv_highgui)
target_compile_options(${PROJECT_NAME} PRIVATE ${PRIVATE_COMPILE_OPTIONS})
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
5 changes: 5 additions & 0 deletions ppmclibs/CMakeLists.txt
Expand Up @@ -77,6 +77,11 @@ add_library(tinycv MODULE
target_link_libraries(tinycv PRIVATE opencv_core opencv_imgcodecs)
target_include_directories(tinycv PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}" "${PERL_INCLUDE_DIRECTORY}")
target_compile_definitions(tinycv PRIVATE "-DVERSION=\"1.0\"" "-DXS_VERSION=\"1.0\"" "-D_LARGEFILE_SOURCE" "-D_FILE_OFFSET_BITS=64")
target_compile_options(tinycv PRIVATE ${PRIVATE_COMPILE_OPTIONS})
if (ENABLE_WARNINGS AND (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
# disable certain warnings in the XS file as it is generated by xsubpp (and hence we have no way of fixing these)
set_source_files_properties("${PREPROCESSED_XS_FILE}" PROPERTIES COMPILE_OPTIONS "-Wno-old-style-cast;-Wno-sign-conversion;-Wno-conversion")
endif ()
set_target_properties(tinycv PROPERTIES PREFIX "") # remove lib prefix (library is *not* supposed to be called libtinycv.so)

# install the native library and Perl code
Expand Down
1 change: 1 addition & 0 deletions snd2png/CMakeLists.txt
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.3.0)

add_executable(${PROJECT_NAME} ${PROJECT_NAME}.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE opencv_core opencv_imgcodecs)
target_compile_options(${PROJECT_NAME} PRIVATE ${PRIVATE_COMPILE_OPTIONS})
target_use_pkg_config_module(${PROJECT_NAME} "fftw3")
target_use_pkg_config_module(${PROJECT_NAME} "sndfile")
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")