summaryrefslogtreecommitdiff
path: root/buildvertical.targets
blob: 891061899dc0e32c61b481539e2af7b72590d68f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <UsingTask TaskName="FindBestConfigurations" AssemblyFile="$(CoreFxToolsTaskDir)CoreFx.Tools.dll"/>
  <PropertyGroup>
    <ImportedBuildVerticalTargets>true</ImportedBuildVerticalTargets>
    <_traversalBuildConfigurations>$(BuildConfiguration)</_traversalBuildConfigurations>
    <_traversalBuildConfigurations Condition="'$(AdditionalBuildConfigurations)' != ''">$(_traversalBuildConfigurations);$(AdditionalBuildConfigurations)</_traversalBuildConfigurations>
  </PropertyGroup>

  <!-- Import configuration data model -->
  <Import Project="$(MSBuildThisFileDirectory)src/Tools/GenerateProps/properties.props" />

  <!-- Runs during traversal when BuildAllConfigurations is set to expand each project
       to a seperate instance for each of its BuildConfigurations -->
  <Target Name="ExpandAllBuildConfigurations"
          Condition="'$(BuildAllConfigurations)' == 'true'">
    <MSBuild Targets="GetBuildConfigurations"
             Projects="@(Project)"
             Condition="'$(BuildAllConfigurations)' == 'true'">
      <Output TaskParameter="TargetOutputs"
              ItemName="_projectBuildConfigurations" />
    </MSBuild>

    <ItemGroup>
      <!-- assign configuration as a seperate step to prevent batching during the transform which can reorder the list.
           order or projects matters here since this applies to traversal projects which build projects serially. -->
      <_projectBuildConfigurations>
        <AdditionalProperties>Configuration=%(Identity);%(_projectBuildConfigurations.AdditionalProperties)</AdditionalProperties>
      </_projectBuildConfigurations>

      <!-- transform back to project -->
      <_projectWithConfiguration Include="@(_projectBuildConfigurations->'%(OriginalItemSpec)')" />
    </ItemGroup>

    <ItemGroup>
      <Project Remove="@(Project)" />
      <Project Include="@(_projectWithConfiguration)" />
    </ItemGroup>
  </Target>

  <!-- Runs during traversal to select which projects and configurations of those projects to build
       Batches over the projects because we need to treat BuildConfigurations as a Property
       as well as copy the Project based on results of FindBestConfigurations. -->
  <Target Name="FilterBuildConfiguration"
          Condition="'$(BuildAllConfigurations)' != 'true'"
          Inputs="%(Project.Identity)"
          Outputs="unused">

    <MSBuild Targets="GetBuildConfigurations"
             Projects="@(Project)">
      <Output TaskParameter="TargetOutputs"
              PropertyName="_projectBuildConfigurations" />
    </MSBuild>

    <FindBestConfigurations Properties="@(Property)"
                            PropertyValues="@(PropertyValue)"
                            SupportedConfigurations="$(_projectBuildConfigurations)"
                            Configurations="$(_traversalBuildConfigurations)">
      <Output TaskParameter="BestConfigurations" ItemName="_projectBestConfigurations" />
    </FindBestConfigurations>

    <ItemGroup>
      <!-- Apply configuration, this does a catesian product between @(Project) and @(_projectBestConfigurations) -->
      <_projectWithConfiguration Include="@(Project)" Condition="'@(_projectBestConfigurations)' != ''">
        <AdditionalProperties>Configuration=%(_projectBestConfigurations.Identity);%(_projectWithConfiguration.AdditionalProperties)</AdditionalProperties>
      </_projectWithConfiguration>
      <Project Remove="@(Project)"/>
      <Project Include="@(_projectWithConfiguration)"/>
    </ItemGroup>
  </Target>

  <Target Name="FilterProjects"
          DependsOnTargets="ExpandAllBuildConfigurations;FilterBuildConfiguration" />

  <!-- Runs in a leaf project (eg: csproj) to determine all configurations -->
  <Target Name="GetBuildConfigurations"
          Returns="$(_AllBuildConfigurations)">
     <PropertyGroup>
       <_AllBuildConfigurations>$(BuildConfigurations)</_AllBuildConfigurations>
       <_AllBuildConfigurations Condition="'$(BuildConfigurations)' == ''">$(_traversalBuildConfigurations)</_AllBuildConfigurations>
     </PropertyGroup>
  </Target>

  <!-- Runs in a leaf project (csproj) to determine best configuration for ProjectReferences -->
  <Target Name="AnnotateProjectReference"
          BeforeTargets="AssignProjectConfiguration"
          Condition="'@(ProjectReference)' != ''"
          Inputs="%(ProjectReference.Identity)"
          Outputs="unused">

    <MSBuild Targets="GetBuildConfigurations"
             Projects="@(ProjectReference)">
      <Output TaskParameter="TargetOutputs" PropertyName="_projectReferenceBuildConfigurations" />
    </MSBuild>

    <PropertyGroup>
      <!--
        When building in VS or using the solution file Configuration for some projects is set to Debug/Release
        in such cases we should treat it as an empty configuration and set it to be $BuildConfiguration for the
        purposes of annotating project references.
      -->
      <Configuration Condition="'$(Configuration)' == 'Debug' or '$(Configuration)' == 'Release'">$(BuildConfiguration)</Configuration>
    </PropertyGroup>

    <!-- Find the best configuration for the current Project's Configuration -->
    <FindBestConfigurations Properties="@(Property)"
                            PropertyValues="@(PropertyValue)"
                            SupportedConfigurations="$(_ProjectReferenceBuildConfigurations)"
                            Configurations="$(Configuration)">
      <Output TaskParameter="BestConfigurations" PropertyName="_projectReferenceConfiguration" />
    </FindBestConfigurations>

    <Error Condition="'$(_projectReferenceConfiguration)' == ''" Text="Could not find a configuration for ProjectReference '@(ProjectReference)' from configurations '$(_projectReferenceBuildConfigurations)' when building '$(MSBuildProjectName)' for configuration '$(Configuration)' ." />

    <ItemGroup>
      <ProjectReference>
        <AdditionalProperties>Configuration=$(_projectReferenceConfiguration);%(ProjectReference.AdditionalProperties)</AdditionalProperties>
      </ProjectReference>

      <!-- Project references for non-test assemblies should never be copied to the output. -->
      <ProjectReference Condition="'$(IsTestProject)' != 'true'">
        <Private>false</Private>
      </ProjectReference>
    </ItemGroup>
  </Target>

  <Target Name="BuildAllConfigurations">
    <ItemGroup>
      <_buildConfigurations Include="$(BuildConfigurations)" />
      <_buildConfigurations Condition="'@(_buildConfigurations)' == ''" Include="$(_traversalBuildConfigurations)" />
    </ItemGroup>

    <MSBuild Projects="$(MSBuildProjectFullPath)"
             Properties="Configuration=%(_buildConfigurations.Identity);BuildAllConfigurations=true" />
  </Target>
</Project>