Skip to content

Commit

Permalink
[DllImportGenerator] Enable on projects without System.Memory and Sys…
Browse files Browse the repository at this point in the history
…tem.Runtime.CompilerServices.Unsafe
  • Loading branch information
elinor-fung committed Nov 17, 2021
1 parent a1b391b commit 1c61e06
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
25 changes: 15 additions & 10 deletions eng/generators.targets
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,15 @@
<!-- If the current project is not System.Private.CoreLib, we enable the DllImportGenerator source generator
when the project is a C# source project that either:
- references System.Private.CoreLib, or
- references the following assemblies:
- System.Runtime.InteropServices
- System.Runtime.CompilerServices.Unsafe
- System.Memory -->
- references System.Runtime.InteropServices -->
<EnabledGenerators Include="DllImportGenerator"
Condition="'$(EnableDllImportGenerator)' == ''
and '$(IsFrameworkSupportFacade)' != 'true'
and '$(IsSourceProject)' == 'true'
and '$(MSBuildProjectExtension)' == '.csproj'
and (
('@(Reference)' != ''
and @(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices'))
and (@(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.CompilerServices.Unsafe'))
or ('@(ProjectReference)' != ''
and @(ProjectReference->AnyHaveMetadataValue('Identity', $([MSBuild]::NormalizePath('$(LibrariesProjectRoot)', 'System.Runtime.CompilerServices.Unsafe', 'src', 'System.Runtime.CompilerServices.Unsafe.ilproj'))))))
and @(Reference->AnyHaveMetadataValue('Identity', 'System.Memory')))
and @(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices')))
or ('@(ProjectReference)' != ''
and @(ProjectReference->AnyHaveMetadataValue('Identity', '$(CoreLibProject)'))))" />
<EnabledGenerators Include="DllImportGenerator"
Expand All @@ -45,7 +38,19 @@

<!-- Only add the following files if we are on the latest TFM (that is, net7). -->
<Compile Condition="'$(NetCoreAppCurrentTargetFrameworkMoniker)' == '$(TargetFrameworkMoniker)'" Include="$(LibrariesProjectRoot)Common\src\System\Runtime\InteropServices\GeneratedMarshallingAttribute.cs" />
<Compile Condition="'$(NetCoreAppCurrentTargetFrameworkMoniker)' == '$(TargetFrameworkMoniker)'" Include="$(LibrariesProjectRoot)Common\src\System\Runtime\InteropServices\ArrayMarshaller.cs" />

<!-- 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 -->
<Compile Condition="'$(NetCoreAppCurrentTargetFrameworkMoniker)' == '$(TargetFrameworkMoniker)'
and (
'$(MSBuildProjectName)' == 'System.Private.CoreLib'
or '$(EnableDllImportGenerator)' == 'true'
or ('@(Reference)' != ''
and (@(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.CompilerServices.Unsafe'))
or ('@(ProjectReference)' != ''
and @(ProjectReference->AnyHaveMetadataValue('Identity', $([MSBuild]::NormalizePath('$(LibrariesProjectRoot)', 'System.Runtime.CompilerServices.Unsafe', 'src', 'System.Runtime.CompilerServices.Unsafe.ilproj'))))))
and @(Reference->AnyHaveMetadataValue('Identity', 'System.Memory')))
or ('@(ProjectReference)' != ''
and @(ProjectReference->AnyHaveMetadataValue('Identity', '$(CoreLibProject)'))))" Include="$(LibrariesProjectRoot)Common\src\System\Runtime\InteropServices\ArrayMarshaller.cs" />
</ItemGroup>

<Target Name="ConfigureGenerators"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<IncludeDllSafeSearchPathAttribute>true</IncludeDllSafeSearchPathAttribute>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0</TargetFrameworks>
<NoWarn>$(NoWarn);CA2249</NoWarn>
<Nullable>annotations</Nullable>
<IsPackable>true</IsPackable>
<AddNETFrameworkPlaceholderFileToPackage>true</AddNETFrameworkPlaceholderFileToPackage>
<AddNETFrameworkAssemblyReferenceToPackage>true</AddNETFrameworkAssemblyReferenceToPackage>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<IncludeDllSafeSearchPathAttribute>true</IncludeDllSafeSearchPathAttribute>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum)-OSX;$(NetCoreAppMinimum)-Linux;$(NetCoreAppMinimum);netstandard2.0</TargetFrameworks>
<IsPackable>true</IsPackable>
<Nullable>annotations</Nullable>
<AddNETFrameworkPlaceholderFileToPackage>true</AddNETFrameworkPlaceholderFileToPackage>
<AddNETFrameworkAssemblyReferenceToPackage>true</AddNETFrameworkAssemblyReferenceToPackage>
<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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ BoundGenerator CreateGenerator(TypePositionInfo p)
{
try
{
// TODO: Remove once helper types (like ArrayMarshaller) are part of the runtime
// This check is to help with enabling the source generator for runtime libraries without making each
// library directly reference System.Memory and System.Runtime.CompilerServices.Unsafe unless it needs to
if (p.MarshallingAttributeInfo is MissingSupportMarshallingInfo
&& (environment.TargetFramework == TargetFramework.Net && environment.TargetFrameworkVersion.Major >= 7))
{
throw new MarshallingNotSupportedException(p, this);
}

return new BoundGenerator(p, generatorFactory.Create(p, this));
}
catch (MarshallingNotSupportedException e)
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Speech/src/System.Speech.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<!-- CS0649: uninitialized interop type fields -->
<!-- SA1129: https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3277 -->
<NoWarn>$(NoWarn);CS0649;SA1129;CA1847</NoWarn>
<Nullable>annotations</Nullable>
<SetIsTrimmable>false</SetIsTrimmable>
<IsPackable>true</IsPackable>
<AddNETFrameworkPlaceholderFileToPackage>true</AddNETFrameworkPlaceholderFileToPackage>
Expand Down

0 comments on commit 1c61e06

Please sign in to comment.