Skip to content

Commit a508fb5

Browse files
authored
[DllImportGenerator] Enable on projects without System.Memory and System.Runtime.CompilerServices.Unsafe (#61704)
1 parent de883e3 commit a508fb5

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

eng/generators.targets

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,15 @@
1010
<!-- If the current project is not System.Private.CoreLib, we enable the DllImportGenerator source generator
1111
when the project is a C# source project that either:
1212
- references System.Private.CoreLib, or
13-
- references the following assemblies:
14-
- System.Runtime.InteropServices
15-
- System.Runtime.CompilerServices.Unsafe
16-
- System.Memory -->
13+
- references System.Runtime.InteropServices -->
1714
<EnabledGenerators Include="DllImportGenerator"
1815
Condition="'$(EnableDllImportGenerator)' == ''
1916
and '$(IsFrameworkSupportFacade)' != 'true'
2017
and '$(IsSourceProject)' == 'true'
2118
and '$(MSBuildProjectExtension)' == '.csproj'
2219
and (
2320
('@(Reference)' != ''
24-
and @(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices'))
25-
and (@(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.CompilerServices.Unsafe'))
26-
or ('@(ProjectReference)' != ''
27-
and @(ProjectReference->AnyHaveMetadataValue('Identity', $([MSBuild]::NormalizePath('$(LibrariesProjectRoot)', 'System.Runtime.CompilerServices.Unsafe', 'src', 'System.Runtime.CompilerServices.Unsafe.ilproj'))))))
28-
and @(Reference->AnyHaveMetadataValue('Identity', 'System.Memory')))
21+
and @(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices')))
2922
or ('@(ProjectReference)' != ''
3023
and @(ProjectReference->AnyHaveMetadataValue('Identity', '$(CoreLibProject)'))))" />
3124
<EnabledGenerators Include="DllImportGenerator"
@@ -45,7 +38,19 @@
4538

4639
<!-- Only add the following files if we are on the latest TFM (that is, net7). -->
4740
<Compile Condition="'$(NetCoreAppCurrentTargetFrameworkMoniker)' == '$(TargetFrameworkMoniker)'" Include="$(LibrariesProjectRoot)Common\src\System\Runtime\InteropServices\GeneratedMarshallingAttribute.cs" />
48-
<Compile Condition="'$(NetCoreAppCurrentTargetFrameworkMoniker)' == '$(TargetFrameworkMoniker)'" Include="$(LibrariesProjectRoot)Common\src\System\Runtime\InteropServices\ArrayMarshaller.cs" />
41+
42+
<!-- Only add the following files if we are on the latest TFM (that is, net7) and the project is SPCL or has references to System.Runtime.CompilerServices.Unsafe and System.Memory -->
43+
<Compile Condition="'$(NetCoreAppCurrentTargetFrameworkMoniker)' == '$(TargetFrameworkMoniker)'
44+
and (
45+
'$(MSBuildProjectName)' == 'System.Private.CoreLib'
46+
or '$(EnableDllImportGenerator)' == 'true'
47+
or ('@(Reference)' != ''
48+
and (@(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.CompilerServices.Unsafe'))
49+
or ('@(ProjectReference)' != ''
50+
and @(ProjectReference->AnyHaveMetadataValue('Identity', $([MSBuild]::NormalizePath('$(LibrariesProjectRoot)', 'System.Runtime.CompilerServices.Unsafe', 'src', 'System.Runtime.CompilerServices.Unsafe.ilproj'))))))
51+
and @(Reference->AnyHaveMetadataValue('Identity', 'System.Memory')))
52+
or ('@(ProjectReference)' != ''
53+
and @(ProjectReference->AnyHaveMetadataValue('Identity', '$(CoreLibProject)'))))" Include="$(LibrariesProjectRoot)Common\src\System\Runtime\InteropServices\ArrayMarshaller.cs" />
4954
</ItemGroup>
5055

5156
<Target Name="ConfigureGenerators"

src/libraries/System.DirectoryServices.AccountManagement/src/System.DirectoryServices.AccountManagement.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<IncludeDllSafeSearchPathAttribute>true</IncludeDllSafeSearchPathAttribute>
55
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0</TargetFrameworks>
66
<NoWarn>$(NoWarn);CA2249</NoWarn>
7+
<Nullable>annotations</Nullable>
78
<IsPackable>true</IsPackable>
89
<AddNETFrameworkPlaceholderFileToPackage>true</AddNETFrameworkPlaceholderFileToPackage>
910
<AddNETFrameworkAssemblyReferenceToPackage>true</AddNETFrameworkAssemblyReferenceToPackage>

src/libraries/System.DirectoryServices.Protocols/src/System.DirectoryServices.Protocols.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<IncludeDllSafeSearchPathAttribute>true</IncludeDllSafeSearchPathAttribute>
55
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum)-OSX;$(NetCoreAppMinimum)-Linux;$(NetCoreAppMinimum);netstandard2.0</TargetFrameworks>
66
<IsPackable>true</IsPackable>
7+
<Nullable>annotations</Nullable>
78
<AddNETFrameworkPlaceholderFileToPackage>true</AddNETFrameworkPlaceholderFileToPackage>
89
<AddNETFrameworkAssemblyReferenceToPackage>true</AddNETFrameworkAssemblyReferenceToPackage>
910
<PackageDescription>Provides the methods defined in the Lightweight Directory Access Protocol (LDAP) version 3 (V3) and Directory Services Markup Language (DSML) version 2.0 (V2) standards.</PackageDescription>

src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/PInvokeStubCodeGenerator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@ BoundGenerator CreateGenerator(TypePositionInfo p)
197197
{
198198
try
199199
{
200+
// TODO: Remove once helper types (like ArrayMarshaller) are part of the runtime
201+
// This check is to help with enabling the source generator for runtime libraries without making each
202+
// library directly reference System.Memory and System.Runtime.CompilerServices.Unsafe unless it needs to
203+
if (p.MarshallingAttributeInfo is MissingSupportMarshallingInfo
204+
&& (environment.TargetFramework == TargetFramework.Net && environment.TargetFrameworkVersion.Major >= 7))
205+
{
206+
throw new MarshallingNotSupportedException(p, this);
207+
}
208+
200209
return new BoundGenerator(p, generatorFactory.Create(p, this));
201210
}
202211
catch (MarshallingNotSupportedException e)

src/libraries/System.Speech/src/System.Speech.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<!-- CS0649: uninitialized interop type fields -->
66
<!-- SA1129: https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3277 -->
77
<NoWarn>$(NoWarn);CS0649;SA1129;CA1847</NoWarn>
8+
<Nullable>annotations</Nullable>
89
<SetIsTrimmable>false</SetIsTrimmable>
910
<IsPackable>true</IsPackable>
1011
<AddNETFrameworkPlaceholderFileToPackage>true</AddNETFrameworkPlaceholderFileToPackage>

0 commit comments

Comments
 (0)