Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
<!-- Silk.NET.Core -->
<PackageVersion Include="Fody" Version="6.9.3" PrivateAssets="all" />
<PackageVersion Include="InlineIL.Fody" Version="1.10.2" PrivateAssets="all" />
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="5.0.0-1.25277.114" />
<PackageVersion
Include="Microsoft.CodeAnalysis.PublicApiAnalyzers"
Version="5.0.0-1.25277.114"
/>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just CSharpier reformatting outside of removing Autofac here.

<!-- Analyzers -->
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="5.3.0" />
<!-- SilkTouch -->
<PackageVersion Include="Autofac" Version="9.3.4" />
<PackageVersion Include="Autofac.Extensions.DependencyInjection" Version="11.0.2" />
<PackageVersion Include="ClangSharp.PInvokeGenerator" Version="22.1.8" />
<PackageVersion Include="CSharpier.Core" Version="1.3.0" />
<PackageVersion Include="Humanizer.Core" Version="2.14.1" />
Expand All @@ -34,11 +35,17 @@
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="5.3.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="10.0.9" />
<PackageVersion Include="Microsoft.Extensions.Configuration.CommandLine" Version="10.0.9" />
<PackageVersion Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="10.0.9" />
<PackageVersion
Include="Microsoft.Extensions.Configuration.EnvironmentVariables"
Version="10.0.9"
/>
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="10.0.9" />
<PackageVersion Include="Microsoft.Extensions.FileSystemGlobbing" Version="10.0.9" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="10.0.9" />
<PackageVersion Include="Microsoft.VisualStudio.Setup.Configuration.Interop" Version="3.14.2075" />
<PackageVersion
Include="Microsoft.VisualStudio.Setup.Configuration.Interop"
Version="3.14.2075"
/>
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageVersion Include="System.Configuration.ConfigurationManager" Version="10.0.9" />
<PackageVersion Include="System.IO.Hashing" Version="10.0.9" />
Expand Down
41 changes: 35 additions & 6 deletions sources/SilkTouch/SilkTouch/Clang/ClangScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Diagnostics;
using ClangSharp;
using ClangSharp.Interop;
Expand All @@ -10,6 +12,7 @@
using Microsoft.CodeAnalysis.Text;
using Microsoft.Extensions.Logging;
using Silk.NET.SilkTouch.Caching;
using Silk.NET.SilkTouch.CommandLine;
using Silk.NET.SilkTouch.Mods;
using Silk.NET.SilkTouch.Sources;
using static ClangSharp.Interop.CXDiagnosticSeverity;
Expand All @@ -25,7 +28,6 @@ namespace Silk.NET.SilkTouch.Clang;
/// </param>
/// <param name="documentFactory">The document factory to use.</param>
/// <param name="config">The configuration to use.</param>
/// <param name="hostConfig">The host configuration to use.</param>
/// <param name="logger">The logger to use.</param>
/// <param name="inputResolver">The input resolver to use.</param>
/// <param name="responseFileMods">The mods that modify response files before they are fed to ClangSharp.</param>
Expand All @@ -34,15 +36,29 @@ public sealed class ClangScraper(
ResponseFileHandler rspHandler,
DocumentFactory documentFactory,
ClangScraper.Configuration config,
SilkTouchHostConfiguration hostConfig,
ILogger<ClangScraper> logger,
IInputResolver inputResolver,
ICacheProvider? cacheProvider = null,
IEnumerable<IResponseFileMod>? responseFileMods = null
) : IMod
) : IMod, ICommandLineConfigurable
{
// TODO add Windows SDK and Xcode configuration

private static Option<bool> UseCacheOnlyOption { get; } =
new(["--use-cache-only"], () => false)
{
Description = $"Limits {nameof(ClangScraper)} to only using the cache.",
};

private static Option<bool> GenerateCacheOnlyOption { get; } =
new(["--generate-cache-only"], () => false)
{
Description = $"Limits {nameof(ClangScraper)} to only generating the cache.",
};

private bool _useCacheOnly = false;
private bool _generateCacheOnly = false;

/// <summary>
/// Configuration for the <see cref="ClangScraper"/> mod.
/// </summary>
Expand Down Expand Up @@ -88,12 +104,25 @@ public record Configuration
/// Whether this mod should end the generation job after a "generate cache only" run.
/// </summary>
/// <remarks>
/// A "generate cache only" run is a run where <see cref="SilkTouchHostConfiguration.GenerateCacheOnly"/> is set to true.
/// The intended use pattern is setting this to true for the last <see cref="ClangScraper"/> mod in a job.
/// </remarks>
public bool ExitAfterGenerateCacheOnlyRun { get; init; }
}

/// <inheritdoc />
public void RegisterCommandLineOptions(Action<Option> register)
{
register.Invoke(UseCacheOnlyOption);
register.Invoke(GenerateCacheOnlyOption);
}

/// <inheritdoc />
public void ReadCommandLineOptions(InvocationContext ctx)
{
_useCacheOnly = ctx.ParseResult.GetValueForOption(UseCacheOnlyOption);
_generateCacheOnly = ctx.ParseResult.GetValueForOption(GenerateCacheOnlyOption);
}

/// <summary>
/// Runs ClangSharp to obtain the raw bindings for the given response file.
/// </summary>
Expand Down Expand Up @@ -395,7 +424,7 @@ public async Task ExecuteAsync(IModContext ctx, CancellationToken ct = default)

// Try generating the raw bindings with ClangSharp
// If scraping needs to be skipped or if the scrape fails, then fall back to a cache if available
var shouldScrape = !hostConfig.UseCacheOnly && config.ScrapeIf.Invoke();
var shouldScrape = !_useCacheOnly && config.ScrapeIf.Invoke();

var cacheKey =
resolvedConfig.CacheKey != null
Expand Down Expand Up @@ -431,7 +460,7 @@ public async Task ExecuteAsync(IModContext ctx, CancellationToken ct = default)
}
}

if (hostConfig.GenerateCacheOnly)
if (_generateCacheOnly)
{
// GenerateCacheOnly is intended to be used when not all caches are available (eg: for multi-platform cache population),
// so we don't validate whether a cache was generated or not in this branch
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.CommandLine;
using System.CommandLine.Invocation;

namespace Silk.NET.SilkTouch.CommandLine;

/// <summary>
/// Provides a way to register command line options and read them after they have been parsed.
/// </summary>
public interface ICommandLineConfigurable
{
/// <summary>
/// Registers the command line options through the provided callback.
/// </summary>
/// <remarks>
/// Options are recommended to be statically defined so they can be deduplicated.
/// </remarks>
public void RegisterCommandLineOptions(Action<Option> register);

/// <summary>
/// Reads the command line options from the provided context.
/// </summary>
public void ReadCommandLineOptions(InvocationContext ctx);
}
2 changes: 0 additions & 2 deletions sources/SilkTouch/SilkTouch/Silk.NET.SilkTouch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" />
<PackageReference Include="ClangSharp.PInvokeGenerator" />
<PackageReference Include="CSharpier.Core" />
<PackageReference Include="Humanizer.Core" />
Expand Down
Loading
Loading