|
| 1 | +# ENABLE_DEVELOPER_MODE: sets defaults appropriate for developers, this is defaulted to ON |
| 2 | +# * WARNINGS_AS_ERRORS: ON |
| 3 | +# * ENABLE_SANITIZER_ADDRESS: ON |
| 4 | +# * ENABLE_CLANG_TIDY: ON for Ninja/Makefiles |
| 5 | +# * ENABLE_SANITIZER_UNDEFINED: ON for Compilers that support it |
| 6 | +# * ENABLE_CPPCHECK: ON for Ninja/Makefiles |
| 7 | + |
| 8 | +# For non-developer builds |
| 9 | +# -DENABLE_DEVELOPER_MODE:BOOL=OFF |
| 10 | +# Is recommended |
| 11 | + |
| 12 | +# In developer mode, all features have options that show up in the CMake GUI tools |
| 13 | + |
| 14 | +# dynamic_project_options() macro enables all recommended defaults with appropriately |
| 15 | +# applied options from the GUI which are set |
| 16 | + |
| 17 | +# Any default can be overridden |
| 18 | +# set(<feature_name>_DEFAULT <value>) - set default for both user and developer modes |
| 19 | +# set(<feature_name>_DEVELOPER_DEFAULT <value>) - set default for developer mode |
| 20 | +# set(<feature_name>_USER_DEFAULT <value>) - set default for user mode |
| 21 | + |
| 22 | +option(ENABLE_DEVELOPER_MODE "Set up defaults for a developer of the project, and let developer change options" ON) |
| 23 | + |
| 24 | +if((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*" OR CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*") AND NOT WIN32) |
| 25 | + set(SUPPORTS_UBSAN ON) |
| 26 | +else() |
| 27 | + set(SUPPORTS_UBSAN OFF) |
| 28 | +endif() |
| 29 | + |
| 30 | +if((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*" OR CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*") AND WIN32) |
| 31 | + set(SUPPORTS_ASAN OFF) |
| 32 | +else() |
| 33 | + set(SUPPORTS_ASAN ON) |
| 34 | +endif() |
| 35 | + |
| 36 | +# ccache, clang-tidy, cppcheck are only supported with Ninja and Makefile based generators |
| 37 | +# note that it is possible to use Ninja with cl, so this still allows clang-tidy on Windows |
| 38 | +# with CL. |
| 39 | +# |
| 40 | +# We are only setting the default options here. If the user attempts to enable |
| 41 | +# these tools on a platform with unknown support, they are on their own. |
| 42 | +# |
| 43 | +# Also note, cppcheck has an option to be run on VCproj files, so we should investigate that |
| 44 | +# Further note: MSVC2022 has builtin support for clang-tidy, but I can find |
| 45 | +# no way to enable that via CMake |
| 46 | +if(CMAKE_GENERATOR MATCHES ".*Makefile*." OR CMAKE_GENERATOR MATCHES ".*Ninja*") |
| 47 | + set(MAKEFILE_OR_NINJA ON) |
| 48 | +else() |
| 49 | + set(MAKEFILE_OR_NINJA OFF) |
| 50 | +endif() |
| 51 | + |
| 52 | +include(CMakeDependentOption) |
| 53 | + |
| 54 | +# <option name>;<user mode default>;<developer mode default>;<description> |
| 55 | +set(options |
| 56 | + "ENABLE_CACHE\;${MAKEFILE_OR_NINJA}\;${MAKEFILE_OR_NINJA}\;Enable ccache on Unix" |
| 57 | + "WARNINGS_AS_ERRORS\;OFF\;ON\;Treat warnings as Errors" |
| 58 | + "ENABLE_CLANG_TIDY\;OFF\;${MAKEFILE_OR_NINJA}\;Enable clang-tidy analysis during compilation" |
| 59 | + "ENABLE_CONAN\;ON\;ON\;Automatically integrate Conan for package management" |
| 60 | + "ENABLE_COVERAGE\;OFF\;OFF\;Analyze and report on coverage" |
| 61 | + "ENABLE_SANITIZER_ADDRESS\;OFF\;${SUPPORTS_ASAN}\;Make memory errors into hard runtime errors (windows/linux/macos)" |
| 62 | + "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR\;OFF\;${SUPPORTS_UBSAN}\;Make certain types (numeric mostly) of undefined behavior into runtime errors" |
| 63 | + "ENABLE_CPPCHECK\;OFF\;${MAKEFILE_OR_NINJA}\;Enable cppcheck analysis during compilation" |
| 64 | + "ENABLE_IPO\;OFF\;OFF\;Enable whole-program optimization" |
| 65 | + "ENABLE_INCLUDE_WHAT_YOU_USE\;OFF\;OFF\;Enable include-what-you-use analysis during compilation" |
| 66 | + "ENABLE_PCH\;OFF\;OFF\;Enable pre-compiled-headers support" |
| 67 | + "ENABLE_DOXYGEN\;OFF\;OFF\;Build documentation with Doxygen" |
| 68 | + "ENABLE_USER_LINKER\;OFF\;OFF\;Allow custom linker settings" |
| 69 | + "ENABLE_BUILD_WITH_TIME_TRACE\;OFF\;OFF\;Generates report of where compile-time is spent" |
| 70 | + "ENABLE_UNITY\;OFF\;OFF\;Merge C++ files into larger C++ files, can speed up compilation sometimes" |
| 71 | + "ENABLE_SANITIZER_LEAK\;OFF\;OFF\;Make memory leaks into hard runtime errors" |
| 72 | + "ENABLE_SANITIZER_THREAD\;OFF\;OFF\;Make thread race conditions into hard runtime errors" |
| 73 | + "ENABLE_SANITIZER_MEMORY\;OFF\;OFF\;Make other memory errors into runtime errors") |
| 74 | + |
| 75 | +foreach(option ${options}) |
| 76 | + list( |
| 77 | + GET |
| 78 | + option |
| 79 | + 0 |
| 80 | + option_name) |
| 81 | + list( |
| 82 | + GET |
| 83 | + option |
| 84 | + 1 |
| 85 | + option_user_default) |
| 86 | + list( |
| 87 | + GET |
| 88 | + option |
| 89 | + 2 |
| 90 | + option_developer_default) |
| 91 | + list( |
| 92 | + GET |
| 93 | + option |
| 94 | + 3 |
| 95 | + option_description) |
| 96 | + |
| 97 | + if(DEFINED ${option_name}_DEFAULT) |
| 98 | + if(DEFINED ${option_name}_DEVELOPER_DEFAULT OR DEFINED ${option_name}_USER_DEFAULT) |
| 99 | + message( |
| 100 | + SEND_ERROR |
| 101 | + "You have separately defined user/developer defaults and general defaults for ${option_name}. Please either provide a general default OR separate developer/user overrides" |
| 102 | + ) |
| 103 | + endif() |
| 104 | + |
| 105 | + set(option_user_default ${${option_name}_DEFAULT}) |
| 106 | + set(option_developer_default ${${option_name}_DEFAULT}) |
| 107 | + endif() |
| 108 | + |
| 109 | + if(DEFINED ${option_name}_USER_DEFAULT) |
| 110 | + set(option_user_default ${${option_name}_USER_DEFAULT}) |
| 111 | + endif() |
| 112 | + |
| 113 | + if(DEFINED ${option_name}_DEVELOPER_DEFAULT) |
| 114 | + set(option_developer_default ${${option_name}_DEVELOPER_DEFAULT}) |
| 115 | + endif() |
| 116 | + |
| 117 | + cmake_dependent_option( |
| 118 | + OPT_${option_name} |
| 119 | + "${option_description}" |
| 120 | + ${option_developer_default} |
| 121 | + ENABLE_DEVELOPER_MODE |
| 122 | + ${option_user_default}) |
| 123 | + |
| 124 | + if(OPT_${option_name}) |
| 125 | + set(${option_name}_VALUE ${option_name}) |
| 126 | + else() |
| 127 | + unset(${option_name}_VALUE) |
| 128 | + endif() |
| 129 | +endforeach() |
| 130 | + |
| 131 | +macro(dynamic_project_options) |
| 132 | + project_options( |
| 133 | + ${ENABLE_CONAN_VALUE} |
| 134 | + ${ENABLE_CACHE_VALUE} |
| 135 | + ${WARNINGS_AS_ERRORS_VALUE} |
| 136 | + ${ENABLE_CPPCHECK_VALUE} |
| 137 | + ${ENABLE_CLANG_TIDY_VALUE} |
| 138 | + ${ENABLE_COVERAGE_VALUE} |
| 139 | + ${ENABLE_IPO_VALUE} |
| 140 | + ${ENABLE_INCLUDE_WHAT_YOU_USE_VALUE} |
| 141 | + ${ENABLE_PCH_VALUE} |
| 142 | + ${ENABLE_DOXYGEN_VALUE} |
| 143 | + ${ENABLE_USER_LINKER_VALUE} |
| 144 | + ${ENABLE_BUILD_WITH_TIME_TRACE_VALUE} |
| 145 | + ${ENABLE_UNITY_VALUE} |
| 146 | + ${ENABLE_SANITIZER_ADDRESS_VALUE} |
| 147 | + ${ENABLE_SANITIZER_LEAK_VALUE} |
| 148 | + ${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_VALUE} |
| 149 | + ${ENABLE_SANITIZER_THREAD_VALUE} |
| 150 | + ${ENABLE_SANITIZER_MEMORY_VALUE} |
| 151 | + ${ARGN}) |
| 152 | +endmacro() |
0 commit comments