Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild ProjectReference item documentation

Tags:

I cannot find documentation about ProjectReference tag in MSBuild projects. Where can I read detailed description of it?

Edit: I have a .vcxproj created by others. It contains ProjectReference item. ProjectReference contains subtags: Private, ReferenceOutputAssembly, CopyLocalSatelliteAssemblies, LinkLibraryDependencies, UseLibraryDependencyInputs. Where I can read about those tags? Which values can they contain? What other subtags can ProjectReference contain?

I have searched in MSDN and Google but have not found documentation pages, only discussions and documentation about other products, not MSBuild.

like image 752
Andrey Epifantsev Avatar asked Jun 22 '12 08:06

Andrey Epifantsev


People also ask

What is ItemGroup in MSBuild?

In addition to the generic Item element, ItemGroup allows child elements that represent types of items, such as Reference , ProjectReference , Compile , and others as listed at Common MSBuild project items.

Will MSBuild compile a file without any target?

If MSBuild determines that any output files are out of date with respect to the corresponding input file or files, then MSBuild executes the target. Otherwise, MSBuild skips the target. After the target is executed or skipped, any other target that lists it in an AfterTargets attribute is run.

What is ItemGroup?

An item group is a collection of products which share similar attributes like color, production, features, or usage. Item groups can also be formed based on the markets in which they're sold or if they're similar in price.

What is the difference between MSBuild and Visual Studio build?

Visual Studio determines the build order and calls into MSBuild separately (as needed), all completely under Visual Studio's control. Another difference arises when MSBuild is invoked with a solution file, MSBuild parses the solution file, creates a standard XML input file, evaluates it, and executes it as a project.


2 Answers

Starting from the MSBuild source code links that Jason Pyeron provided in his comment, I learned that when MSBuild prepares build dependencies, it includes all item metadata (what you refer to as subtags) from each ProjectReference item. As a result, downstream Tasks and Targets can and sometimes do read arbitrary ProjectReference metadata.

For answers to your questions about C++ projects, you can examine Microsoft.CppBuild.targets and Microsoft.CppCommon.targets (their default path in MSBuild 14, coinciding with Visual Studio 2015, is C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\). As the following example shows, however, it is not simple to do so:

  1. In Microsoft.CppBuild.targets, the Target ResolvedXDCMake creates _ResolvedNativeProjectReferencePaths Items dynamically.
  2. From those Items, the Target ComputeReferenceLinkInputs creates ProjectReferenceToLink items dynamically.
  3. For each of those Items that do not have CopyLocal metadata, the same Target adds it, copying any Private metadata value.
  4. For each those Items with a distinct path, the same target creates a Link item dynamically.
  5. Switching now to Microsoft.CppCommon.targets, Link items are passed in the Sources parameter of the Link Task in the Link Target! Although to be fair, their metadata was cleared in the previous step, so you do not have to dive into the Link Target documentation in this particular case.

Here are additional portions that relate your question:

Parameters

  • Include (attribute): Path to project file
  • Project (metadata): Project GUID, in the form {00000000-0000-0000-0000-000000000000}
  • ReferenceOutputAssembly (metadata): Boolean specifying whether the outputs of the project referenced should be passed to the compiler. Default is true.
  • SpecificVersion (metadata): Whether the exact version of the assembly should be used.
  • Targets (metadata): Semicolon-separated list of targets in the referenced projects that should be built. Default is the value of $(ProjectReferenceBuildTargets) whose default is blank, indicating the default targets.
  • OutputItemType (metadata): Item type to emit target outputs into. Default is blank. If ReferenceOutputAssembly is set to "true" (default) then target outputs will become references for the compiler.
  • EmbedInteropTypes (metadata): Optional boolean. Whether the types in this reference need to embedded into the target assembly - interop asemblies only

Remarks

When the OutputItemType parameter is used, additional parameters (metadata) may be applicable. For example, when OutputItemType is set to Content, CopyToOutputDirectory can be used:

  • CopyToOutputDirectory (metadata): Optional string. Determines whether to copy the file to the output directory. Values: Never, Always, PreserveNewest.
like image 56
weir Avatar answered Sep 28 '22 11:09

weir


You will notice that the ProjectReference element is a child of an ItemGroup element.

ItemGroups are a fully documented schema item thankfully. What you will find is that child elements of ItemGroups can by anything. Think of it as the name of a collection. That item within the ItemGroup can have "metadata" values.

For example,

<ItemGroup>
    <WindowsFiles Include="C:\Windows\*">
       <IsSystem>true</IsSystem>
    </WindowsFiles>
</ItemGroup>

This is defining an itemgroup called WindowsFiles. It essentially becomes a collection of files that match the Include attribute. All item group items have metadata built in - such as filename, extension, fullpath, directory, etc. but you can add your own - in this case IsSystem is an additional one.

Referencing item groups is done in one of two ways:

<Message Text="%(WindowsFiles.FullPath)"/>

<Message Text="@(WindowsFiles->'%(FullPath)')"/>

The latter is referred to as a transform which is more advanced. Best stick to the former until you get your head around ItemGroups or transforms just won't go in.

The ProjectReference itemgroup you are interested in will be processed by a targets file somewhere. As itemgroups are fairly arbitrary in what they are called, conceptually they are variables so it's how they get processed by the targets file that defines the usage.

Work your way up the files mentioned in the Import statements to see where the ProjectReference item group gets consumed.

like image 45
daughey Avatar answered Sep 28 '22 10:09

daughey