diff --git a/cmake/GtsamBuildTypes.cmake b/cmake/GtsamBuildTypes.cmake index e76bfafbf3..2088709884 100644 --- a/cmake/GtsamBuildTypes.cmake +++ b/cmake/GtsamBuildTypes.cmake @@ -86,6 +86,13 @@ mark_as_advanced(GTSAM_COMPILE_DEFINITIONS_PRIVATE_RELEASE) mark_as_advanced(GTSAM_COMPILE_DEFINITIONS_PRIVATE_PROFILING) mark_as_advanced(GTSAM_COMPILE_DEFINITIONS_PRIVATE_TIMING) +if("${CMAKE_PROJECT_NAME}" STREQUAL "${PROJECT_NAME}") + set(GTSAM_BUILD_WITH_WERROR_DEFAULT ON) +else() + set(GTSAM_BUILD_WITH_WERROR_DEFAULT OFF) +endif() +option(GTSAM_BUILD_WITH_WERROR "Enable warnings as errors when building GTSAM." ${GTSAM_BUILD_WITH_WERROR_DEFAULT}) + if(MSVC) # Common to all configurations: list_append_cache(GTSAM_COMPILE_DEFINITIONS_PRIVATE @@ -108,11 +115,15 @@ if(MSVC) endif() # Other (non-preprocessor macros) compiler flags: +set(gtsam_compile_options_private_werror) if(MSVC) set(CMAKE_3_15 $) set(CMAKE_3_25 $) # Common to all configurations, next for each configuration: - set(GTSAM_COMPILE_OPTIONS_PRIVATE_COMMON /W3 /WX /GR /EHsc /MP CACHE STRING "(User editable) Private compiler flags for all configurations.") + set(gtsam_compile_options_private_common /W3 /GR /EHsc /MP) + if(GTSAM_BUILD_WITH_WERROR) + set(gtsam_compile_options_private_werror /WX) + endif() set(GTSAM_COMPILE_OPTIONS_PRIVATE_DEBUG $<${CMAKE_3_15}:/MDd> $<${CMAKE_3_25}:/Zi> /Ob0 /Od /RTC1 CACHE STRING "(User editable) Private compiler flags for Debug configuration.") set(GTSAM_COMPILE_OPTIONS_PRIVATE_RELWITHDEBINFO $<${CMAKE_3_15}:/MD> /O2 $<${CMAKE_3_25}:/Zi> CACHE STRING "(User editable) Private compiler flags for RelWithDebInfo configuration.") set(GTSAM_COMPILE_OPTIONS_PRIVATE_RELEASE $<${CMAKE_3_15}:/MD> /O2 CACHE STRING "(User editable) Private compiler flags for Release configuration.") @@ -131,8 +142,7 @@ else() endif() endif() - set(GTSAM_COMPILE_OPTIONS_PRIVATE_COMMON - -Werror # Enable warnings as errors + set(gtsam_compile_options_private_common -Wall # Enable common warnings -Wpedantic # Enable pedantic warnings $<$:-Wextra -Wno-unused-parameter> # Enable extra warnings, but ignore no-unused-parameter (as we ifdef out chunks of code) @@ -145,7 +155,10 @@ else() -Wformat -Werror=format-security # Error on wrong printf() arguments $<$:${flag_override_}> # Enforce the use of the override keyword # - CACHE STRING "(User editable) Private compiler flags for all configurations.") + ) + if(GTSAM_BUILD_WITH_WERROR) + set(gtsam_compile_options_private_werror -Werror) # Enable warnings as errors + endif() set(GTSAM_COMPILE_OPTIONS_PRIVATE_DEBUG -g -fno-inline CACHE STRING "(User editable) Private compiler flags for Debug configuration.") set(GTSAM_COMPILE_OPTIONS_PRIVATE_RELWITHDEBINFO -g -O3 CACHE STRING "(User editable) Private compiler flags for RelWithDebInfo configuration.") set(GTSAM_COMPILE_OPTIONS_PRIVATE_RELEASE -O3 CACHE STRING "(User editable) Private compiler flags for Release configuration.") @@ -153,6 +166,18 @@ else() set(GTSAM_COMPILE_OPTIONS_PRIVATE_TIMING -g -O3 CACHE STRING "(User editable) Private compiler flags for Timing configuration.") endif() +if(DEFINED GTSAM_COMPILE_OPTIONS_PRIVATE_COMMON) + set(gtsam_compile_options_private_common_cache "${GTSAM_COMPILE_OPTIONS_PRIVATE_COMMON}") + list(REMOVE_ITEM gtsam_compile_options_private_common_cache -Werror /WX) + set(GTSAM_COMPILE_OPTIONS_PRIVATE_COMMON + "${gtsam_compile_options_private_common_cache}" + CACHE STRING "(User editable) Private compiler flags for all configurations." FORCE) +else() + set(GTSAM_COMPILE_OPTIONS_PRIVATE_COMMON + ${gtsam_compile_options_private_common} + CACHE STRING "(User editable) Private compiler flags for all configurations.") +endif() + mark_as_advanced(GTSAM_COMPILE_OPTIONS_PRIVATE_COMMON) mark_as_advanced(GTSAM_COMPILE_OPTIONS_PRIVATE_DEBUG) mark_as_advanced(GTSAM_COMPILE_OPTIONS_PRIVATE_RELWITHDEBINFO) @@ -166,6 +191,9 @@ set(GTSAM_COMPILE_FEATURES_PUBLIC "cxx_std_17" CACHE STRING "CMake compile featu set(CMAKE_CXX_EXTENSIONS OFF) # Merge all user-defined flags into the variables that are to be actually used by CMake: +if(NOT "${gtsam_compile_options_private_werror}" STREQUAL "") + list_append_cache(GTSAM_COMPILE_OPTIONS_PRIVATE ${gtsam_compile_options_private_werror}) +endif() foreach(build_type "common" ${GTSAM_CMAKE_CONFIGURATION_TYPES}) append_config_if_not_empty(GTSAM_COMPILE_OPTIONS_PRIVATE ${build_type}) append_config_if_not_empty(GTSAM_COMPILE_OPTIONS_PUBLIC ${build_type})