user1707330
user1707330

Reputation: 81

MSBuild/VS2010: How to reference "RuntimeLibrary" compiler setting in a VS2010 "Property Sheet"

I am writing a Visual Studio 2010 property sheet to integrate a complex 3rd party C++ library.

To determine what pieces of the library I need to link to my projects (as well as configuring various defines, includes, directories, etc.), my property sheet needs to determine the project's currently configured C runtime library (i.e. "MultiThreaded", "MultiThreadedDebug", "MultiThreadedDLL", or "MultiThreadedDebugDLL").

However, as a substantially similar question here on stackoverflow pointed out, this MSBuild conditional does not work:

Condition = " '$(RuntimeLibrary)' == 'MultiThreadedDLL' "

Another option was provided, but it was for a subsequent build task. I need this value before ever getting to the build.

I've also scoured Google and Microsoft's MSDN website looking for a way to get this value and have come up empty. Any ideas?

Upvotes: 5

Views: 2104

Answers (1)

user1707330
user1707330

Reputation: 81

Since there was no way via MSBuild's XML to directly get the configured runtime library, I regex'ed the project file. Here is the XML PropertyGroup snippet to do this:

<PropertyGroup Label="UserMacros">
  <RuntimeLibraryRegex>
    <![CDATA[<ItemDefinitionGroup Condition=".*']]>$(Configuration)\|$(Platform)<![CDATA['">(?:.*\n)*?.*<RuntimeLibrary>(.*)</RuntimeLibrary>(?:.*\n)*?.*</ItemDefinitionGroup>]]>
  </RuntimeLibraryRegex>
  <RuntimeLibrary>
    $([System.Text.RegularExpressions.Regex]::Match($([System.IO.File]::ReadAllText($(MSBuildProjectFullPath))), $(RuntimeLibraryRegex)).Result('$1'))
  </RuntimeLibrary>
</PropertyGroup>

Now the Condition statement in the question will work as-is.

Also, please note that this MSBuild property group XML does not take into account runtime library default (e.g. if the project doesn't have the runtime library set) but can be made to easily.

Upvotes: 3

Related Questions