Skip to content

Commit 7570027

Browse files
authored
Basic infrastructure to enable DllImportGenerator (dotnet#52486)
1 parent 74cafe7 commit 7570027

File tree

6 files changed

+116
-1
lines changed

6 files changed

+116
-1
lines changed

Directory.Build.targets

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<Import Project="Sdk.targets" Sdk="Microsoft.DotNet.Arcade.Sdk" />
1111
<Import Project="$(RepositoryEngineeringDir)liveBuilds.targets" />
12+
<Import Project="$(RepositoryEngineeringDir)generators.targets" />
1213
<Import Project="$(RepositoryEngineeringDir)python.targets" />
1314

1415
<PropertyGroup>
@@ -24,7 +25,7 @@
2425
<InformationalVersion Condition="'$(InformationalVersion)' == '' and '$(VersionSuffix)' == ''">$(ProductVersion)</InformationalVersion>
2526
<InformationalVersion Condition="'$(InformationalVersion)' == '' and '$(VersionSuffix)' != ''">$(ProductVersion)-$(VersionSuffix)</InformationalVersion>
2627
</PropertyGroup>
27-
28+
2829
<!-- The Default behavior in VS is to show files for the first target framework in TargetFrameworks property.
2930
This is required to show all the files corresponding to all target frameworks in VS. -->
3031
<ItemGroup Condition="'$(DefaultLanguageSourceExtension)' != '' and

NuGet.config

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
<add key="dotnet6-transport" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6-transport/nuget/v3/index.json" />
2020
<!-- Used for the Rich Navigation indexing task -->
2121
<add key="richnav" value="https://pkgs.dev.azure.com/azure-public/vside/_packaging/vs-buildservices/nuget/v3/index.json" />
22+
<!-- Used for DllImportGenerator -->
23+
<add key="dotnet-experimental" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-experimental/nuget/v3/index.json" />
2224
</packageSources>
2325
<disabledPackageSources>
2426
<clear />

eng/Versions.props

+2
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@
168168
<runtimewinx64MicrosoftNETCoreRuntimeMonoLLVMToolsVersion>9.0.1-alpha.1.21253.1</runtimewinx64MicrosoftNETCoreRuntimeMonoLLVMToolsVersion>
169169
<runtimeosx1012x64MicrosoftNETCoreRuntimeMonoLLVMSdkVersion>9.0.1-alpha.1.21253.1</runtimeosx1012x64MicrosoftNETCoreRuntimeMonoLLVMSdkVersion>
170170
<runtimeosx1012x64MicrosoftNETCoreRuntimeMonoLLVMToolsVersion>9.0.1-alpha.1.21253.1</runtimeosx1012x64MicrosoftNETCoreRuntimeMonoLLVMToolsVersion>
171+
<!-- Experimental -->
172+
<MicrosoftInteropDllImportGeneratorVersion>1.0.0-alpha.21258.2</MicrosoftInteropDllImportGeneratorVersion>
171173
</PropertyGroup>
172174
<!-- Override isolated build dependency versions with versions from Repo API. -->
173175
<Import Project="$(DotNetPackageVersionPropsPath)" Condition="'$(DotNetPackageVersionPropsPath)' != ''" />

eng/generators.targets

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<Project InitialTargets="ConfigureGenerators">
2+
3+
<Target Name="ConfigureGenerators"
4+
DependsOnTargets="ConfigureDllImportGenerator" />
5+
6+
<!-- Microsoft.Interop.DllImportGenerator -->
7+
<Target Name="ConfigureDllImportGenerator" DependsOnTargets="EnableDllImportGeneratorForNetCoreApp">
8+
<PropertyGroup Condition="'$(EnableDllImportGenerator)' == 'true'">
9+
<DllImportGenerator_UseMarshalType>true</DllImportGenerator_UseMarshalType>
10+
</PropertyGroup>
11+
<ItemGroup Condition="'$(EnableDllImportGenerator)' == 'true' and $([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
12+
<PackageReference Include="Microsoft.Interop.DllImportGenerator" Version="$(MicrosoftInteropDllImportGeneratorVersion)" PrivateAssets="all" />
13+
14+
<!-- For testing purposes, compile sources files with attributes directly into the project.
15+
This is mimicking the case where the source generator always generates the attributes. -->
16+
<Compile Include="$(LibrariesProjectRoot)Common/src/System/Runtime/InteropServices/GeneratedDllImportAttribute.cs" />
17+
<Compile Include="$(LibrariesProjectRoot)Common/src/System/Runtime/InteropServices/GeneratedMarshallingAttribute.cs" />
18+
</ItemGroup>
19+
</Target>
20+
21+
<Target Name="EnableDllImportGeneratorForNetCoreApp"
22+
Condition="'$(EnableDllImportGenerator)' == ''
23+
and ('$(IsNetCoreAppSrc)' == 'true' or '$(MSBuildProjectName)' == 'System.Private.CoreLib')
24+
and '$(MSBuildProjectExtension)' == '.csproj'">
25+
<PropertyGroup>
26+
<!-- Enable DllImportGenerator by default for System.Private.CoreLib -->
27+
<EnableDllImportGenerator Condition="'$(MSBuildProjectName)' == 'System.Private.CoreLib'">true</EnableDllImportGenerator>
28+
29+
<!-- Enable DllImportGenerator by default for NETCoreApp libraries that reference either System.Runtime.InteropServices or System.Private.CoreLib -->
30+
<EnableDllImportGenerator Condition="'@(Reference)' != '' and @(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices'))">true</EnableDllImportGenerator>
31+
<EnableDllImportGenerator Condition="'@(ProjectReference)' != '' and @(ProjectReference->AnyHaveMetadataValue('Identity', '$(CoreLibProject)'))">true</EnableDllImportGenerator>
32+
</PropertyGroup>
33+
</Target>
34+
35+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#nullable enable
5+
6+
//
7+
// Types in this file are used for generated p/invokes (docs/design/features/source-generator-pinvokes.md).
8+
// See the DllImportGenerator experiment in https://github.com/dotnet/runtimelab.
9+
//
10+
namespace System.Runtime.InteropServices
11+
{
12+
/// <summary>
13+
/// Indicates that method will be generated at compile time and invoke into an unmanaged library entry point
14+
/// </summary>
15+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
16+
internal sealed class GeneratedDllImportAttribute : Attribute
17+
{
18+
public bool BestFitMapping { get; set; }
19+
public CallingConvention CallingConvention { get; set; }
20+
public CharSet CharSet { get; set; }
21+
public string? EntryPoint { get; set; }
22+
public bool ExactSpelling { get; set; }
23+
public bool PreserveSig { get; set; }
24+
public bool SetLastError { get; set; }
25+
public bool ThrowOnUnmappableChar { get; set; }
26+
27+
public GeneratedDllImportAttribute(string dllName)
28+
{
29+
this.Value = dllName;
30+
}
31+
32+
public string Value { get; private set; }
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
//
5+
// Types in this file are used for generated p/invokes (docs/design/features/source-generator-pinvokes.md).
6+
// See the DllImportGenerator experiment in https://github.com/dotnet/runtimelab.
7+
//
8+
namespace System.Runtime.InteropServices
9+
{
10+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
11+
internal class GeneratedMarshallingAttribute : Attribute
12+
{
13+
}
14+
15+
[AttributeUsage(AttributeTargets.Struct)]
16+
internal class BlittableTypeAttribute : Attribute
17+
{
18+
}
19+
20+
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class)]
21+
internal class NativeMarshallingAttribute : Attribute
22+
{
23+
public NativeMarshallingAttribute(Type nativeType)
24+
{
25+
NativeType = nativeType;
26+
}
27+
28+
public Type NativeType { get; }
29+
}
30+
31+
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.Field)]
32+
internal class MarshalUsingAttribute : Attribute
33+
{
34+
public MarshalUsingAttribute(Type nativeType)
35+
{
36+
NativeType = nativeType;
37+
}
38+
39+
public Type NativeType { get; }
40+
}
41+
}

0 commit comments

Comments
 (0)