Skip to content

Commit b773128

Browse files
authored
Merge pull request #82 from cpp-best-practices/msvc
2 parents f07b5fe + 96b02bb commit b773128

4 files changed

Lines changed: 41 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ project_options(
3838
ENABLE_CACHE
3939
ENABLE_CPPCHECK
4040
ENABLE_CLANG_TIDY
41+
ENABLE_VS_ANALYSIS
4142
# ENABLE_CONAN
4243
# ENABLE_INTERPROCEDURAL_OPTIMIZATION
4344
# ENABLE_NATIVE_OPTIMIZATION
@@ -55,7 +56,6 @@ project_options(
5556
# ENABLE_USER_LINKER
5657
# ENABLE_BUILD_WITH_TIME_TRACE
5758
# ENABLE_UNITY
58-
# CONAN_OPTIONS
5959
)
6060
```
6161

@@ -160,6 +160,7 @@ It accepts the following named flags:
160160
- `ENABLE_CACHE`: Enable cache if available
161161
- `ENABLE_CPPCHECK`: Enable static analysis with Cppcheck
162162
- `ENABLE_CLANG_TIDY`: Enable static analysis with clang-tidy
163+
- `ENABLE_VS_ANALYSIS`: Enable Visual Studio IDE code analysis if the generator is Visual Studio.
163164
- `ENABLE_CONAN`: Use Conan for dependency management
164165
- `ENABLE_INTERPROCEDURAL_OPTIMIZATION`: Enable Interprocedural Optimization (Link Time Optimization, LTO) in the release build
165166
- `ENABLE_NATIVE_OPTIMIZATION`: Enable the optimizations specific to the build machine (e.g. SSE4_1, AVX2, etc.).
@@ -186,6 +187,7 @@ It gets the following named parameters that can have different values in front o
186187
- `GCC_WARNINGS`: Override the defaults for the GCC warnings
187188
- `CUDA_WARNINGS`: Override the defaults for the CUDA warnings
188189
- `CPPCHECK_WARNINGS`: Override the defaults for the options passed to cppcheck
190+
- `VS_ANALYSIS_RULESET`: Override the defaults for the code analysis rule set in Visual Studio.
189191
- `CONAN_OPTIONS`: Extra Conan options
190192

191193
## `run_vcpkg` function

src/DynamicProjectOptions.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ macro(dynamic_project_options)
6868
"ENABLE_CACHE\;${MAKEFILE_OR_NINJA}\;${MAKEFILE_OR_NINJA}\;Enable ccache on Unix"
6969
"WARNINGS_AS_ERRORS\;OFF\;ON\;Treat warnings as Errors"
7070
"ENABLE_CLANG_TIDY\;OFF\;${MAKEFILE_OR_NINJA}\;Enable clang-tidy analysis during compilation"
71+
"ENABLE_VS_ANALYSIS\;ON\;ON\;Enable Visual Studio IDE code analysis if the generator is Visual Studio."
7172
"ENABLE_CONAN\;OFF\;OFF\;Automatically integrate Conan for package management"
7273
"ENABLE_COVERAGE\;OFF\;OFF\;Analyze and report on coverage"
7374
"ENABLE_SANITIZER_ADDRESS\;OFF\;${SUPPORTS_ASAN}\;Make memory errors into hard runtime errors (windows/linux/macos)"
@@ -147,6 +148,7 @@ macro(dynamic_project_options)
147148
${WARNINGS_AS_ERRORS_VALUE}
148149
${ENABLE_CPPCHECK_VALUE}
149150
${ENABLE_CLANG_TIDY_VALUE}
151+
${ENABLE_VS_ANALYSIS_VALUE}
150152
${ENABLE_COVERAGE_VALUE}
151153
${ENABLE_INTERPROCEDURAL_OPTIMIZATION_VALUE}
152154
${ENABLE_NATIVE_OPTIMIZATION_VALUE}

src/Index.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ macro(project_options)
5151
ENABLE_COVERAGE
5252
ENABLE_CPPCHECK
5353
ENABLE_CLANG_TIDY
54+
ENABLE_VS_ANALYSIS
5455
ENABLE_INCLUDE_WHAT_YOU_USE
5556
ENABLE_CACHE
5657
ENABLE_PCH
@@ -67,7 +68,7 @@ macro(project_options)
6768
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
6869
ENABLE_SANITIZER_THREAD
6970
ENABLE_SANITIZER_MEMORY)
70-
set(oneValueArgs DOXYGEN_THEME)
71+
set(oneValueArgs DOXYGEN_THEME VS_ANALYSIS_RULESET)
7172
set(multiValueArgs
7273
MSVC_WARNINGS
7374
CLANG_WARNINGS
@@ -174,6 +175,10 @@ macro(project_options)
174175
enable_clang_tidy()
175176
endif()
176177

178+
if(${ProjectOptions_ENABLE_VS_ANALYSIS})
179+
enable_vs_analysis("${ProjectOptions_VS_ANALYSIS_RULESET}")
180+
endif()
181+
177182
if(${ProjectOptions_ENABLE_INCLUDE_WHAT_YOU_USE})
178183
enable_include_what_you_use()
179184
endif()

src/StaticAnalyzers.cmake

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,36 @@ macro(enable_clang_tidy)
117117
endif()
118118
endmacro()
119119

120+
# Enable static analysis inside Visual Studio IDE
121+
macro(enable_vs_analysis VS_ANALYSIS_RULESET)
122+
if("${VS_ANALYSIS_RULESET}" STREQUAL "")
123+
# See for other rulesets: C:\Program Files (x86)\Microsoft Visual Studio\20xx\xx\Team Tools\Static Analysis Tools\Rule Sets\
124+
set(VS_ANALYSIS_RULESET "AllRules.ruleset")
125+
endif()
126+
if(NOT
127+
"${CMAKE_CXX_CLANG_TIDY}"
128+
STREQUAL
129+
"")
130+
set(_VS_CLANG_TIDY "true")
131+
else()
132+
set(_VS_CLANG_TIDY "false")
133+
endif()
134+
if(CMAKE_GENERATOR MATCHES "Visual Studio")
135+
get_all_targets(_targets_list)
136+
foreach(target IN LISTS ${_targets_list})
137+
set_target_properties(
138+
${target}
139+
PROPERTIES
140+
VS_GLOBAL_EnableMicrosoftCodeAnalysis true
141+
VS_GLOBAL_CodeAnalysisRuleSet "${VS_ANALYSIS_RULESET}"
142+
VS_GLOBAL_EnableClangTidyCodeAnalysis "${_VS_CLANG_TIDY}"
143+
# TODO(disabled) This is set to false deliberately. The compiler warnings are already given in the CompilerWarnings.cmake file
144+
# VS_GLOBAL_RunCodeAnalysis false
145+
)
146+
endforeach()
147+
endif()
148+
endmacro()
149+
120150
# Enable static analysis with include-what-you-use
121151
macro(enable_include_what_you_use)
122152
find_program(INCLUDE_WHAT_YOU_USE include-what-you-use)

0 commit comments

Comments
 (0)