How to get Visual Studio use a .clang-tidy configuration file when invoking clang-tidy? - c++

Visual Studio (Desktop) has a clang-tidy integration. you can make VS invoke clang-tidy with a list of checks.
however, I could not find a way to make it use an existing .clang-tidy configuration file.
The documentation hints that it is possible:
Clang-Tidy configuration By default, Clang-Tidy does not set any
checks when enabled. To see the list of checks in the command-line
version, run clang-tidy -list-checks in a developer command prompt.
You can configure the checks that Clang-Tidy runs inside Visual
Studio. In the project Property Pages dialog, open the Configuration
Properties > Code Analysis > Clang-Tidy page. Enter checks to run in
the Clang-Tidy Checks property. A good default set is
clang-analyzer-*. This property value is provided to the --checks
argument of the tool. Any further configuration can be included in
custom .clang-tidy files. For more information, see the Clang-Tidy
documentation on LLVM.org.
This is what I tried to do manually via the VS property pages:
But when running analyses on a file, it doesn't work.
So How to get Visual Studio use a .clang-tidy configuration file when invoking clang-tidy?

The property pages are for setting clang-tidy checks directly (not a path to the .clang-tidy file). Visual Studio should automatically detect the .clang-tidy file in your workspace, as long as it's in the same or a parent folder of your source files.

It seems Visual Studio may invoke clang-tidy from a directory outside the source tree in cases where the build is generated out-of-source (as commonly happens when using CMake).
I found a small hack around it. Basically I trick Visual Studio into thinking I am giving it a list of checks, where in fact I give it the path to the config file along with whatever arguments I want. (note the extra " characters).
This is of course a hack. But still might serve somebody.
Here is a way to do that with CMake:
set_target_properties(MyTarget PROPERTIES
VS_GLOBAL_RunCodeAnalysis false
# Use visual studio core guidelines
VS_GLOBAL_EnableMicrosoftCodeAnalysis false
#VS_GLOBAL_CodeAnalysisRuleSet ${CMAKE_CURRENT_SOURCE_DIR}/foo.ruleset
#VS_GLOBAL_CodeAnalysisRuleSet ${CMAKE_CURRENT_SOURCE_DIR}/foo.ruleset
# Use clangtidy
VS_GLOBAL_EnableClangTidyCodeAnalysis true
VS_GLOBAL_ClangTidyChecks "-* \"\"--config-file=${MY_CLANG_TIDY_CONFIG_PATH} --header-filter=.*"
)

Related

How build a project with platform configuration via devenv.exe\com?

I am trying to build a solution via devenv.com and I get errors. The command I use:
set devenvCom=C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.com
"%devenvCom%" "C:\...\mySolution.sln" /Build "Release|AnyCPU"
I get an error: "The operation could not be completed"
But if I run it without a platform configuration:
set devenvCom=C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.com
"%devenvCom%" "C:\...\mySolution.sln" /Build "Release"
This works fine.
But in their documentation they say that you can use the format: /Build "Release|AnyCPU"
And it doesn't matter if Irun the command using devenv.exe or devenv.com they both fail.
How can I run the command successfuly?
The syntax you have in both of your commands - with and without platform - is correct for devenv.com in Visual Studio 2017, according to Microsoft documentation at https://learn.microsoft.com/en-us/visualstudio/ide/reference/build-devenv-exe?view=vs-2017.
I run similar commands with devenv.com in VS2019 without any difficulty, via a batch file run as part of a Jenkins job.
The following notes may assist in resolving this issue:
When defining environment variables for file or folder paths, I suggest using double quotes as follows:
set "devenvCom=C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.com"
These double quotes make it easier to ensure you have not added any trailing whitespace to the environment variable value by mistake. The double quotes also ensure that any parentheses or ampersands in the path will not be misinterpreted by the cmd.exe shell.
It is fine to double quote the parameter which follows /Build, e.g. /Build "Release". These double quotes are REQUIRED when specifying the platform such as /Build "Release|AnyCPU", to ensure the vertical bar is not misinterpreted by the cmd.exe shell, or when the build configuration name contains a space.
I recommend /Rebuild rather than /Build when performing automated builds. Only the former is guaranteed to perform a complete build from scratch.
Microsoft documentation states "Insert one space character between switches and arguments on the same line", i.e. put ONE SPACE between the /Build parameter and the value which follows it, e.g. "Release|AnyCPU".
The name of the build configuration and platform, e.g. "Release|AnyCPU", is expected to be found in the specified solution file. These solution build configurations names and platforms do not have to correspond to the project build configuration names and platforms (though they should whenever possible).
If you wish to build a single project only, I have been successful with the following syntax in VS2019:
"%devenvCom%" "C:\...\myProject.vcxproj" /Build "Release|AnyCPU" /Project "myProject"
Note that the first parameter in the above example is a project file, rather than a solution file. I believe that the specified build configuration and platform refer to the project rather than the unspecified solution (note: Visual Studio may create a temporary solution when performing single project builds if it cannot find a solution that corresponds to the specified project).
For single project builds, I previously had success specifying the solution file first, and the project file after /Project, but this previous syntax does not consistently succeed in VS2019 build 16.11.2 - it can lead to devenv.exe remaining running indefinitely after the project builds successfully (devenv.exe gets launched by devenv.com).
Your VS2017 installation may be corrupt, or you may have an extension that is interfering with VS2017.

Let CMake set the "Exclude From Build" option for a single source file in Visual Studio

In Visual Studio there is an "Exclude From Build" option in the properties page of each source file, that can be set to exclude the file from build, but keep it visible in the source tree:
Is there a way to set that specific property with CMake?
I found a VS_DEPLOYMENT_CONTENT property and tried that but without success (it doesn't seem to do anything).
The reason for using that property is mainly to keep the file in the project to be able to open and edit it from within Visual Studio.
Thanks in advance!
Unfortunately, I also can't find the answer how to set "Exclude From Build" for some files with cmake in the same project. For now, I have added all such files to the target created by add_custom_target.
if(MSVC)
add_custom_target(myproj.additional SOURCES ${otherHeaders} ${otherSources})
endif()
This will create an extra project in the solution. The files in this project will not compile and I can still edit and search in them.
Another option is to set the HEADER_FILE_ONLY property for sources that should not be compile:
if(MSVC)
set_source_files_properties(${otherSources} PROPERTIES
HEADER_FILE_ONLY TRUE
)
endif()
In this case, the files stay in the project but without any marks - such files will not differ in appearance from those that are compiled
Since CMake 3.18 you can use VS_SETTINGS. That allows you to set any VS Project setting you like.
set_property(SOURCE ${SourceFiles} PROPERTY VS_SETTINGS "ExcludedFromBuild=true")
Note that you have to set the settings values with the name as it is written in the vcxproj file. To know what the setting is called you can set the setting to the correct value via the VS IDE, save the project and then open the vcxproj file in a text editor and search for the correct setting-value pair.
For example, in case of Excluded from Build:
<FXCompile Include="C:\path\to\source\file\file.hlsl">
<ExcludedFromBuild>true</ExcludedFromBuild>
</FXCompile>

Compile a specific obj in a Visual Studio Project from the Command Line?

I want to compile file.obj from the commandline. Within the IDE, if I'm viewing file.cpp, I can click on Build -> Compile (or just hit Ctrl-F7), and it will compile just the file.obj object. I would like to be able to do this from the commandline. Ideally, something akin to:
vcbuild project.vcproj Debug file.obj // not a valid command
I have looked at the documentation for vcbuild, msbuild, and devenv. I've also experimented with all three, but I cannot find a way to do this. I can find a way to build an entire project, but that's not what I want. I want to build a specific source file. /pass1 tells vcbuild to just compile (not link), but it compiles the entire project.
I also looked at using cl, but that is just the compiler. In order to use it, I would have to know all the right parameters to pass to set up my environment correctly. All that is automatically taken care of with msbuild/vcbuild.
With Makefiles, I could always do make file.obj, and it would properly set path, include dirs, etc.
Any options for this? Is there an automated way to extract the appropriate settings from the .vcproj file, and pass them to cl?
Using cl is the way to compile single files from the command line. Like you say, it requires/allows you to specify exactly the options you want to use. All the options!
If you actually don't want to do that, why not use the IDE to have it done automagically for you? Why do it the hardest way, if you don't like that?
if you just want to compile the project, run the visual studio command line and call msbuild.
Example:
MSBuild.exe MyProj.proj /property:Configuration=Debug
this will compile the MyProj Project from the current directory.
more info on msbuild
http://msdn.microsoft.com/en-us/library/dd393574.aspx
Or if you need to build a single file you can use cl as stated above. You can see all the parameters passed by visual studio to cl if you go in the properties of the project. Usually under:
Configuration Properties -> C/C++ -> Command Line
and for linking:
Configuration Properties -> Linker -> Command Line

Parsing VS2010 MSBuild vcxproj file

I'm currently working on a console application that will pass a vcxproj file and compile it using GCC. Unfortunately, I've come up against a whole load of problems pretty much instantly.
I notice a bunch of directory shortcuts such as:
VCTargetsPath
VCInstallDir
SolutionDir
ProjectDir
UserRootDir
and so on.
Where does MSBuild get these values from? I assumed they were environment variables set up for the MSBuild process (afterall known environment variables are addressed the same way, ie "$(...)"). This was a bad assumption so I'm left wondering exactly how I get at these. Has anyone any idea on this?
Any info would be much appreciated :)
The MSBuild executable (and dependent DLLs) processes those properties the same way it processes any other property in the build file. In this case, they're simply predefined properties that it looks for explicitly.
If you really want to dig into it, open up the Microsoft.Build.dll in Reflector and look for the Microsoft.Build.Construction.SolutionProjectGenerator.AddGlobalProperties(...) method to get an idea of how it's handling some of the well known properties.
As an additional note, make sure you fully navigate down Import directives and handle overwriting of property and item values with each Import. There's a number of properties and items that are part of a Visual Studio build that are not always necessary for your code to compile correctly.
I believe that these are tied to Visual Studio macros: MSDN
Some of them are defined by the location of your files
SolutionDir - the directory containing the solution (.sln) file including this project
ProjectDir - the directory containing the project file (.vcproj, .vcxproj)
Others are defined by the location of the MSVC install
VCInstallDir - where the Visual C portion of Visual Studio is installed. ie. C:\Program Files\Microsoft Visual Studio 8\VC
and so on, and would be internal to msbuild based on what you loaded.

How do I build all configurations of a Visual Studio 2008 C++ project on the command line?

I'd like to build all the configurations of a VS 2008 C++ project on the command line. Something like:
devenv TheProject.vcproj /build /nologo
But this doesn't work because the /build command insists on having a configuration following it like this:
devenv TheProject.vcproj /build "Release|Win32" /nologo
Is there a way to get a command line build of all configurations within the vcproj file?
I was thinking you can do what you want with MSBUILD, but it appears that it isn't much better for this than DEVENV.
You still have to specify each configuration on the command line, although it would be easy to write a batch file to accomplish this with either MSBUILD or DEVENV.
It looks like previous versions of the development environment may have supported an "ALL" option, but VS 2008 does not.
http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/8701b3d0-d5c9-45fb-8dd4-e7700c8caca6/
Not directly, but you can have projects depend on other projects - so you could have an 'all' or 'install' project with a dependacy of everything else.
Haven't used VS in a long time. But the project properties panel used to show the command line generated for linking and compiling a project for a particular configuration. It used to be under the Advanced tab. Will using that directly from the command line serve your purpose? This method will not use the VS IDE at all.
Alternatively,
Steps:
Create a project which has a dependency on all other projects.
Write a script which builds this project with different configurations sequentially. You cannot create a single configuration which encapsulates all other configurations.