Skip to content

Commit

Permalink
Merge pull request #946 from microsoft/fix895
Browse files Browse the repository at this point in the history
Add the WDK as a default metadata input
  • Loading branch information
AArnott committed May 26, 2023
2 parents ed29c9e + c2db916 commit d18600d
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>

<MetadataVersion>52.0.65-preview</MetadataVersion>
<WDKMetadataVersion>0.7.3-experimental</WDKMetadataVersion>
<!-- <DiaMetadataVersion>0.2.185-preview-g7e1e6a442c</DiaMetadataVersion> -->
<ApiDocsVersion>0.1.41-alpha</ApiDocsVersion>

Expand All @@ -25,6 +26,7 @@
<PackageVersion Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageVersion Include="Microsoft.Windows.SDK.Win32Docs" Version="$(ApiDocsVersion)" />
<PackageVersion Include="Microsoft.Windows.SDK.Win32Metadata" Version="$(MetadataVersion)" />
<PackageVersion Include="Microsoft.Windows.WDK.Win32Metadata" Version="$(WDKMetadataVersion)" />
<PackageVersion Include="NuGet.Protocol" Version="6.5.0" />
<PackageVersion Include="System.Collections.Immutable" Version="6.0.0" />
<PackageVersion Include="System.Memory" Version="4.5.5" />
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,7 @@ To update the metadata used as the source for code generation, you may install a
dotnet add package Microsoft.Windows.SDK.Win32Metadata --prerelease
```

Alternatively, you may set the `MicrosoftWindowsSdkWin32MetadataBasePath` property in your project file to the path of the directory containing `Windows.Win32.winmd`:

```xml
<MicrosoftWindowsSdkWin32MetadataBasePath>c:\path\to\dir</MicrosoftWindowsSdkWin32MetadataBasePath>
```
CsWin32 also consumes the WDK from a similarly named package: `Microsoft.Windows.WDK.WDKMetadata`.

## Consuming daily builds

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
<PackageReference Include="Microsoft.Windows.SDK.Win32Metadata" GeneratePathProperty="true" PrivateAssets="none" />
<PackageReference Include="Microsoft.Windows.SDK.Win32Docs" GeneratePathProperty="true" PrivateAssets="none" />
<PackageReference Include="Microsoft.Windows.WDK.Win32Metadata" GeneratePathProperty="true" PrivateAssets="none" />
<PackageReference Include="System.Memory" PrivateAssets="none" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" PrivateAssets="none" />
<PackageReference Include="System.Reflection.Metadata" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,33 @@
<dependencies>
<group targetFramework="net20">
<dependency id="Microsoft.Windows.SDK.Win32Metadata" version="$MetadataVersion$" include="buildTransitive" />
<dependency id="Microsoft.Windows.WDK.Win32Metadata" version="$WDKMetadataVersion$" include="buildTransitive" />
<dependency id="Microsoft.Windows.SDK.Win32Docs" version="$ApiDocsVersion$" include="buildTransitive" />
</group>
<group targetFramework="net45">
<dependency id="Microsoft.Windows.SDK.Win32Metadata" version="$MetadataVersion$" include="buildTransitive" />
<dependency id="Microsoft.Windows.WDK.Win32Metadata" version="$WDKMetadataVersion$" include="buildTransitive" />
<dependency id="Microsoft.Windows.SDK.Win32Docs" version="$ApiDocsVersion$" include="buildTransitive" />
<dependency id="System.Memory" version="4.5.5" include="All" />
<dependency id="System.Runtime.CompilerServices.Unsafe" version="5.0.0" include="All" />
</group>
<group targetFramework="net461">
<dependency id="Microsoft.Windows.SDK.Win32Metadata" version="$MetadataVersion$" include="buildTransitive" />
<dependency id="Microsoft.Windows.WDK.Win32Metadata" version="$WDKMetadataVersion$" include="buildTransitive" />
<dependency id="Microsoft.Windows.SDK.Win32Docs" version="$ApiDocsVersion$" include="buildTransitive" />
<dependency id="System.Memory" version="4.5.5" include="All" />
<dependency id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" include="All" />
</group>
<group targetFramework=".NETStandard1.1">
<dependency id="Microsoft.Windows.SDK.Win32Metadata" version="$MetadataVersion$" include="buildTransitive" />
<dependency id="Microsoft.Windows.WDK.Win32Metadata" version="$WDKMetadataVersion$" include="buildTransitive" />
<dependency id="Microsoft.Windows.SDK.Win32Docs" version="$ApiDocsVersion$" include="buildTransitive" />
<dependency id="System.Memory" version="4.5.5" include="All" />
<dependency id="System.Runtime.CompilerServices.Unsafe" version="5.0.0" include="All" />
</group>
<group targetFramework=".NETStandard2.0">
<dependency id="Microsoft.Windows.SDK.Win32Metadata" version="$MetadataVersion$" include="buildTransitive" />
<dependency id="Microsoft.Windows.WDK.Win32Metadata" version="$WDKMetadataVersion$" include="buildTransitive" />
<dependency id="Microsoft.Windows.SDK.Win32Docs" version="$ApiDocsVersion$" include="buildTransitive" />
<dependency id="System.Memory" version="4.5.5" include="All" />
<dependency id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" include="All" />
Expand Down
11 changes: 10 additions & 1 deletion src/Microsoft.Windows.CsWin32/Microsoft.Windows.CsWin32.targets
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@

<Target Name="SetNuSpecProperties" BeforeTargets="GenerateNuspec" DependsOnTargets="GetBuildVersion">
<PropertyGroup>
<NuspecProperties>$(NuspecProperties);Version=$(Version);BaseOutputPath=$(OutputPath);MetadataVersion=$(MetadataVersion);ApiDocsVersion=$(ApiDocsVersion);PackageReleaseNotes=$(PackageReleaseNotes);commit=$(GitCommitId);</NuspecProperties>
<NuspecProperties>
$(NuspecProperties);
Version=$(Version);
BaseOutputPath=$(OutputPath);
MetadataVersion=$(MetadataVersion);
WDKMetadataVersion=$(WDKMetadataVersion);
ApiDocsVersion=$(ApiDocsVersion);
PackageReleaseNotes=$(PackageReleaseNotes);
commit=$(GitCommitId);
</NuspecProperties>
</PropertyGroup>
</Target>

Expand Down
28 changes: 28 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/DelegateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

public class DelegateTests : GeneratorTestBase
{
public DelegateTests(ITestOutputHelper logger)
: base(logger)
{
}

[Theory]
[CombinatorialData]
public void InterestingDelegates(
[CombinatorialValues(
"LPD3DHAL_RENDERSTATECB")] // A delegate with a pointer parameter to a managed struct.
string name,
bool allowMarshaling)
{
var options = DefaultTestGeneratorOptions with
{
AllowMarshaling = allowMarshaling,
};
this.generator = this.CreateGenerator(options);
Assert.True(this.generator.TryGenerate(name, CancellationToken.None));
this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();
}
}
13 changes: 13 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/ExternMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,17 @@ public void AssociatedEnumOnParameterWithVoidReturn()
this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();
}

/// <summary>
/// Verifies that we can generate APIs from the WDK, which includes references to the SDK.
/// </summary>
[Fact]
public void WdkMethod_NtCreateFile()
{
const string Method = "NtCreateFile";
this.generator = this.CreateGenerator();
Assert.True(this.generator.TryGenerate(Method, CancellationToken.None));
this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();
}
}
5 changes: 4 additions & 1 deletion test/Microsoft.Windows.CsWin32.Tests/GeneratorTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public abstract class GeneratorTestBase : IDisposable, IAsyncLifetime
protected static readonly GeneratorOptions DefaultTestGeneratorOptions = new GeneratorOptions { EmitSingleFile = true };
protected static readonly string FileSeparator = new string('=', 140);
protected static readonly string MetadataPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location!)!, "Windows.Win32.winmd");
protected static readonly string WdkMetadataPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location!)!, "Windows.Wdk.winmd");
protected static readonly string[] DefaultMetadataPaths = new[] { MetadataPath, WdkMetadataPath };
////protected static readonly string DiaMetadataPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location!)!, "Microsoft.Dia.winmd");
protected static readonly string ApiDocsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location!)!, "apidocs.msgpack");

Expand Down Expand Up @@ -319,7 +321,8 @@ protected async Task<CSharpCompilation> CreateCompilationAsync(ReferenceAssembli
return compilation;
}

protected SuperGenerator CreateGenerator(GeneratorOptions? options = null, CSharpCompilation? compilation = null, bool includeDocs = false) => this.CreateSuperGenerator(new[] { MetadataPath }, options, compilation, includeDocs);
protected SuperGenerator CreateGenerator(GeneratorOptions? options = null, CSharpCompilation? compilation = null, bool includeDocs = false)
=> this.CreateSuperGenerator(DefaultMetadataPaths, options, compilation, includeDocs);

protected SuperGenerator CreateSuperGenerator(string[] metadataPaths, GeneratorOptions? options = null, CSharpCompilation? compilation = null, bool includeDocs = false) =>
SuperGenerator.Combine(metadataPaths.Select(path => new Generator(path, includeDocs ? Docs.Get(ApiDocsPath) : null, options ?? DefaultTestGeneratorOptions, compilation ?? this.compilation, this.parseOptions)));
Expand Down
9 changes: 9 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/HandleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ public void AvoidSafeHandles()
Assert.Equal("HANDLE", Assert.IsType<QualifiedNameSyntax>(friendlyOverload.ParameterList.Parameters[0].Type).Right.Identifier.ValueText);
}

[Fact]
public void SafeHandleInWDK()
{
this.generator = this.CreateGenerator(DefaultTestGeneratorOptions);
Assert.True(this.generator.TryGenerate("OROpenHive", CancellationToken.None));
this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();
}

/// <summary>
/// Verifies that MSIHANDLE is wrapped with a SafeHandle even though it is a 32-bit handle.
/// This is safe because we never pass SafeHandle directly to extern methods, so we can fix the length of the parameter or return value.
Expand Down
3 changes: 3 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/StructTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ public void FieldWithAssociatedEnum()
[CombinatorialData]
public void InterestingStructs(
[CombinatorialValues(
"DRIVER_OBJECT", // has an inline array of delegates
"DEVICE_RELATIONS", // ends with an inline "flexible" array
"D3DHAL_CONTEXTCREATEDATA", // contains a field that is a pointer to a struct that is normally managed
"WSD_EVENT")] // has a pointer field to a managed struct
string name,
bool allowMarshaling)
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "0.2-beta",
"version": "0.3-beta",
"assemblyVersion": {
"precision": "revision"
},
Expand Down

0 comments on commit d18600d

Please sign in to comment.