MSBuild: parallel builds and .Net projects - build

I've written a MSBuild project files which try to build in parallel all the configs of my VS2010 solution:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<ItemGroup>
<BuildFile Include="$(SourceRoot)MyProject.sln" />
<Config Include="Debug">
<Configuration>Debug</Configuration>
</Config>
<Config Include="Release">
<Configuration>Release</Configuration>
</Config>
</ItemGroup>
<Target Name="BuildAll" Outputs="%(Config.Configuration)">
<Message Text="Start building for configuration: %(Config.Configuration)" />
<MSBuild Projects="#(BuildFile)"
Properties="Configuration=%(Config.Configuration)"
Targets="Build" />
</Target>
</Project>
And I launch msbuild with:
msbuild /m /p:BuildInParallel=true /t:BuildAll buildall.proj
The problem is that my solution have many .Net projects which all have the same output folder. These projects use also the same external assemblies.
So very often, two output executables are generated at the same time and their dependencies copied at the same time. This leads to errors like:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3001,9):
error MSB3021:
Unable to copy file "xxx\NLog.dll" to "D:\src\Blackbird\shared\bin\debug\NLog.xml". The process
cannot access the file 'xxx\NLog.dll because it is being used by another process.
which I think means: "2 different projects use NLog and try to copy its assembly in the output folder at the same time"...
Is there a way to get around that? I really would like to avoid to modify all the projects in the solution.
Looking at the task source code "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3001,9)", I've seen that it is possible to make msbuild retry the copy:
<Copy
SourceFiles="#(ReferenceCopyLocalPaths)"
DestinationFiles="#(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')"
SkipUnchangedFiles="$(SkipCopyUnchangedFiles)"
OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)"
Retries="$(CopyRetryCount)"
RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)"
UseHardlinksIfPossible="$(CreateHardLinksForCopyLocalIfPossible)"
Condition="'$(UseCommonOutputDirectory)' != 'true'"
>
I've try to set the variables CopyRetryCount, CopyRetryDelayMilliseconds, ... I was hoping that if the copy fails, another copy done a few milliseconds later would succeed. But I've been unable to set these parameters. How can I change them?
Is there another solution?

I've found the solution
<MSBuild Projects="#(BuildFile)"
Properties="Configuration=%(Config.Configuration);Retries=10;RetryDelayMilliseconds=50"
Targets="Build" />
It works as expected but now it generates a warning before retrying the copy
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3001,9):
warning MSB3026: Could not copy
"D:\src\xxx\System.Data.SQLite.pdb" to "..\Debug\System.Data.SQLite.pdb".
Beginning retry 1 in 50ms. The process cannot access the file
'..\Debug\System.Data.SQLite.pdb' because it is being used by another process.
During my last test, it generated 36 times this warning! Is there a way for suppressing the warning MSB0326?

In general, anything that runs during build must not acquire an exclusive lock on its inputs - only shared-read lock. It appears that something in your build process (probably NLog, whatever that is) violates this - it takes exclusive lock on the input "xxx\NLog.dll", so when another msbuild node tries to copy the same input it fails.
Retry is a reasonable workaround for the particular symptom you have - though it's not guaranteed to always succeed.

I had the same problem. I needed to add appropriate exception to my AntiVirus to avoid sacanning DLLs produced by MsBuild.

Related

How do I put a condition on msbuild built-in targets like Build/Rebuild?

I am working differentially building a huge monolithic solution that includes about 80 projects. In my build pipeline right now I include a step to build the entire solution. But what I'd like to do is to build the solution but provide conditions as msbuild arguments so that I can exclude some of the projects that might not have any changes associated with them. I already have scripts to go through my commits and realize what changed and which projects need to be built.
I just need a way to send that info to MSBuild so that it does not build all projects everytime. I tried building projects separately but that takes a whole lot more time than just building the solution together.
So, I'm looking for any solutions out there through which I can specify to MSBuild that skip a specific project would help a lot. Thanks much!
I already have scripts to go through my commits and realize what
changed and which projects need to be built.
Since I could get clearly know that which script are you using to realize what changed and which projects need to be built. I am assuming that you are using MSbuildTarget script which in the xx.csproj to do these judgement.
=If I did not have misunderstanding, you can get help from this similar issue (See ilya's answer).
See this document and you'll find the build action is performed by these three targets, BeforeBuild,CoreBuild and AfterBuild. So assuming you have a target to go through my commits and realize what changed and if a project need to be built, you can add script like below to xx.csproj:
<PropertyGroup>
<BuildWrapperDependsOn>$(BuildDependsOn)</BuildWrapperDependsOn>
<BuildDependsOn>CheckIfBuildIsNeeded;BuildWrapper</BuildDependsOn>
</PropertyGroup>
<Target Name="CheckIfBuildIsNeeded">
<!-- Execute command here that checks if proceed with the build and sets the exit code -->
<Exec Command="exit /b 1" WorkingDirectory="$(SourcesPath)" IgnoreExitCode="true">
<Output TaskParameter="ExitCode" PropertyName="ExecExitCode"/>
</Exec>
<Message Text="Exit Code: $(ExecExitCode)" Importance="high" />
<PropertyGroup Condition="'$(ExecExitCode)' == '1'">
<DoBuild>false</DoBuild>
</PropertyGroup>
</Target>
<Target Name="BuildWrapper" Condition=" '$(DoBuild)' != 'false' " DependsOnTargets="$(BuildWrapperDependsOn)" Returns="$(TargetPath)" />
Above is the script from ilys, and hope my description can help you understand it. With this script, when we start a build target, it will firstly run the targets it depends on, so it will run the CheckIfBuildIsNeeded target and BuildWrapper target. And only when the DoBuild property is true, the BuildWrapper will actually execute. And since buildwrapper depends on original $(BuildDependsOn), it will continue the real build process.
The total logic is: Run CheckIfBuildIsNeeded script and output value to indicates whether need to build=>Try to Run BuildWrapper=>IF need to build, then run the real build success(BeforeBuild, Corebuild,Afterbuild), if the value is false, finish the build process. So I think you can do some little changes to this script then it can work for your situation. (Not sure what your script looks like, I can't complete it for you)
And since you have many projects, you don't need to add this script to every project manually. You can create a Directory.Build.props file, copy the script into it, and place the file in solution folder, then it will work for all projects in the solution.

Optional PreBuildEvent in MSBuild?

Is it possible to have an optional <PreBuildEvent> in a *.csproj file? I have the following:
<PropertyGroup>
<PreBuildEvent>git rev-parse HEAD >../../git-hash.txt</PreBuildEvent>
</PropertyGroup>
This outputs the latest git hash to a file, which is embedded in the executable elsewhere.
Since I'm a University student, I'm often writing code on the University machines (and not my linux machine at home) which have SVN and not git, causing the build process to fail. Is it possible to make the above <PreBuildEvent /> optional so that if git isn't installed the build process doesn't fail?
Just skipping the build event would leave you with an empty git-hash.txt so that doesn't seem the best idea. Instead you could just try to run the git command, and if it fails write a dummy hash to the file. I don't know the command line syntax to do that (a PreBuildEvent runs under cmd.exe) so here's an msbuild solution. Because of the BeforeTargets="Build" it will run before the build as well.
<Target Name="WriteGitHash" BeforeTargets="Build">
<Exec Command="git --work-tree=$(Repo) --git-dir=$(Repo)\.git rev-parse HEAD 2> NUL" ConsoleToMSBuild="true" IgnoreExitCode="True">
<Output TaskParameter="ConsoleOutput" PropertyName="GitTag" />
</Exec>
<PropertyGroup>
<GitTag Condition="'$(GitTag)' == ''">unknown</GitTag>
</PropertyGroup>
<WriteLinesToFile File="$(Repo)\git-hash.txt" Lines="$(GitTag)" Overwrite="True"/>
</Target>
Some notes:
The 2> NUL redirects standard error to the output so GitTag will be empty in case of an error, in which case it's set to 'unknown'
Relying on the current directory is nearly always a bad idea so specify the directory to run git in explicitly in a property
Same for the output file

Integrate PVS-Studio into MSBuild file

I have many projects that I have created a common MSBuild file to run against. I am attempting to integrate PVS Studio's static analysis into the build without having it build a second time. I followed a bit of the documentation on PVS's site, but I must be missing something.
I say that because when I build the PVS does not seem to my triggered/called.
Does anybody have any experience with this and could give me a hand?
Here is the PVS bit of my build file.
<UsingTask TaskName="ProgramVerificationSystems.PVSStudio.PVSStudio"
AssemblyFile="C:\Program Files (x86)\PVS-Studio\PVS-Studio-MSBuild.dll" />
<Target Name="PVSStudioAnalysisBeforeCompile" BeforeTargets="ClCompile">
<Exec Command="echo PVSStudio initiating now."/>
<PVSStudio Condition="'%(ClCompile.ExcludedFromBuild)'!='true'"
Sources="#(ClCompile)"
BeforeClCompile="true"
BuildingInIDE="false"
TrackerLogDirectory="%(ClCompile.TrackerLogDirectory)"
PreprocessorPath="$(VCInstallDir)"
Platform="$(Platform)"
ProjectFullPath="$(MSBuildProjectFullPath)"
SolutionDir="$(SolutionDir)">
<Output TaskParameter="SourcesAfterTlogParsing"
ItemName="CLCompileAfterTlogParsing" />
</PVSStudio>
</Target>
<Target Name="PVSStudioAnalysisAfterCompile" AfterTargets="ClCompile">
<PVSStudio Sources="#(CLCompileAfterTlogParsing)"
BeforeClCompile="false"
BuildingInIDE="$(BuildingInsideVisualStudio)"
PreprocessorPath="$(VCInstallDir)"
OutputFilePath ="$(OutputDir)"
Platform="$(Platform)"
ProjectFullPath="$(MSBuildProjectFullPath)"
SolutionDir="$(SolutionDir)" />
<Exec Command="echo PVSStudio finished"/>
I'm sure you all will need a bit more info to figure this out so let me know what I should get for you.
Thanks,
TBG
You should do one of the following:
If you want analyzer's output to be saved to a file, the you should set BuildingInIDE attribute to false in both tasks, you should also specify a file for the output to be saved to, for example, OutputFilePath = "$(OutputDir)"/pvs.log.
You will be able to view such log from PVS-Studio IDE plug-in / Standalone by opening it as unparsed log.
If you want to build your project from inside Visual Studio and PVS-Studio plug-in to hook up the analyzer results to it output window immediately, then you should set both BuildingInIDE attributes to true (or "$(BuildingInsideVisualStudio)") and enable the MSBuild mode by going to PVS-Studio -> Options -> Specific Analyzer Settings -> MSBuild Output Log Monitoring and set it to true.

How do I add a custom build target to a Visual C++ 2010 project?

There are plenty of guides out there which help you mimic VS2008's "Custom Build Step" in VS2010 with MSBuild. However, I'd like my build to be smarter and make use of MSBuild. I've written a little MSBuild task which invokes the ANTLR parser generator. That build task works flawlessly when I run it in a simple test MSBuild file. However, when I try to add my task to a C++ project, I run into problems. Essentially I've added this to the top of my project file (Right after the <project> element):
<UsingTask TaskName="ANTLR.MSBuild.AntlrGrammar"
AssemblyName = "ANTLR.MSBuild, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d50cc80512acc876" />
<Target Name="BeforeBuild"
Inputs="ConfigurationParser.g"
Outputs="ConfigurationParserParser.h;ConfigurationParserParser.cpp;ConfigurationParserLexer.h;ConfigurationParserLexer.cpp">
<AntlrGrammar
AntlrLocation="$(MSBuildProjectDirectory)Antlr.jar"
Grammar="ConfigurationParser.g"
RenameToCpp="true" />
</Target>
However, my target is not being called before build.
How can I add my task to a C++ build?
Before reading this answer, you'll probably want to see:
General .vcxproj File Reference
The New Way of doing Build Extensibility in .NET 4
The old way of extending MSBuild, and the one mentioned by the reference book I have, essentially is based on overriding default-empty targets supplied by Microsoft. The new way, as specified in the second link above, is to define your own arbitrary target, and use the "BeforeTargets" and "AfterTargets" properties to force your target to run before or after your intended target.
In my specific case, I needed the ANTLR Grammars task to run before the CLCompile target, which actually builds the C++ files, because the ANTLR Grammars task builds .cpp files. Therefore, the XML looks like this:
<Project ...
<!-- Other things put in by VS2010 ... this is the bottom of the file -->
<UsingTask TaskName="ANTLR.MSBuild.AntlrGrammar"
AssemblyName = "ANTLR.MSBuild, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d50cc80512acc876" />
<Target Name="AntlrGrammars"
Inputs="Configuration.g"
Outputs="ConfigurationParser.h;ConfigurationParser.cpp;ConfigurationLexer.h;ConfigurationLexer.cpp"
BeforeTargets="ClCompile">
<AntlrGrammar
AntlrLocation="$(MSBuildProjectDirectory)\Antlr.jar"
Grammar="Configuration.g"
RenameToCpp="true" />
</Target>
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
</Project>
As for why this is superior to a PreBuildEvent and/or PostBuildEvent; this is smart enough to not rebuild the .cpps when the grammar itself is not updated. You'll get something like:
1>AntlrGrammars:
1>Skipping target "AntlrGrammars" because all output files are up-to-date with respect to the input files.
1>ClCompile:
1> All outputs are up-to-date.
1> All outputs are up-to-date.
This also silences Visual Studio's incessant complaining every time you run the program that it needs to rebuild things, like it does with plain pre- and post- build steps.
Hope this helps someone -- took me frickin forever to figure out.

ant cpptask with ivy

A company I am working for, has some c binaries build with ant using cpptask. They use ivy to retrieve shared c libraries every time we start a build which wastes a significant amount of time comparing the revisions and downloading, when then only need to be download if the header files have changed. I have added a target which sets a var, which causes the build to skip over the ivy steps but I'd like a better solution. I see that cpptask creates a file history.xml and only rebuilds to binary if any of the sources have change. I'd like to know if there is way to independently test if the binary needs to build, and it does, I'd like it fire off the ivy targets. I'd also like for a variable to be set if the binary was rebuilt so that I can conditionally start an rpm generation task
<project name="conditional_compile" default="build">
<condition property="file.modified">
<isfileselected file="test.txt">
<modified/>
</isfileselected>
</condition>
<target name="build" if="file.modified">
<echo message="This is a compile step that depends on the modification of a file"/>
</target>
</project>