I have a VC project file that I'm building from command line using MSBuild. I want to specify the /MP flag without editing the project file. Is that possible?
I've tried set CL=/MP prior to calling MSBuild, but it has no effect.
This can be accomplished by accessing the CL_MPCount Visual Studio option:
MSBuild /m:2 /p:CL_MPCount=2 /p:Configuration=Release tf_tutorials_example_trainer.vcxproj
The above instructs the compiler to perform a maximum of 2 parallel compilation tasks. The /m:2 flag allows MSBuild to build two projects in parallel. The net result is that we have a maximum of 4 cl.exe processes running in parallel.
UPDATE: The CL_MPCount=2 flag gets passed on to cl.exe as /MP2. This allows parallel compilation of 2 .cpp files within the same project.
You need a property that you can override from the command line. Open the .vcxproj file in a text editor, Notepad will do. Locate the "Globals" property group and add a property named, say, "Turbo"
<PropertyGroup Label="Globals">
<Turbo>false</Turbo>
...etc...
</PropertyGroup>
And use the property to specify the compile option. Since it can only work in the Release build:
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<MultiProcessorCompilation>$(Turbo)</MultiProcessorCompilation>
...etc...
</ClCompile>
And run MSBuild:
msbuild /p:Configuration=Release /p:Turbo=true
Related
I have defined a custom build step in a Visual Studio 2019 C++ project file (.vcxproj) that generates .h and .cc files. There is a Custom Build Tool option entitled "Add Outputs to Item Type", where I can select one of "C/C++ header" or "C/C++ compiler". Is there some way to edit the resulting project file such that the .cc files are added as "C/C++ compiler" and the .h files are added as "C/C++ header"?
My intended work-around is to simply remove the generated .h files from the list of Custom Build Tool outputs. That work-around has the disadvantage that the build will fail if one of the .h files is missing or stale. I'm hoping for a solution that resolves this deficiency.
The following illustrates what I have attempted within the .vcxproj file:
<OutputItemType Condition="'%(Extension)'=='.cc'">ClCompile</OutputItemType>
Not sure about how you define the Custom Build Step. But custom build step is not the only way.
For C++ projects, msbuild provides Custom Build Tool, Custom Build Step, Build events and custom targets. You can achieve same requirements by this script:
<Target Name="CustomTarget" BeforeTargets="PrepareForBuild">
<Exec Command="xx.exe ..."/> <!--Call the tool.exe to generate files somewhere. We can save them in temp folder.-->
<Copy SourceFiles="xxx" DestinationFolder="xxx"/> <!--Use copy task to copy the generated files to project folder-->
<ItemGroup>
<ClCompile Include="Path of Temp folder\*.cc" />
</ItemGroup>
</Target>
And see this, I checked some related documents and confirm OutputItemType is something about ProjectReference. I'm not sure if this syntax supports other Items.
We have set the parallel execution count to 1 so that all the projects will build one after other.In the Visual Studio settings.
With below command from Devenv.exe we are able to compile the solution correctly.
call vcvarsall.bat x86_amd64
devenv /Build "Release|x64" "w:/Frameworks/Frameworks.sln"
But with MSBuild, with below command, the solution is failing to compile, when I cross check the log, the order of compilation of projects is not correct, not taking from solution file.
call vcvarsall.bat x86_amd64
"C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe" "W:\Frameworks\Frameworks.sln" /t:build /fl /flp:logfile=Frameworks.log;verbosity=normal /p:Configuration=Release;Platform=x64 /m:1
Any suggestion, how to fix set msbuild to compile only from order mentioned in solution file.
Open your solution in VisualStudio, on the solution explorer right click the solution name and look for "Project Dependencies..." and/or "Project Build Order..."
The UI is self explanatory, so configure as required, save all and try the MsBuild command again.
Is it possible to pass options to linker via comamnd line of msbuild? For example I want to set VC linker option /PROFILE. How to do it without changing of C++ project file?
PS: Visual Studio Express 2012.
Inside the projectfile the linker options are set in an ItemGroup so you cannot simply add or override this from the commandline. Instead you'll have to make msbuild include them which can only be done by importing another msbuild file. This functionality is supported: if you set the ForceImportBeforeCppTargets on the commandline, msbuild will import the file it points to.
Practically: create this file, let's call it c:\props\profile.props
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemDefinitionGroup>
<Link>
<Profile>true</Profile>
</Link>
</ItemDefinitionGroup>
</Project>
Then build your (unmodified) project like this:
msbuild myProject.vcxproj /p:ForceImportBeforeCppTargets=c:\props\profile.props
I am trying to enable code analysis for my CMake driven project. I generate Visual Studio 2013 solutions, and would like to enable code analysis with a specific rule set. I know that I can enable the code analysis by specifying add_definitions("/analyze") in my CMake file, but how do I control which rule set is used?
As far as I can see, the only way to control the ruleset used is via the xml element CodeAnalysisRuleSet in the project file, but I cannot access this from CMake.
If you are trying to solve this for a command line build--either via cmake --build or direct invocation of msbuild--and not when using the generated solution with Visual Studio, you can set the relevant properties when you invoke the build.
With cmake driving the build:
PS c:\build-dir> cmake --build . -- '/p:RunCodeAnalysis=true' `
'/p:CodeAnalysisRuleSet=NativeRecommendedRules.ruleset'
With MSBuild driving the build:
PS c:\build-dir> msbuild ALL_BUILD.vcxproj '/p:RunCodeAnalysis=true' `
'/p:CodeAnalysisRuleSet=NativeRecommendedRules'
If you have a custom ruleset in a custom directory, you will also need to set the property CodeAnalysisRuleSetDirectories:
PS c:\build-dir> cmake --build . -- '/p:RunCodeAnalysis=true' `
'/p:CodeAnalysisRuleSet=custom.ruleset' `
'/p:CodeAnalysisRuleSetDirectories=c:\src\ruletsets'
(Tested with CMake 3.8.0, MSBuild 15.3.409.57025, and Visual C++ 2017 19.11.25506.)
I am not sure how far this works for VS2013 solutions. With Visual Studio 2015 I am using Project User Templates (*.vcxproj.user) with something along the following lines:
C:\MyProject\Template.USERNAME.user
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<CodeAnalysisRuleSet>#CMAKE_SOURCE_DIR#\SecurityRecommended.ruleset</CodeAnalysisRuleSet>
<RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>
</Project>
You will probably have to change a few things like the ToolsVersion to 12.x.
The #CMAKE_SOURCE_DIR# will automatically get replaced by cmake when you use the CONFIGURE_FILE function to generate the actual PROJECT.vcxproj.user file. I have the following line in the CMakeLists.txt of my projects:
SET(USER_NAME $ENV{USERNAME} CACHE STRING UserName)
SET(USER_FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.vcxproj.user)
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/Template.${USER_NAME}.user ${USER_FILE} #ONLY)
In the end, I would assume you could user-define anything that's in the PROJECT.vcxproj with those templates.
VS2017:In CMakeSettings.json section "configuration" add parameter "codeAnalysisRuleset": "NativeRecommendedRules.ruleset". Then in Solution Explorer right-click on need file .cpp select menu "Run Code Analysis on File". You can choose from a variety of analyzers in VS folder "c:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Team Tools\Static Analysis Tools\Rule Sets\NativeRecommendedRules.ruleset"
I have a solution with several C++ projects. For some of the projects I need some custom file copy, e.g. to copy some configuration files to the output directory or to copy the output files of one project to a specific folder after build.
In some cases I don't want or cannot add these files to the projects directly through the Visual Studio IDE. I created simple .targets files which I can reuse and add to the projects which need the file copying.
Here is a simple example .targets file for copying configuration files:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildDependsOn>
$(BuildDependsOn);
CopyCustom
</BuildDependsOn>
</PropertyGroup>
<ItemGroup>
<CustomFiles Include="$(ProjectDir)Config\**\*.xml" />
</ItemGroup>
<PropertyGroup>
<DestCustFolder>$(OutDir)Config\</DestCustFolder>
</PropertyGroup>
<Target Name="CopyCustom"
Inputs="#(CustomFiles )"
Outputs="#(CustomFiles ->'$(DestCustFolder)%(RecursiveDir)%(FileName)%(Extension)')">
<Message Text="Copy custom files..." />
<Copy SourceFiles="#(CustomFiles )" DestinationFiles="#(CustomFiles->'$(DestCustFolder)%(RecursiveDir)%(FileName)%(Extension)')" SkipUnchangedFiles="true" />
</Target>
</Project>
Through the "Build Customization" dialog in Visual Studio I add it to the project so it will be included like this at the end of the project file:
<ImportGroup Label="ExtensionTargets">
<Import Project="..\Targets\CopyCustom.targets" />/
</ImportGroup>
This should enable incremental build of my custom target. If I just edit one of my custom files (and none of the C++ files) and build it form the console with
msbuild foo1.vcxproj
it will actually detect the changes and does an incremental build for my custom target. If no changes are made the target is skipped.
If I do however build inside Visual Studio it will not detect changes to the custom files and only and gives me the message that the project is up to data:
========== Build: 0 succeeded, 0 failed, 5 up-to-date, 0 skipped ==========
I would have to additionally change one of the C++ files to make it check all targets again and to the incremental build.
I was expecting that Visual Studio just executes MSBuild which will then do the up-to-date check on the projects, so it should be the same result as running MSBuild from the console. I was trying to get more information by setting the verbosity level to diagnostic but I just get the same line. It seems to me that MSBuild is not even executed for the project but Visual Studio itself determines that the project is up-to-date.
So I was wondering how Visual Studio actually determines when it should execute MSBuild for a project.
I asked basically the same question before on the MSDN forum but couldn't get a clear answer.
See this suggestion on Microsoft Connect.
Basically you need to set DisableFastUpToDateCheck property to true to disable the fast-up-to-date check.
Just add to your vcxproj or your targets file:
<PropertyGroup>
<DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
</PropertyGroup>
I found an answer by looking into the book "Inside the Microsoft Build Engine, Second Edition".
Note: I also updated the same in my question in the MSDN forum but I will mainly duplicate the text here again for completeness.
On page 280 they actually saying that the IDE does a "fast up-to-date check" on the project-level. It only spawns a project build and does a more fine-grained check on the individual tasks if this rough project-level check fails.
When running MSBuild from the command line however there is always a fine-grained up-to-date check on the individual tools.
So the IDE only seems to do this fast check on the files which are added to the projects directly and set as one of the "Input File" types.
In my opinion this is not a good design. I would prefer that the IDE is only used to edit the MSBuild project files and then just invokes MSBuild to do the up-to-date check. This would make it much clearer.
I can understand that in a solution with a lot of projects it can make the up-to-date check much faster but there should be at least an option to disable the fast up-to-date check. I was checking the IDE if there is a way to disable this behavior but could not find anything.
The solution suggested here actually works and I am using it at the moment. But I added several custom targets for different kinds of custom files. If I add a new custom file I should never forget to set it to "Custom Build Tool" otherwise the incremental build for this file will not work.
I guess a solution would be to make a full build customization for my custom files and use specific file extensions for them so Visual Studio will automatically detect them when I add them to the project and sets the right Item Type.