{"id":4543,"date":"2009-04-01T17:49:00","date_gmt":"2009-04-01T17:49:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2009\/04\/01\/vc-msbuild-extensibility-example\/"},"modified":"2019-02-18T18:45:56","modified_gmt":"2019-02-18T18:45:56","slug":"vc-msbuild-extensibility-example","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/vc-msbuild-extensibility-example\/","title":{"rendered":"VC MSBuild Extensibility Example"},"content":{"rendered":"<p><font face=\"Calibri\"><\/p>\n<p class=\"MsoNormal\"><font size=\"3\">Hello, my name is Felix Huang and I am a developer on the Visual C++ team. &nbsp;Over the last year, I worked on making the build system in Visual Studio 2010 more flexible with MSBuild system as the foundation.&nbsp; For those who felt the need to use batch scripts to power your builds, I hope that this new system would provide you with that much flexibility while maintaining a similar UI feeling as the previous version of Visual Studio.&nbsp; Previous article written by <\/font><a href=\"http:\/\/blogs.msdn.com\/vcblog\/archive\/2008\/11\/20\/printf-hello-msbuild-n.aspx\"><font size=\"3\">Marian Luparu<\/font><\/a><font size=\"3\"> touches more on the benefits of MSBuild.&nbsp; In this post, I will provide an example augmenting the build system and property page to better suit development life cycle.<\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\">Suppose you want to add another switch to an existing tool.&nbsp; Let\u2019s take \/WS:AGGRESSIVE found in link.exe as an example.&nbsp; We create a file called myfavorite.xml next to a project.&nbsp; This file describes the structure of the UI property page.&nbsp; We want to use a Boolean switch inside the Optimization category.&nbsp; By copying and pasting comments from MSDN, we get the snippet below.<\/font><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&lt;ProjectSchemaDefinitions xmlns=&#8221;clr-namespace:Microsoft.Build.Framework.XamlTypes;assembly=Microsoft.Build.Framework&#8221; xmlns:x=&#8221;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml&#8221; xmlns:sys=&#8221;clr-namespace:System;assembly=mscorlib&#8221;&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&lt;Rule Name=&#8221;MyFavoriteOptions&#8221; PageTemplate=&#8221;generic&#8221;&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp; &lt;Rule.DisplayName&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp; &lt;sys:String&gt;My Favorite Options&lt;\/sys:String&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp; &lt;\/Rule.DisplayName&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp; &lt;Rule.Categories&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp; &lt;Category Name=&#8221;Optimization&#8221;&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Category.DisplayName&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;sys:String&gt;Optimization&lt;\/sys:String&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/Category.DisplayName&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp; &lt;\/Category&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp; &lt;\/Rule.Categories&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp; &lt;Rule.DataSource&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp; &lt;DataSource Persistence=&#8221;ProjectFile&#8221; ItemType=&#8221;&#8221;\/&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp; &lt;\/Rule.DataSource&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp; &lt;BoolProperty Switch=&#8221;WS:AGGRESSIVE&#8221; Name=&#8221;WSAggressive&#8221; Category=&#8221;Optimization&#8221;&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp; &lt;BoolProperty.DisplayName&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;sys:String&gt;Aggressively Trim Process Memory&lt;\/sys:String&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp; &lt;\/BoolProperty.DisplayName&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp; &lt;BoolProperty.Description&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;sys:String&gt;Use the \/WS:AGGRESSIVE option to add the WS_AGGRESSIVE attribute to your application&#8217;s image. The Windows NT 4.0 (and later) loader will recognize this attribute and aggressively trim the working set of the process when it is not active.&lt;sys:String&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp; &lt;\/BoolProperty.Description&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp; &lt;\/BoolProperty&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&lt;\/Rule&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><span>&lt;\/ProjectSchemaDefinitions&gt;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\">Refer to <\/font><a href=\"http:\/\/blogs.msdn.com\/vcblog\/archive\/2008\/12\/16\/msbuild-task.aspx\"><font size=\"3\">Li Shao\u2019s blog<\/font><\/a><font size=\"3\"> on the topic for writing xml.&nbsp; Restart Visual Studio to load the changes.&nbsp;&nbsp;<\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><\/font>&nbsp;<\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/vcmsbuildpic.png\"><a class=\"\" title=\"Photo\" href=\"https:\/\/docs.microsoft.com\/en-us\/archive\/blogs\/\"><\/a><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><\/font>&nbsp;<\/p>\n<p class=\"MsoNormal\"><font size=\"3\">Next, we will attach the switch to the target file, thus passing it to the linker during execution.&nbsp; There are many ways to do this; but let\u2019s use a new feature in MSBuild 4.0 called Before\/After targets.&nbsp; Inside one of your project file (.vcxproj), add the &lt;Target&gt; below to the &lt;Project&gt; scope.&nbsp; By using the BeforeTargets, it will insert this target into the execution of the build before the link target gets run.&nbsp; Thus, we are able to modify the value in the Additional Options appending our switch to the end.&nbsp; <\/font><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp; &lt;ItemGroup&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp; &lt;PropertyPageSchema<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Include=&#8221;$(MSBuildThisFileDirectory)\\myfavorite.xml&#8221; \/&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp; &lt;\/ItemGroup&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp; &lt;Target Name=&#8221;WSAggressiveTarget&#8221; BeforeTargets=&#8221;Link&#8221;&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp; &lt;ItemGroup&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Link&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;AdditionalOptions Condition=&#8221;&#8216;$(WSAggressive)&#8217;==&#8217;true'&#8221;&gt;%(Link.AdditionOptions) \/WS:Aggressive&lt;\/AdditionalOptions&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/Link&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">&nbsp;&nbsp;&nbsp; &lt;\/ItemGroup&gt;<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><span>&nbsp; &lt;\/Target&gt;<\/span>&nbsp;&nbsp; <\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><\/font>&nbsp;<\/p>\n<p class=\"MsoNormal\"><font size=\"3\">The PropertyPageSchema ItemGroup will let the IDE to know where to look for the xml.&nbsp; Now your project is ready for building.<\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\">Thanks for reading.<\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\">Felix Huang<\/font><\/p>\n<p><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, my name is Felix Huang and I am a developer on the Visual C++ team. &nbsp;Over the last year, I worked on making the build system in Visual Studio 2010 more flexible with MSBuild system as the foundation.&nbsp; For those who felt the need to use batch scripts to power your builds, I hope [&hellip;]<\/p>\n","protected":false},"author":289,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4543","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus"],"acf":[],"blog_post_summary":"<p>Hello, my name is Felix Huang and I am a developer on the Visual C++ team. &nbsp;Over the last year, I worked on making the build system in Visual Studio 2010 more flexible with MSBuild system as the foundation.&nbsp; For those who felt the need to use batch scripts to power your builds, I hope [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/4543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/289"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=4543"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/4543\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=4543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=4543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=4543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}