msbuild - incremental build for .dll
Asked Answered
B

0

1

Team develops C++ Utils project using the Visual Studio 2022. I have read about incremental builds: https://learn.microsoft.com/en-us/visualstudio/msbuild/how-to-build-incrementally?view=vs-2022 https://learn.microsoft.com/en-us/visualstudio/msbuild/incremental-builds?view=vs-2022

I am running msbuild and output of the build is Utils.dll file. That works fine, but if we re-run msbuild command currently .dll is overridden. So that is why I want to implement increment build so in case source files are changed only in that case .dll should be re-generated.

My simplified folder structure looks like this:

base/Utils
base/Utils/Atomic.cpp
base/Utils/x64/Release/Utils.dll

In that regards, I have defined Target segment to implement increment builds.

<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <ItemGroup>
            <CppFiles Include="Atomic.cpp"/>
            <DllFiles Include="x64\Release\Utils.dll"/>
      </ItemGroup>
      <Target Name = "Build"
            Inputs="@(CppFiles)"
            Outputs="@(DllFiles)">
      </Target>
</Project>

In this case, skipping works (if nothing is changed in Source file .dll file remains the same and it is not overwritten). That is good.

But if I change source .cpp file, unfortunately .dll is not incremented and re-built, but the older version remains (.dll file is not changed) What I am missing in the configuration?

Thanks a lot!!!

msbuild references my Utils.vcxproj, and we run msbuild from the level of the CI tool (not from IDE).

Butyraceous answered 22/6, 2023 at 14:30 Comment(14)
Are you sure you need to hand-edit the .vcxproj file to achieve this? I just enable incremental builds in the IDE.Cindycine
Hi @PaulSanders, thanks for the feedback but our use case is that builds are run from CI tool, not from IDE. Can you maybe assist us having in mind this fact? Thank youButyraceous
My experience is that, when building, MSBuild will do whatever building in the IDE would do. What I meant was that I enable incremental builds in the project's property pages and then it all 'just works'. In fact, if no source files that need to be built for your DLL have changed then building it should do nothing anyway. Incremental builds perhaps don't mean what you think they mean.Cindycine
@PaulSanders well yes, I have explained that if no source files have changed then building should do nothing. That WORKS!!! Other part does not work - if source is changed - nothing is triggered again and nothing is built.Butyraceous
Perhaps I haven't made myself clear: it shouldn't be necessary to monkey with your .vcxproj file to get VS to only build your DLL when one or more of the source files has changed. Now that you have altered it, I wouldn't like to predict what MSBuild will make of it. If the project is not too complex, it might be better to start again and create a new project file from scratch. Like I say, I've never had a situation where VS does the wrong thing in this regard.Cindycine
@PaulSanders I had to altered it since we were always getting that .dll is overwritten, although no source files are changed. Is this regular situation? What happens in your case? Does your .dll get overwritten although it is with the same size and nothing is changed, only timestamp of .dll is different. Also can you tell where exactly in VS you enable increments?Butyraceous
No, that's not the behaviour I see. When no source files have changed, building does nothing. Incremental builds are actually something different - when VS (or MSBuild) determines that your DLL does need to be rebuilt, it tries to do so with the least amount of work. This is to speed up the build, and is the default for Debug builds. In fact, I'm note sure if it's supported for Release builds at all, you'd have to check.Cindycine
@PaulSanders I am investigating further and I came across <EnableIncrementalBuild>true</EnableIncrementalBuild> property that can be added in .vcxproj? maybe its related to that?Dollar
I thought I had explained the situation. Your problem has nothing to do with incremental builds.Cindycine
@PaulSanders ok if I am missing term incremental build - how I can achieve that my output .dll is not re-generated in case no source files are changed? thank youButyraceous
Also I thought that incremental build will skip creation of .dll because that is stated in Incremental documentation learn.microsoft.com/en-us/visualstudio/msbuild/… If all output items are up-to-date, MSBuild skips the target. Butyraceous
I can only repeat what I said before: create a new project for your DLL in VS, import your source files into it and see if the problem goes away. I would expect it to - this is how VS works by default.Cindycine
You can use MSBuild to build a project created by VS from the command line, BTW. I do it all the time.Cindycine
Checked up on a few things. I think you're right about the term 'incremental build'. I was thinking of incremental linking, which is a different thing. But my advice doesn't change. Don't hand-craft .vcxproj files - let Visual Studio do all the hard workCindycine

© 2022 - 2024 — McMap. All rights reserved.