24

As part of one of my projects, there are "BeforeBuild" tasks that ultimately generate some files. In particular, it compiles a small static class (included as "do not compile" in the project) into it's own executable and then executes it, passing in an external input file outputting a new generated class to be included in the project.

I programmed it to put the intermediate files in the projects $(OutDir), but found that on "Rebuild" (and ultimately "Clean"), these files aren't picked up. After some thought, I realized that the final, generated class which is placed right in $(ProjectDir) should probably be deleted on "Clean" too.

Some investigation into Microsoft.Common.targets revealed that there was some "master list" from the intermediate path (obj\build\assembly.FileListAbsolute.txt) that was queried for the files to delete.

Is there some standard method of adding my new files to this list in MSBuild to have them cleaned up, or would this sort of thing fit better in a "BeforeClean" (or "AfterClean") target override?

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Jeff Wight
  • 833
  • 5
  • 11

1 Answers1

23

See Extending the Clean Process for details of the FileWrites mechanism (which is the system writing the FileListAbsolute.txt you're seeing) in this MSBuild article by Hashimi. And get the book right now if you're going to spend more than 2 hours writing build scripts in the next year.

Carl Walsh
  • 6,100
  • 2
  • 46
  • 50
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
  • Perfect! *Extending the Clean Process* was exactly what I was looking for! I really hope to not have to spend more than 2 hours on build scripts over the next year, but that book will likely find its way to my desk in the near future. – Jeff Wight Aug 17 '10 at 16:16
  • @Jeff Wight: You can't hide from it -- it's well worth understanding MSBuild-ese, and you'll even come to appreciate it as elegant in time! http://stackoverflow.com/questions/47884/is-it-worth-learning-to-use-msbuild and http://stackoverflow.com/questions/1326445/is-there-a-good-tutorial-on-msbuild-scripts – Ruben Bartelink Aug 17 '10 at 16:32
  • 7
    Yeah I agree with Ruben get the book! – Sayed Ibrahim Hashimi Sep 01 '10 at 04:01
  • 1
    The MSBuild article referenced is from 2009, so when it says specifically "C# and VB.NET projects" I think in general that it means "projects that use MSBuild". In VS2010 C++ was added to that list. But, FileListAbsolute.txt is not used for C++ it seems. – JDługosz Sep 09 '14 at 20:56
  • @jdlugosz Interesting, have never had much cause to understand this, but I have read a newer version of the book and don't recall it describing al alternate mechanism for a clean step to be able to operate 'cleanly' (then again I don't recall any mention of C++) either! – Ruben Bartelink Sep 10 '14 at 10:57
  • @RubenBartelink Part V MSBuild in Visual C++ 2010, pages 267–347. I just got a copy of the latest edition, now titled “Inside the Microsoft Build Engine; Using MSBuild and Team Foundation Build”, 2nd edition, ISBN 978-0-7356-4524-0, circa 2010. This week I traced through a flattened cppxproj file and see that the CL build/clean makes no use of such a file, and none are present on my disk. – JDługosz Sep 10 '14 at 15:27
  • from the article: "There are two drawbacks to doing this, however. [...] you must append items to that list before the clean cache is written to disk, which occurs in either the Clean or IncrementalClean target, depending on the type of build that is executed. These targets will be called before the Build target completes." I have an msbuild task that runs with `AfterTargets="Build"` (as it depends on something built in this project). It is unclear how to mark these files generated at this stage for deletion. Must this be a manual step? – ElFik Jul 05 '18 at 23:05
  • 1
    Getting that book is the best advice anyone can give to a developer working on msbuild. Even today with many of the changes to the project system it is still one of the best resources on msbuild and does an amazing job explaining the intent of the system. – Max Young Dec 19 '18 at 19:05
  • Even if the book is great, the question should be answered in the answer. This is a bad answer because it does not do that. – john01dav Sep 29 '20 at 07:18
  • @john01dav Feel free to edit in a summary of a 22m article ;) And/or put in an actual answer - I would be absolutely fine with the OP accepting a better answer. And if you are doing a lot of MSBuild stuff, IMO the book is def worth having and/or reading, even 10 years on... – Ruben Bartelink Sep 29 '20 at 07:52