Exploring the New MSBuild Features for Visual C++ 2010, Page 2
Tasks in MSBuild take Items and Properties, and complete an activity. Examples of tasks include compiling source code files, signing binary images produced by the build, or sending an email if the build fails. Even though it is possible to use an XML CDATA section to write tasks inline in a MSBuild XML file, tasks generally will be written in a managed language, and will be in the form of a managed class that implements the ITask interface. A Task can expose any number of user-defined properties that are specified in the MSBuild file and passed through to the Task by the MSBuild engine when the Task object is created. When a Task needs to be kicked off by the MSBuild engine, the ITask.Execute method is called.
A task can be imported into a MSBuild file by using the UsingTask element. To access the C++ compilers CppClean task in a MSBuild file, the following declaration needs to be included:
<UsingTask TaskName="CPPClean" AssemblyName="Microsoft.Build.CPPTasks.Common, Version=126.96.36.199, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <ItemGroup> <CoreCPPClean Include="$(IntDir)" /> <CoreCPPClean Include="$(OutDir)"> <FilePatternsToDelete>$(TargetName).exe; $(TargetName).dll; $(TargetName).lib; $(TargetName).pdb; $(TargetName).ilk </FilePatternsToDelete> </CoreCPPClean> </ItemGroup> <CPPClean FoldersToClean = "@(CoreCPPClean)" FilePatternsToDeleteOnClean = "%(CoreCPPClean.FilePatternsToDelete)" />
Targets are the externally visible part of a MSBuild file, and represent a grouping of tasks in a particular order to produce an action that is meaningful to the end user. The CPPClean task can be wrapped in a Target as follows:
<Target Name="CleanTheBuild"> <Message Text="Cleaning The Build" /> <CPPClean FoldersToClean = "@(CoreCPPClean)" FilePatternsToDeleteOnClean = "%(CoreCPPClean.FilePatternsToDelete)" /> </Target>
If this target is included in a file called Win32ConsoleApp.vcxproj (vcxproj is the file extension for Visual C++ projects that use MSBuild), the CleanTheBuild target can be activated by the following command line statement:
MSBuild.exe Win32ConsoleApp.vcxproj /target:CleanTheBuild
A MSBuild file also can specify a default target, which will be the compile action for a vcxproj file. The default target is called by the MSBuild engine when no /target command line switch is supplied.
The move of Visual C++ onto the MSBuild platform brings C++ inline with Microsoft's other compiler offerings that all use this format. Visual C++ 2010 is deeply integrated into MSBuild, with all elements of the build process fully visible and accessible in the new VCXPROJ project file format.
MSBuild is a documented, powerful, and extensible build engine that far exceeds the capabilities of the previous Visual C++ build engine. MSBuild takes Items and associated Properties as inputs into a Task, and Tasks are grouped together into Targets to form a high-level build activity. MSBuild will be welcomed by C++ developers who miss the power and expressiveness of MAK files, and for developers who have yet to explore the world of build customization and extension, the introduction of MSBuild as the project file format for Visual C++ 2010 represents the best time to get started.
About the Author
Nick Wienholt is an independent Windows and .NET consultant based in Sydney. He is the author of Maximizing .NET Performance and co-author of A Programmers Introduction to C# 2.0 from Apress, and specialises in system-level software architecture and development, with a particular focus of performance, security, interoperability, and debugging.
Nick is a keen and active participant in the .NET community. He is the co-founder of the Sydney Deep .NET User group and writes technical articles for Australian Developer Journal, ZDNet, Pinnacle Publishing, CodeGuru, MSDN Magazine (Australia and New Zealand Edition), and the Microsoft Developer Network. An archive of Nick's SDNUG presentations, articles, and .NET blog is available at www.dotnetperformance.com. In recognition of his work in the .NET area, he was awarded the Microsoft Most Valued Professional Award from 2002 through 2007.
Page 2 of 2