| 1 |
cmake_minimum_required(VERSION 3.17.0 FATAL_ERROR) |
| 2 |
|
| 3 |
# prevent multiple inclusion |
| 4 |
if (DEFINED DEVEL_UTILITIES_LOADED) |
| 5 |
return() |
| 6 |
endif () |
| 7 |
set(DEVEL_UTILITIES_LOADED YES) |
| 8 |
|
| 9 |
# add option to enable defaults useful for development |
| 10 |
option(ENABLE_DEVEL_DEFAULTS "enable build system options useful during development by default" OFF) |
| 11 |
|
| 12 |
# enable EXPORT_COMPILE_COMMANDS target property when ENABLE_DEVEL_DEFAULTS is enabled |
| 13 |
if (CMAKE_EXPORT_COMPILE_COMMANDS OR ENABLE_DEVEL_DEFAULTS) |
| 14 |
set(ENABLE_EXPORT_COMPILE_COMMANDS ON) |
| 15 |
else () |
| 16 |
set(ENABLE_EXPORT_COMPILE_COMMANDS OFF) |
| 17 |
endif () |
| 18 |
|
| 19 |
function (configure_development_warnings) |
| 20 |
# parse arguments |
| 21 |
set(OPTIONAL_ARGS) |
| 22 |
set(ONE_VALUE_ARGS TARGET OUTPUT_VAR APPEND_OUTPUT_VAR) |
| 23 |
set(MULTI_VALUE_ARGS) |
| 24 |
cmake_parse_arguments(ARGS "${OPTIONAL_ARGS}" "${ONE_VALUE_ARGS}" "${MULTI_VALUE_ARGS}" ${ARGN}) |
| 25 |
|
| 26 |
# enable useful warnings and explicitly disable not useful ones and treat warnings as errors |
| 27 |
option(ENABLE_WARNINGS "adds additional compiler flags to enable useful warnings" "${ENABLE_DEVEL_DEFAULTS}") |
| 28 |
set(CLANG_WARNINGS |
| 29 |
-Wall |
| 30 |
-Wextra # reasonable and standard |
| 31 |
-Wshadow=local # warn the user if a variable declaration shadows one from a parent context |
| 32 |
-Wnon-virtual-dtor # warn the user if a class with virtual functions has a non-virtual destructor. This helps catch |
| 33 |
# hard to track down memory errors |
| 34 |
-Wold-style-cast # warn for c-style casts |
| 35 |
-Wcast-align # warn for potential performance problem casts |
| 36 |
-Wunused # warn on anything being unused |
| 37 |
-Woverloaded-virtual # warn if you overload (not override) a virtual function |
| 38 |
-Wconversion # warn on type conversions that may lose data |
| 39 |
-Wsign-conversion # warn on sign conversions |
| 40 |
-Wnull-dereference # warn if a null dereference is detected |
| 41 |
-Wdouble-promotion # warn if float is implicit promoted to double |
| 42 |
-Wformat=2 # warn on security issues around functions that format output (e.g. printf) |
| 43 |
-Wno-pedantic # warn NOT if non-standard C++ is used (some vendor extensions are very useful) |
| 44 |
-Wno-missing-field-initializers # warn NOT about missing field initializers |
| 45 |
-Wno-useless-cast # warn NOT about useless casts (those are sometimes very useful in templates) |
| 46 |
-Wno-unused-const-variable # warn NOT about unused constants (usually used in other compile unit) |
| 47 |
-Wno-unknown-warning-option # warn NOT about unknown warning options (depending on compiler/version not all are |
| 48 |
# available) |
| 49 |
) |
| 50 |
set(GCC_WARNINGS |
| 51 |
${CLANG_WARNINGS} |
| 52 |
-Wmisleading-indentation # warn if indentation implies blocks where blocks do not exist |
| 53 |
-Wduplicated-cond # warn if if / else chain has duplicated conditions |
| 54 |
-Wduplicated-branches # warn if if / else branches have duplicated code |
| 55 |
-Wlogical-op # warn about logical operations being used where bitwise were probably wanted |
| 56 |
) |
| 57 |
if (ENABLE_WARNINGS) |
| 58 |
if (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") |
| 59 |
list(APPEND COMPILE_OPTIONS_TO_CONFIGURE ${CLANG_WARNINGS}) |
| 60 |
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| 61 |
list(APPEND COMPILE_OPTIONS_TO_CONFIGURE ${GCC_WARNINGS}) |
| 62 |
else () |
| 63 |
message(AUTHOR_WARNING "Enabling warnings is not supported for compiler '${CMAKE_CXX_COMPILER_ID}'.") |
| 64 |
endif () |
| 65 |
endif () |
| 66 |
option(TREAT_WARNINGS_AS_ERRORS "adds additional compiler flag to treat warnings as errors" "${ENABLE_DEVEL_DEFAULTS}") |
| 67 |
if (TREAT_WARNINGS_AS_ERRORS) |
| 68 |
if (DEFINED META_TREAT_WARNINGS_AS_ERRORS AND NOT META_TREAT_WARNINGS_AS_ERRORS) |
| 69 |
message(AUTHOR_WARNING "TREAT_WARNINGS_AS_ERRORS is disabled by project ${META_TARGET_NAME}.") |
| 70 |
elseif (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| 71 |
list(APPEND COMPILE_OPTIONS_TO_CONFIGURE -Werror -Wno-error=address) |
| 72 |
else () |
| 73 |
message(AUTHOR_WARNING "Treating warnings as errors is not supported for compiler '${CMAKE_CXX_COMPILER_ID}'.") |
| 74 |
endif () |
| 75 |
endif () |
| 76 |
|
| 77 |
# set compile options / set output variable |
| 78 |
if (ARGS_TARGET) |
| 79 |
target_compile_options("${ARGS_TARGET}" PRIVATE ${COMPILE_OPTIONS_TO_CONFIGURE}) |
| 80 |
endif () |
| 81 |
if (ARGS_OUTPUT_VAR) |
| 82 |
set("${ARGS_OUTPUT_VAR}" |
| 83 |
${COMPILE_OPTIONS_TO_CONFIGURE} |
| 84 |
PARENT_SCOPE) |
| 85 |
endif () |
| 86 |
if (ARGS_APPEND_OUTPUT_VAR) |
| 87 |
set("${ARGS_APPEND_OUTPUT_VAR}" |
| 88 |
${${ARGS_APPEND_OUTPUT_VAR}} ${COMPILE_OPTIONS_TO_CONFIGURE} |
| 89 |
PARENT_SCOPE) |
| 90 |
endif () |
| 91 |
|
| 92 |
endfunction () |