Skip to content
This repository was archived by the owner on Jan 11, 2024. It is now read-only.

Commit 5254ae0

Browse files
authored
Support using the Microsoft.NET.Sdk with dotnet/buildtools (#2068)
- Don't import Microsoft.$(Language).targets in buildtools - the SDK will do that. - Use the Sdk's GenerateAssemblyInfo target when possible Still support non-SDK projects for backwards compatiblity. Note: I removed support for @(AssemblyInfoSource) during GenerateAssemblyInfo because it is no longer used as of dotnet/corefx/pull/30400.
1 parent 16a6d1f commit 5254ae0

File tree

4 files changed

+145
-80
lines changed

4 files changed

+145
-80
lines changed

src/Microsoft.DotNet.Build.Tasks/PackageFiles/FrameworkTargeting.targets

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@
104104
Condition="'$(MSBuildProjectExtension)' == '.depproj'" />
105105

106106
<Import Project="IL.targets"
107-
Condition="'$(MSBuildProjectExtension)' == '.ilproj' AND '$(SkipImportILTargets)'!='true'" />
107+
Condition="'$(MSBuildProjectExtension)' == '.ilproj' AND '$(SkipImportILTargets)'!='true' and '$(UsingMicrosoftNETSdk)' != 'true'" />
108108

109-
<!--
110-
workaround file casing issue where it has different casing in different places which fails on linux builds
111-
https://github.com/dotnet/buildtools/issues/1464
112-
-->
113-
<PropertyGroup>
109+
<PropertyGroup Condition="'$(UsingMicrosoftNETSdk)' != 'true'">
114110
<CSharpTargetsFile>$(MSBuildToolsPath)\Microsoft.CSharp.targets</CSharpTargetsFile>
111+
<!--
112+
workaround file casing issue where it has different casing in different places which fails on linux builds
113+
https://github.com/dotnet/buildtools/issues/1464
114+
-->
115115
<CSharpTargetsFile Condition="!Exists('$(CSharpTargetsFile)')">$(MSBuildToolsPath)\Microsoft.CSharp.Targets</CSharpTargetsFile>
116116
</PropertyGroup>
117117

@@ -123,7 +123,7 @@
123123

124124

125125
<Import Project="$(CSharpTargetsFile)"
126-
Condition="'$(TargetFrameworkIdentifier)' != '.NETPortable' and '$(MSBuildProjectExtension)' == '.csproj'" />
126+
Condition="'$(TargetFrameworkIdentifier)' != '.NETPortable' and '$(MSBuildProjectExtension)' == '.csproj' and '$(UsingMicrosoftNETSdk)' != 'true'" />
127127

128128
<Import Project="$(MSBuildExtensionsPath32)\Microsoft.VisualBasic.targets"
129129
Condition="'$(TargetFrameworkIdentifier)' == '.NETPortable' and '$(MSBuildProjectExtension)' == '.vbproj' and '$(RunningOnCore)' == 'true'" />
@@ -133,7 +133,7 @@
133133
Condition="'$(TargetFrameworkIdentifier)' == '.NETPortable' and '$(MSBuildProjectExtension)' == '.vbproj' and '$(RunningOnCore)' != 'true'" />
134134

135135
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets"
136-
Condition="'$(TargetFrameworkIdentifier)' != '.NETPortable' and '$(MSBuildProjectExtension)' == '.vbproj'" />
136+
Condition="'$(TargetFrameworkIdentifier)' != '.NETPortable' and '$(MSBuildProjectExtension)' == '.vbproj' and '$(UsingMicrosoftNETSdk)' != 'true'" />
137137

138138
<PropertyGroup>
139139
<!--
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
3+
<Target Name="GenerateAssemblyInfo"
4+
Inputs="$(MSBuildProjectFile)"
5+
Outputs="$(AssemblyInfoFile)"
6+
Condition="'$(GenerateAssemblyInfo)'=='true'"
7+
DependsOnTargets="CreateVersionFileDuringBuild;DecideIfWeNeedToIncludeDllSafeSearchPathAttribute">
8+
9+
<Error Condition="!Exists('$(IntermediateOutputPath)')" Text="GenerateAssemblyInfo failed because IntermediateOutputPath isn't set to a valid directory" />
10+
11+
<ItemGroup Condition="'$(MSBuildProjectExtension)' == '.csproj'">
12+
<AssemblyInfoUsings Include="using System%3B" />
13+
<AssemblyInfoUsings Include="using System.Reflection%3B" />
14+
<AssemblyInfoLines Include="[assembly:AssemblyTitle(&quot;$(AssemblyTitle)&quot;)]" />
15+
<AssemblyInfoLines Include="[assembly:AssemblyDescription(&quot;$(Description)&quot;)]" />
16+
<AssemblyInfoLines Include="[assembly:AssemblyDefaultAlias(&quot;$(AssemblyName)&quot;)]" />
17+
<AssemblyInfoLines Include="[assembly:AssemblyCompany(&quot;$(Company)&quot;)]" />
18+
<AssemblyInfoLines Include="[assembly:AssemblyProduct(&quot;$(Product)&quot;)]" />
19+
<AssemblyInfoLines Include="[assembly:AssemblyCopyright(&quot;$(Copyright)&quot;)]" />
20+
<AssemblyInfoLines Include="[assembly:AssemblyVersion(&quot;$(AssemblyVersion)&quot;)]" />
21+
<AssemblyInfoLines Include="[assembly:AssemblyFileVersion(&quot;$(FileVersion)&quot;)]" />
22+
<AssemblyInfoLines Include="[assembly:AssemblyInformationalVersion(@&quot;$(InformationalVersion)&quot;)]" />
23+
<AssemblyInfoLines Condition="'$(CLSCompliant)'=='true'" Include="[assembly:CLSCompliant(true)]" />
24+
<AssemblyInfoLines Condition="'$(AssemblyComVisible)'!=''" Include="[assembly:System.Runtime.InteropServices.ComVisible($(AssemblyComVisible))]" />
25+
<AssemblyInfoLines Condition="'$(SkipFrameworkAssemblyMetadata)' != 'true'"
26+
Include="[assembly:System.Reflection.AssemblyMetadata(&quot;%(AssemblyMetadata.Identity)&quot;, &quot;%(AssemblyMetadata.Value)&quot;)]" />
27+
28+
<AssemblyInfoLines Condition="'$(IncludeDllSafeSearchPathAttribute)'=='true'"
29+
Include="[assembly: System.Runtime.InteropServices.DefaultDllImportSearchPathsAttribute(System.Runtime.InteropServices.DllImportSearchPath.AssemblyDirectory | System.Runtime.InteropServices.DllImportSearchPath.System32)]" />
30+
</ItemGroup>
31+
32+
<ItemGroup Condition="'$(MSBuildProjectExtension)' == '.vbproj'">
33+
<AssemblyInfoUsings Include="Imports System" />
34+
<AssemblyInfoUsings Include="Imports System.Reflection" />
35+
<AssemblyInfoLines Include="&lt;Assembly:AssemblyTitle(&quot;$(AssemblyTitle)&quot;)&gt;" />
36+
<AssemblyInfoLines Include="&lt;Assembly:AssemblyDescription(&quot;$(Description)&quot;)&gt;" />
37+
<AssemblyInfoLines Include="&lt;Assembly:AssemblyDefaultAlias(&quot;$(AssemblyName)&quot;)&gt;" />
38+
<AssemblyInfoLines Include="&lt;Assembly:AssemblyCompany(&quot;$(Company)&quot;)&gt;" />
39+
<AssemblyInfoLines Include="&lt;Assembly:AssemblyProduct(&quot;$(Product)k&quot;)&gt;" />
40+
<AssemblyInfoLines Include="&lt;Assembly:AssemblyCopyright(&quot;$(Copyright)&quot;)&gt;" />
41+
<AssemblyInfoLines Include="&lt;Assembly:AssemblyVersion(&quot;$(AssemblyVersion)&quot;)&gt;" />
42+
<AssemblyInfoLines Include="&lt;Assembly:AssemblyFileVersion(&quot;$(FileVersion)&quot;)&gt;" />
43+
<AssemblyInfoLines Include="&lt;Assembly:AssemblyInformationalVersion(&quot;$(InformationalVersion)&quot;)&gt;" />
44+
<AssemblyInfoLines Condition="'$(CLSCompliant)'=='true'" Include="&lt;Assembly:CLSCompliant(True)&gt;" />
45+
<AssemblyInfoLines Condition="'$(AssemblyComVisible)'!=''" Include="&lt;Assembly:System.Runtime.InteropServices.ComVisible($(AssemblyComVisible))&gt;" />
46+
<AssemblyInfoLines Condition="'$(SkipFrameworkAssemblyMetadata)' != 'true'"
47+
Include="&lt;assembly:System.Reflection.AssemblyMetadata(&quot;%(AssemblyMetadata.Identity)&quot;, &quot;%(AssemblyMetadata.Value)&quot;)&gt;" />
48+
49+
<AssemblyInfoLines Condition="'$(IncludeDllSafeSearchPathAttribute)'=='true'"
50+
Include="&lt;Assembly:System.Runtime.InteropServices.DefaultDllImportSearchPathsAttribute(System.Runtime.InteropServices.DllImportSearchPath.AssemblyDirectory | System.Runtime.InteropServices.DllImportSearchPath.System32)&gt;" />
51+
</ItemGroup>
52+
53+
<WriteLinesToFile
54+
File="$(AssemblyInfoFile)"
55+
Lines="@(AssemblyInfoUsings);@(AssemblyInfoLines)"
56+
Overwrite="true" />
57+
58+
<ItemGroup>
59+
<Compile Include="$(AssemblyInfoFile)" />
60+
<FileWrites Include="$(AssemblyInfoFile)" />
61+
</ItemGroup>
62+
63+
</Target>
64+
65+
</Project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
3+
<PropertyGroup>
4+
<!-- BuildTools never generated this attribute before, so don't let the SDK generate it. -->
5+
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
6+
7+
<!--
8+
When using the Microsoft.NET.Sdk, Company and Product are defaulted to $(AssemblyName)
9+
in Microsoft.NET.Sdk.BeforeCommon.targets, which means we would have to default them in
10+
a props file. However, the versioning.props file has to be imported during versioning.targets
11+
in order to get all the version properties set correctly.
12+
So overwrite these two properties here, so they are set correctly.
13+
-->
14+
<Company>Microsoft Corporation</Company>
15+
<Product>Microsoft%AE .NET Framework</Product>
16+
</PropertyGroup>
17+
18+
<Target Name="_ComputeBuildToolsAssemblyInfoAttributes"
19+
DependsOnTargets="CreateVersionFileDuringBuild;DecideIfWeNeedToIncludeDllSafeSearchPathAttribute"
20+
BeforeTargets="GetAssemblyAttributes">
21+
<ItemGroup>
22+
<AssemblyAttribute Include="System.Reflection.AssemblyDefaultAliasAttribute">
23+
<_Parameter1>$(AssemblyName)</_Parameter1>
24+
</AssemblyAttribute>
25+
26+
<AssemblyAttribute Include="System.Reflection.AssemblyMetadata"
27+
Condition="'$(SkipFrameworkAssemblyMetadata)' != 'true'">
28+
<_Parameter1>%(AssemblyMetadata.Identity)</_Parameter1>
29+
<_Parameter2>%(AssemblyMetadata.Value)</_Parameter2>
30+
</AssemblyAttribute>
31+
</ItemGroup>
32+
</Target>
33+
34+
<!-- Work around https://github.com/Microsoft/msbuild/issues/3412 by writing the non-string assembly attributes manually -->
35+
<!-- Note: ReferenceAssemblies.targets still uses @(AssemblyInfoLines) as well. So if you remove this, those need to migrate too. -->
36+
<Target Name="_WriteNonStringAssemblyInfoAttributes"
37+
AfterTargets="CoreGenerateAssemblyInfo"
38+
Inputs="$(MSBuildProjectFile)"
39+
Outputs="$(AssemblyInfoFile)">
40+
41+
<ItemGroup Condition="'$(MSBuildProjectExtension)' == '.csproj'">
42+
<AssemblyInfoLines Condition="'$(CLSCompliant)'=='true'" Include="[assembly:System.CLSCompliant(true)]" />
43+
44+
<AssemblyInfoLines Condition="'$(IncludeDllSafeSearchPathAttribute)'=='true'"
45+
Include="[assembly: System.Runtime.InteropServices.DefaultDllImportSearchPathsAttribute(System.Runtime.InteropServices.DllImportSearchPath.AssemblyDirectory | System.Runtime.InteropServices.DllImportSearchPath.System32)]" />
46+
</ItemGroup>
47+
48+
<ItemGroup Condition="'$(MSBuildProjectExtension)' == '.vbproj'">
49+
<AssemblyInfoLines Condition="'$(CLSCompliant)'=='true'" Include="&lt;Assembly:System.CLSCompliant(True)&gt;" />
50+
51+
<AssemblyInfoLines Condition="'$(IncludeDllSafeSearchPathAttribute)'=='true'"
52+
Include="&lt;Assembly:System.Runtime.InteropServices.DefaultDllImportSearchPathsAttribute(System.Runtime.InteropServices.DllImportSearchPath.AssemblyDirectory | System.Runtime.InteropServices.DllImportSearchPath.System32)&gt;" />
53+
</ItemGroup>
54+
55+
<WriteLinesToFile File="$(AssemblyInfoFile)"
56+
Lines="@(AssemblyInfoLines)"
57+
Condition="'@(AssemblyInfoLines)' != ''"
58+
Overwrite="true" />
59+
60+
<ItemGroup Condition="'@(AssemblyInfoLines)' != ''">
61+
<Compile Include="$(AssemblyInfoFile)" />
62+
<FileWrites Include="$(AssemblyInfoFile)" />
63+
</ItemGroup>
64+
</Target>
65+
66+
</Project>

src/Microsoft.DotNet.Build.Tasks/PackageFiles/versioning.targets

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
</PropertyGroup>
4242

4343
<Target Name="DecideIfWeNeedToIncludeDllSafeSearchPathAttribute"
44-
Condition="'$(IsDotNetFrameworkProductAssembly)' == 'true' AND '$(IsTestProject)' != 'true'"
45-
BeforeTargets="GenerateAssemblyInfo">
44+
Condition="'$(IsDotNetFrameworkProductAssembly)' == 'true' AND '$(IsTestProject)' != 'true'">
4645

4746
<!-- We want to apply the IncludeDllSafeSearchPathAttribute on all DotNet assemblies (non test) that have a reference to System.Runtime.InteropServices -->
4847
<PropertyGroup Condition="'$(IncludeDllSafeSearchPathAttribute)'==''">
@@ -52,77 +51,12 @@
5251
</PropertyGroup>
5352
</Target>
5453

55-
<Target Name="GenerateAssemblyInfo"
56-
Inputs="$(MSBuildProjectFile)"
57-
Outputs="$(AssemblyInfoFile)"
58-
Condition="'$(GenerateAssemblyInfo)'=='true'"
59-
DependsOnTargets="CreateVersionFileDuringBuild">
60-
61-
<Error Condition="!Exists('$(IntermediateOutputPath)')" Text="GenerateAssemblyInfo failed because IntermediateOutputPath isn't set to a valid directory" />
62-
63-
<ItemGroup Condition="'$(MSBuildProjectExtension)' == '.csproj'">
64-
<AssemblyInfoUsings Include="using System%3B" />
65-
<AssemblyInfoUsings Include="using System.Reflection%3B" />
66-
<AssemblyInfoLines Include="[assembly:AssemblyTitle(&quot;$(AssemblyTitle)&quot;)]" />
67-
<AssemblyInfoLines Include="[assembly:AssemblyDescription(&quot;$(Description)&quot;)]" />
68-
<AssemblyInfoLines Include="[assembly:AssemblyDefaultAlias(&quot;$(AssemblyName)&quot;)]" />
69-
<AssemblyInfoLines Include="[assembly:AssemblyCompany(&quot;$(Company)&quot;)]" />
70-
<AssemblyInfoLines Include="[assembly:AssemblyProduct(&quot;$(Product)&quot;)]" />
71-
<AssemblyInfoLines Include="[assembly:AssemblyCopyright(&quot;$(Copyright)&quot;)]" />
72-
<AssemblyInfoLines Include="[assembly:AssemblyVersion(&quot;$(AssemblyVersion)&quot;)]" />
73-
<AssemblyInfoLines Include="[assembly:AssemblyFileVersion(&quot;$(FileVersion)&quot;)]" />
74-
<AssemblyInfoLines Include="[assembly:AssemblyInformationalVersion(@&quot;$(InformationalVersion)&quot;)]" />
75-
<AssemblyInfoLines Condition="'$(CLSCompliant)'=='true'" Include="[assembly:CLSCompliant(true)]" />
76-
<AssemblyInfoLines Condition="'$(AssemblyComVisible)'!=''" Include="[assembly:System.Runtime.InteropServices.ComVisible($(AssemblyComVisible))]" />
77-
<AssemblyInfoLines Condition="'$(SkipFrameworkAssemblyMetadata)' != 'true'"
78-
Include="[assembly:System.Reflection.AssemblyMetadata(&quot;%(AssemblyMetadata.Identity)&quot;, &quot;%(AssemblyMetadata.Value)&quot;)]" />
79-
80-
<AssemblyInfoLines Condition="'$(IncludeDllSafeSearchPathAttribute)'=='true'"
81-
Include="[assembly: System.Runtime.InteropServices.DefaultDllImportSearchPathsAttribute(System.Runtime.InteropServices.DllImportSearchPath.AssemblyDirectory | System.Runtime.InteropServices.DllImportSearchPath.System32)]" />
82-
</ItemGroup>
83-
84-
<ItemGroup Condition="'$(MSBuildProjectExtension)' == '.vbproj'">
85-
<AssemblyInfoUsings Include="Imports System" />
86-
<AssemblyInfoUsings Include="Imports System.Reflection" />
87-
<AssemblyInfoLines Include="&lt;Assembly:AssemblyTitle(&quot;$(AssemblyTitle)&quot;)&gt;" />
88-
<AssemblyInfoLines Include="&lt;Assembly:AssemblyDescription(&quot;$(Description)&quot;)&gt;" />
89-
<AssemblyInfoLines Include="&lt;Assembly:AssemblyDefaultAlias(&quot;$(AssemblyName)&quot;)&gt;" />
90-
<AssemblyInfoLines Include="&lt;Assembly:AssemblyCompany(&quot;$(Company)&quot;)&gt;" />
91-
<AssemblyInfoLines Include="&lt;Assembly:AssemblyProduct(&quot;$(Product)k&quot;)&gt;" />
92-
<AssemblyInfoLines Include="&lt;Assembly:AssemblyCopyright(&quot;$(Copyright)&quot;)&gt;" />
93-
<AssemblyInfoLines Include="&lt;Assembly:AssemblyVersion(&quot;$(AssemblyVersion)&quot;)&gt;" />
94-
<AssemblyInfoLines Include="&lt;Assembly:AssemblyFileVersion(&quot;$(FileVersion)&quot;)&gt;" />
95-
<AssemblyInfoLines Include="&lt;Assembly:AssemblyInformationalVersion(&quot;$(InformationalVersion)&quot;)&gt;" />
96-
<AssemblyInfoLines Condition="'$(CLSCompliant)'=='true'" Include="&lt;Assembly:CLSCompliant(True)&gt;" />
97-
<AssemblyInfoLines Condition="'$(AssemblyComVisible)'!=''" Include="&lt;Assembly:System.Runtime.InteropServices.ComVisible($(AssemblyComVisible))&gt;" />
98-
<AssemblyInfoLines Condition="'$(SkipFrameworkAssemblyMetadata)' != 'true'"
99-
Include="&lt;assembly:System.Reflection.AssemblyMetadata(&quot;%(AssemblyMetadata.Identity)&quot;, &quot;%(AssemblyMetadata.Value)&quot;)&gt;" />
100-
101-
<AssemblyInfoLines Condition="'$(IncludeDllSafeSearchPathAttribute)'=='true'"
102-
Include="&lt;Assembly:System.Runtime.InteropServices.DefaultDllImportSearchPathsAttribute(System.Runtime.InteropServices.DllImportSearchPath.AssemblyDirectory | System.Runtime.InteropServices.DllImportSearchPath.System32)&gt;" />
103-
</ItemGroup>
104-
105-
<ItemGroup Condition="'$(MSBuildProjectExtension)' == '.csproj' And '$(GenerateThisAssemblyClass)' == 'true'">
106-
<AssemblyInfoLines Include="internal static class ThisAssembly" />
107-
<AssemblyInfoLines Include="{" />
108-
<AssemblyInfoLines Include="%20%20%20%20internal const string Title = &quot;$(AssemblyName)&quot;%3B" />
109-
<AssemblyInfoLines Include="%20%20%20%20internal const string Copyright = &quot;\u00A9 Microsoft Corporation. All rights reserved.&quot;%3B" />
110-
<AssemblyInfoLines Include="%20%20%20%20internal const string Version = &quot;$(AssemblyVersion)&quot;%3B" />
111-
<AssemblyInfoLines Include="%20%20%20%20internal const string InformationalVersion = &quot;$(AssemblyFileVersion)&quot;%3B" />
112-
<AssemblyInfoLines Include="}" />
113-
</ItemGroup>
114-
115-
<WriteLinesToFile
116-
File="$(AssemblyInfoFile)"
117-
Lines="@(AssemblyInfoUsings);@(AssemblyInfoLines);@(AssemblyInfoSource)"
118-
Overwrite="true" />
119-
120-
<ItemGroup>
121-
<Compile Include="$(AssemblyInfoFile)" />
122-
<FileWrites Include="$(AssemblyInfoFile)" />
123-
</ItemGroup>
54+
<PropertyGroup>
55+
<_GenerateAssemblyInfoTargetsFilePath Condition="'$(UsingMicrosoftNETSdk)' == 'true'">GenerateAssemblyInfo.Sdk.targets</_GenerateAssemblyInfoTargetsFilePath>
56+
<_GenerateAssemblyInfoTargetsFilePath Condition="'$(UsingMicrosoftNETSdk)' != 'true'">GenerateAssemblyInfo.NonSdk.targets</_GenerateAssemblyInfoTargetsFilePath>
57+
</PropertyGroup>
12458

125-
</Target>
59+
<Import Project="$(_GenerateAssemblyInfoTargetsFilePath)" />
12660

12761
<ItemGroup Condition="'$(GenerateAssemblyInfo)'=='true' AND '$(StringResourcesPath)' != '' AND '$(ExcludeAssemblyInfoPartialFile)' != 'true'">
12862
<Compile Include="$(AssemblyInfoPartialFile)">

0 commit comments

Comments
 (0)