Skip to content

Commit

Permalink
Don't double import WinFX targets (#2976)
Browse files Browse the repository at this point in the history
* Remove trailing whitespace

Respect EditorConfig preferences for whitespace and newlines

* Rearrange WinFX import and add comments

The MSBuild items & targets that were defined after the WinFX targets import doesn't depend on anything from the WinFX targets.
So, it is safe and better to declare the import after those items and targets definitions.

* Don't import .NET Framework's implementation of WinFX targets

When the SDK's implementation is being used.

* Make the Framework WinFX targets import opt-out

Currently there's no way to properly switch between NETFX's and CoreCLR's WinFX targets.
This adds an opt-out, just in case, if we want to use the NETFX's WinFX targets instead.
  • Loading branch information
Nirmal4G committed Jan 12, 2021
1 parent 803f771 commit 08c104b
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 154 deletions.
2 changes: 1 addition & 1 deletion packaging/Microsoft.NET.Sdk.WindowsDesktop/Sdk/Sdk.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and
created a backup copy. Incorrect changes to this file will make it
impossible to load or build your projects from the command-line or the IDE.
Copyright (c) .NET Foundation. All rights reserved.
Copyright (c) .NET Foundation. All rights reserved.
***********************************************************************************************
-->
<Project>
Expand Down
3 changes: 2 additions & 1 deletion packaging/Microsoft.NET.Sdk.WindowsDesktop/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and
created a backup copy. Incorrect changes to this file will make it
impossible to load or build your projects from the command-line or the IDE.
Copyright (c) .NET Foundation. All rights reserved.
Copyright (c) .NET Foundation. All rights reserved.
***********************************************************************************************
-->
<Project>

<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets" />

<!--Post 5.0, Microsoft.NET.Sdk.WindowsDesktop.targets will be imported by SDK conditionally-->

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<Project>

<ItemGroup Condition=" '$(_EnableWindowsDesktopGlobbing)' == 'true' ">
<ApplicationDefinition Include="App.xaml"
Condition="'$(EnableDefaultApplicationDefinition)' != 'false' And Exists('$(MSBuildProjectDirectory)/App.xaml') And '$(MSBuildProjectExtension)' == '.csproj'">
Expand All @@ -19,9 +20,9 @@
</Page>


<!--
<!--
See https://github.com/dotnet/wpf/issues/685
Visual Studio would prefer that we remove **/*.xaml instead of
Visual Studio would prefer that we remove **/*.xaml instead of
being more precise.
<None Remove="@(Page)"
Expand All @@ -33,7 +34,7 @@
Condition="'$(EnableDefaultApplicationDefinition)' != 'false' And '$(EnableDefaultPageItems)' != 'false'" />
</ItemGroup>


<ItemGroup Condition=" '$(_EnableWindowsDesktopNetCoreFrameworkReferences)' == 'true' ">

<FrameworkReference Include="Microsoft.WindowsDesktop.App" IsImplicitlyDefined="true"
Expand All @@ -45,39 +46,37 @@
<FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms" IsImplicitlyDefined="true"
Condition="('$(UseWPF)' != 'true') And ('$(UseWindowsForms)' == 'true')"/>
</ItemGroup>


<!--
Traditionally, Visual Studio has supplied these references for .NET Framework based
WPF Projects:
.NET 3.x: PresentationCore, PresentationFramework, WindowsBase
.NET 4.x: PresentationCore, PresentationFramework, WindowsBase, System.Xaml
Microsoft.NET.WindowsDesktop.SDK will supply the following references to .NET Framework based
WPF Projects:
.NET 3.x: PresentationCore, PresentationFramework, WindowsBase
.NET 4.0: PresentationCore, PresentationFramework, WindowsBase, System.Xaml,
WPF Projects:
.NET 3.x: PresentationCore, PresentationFramework, WindowsBase
.NET 4.x: PresentationCore, PresentationFramework, WindowsBase, System.Xaml
Microsoft.NET.WindowsDesktop.SDK will supply the following references to .NET Framework based
WPF Projects:
.NET 3.x: PresentationCore, PresentationFramework, WindowsBase
.NET 4.0: PresentationCore, PresentationFramework, WindowsBase, System.Xaml,
UIAutomationClient, UIAutomationClientSideProviders, UIAutomationProvider, UIAutomationTypes
.NET 4.5+: PresentationCore, PresentationFramework, WindowsBase, System.Xaml,
.NET 4.5+: PresentationCore, PresentationFramework, WindowsBase, System.Xaml,
UIAutomationClient, UIAutomationClientSideProviders, UIAutomationProvider, UIAutomationTypes
System.Windows.Controls.Ribbon
-->
<ItemGroup Condition=" '$(_EnableWindowsDesktopNETFrameworkImplicitReference)' == 'true' ">

<!--
The following 3 _WpfCommonNetFxReference items normally require Condition="'$(_TargetFrameworkVersionValue)' >= '3.0'", since
they are supported on .NET Framework 3.0 and above.
This condition is implicitly satisfied by '$(_TargetFrameworkVersionValue)' >= '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)'
they are supported on .NET Framework 3.0 and above.
This condition is implicitly satisfied by '$(_TargetFrameworkVersionValue)' >= '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)'
in the outer ItemGroup
-->
<_WpfCommonNetFxReference Include="WindowsBase" />
<_WpfCommonNetFxReference Include="WindowsBase" />
<_WpfCommonNetFxReference Include="PresentationCore" />
<_WpfCommonNetFxReference Include="PresentationFramework" />

Expand All @@ -93,39 +92,38 @@
</ItemGroup>

<ItemGroup Condition=" '$(_EnableWindowsDesktopNETFrameworkImplicitReference)' == 'true' ">

<_SDKImplicitReference Include="@(_WpfCommonNetFxReference)"
Condition="'$(UseWPF)' == 'true'"/>

<_SDKImplicitReference Include="System.Windows.Forms"
Condition="('$(UseWindowsForms)' == 'true') " />

<_SDKImplicitReference Include="WindowsFormsIntegration"
Condition=" ('$(UseWindowsForms)' == 'true') And ('$(UseWPF)' == 'true') "/>
</ItemGroup>
<!--
Supported (and unsupported) TargetFrameworks
Visual Studio Project System determines the list of valid TargetFrameworks to show
in the Project properties by querying SupportedTargetFramework values.


<!--
Supported (and unsupported) TargetFrameworks
Visual Studio Project System determines the list of valid TargetFrameworks to show
in the Project properties by querying SupportedTargetFramework values.
The Project System does not refer to this list at this time for .NET Framework TFM's.
-->

<!--
<!--
When WindowsDesktop SDK is used without setting UseWPF or UseWindowsForms, it shows a (suppressible) warning and functions much
like Microsoft.NET.Sdk
Likewise, when WindowsDesktop SDK is used with a netcore TFM that is less than 3.0, it will simply act as if it were an
Microsoft.NET.Sdk project (and show a suppressible build-time warning).
Detect these situations and skip updates to @(SupportedTargetFramework) etc.
-->
<ItemGroup Condition=" '$(_RemoveUnsupportedTargetFrameworksForWindowsDesktop)' == 'true' ">
Microsoft.NET.Sdk project (and show a suppressible build-time warning).
<!--
Detect these situations and skip updates to @(SupportedTargetFramework) etc.
-->
<ItemGroup Condition=" '$(_RemoveUnsupportedTargetFrameworksForWindowsDesktop)' == 'true' ">
<!--
Windows Forms and WPF are supported only on .NET Core 3.0+
-->
<_UnsupportedNETCoreAppTargetFramework Include=".NETCoreApp,Version=v1.0" />
Expand All @@ -140,17 +138,17 @@
<_UnsupportedNETStandardTargetFramework Include="@(SupportedNETStandardTargetFramework)" />

<!--
Windows Forms was supported since .NET Framework 1.0, and is currently supported on
.NET Framework 2.0+.
Windows Forms was supported since .NET Framework 1.0, and is currently supported on
.NET Framework 2.0+.
WPF is supported on .NET Framework and WPF are supported on .NET Framework 3.0+
In practice, the WindowsDesktop SDK is only supported on .NET Framework 3.0+ - this is controlled
by $(_WindowsDesktopSdkTargetFrameworkVersionFloor), defined as 3.0, which applies to both .NETFramework
and .NETCore.
Here, we will encode .NET Framework 3.0 as the lowest supported version for both Windows Forms and WPF.
Here, we will encode .NET Framework 3.0 as the lowest supported version for both Windows Forms and WPF.
The SDK does not define versions < 2.0 in @(SupportedNETFrameworkTargetFramework) list, so none of those
need to be excluded here - removing 2.0 would suffice.
-->
Expand All @@ -159,7 +157,18 @@
<SupportedNETCoreAppTargetFramework Remove="@(_UnsupportedNETCoreAppTargetFramework)" />
<SupportedNETStandardTargetFramework Remove="@(_UnsupportedNETStandardTargetFramework)" />
<SupportedNETFrameworkTargetFramework Remove="@(_UnsupportedNETFrameworkTargetFramework)" />

<SupportedTargetFramework Remove="@(_UnsupportedNETCoreAppTargetFramework);@(_UnsupportedNETStandardTargetFramework);@(_UnsupportedNETFrameworkTargetFramework)" />
</ItemGroup>

<!--
Workaround: https://github.com/microsoft/msbuild/issues/4948
Disable .NET Framework's inbox WinFX targets when using the SDK, since, we really don't use it's build logic
and is superseded by 'WindowsDesktop' SDK that provides it's own WinFX for both NETFX and CoreCLR targets.
Make it opt-out, just in case, if something fails or we don't want to use 'WindowsDesktop' SDK's version.
-->
<PropertyGroup>
<ImportFrameworkWinFXTargets Condition="'$(ImportFrameworkWinFXTargets)' == ''">false</ImportFrameworkWinFXTargets>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,53 +1,51 @@
<Project>

<PropertyGroup>
<_MicrosoftNetSdkWindowsDesktop>true</_MicrosoftNetSdkWindowsDesktop>
<_MicrosoftNETSdkWindowsDesktop>true</_MicrosoftNETSdkWindowsDesktop>
<EnableDefaultPageItems Condition="'$(EnableDefaultPageItems)' == ''">true</EnableDefaultPageItems>
<EnableDefaultApplicationDefinition Condition="'$(EnableDefaultApplicationDefinition)' == ''">true</EnableDefaultApplicationDefinition>
<DefaultXamlRuntime Condition="'$(DefaultXamlRuntime)'==''">Wpf</DefaultXamlRuntime>
</PropertyGroup>

<PropertyGroup>
<!--
WindowsDesktop SDK supports WPF and WinForms on
<!--
WindowsDesktop SDK supports WPF and WinForms on
- .NET Core 3.0 and greater
- .NET Framework 3.0 and greater
Note that on .NET Framework versions < 4.0, additional workarounds may be required to build applications
Note that on .NET Framework versions < 4.0, additional workarounds may be required to build applications
using the SDK style projects. For e.g., see https://github.com/microsoft/msbuild/issues/1333
Irrespective of whether '$(TargetFrameworkIdentifier)' is '.NETCoreApp' or '.NETFramework',
Irrespective of whether '$(TargetFrameworkIdentifier)' is '.NETCoreApp' or '.NETFramework',
the minimum value of $(_TargetFrameworkVersionValue) we will be testing for is '3.0'
-->
<_WindowsDesktopSdkTargetFrameworkVersionFloor>3.0</_WindowsDesktopSdkTargetFrameworkVersionFloor>

<!--
<!--
Represents an undefined TFV value. This will be used in comparisons of _TargetFrameworkVersionValue (defined in Microsoft.NET.WindowsDesktop.targets)
to identify when a TFV is undefined
-->
<_UndefinedTargetFrameworkVersion>0.0</_UndefinedTargetFrameworkVersion>
</PropertyGroup>


<!--
$(TargetFrameworkVersion), $(_TargetFrameworkVersionWithoutV) etc. are defined in
Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets.
<!--
$(TargetFrameworkVersion), $(_TargetFrameworkVersionWithoutV) etc. are defined in
Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets.
Microsoft.NET.Sdk.targets is included prior to this file by Sdk.targets, so it is safe to define _TargetFrameworkValue
here.
_TargetFrameworkValue will be used in WindowsDesktop SDK as reliable proxy for _TargetFrameworkVersionWithoutV.
here.
_TargetFrameworkValue will be used in WindowsDesktop SDK as reliable proxy for _TargetFrameworkVersionWithoutV.
_TargetFrameworkVersionWithoutV can be empty ('') or it can be a numeric value (e.g., '3.0') - which makes it hard to work with
esp. in outer builds. _TargetFrameworkValue will always be guaranteed to have a numeric value ('0.0' or a valid TFM).
esp. in outer builds. _TargetFrameworkValue will always be guaranteed to have a numeric value ('0.0' or a valid TFM).
_TargetFrameworkVersionValue will be used in Microsoft.NET.Sdk.WindowsDesktop.props in Item conditions. This will be valid
because Items are evaluated after Properties (see https://docs.microsoft.com/en-us/visualstudio/msbuild/comparing-properties-and-items?view=vs-2019).
because Items are evaluated after Properties (see https://docs.microsoft.com/en-us/visualstudio/msbuild/comparing-properties-and-items?view=vs-2019).
Note:
Please see https://github.com/microsoft/msbuild/issues/3212 for a discussion about the use of
Please see https://github.com/microsoft/msbuild/issues/3212 for a discussion about the use of
the private $(_TargetFrameworkVersionWithoutV) property - which will likely remain supported and
is safe to use here.
is safe to use here.
-->
<PropertyGroup>
<_TargetFrameworkVersionValue>$([MSBuild]::ValueOrDefault('$(_TargetFrameworkVersionWithoutV)', '$(_UndefinedTargetFrameworkVersion)'))</_TargetFrameworkVersionValue>
Expand All @@ -71,33 +69,28 @@
'$(_TargetFrameworkVersionValue)' >= '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)'">true</_RemoveUnsupportedTargetFrameworksForWindowsDesktop>
</PropertyGroup>


<Import Project="Microsoft.WinFX.targets" />

<ItemGroup Condition=" ('$(EnableDefaultItems)' == 'true') And ('$(UseWPF)' == 'true') And ('$(_TargetFrameworkVersionValue)' != '$(_UndefinedTargetFrameworkVersion)') And
<ItemGroup Condition=" ('$(EnableDefaultItems)' == 'true') And ('$(UseWPF)' == 'true') And ('$(_TargetFrameworkVersionValue)' != '$(_UndefinedTargetFrameworkVersion)') And
('$(_TargetFrameworkVersionValue)' >= '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)')">

<!--
In the WindowsDesktop .props, we globbed all .xaml files as Page items. If any of those files are included
as Resource, Content then remove them from the Page items.
-->
<Page Remove="@(Resource);@(Content)"
Condition="'$(EnableDefaultPageItems)' != 'false'" />

<!--
ApplicationDefinition must also be removed from Page. Otherwise, PresentationBuildTasks will include the
generated code for the ApplicationDefinition twice. We cannot do this in all cases in the props file as
ApplicationDefinition isn't available at that point in the evaluation when EnableDefaultApplicationDefinition
is false. We unconditionally remove it here as ApplicationDefinition is considered a special singleton Item
is false. We unconditionally remove it here as ApplicationDefinition is considered a special singleton Item
in WPF.
-->
<Page Remove="@(ApplicationDefinition)" />

<!--
The WindowsDesktop props file removes "**/*.xaml" from None if both EnableDefaultApplicationDefinition and
The WindowsDesktop props file removes "**/*.xaml" from None if both EnableDefaultApplicationDefinition and
EnableDefaultPageItems are true. This is done so that we remove any implicitly globbed XAML files from None
in support of Visual Studio.
in support of Visual Studio.
In the case where one or both of these properties are not true, the WindowsDesktop props file does not remove
any XAML files from None. Under those conditions, removing None from Page here will remove explicitly specified
pages causing compilation errors. We match the condition from the WindowsDesktop props file here so that we only
Expand All @@ -110,7 +103,7 @@
<!-- Generate error if there are duplicate page items. The task comes from the .NET SDK, and this target follows
the pattern in the CheckForDuplicateItems task, where the .NET SDK checks for duplicate items for the item
types it knows about. -->
<Target Name="CheckForDuplicatePageItems"
<Target Name="CheckForDuplicatePageItems"
BeforeTargets="_CheckForInvalidConfigurationAndPlatform;CoreCompile"
DependsOnTargets="CheckForDuplicateItems"
Condition="('$(UseWPF)' == 'true') And ('$(_TargetFrameworkVersionValue)' != '$(_UndefinedTargetFrameworkVersion)') And ('$(_TargetFrameworkVersionValue)' >= '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)')">
Expand All @@ -133,29 +126,33 @@

</Target>

<!--
<!--
This warning can be suppressed by setting $(MSBuildWarningsAsMessages) property, like this:
<PropertyGroup>
<MSBuildWarningsAsMessages>$(MSBuildWarningsAsMessages);NETSDK1106</MSBuildWarningsAsMessages>
</PropertyGroup>
-->
-->
<Target Name="_WindowsDesktopFrameworkRequiresUseWpfOrUseWindowsForms"
BeforeTargets="_CheckForInvalidConfigurationAndPlatform"
Condition="('$(_TargetFrameworkVersionValue)' != '$(_UndefinedTargetFrameworkVersion)') And ('$(_TargetFrameworkVersionValue)' >= '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)')">
<NetSdkWarning ResourceName="WindowsDesktopFrameworkRequiresUseWpfOrUseWindowsForms"
<NetSdkWarning ResourceName="WindowsDesktopFrameworkRequiresUseWpfOrUseWindowsForms"
Condition="'$(UseWpf)' != 'true' And '$(UseWindowsForms)' != 'true'"/>
</Target>

<!--
<!--
This warning can be suppressed by setting $(MSBuildWarningsAsMessages) property, like this:
<PropertyGroup>
<MSBuildWarningsAsMessages>$(MSBuildWarningsAsMessages);NETSDK1105</MSBuildWarningsAsMessages>
</PropertyGroup>
-->
-->
<Target Name="_WindowsDesktopFrameworkRequiresVersion30"
BeforeTargets="_CheckForInvalidConfigurationAndPlatform"
Condition="('$(TargetFrameworkIdentifier)' == '.NETCoreApp') And
Condition="('$(TargetFrameworkIdentifier)' == '.NETCoreApp') And
('$(_TargetFrameworkVersionValue)' != '$(_UndefinedTargetFrameworkVersion)') And ('$(_TargetFrameworkVersionValue)' &lt; '$(_WindowsDesktopSdkTargetFrameworkVersionFloor)')">
<NetSdkWarning ResourceName="WindowsDesktopFrameworkRequiresVersion30" />
</Target>

<!-- Import WPF Build logic only when we don't import NETFX's WinFX targets -->
<Import Project="Microsoft.WinFX.targets" Condition="'$(ImportFrameworkWinFXTargets)' != 'true'"/>

</Project>
Loading

0 comments on commit 08c104b

Please sign in to comment.