Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Fix circular dependency, add GLsync
Browse files Browse the repository at this point in the history
  • Loading branch information
Perksey committed Nov 11, 2023
1 parent 55a1148 commit b4e71bf
Show file tree
Hide file tree
Showing 11 changed files with 17,445 additions and 17,399 deletions.
2 changes: 1 addition & 1 deletion eng/silktouch/opengl/remap.rsp
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
--remap
TODO_ADD_REMAPPINGS=HERE
__GLsync=GLsync
5,380 changes: 2,690 additions & 2,690 deletions sources/OpenGL/IGL.gen.cs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions sources/OpenGL/Sync.gen.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Silk.NET.OpenGL;

public readonly struct Sync { }
29,288 changes: 14,642 additions & 14,646 deletions sources/OpenGL/gl/GL.gen.cs

Large diffs are not rendered by default.

29 changes: 9 additions & 20 deletions sources/SilkTouchX/Mods/PrettifyNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@ namespace SilkTouchX.Mods;
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="config">Configuration snapshot.</param>
/// <param name="trimmers">Name trimmers.</param>
/// <param name="otherMods">Other mods present in the generator.</param>
/// <param name="jobConfig">The general job configuration.</param>
/// <param name="trimmerProviders">Name trimmer providers.</param>
[ModConfiguration<Configuration>]
public class PrettifyNames(
ILogger<PrettifyNames> logger,
IOptionsSnapshot<PrettifyNames.Configuration> config,
IEnumerable<INameTrimmer> trimmers,
IEnumerable<IMod> otherMods,
IOptionsSnapshot<SilkTouchConfiguration> jobConfig
IEnumerable<INameTrimmerProvider> trimmerProviders
) : IMod
{
/// <summary>
Expand Down Expand Up @@ -73,16 +69,9 @@ public Task<GeneratedSyntax> AfterScrapeAsync(string key, GeneratedSyntax syntax
if (typeNames.Count > 1 || cfg.GlobalPrefixHint is not null)
{
foreach (
var trimmer in (
jobConfig
.Get(key)
.Mods?.Select(x => otherMods.First(y => y.GetType().Name == x))
.OfType<INameTrimmer>() ?? Enumerable.Empty<INameTrimmer>()
).Concat(
trimmers
.Where(x => x.Version >= cfg.TrimmerBaseline)
.OrderBy(x => x.Version)
)
var trimmer in trimmerProviders
.SelectMany(x => x.GetTrimmers(key))
.OrderBy(x => x.Version)
)
{
trimmer.Trim(null, cfg.GlobalPrefixHint, typeNames, cfg.PrefixOverrides);
Expand All @@ -99,8 +88,8 @@ var trimmer in (
if (constNames is not null)
{
foreach (
var trimmer in trimmers
.Where(x => x.Version >= cfg.TrimmerBaseline)
var trimmer in trimmerProviders
.SelectMany(x => x.GetTrimmers(key))
.OrderBy(x => x.Version)
)
{
Expand All @@ -124,8 +113,8 @@ var trimmer in trimmers
if (functionNames is not null)
{
foreach (
var trimmer in trimmers
.Where(x => x.Version >= cfg.TrimmerBaseline)
var trimmer in trimmerProviders
.SelectMany(x => x.GetTrimmers(key))
.OrderBy(x => x.Version)
)
{
Expand Down
15 changes: 15 additions & 0 deletions sources/SilkTouchX/Naming/INameTrimmerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;

namespace SilkTouchX.Naming;

/// <summary>
/// Provides name trimmers.
/// </summary>
public interface INameTrimmerProvider
{
/// <summary>
/// The name trimmers.
/// </summary>
/// <param name="key">The job key.</param>
public IEnumerable<INameTrimmer> GetTrimmers(string? key);
}
31 changes: 31 additions & 0 deletions sources/SilkTouchX/Naming/NameTrimmerProviders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Options;
using SilkTouchX.Mods;

namespace SilkTouchX.Naming;

internal class NameTrimmerProviders
{
public class Global(
IEnumerable<INameTrimmer> trimmers,
IOptionsSnapshot<PrettifyNames.Configuration> cfg
) : INameTrimmerProvider
{
public IEnumerable<INameTrimmer> GetTrimmers(string? key)
{
var baseline = cfg.Get(key).TrimmerBaseline;
return trimmers.Where(x => x.Version >= baseline);
}
}

public class ModTrimmer<T>(T mod, IOptionsSnapshot<SilkTouchConfiguration> job)
: INameTrimmerProvider
where T : INameTrimmer
{
public IEnumerable<INameTrimmer> GetTrimmers(string? key) =>
job.Get(key).Mods?.Contains(typeof(T).Name) ?? false
? Enumerable.Repeat<INameTrimmer>(mod, 1)
: Enumerable.Empty<INameTrimmer>();
}
}
4 changes: 0 additions & 4 deletions sources/SilkTouchX/Naming/NameUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ public static string Prettify(this string str)
.Pascalize()
.Where(x => char.IsLetter(x) || char.IsNumber(x))
);
if (ret == "GlDependentArTexture2dNV")
{
Debugger.Break();
}
return !char.IsLetter(ret[0]) ? $"X{ret}" : ret;
}

Expand Down
13 changes: 13 additions & 0 deletions sources/SilkTouchX/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ IConfiguration config
services.AddSingleton<Microsoft.Build.Framework.ILogger, WorkspaceLogger>();
services.AddSingleton<NameTrimmer>();
services.AddSingleton<INameTrimmer>(s => s.GetRequiredService<NameTrimmer>());
services.AddSingleton<INameTrimmerProvider, NameTrimmerProviders.Global>();
services.TryAddSingleton<IOutputWriter, DirectOutputWriter>();
if (OperatingSystem.IsWindows())
{
Expand Down Expand Up @@ -150,6 +151,18 @@ IConfiguration config
{
services.AddSingleton(loadedMods[m]);
services.AddSingleton<IMod>(s => (IMod)s.GetRequiredService(loadedMods[m]));
if (!loadedMods[m].IsAssignableTo(typeof(INameTrimmer)))
{
continue;
}

var provider = typeof(NameTrimmerProviders.ModTrimmer<>).MakeGenericType(
loadedMods[m]
);
services.AddSingleton(provider);
services.AddSingleton<INameTrimmerProvider>(
s => (INameTrimmerProvider)s.GetRequiredService(provider)
);
}
}

Expand Down
73 changes: 35 additions & 38 deletions sources/SilkTouchX/SilkTouchGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.Extensions.Logging;
using SilkTouchX.Clang;
using SilkTouchX.Mods;
using SilkTouchX.Naming;
using SilkTouchX.Workspace;
using Diagnostic = ClangSharp.Diagnostic;

Expand All @@ -24,36 +25,31 @@ namespace SilkTouchX;
/// <summary>
/// The main entry-point class for SilkTouch.
/// </summary>
public class SilkTouchGenerator
/// <param name="scraper">The scraper.</param>
/// <param name="rspHandler">The response file handler.</param>
/// <param name="logger">The logger.</param>
/// <param name="mods">The mods to use.</param>
/// <param name="outputWriter">The output writer to use.</param>
public class SilkTouchGenerator(
ClangScraper scraper,
ResponseFileHandler rspHandler,
// ReSharper disable once SuggestBaseTypeForParameterInConstructor
ILogger<SilkTouchGenerator> logger,
IEnumerable<IMod> mods,
IOutputWriter outputWriter
)
{
private readonly IReadOnlyList<IMod> _mods;
private readonly ClangScraper _scraper;
private readonly ResponseFileHandler _rspHandler;
private readonly ILogger<SilkTouchGenerator> _logger;
private readonly IOutputWriter _outputWriter;
private AsyncLocal<SilkTouchConfiguration> _jobConfig = new();

/// <summary>
/// Creates an instance with the given underlying scraper, response file handler, and logger.
/// Gets the loaded mods for this generator instance.
/// </summary>
/// <param name="scraper">The scraper.</param>
/// <param name="rspHandler">The response file handler.</param>
/// <param name="logger">The logger.</param>
/// <param name="mods">The mods to use.</param>
/// <param name="outputWriter">The output writer to use.</param>
public SilkTouchGenerator(
ClangScraper scraper,
ResponseFileHandler rspHandler,
ILogger<SilkTouchGenerator> logger,
IEnumerable<IMod> mods,
IOutputWriter outputWriter
) =>
(_scraper, _rspHandler, _logger, _mods, _outputWriter) = (
scraper,
rspHandler,
logger,
mods.ToArray(),
outputWriter
);
public IReadOnlyList<IMod> Mods { get; } = mods.ToArray();

/// <summary>
/// The current job.
/// </summary>
public SilkTouchConfiguration? Current => _jobConfig.Value;

/// <summary>
/// Generates binding syntax trees per the given configuration.
Expand All @@ -68,20 +64,21 @@ public async Task<GeneratedSyntax> GenerateSyntaxAsync(
CancellationToken ct = default
)
{
_jobConfig.Value = job;
// Prepare the mods
var jobMods =
job.Mods?.Select(x => _mods.First(y => y.GetType().Name == x)).ToArray()
job.Mods?.Select(x => Mods.First(y => y.GetType().Name == x)).ToArray()
?? Array.Empty<IMod>();
foreach (var mod in jobMods)
{
_logger.LogDebug("Using mod {0} for {1}", mod.GetType().Name, key);
logger.LogDebug("Using mod {0} for {1}", mod.GetType().Name, key);
await mod.BeforeJobAsync(key, job);
}

// Read the response files
_logger.LogInformation("Reading response files for {0}, please wait...", key);
logger.LogInformation("Reading response files for {0}, please wait...", key);
var rsps = job.ClangSharpResponseFiles
.SelectMany(file => _rspHandler.ReadResponseFiles(file, job.ClangSharpResponseFiles))
.SelectMany(file => rspHandler.ReadResponseFiles(file, job.ClangSharpResponseFiles))
.ToList();

// Figure out what the common root is so we can aggregate the file paths without collisions
Expand All @@ -98,7 +95,7 @@ public async Task<GeneratedSyntax> GenerateSyntaxAsync(
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var mod in jobMods)
{
_logger.LogInformation(
logger.LogInformation(
"Applying {0} mod to response files for {1}...",
mod.GetType().Name,
key
Expand All @@ -116,7 +113,7 @@ await Parallel.ForEachAsync(
await Task.Run(
() =>
{
var rawBindings = _scraper.ScrapeRawBindings(rsp);
var rawBindings = scraper.ScrapeRawBindings(rsp);
foreach (var (k, v) in rawBindings.Files)
{
string relativeKey;
Expand Down Expand Up @@ -158,14 +155,14 @@ await Task.Run(
)
)
{
_logger.LogError(
logger.LogError(
"Failed to add {0} - are the response file outputs conflicting?",
relativeKey
);
}
else
{
_logger.LogTrace("ClangSharp generated {0}", relativeKey);
logger.LogTrace("ClangSharp generated {0}", relativeKey);
}
}

Expand All @@ -179,7 +176,7 @@ await Task.Run(
);

// Read the bindings as syntax trees
_logger.LogInformation("Parsing bindings for {0}...", key);
logger.LogInformation("Parsing bindings for {0}...", key);
var syntaxTrees = aggregatedBindings.ToDictionary(
kvp => kvp.Key,
kvp => CSharpSyntaxTree.ParseText(SourceText.From(kvp.Value)).GetRoot()
Expand All @@ -191,7 +188,7 @@ await Task.Run(
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var mod in jobMods)
{
_logger.LogInformation(
logger.LogInformation(
"Applying {0} mod to syntax trees for {1}...",
mod.GetType().Name,
key
Expand Down Expand Up @@ -219,11 +216,11 @@ public async Task<IReadOnlyList<Diagnostic>> OutputBindingsAsync(
var result = await GenerateSyntaxAsync(key, job, ct);
if (result.Files.Count == 0)
{
_logger.LogWarning("Not outputting as no files were generated.");
logger.LogWarning("Not outputting as no files were generated.");
return result.Diagnostics;
}

await _outputWriter.OutputAsync(key, job, result, ct);
await outputWriter.OutputAsync(key, job, result, ct);
return result.Diagnostics;
}

Expand Down
6 changes: 6 additions & 0 deletions sources/SilkTouchX/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"Mods": [
"AddStdIncludes",
"AddApiProfiles",
"AddOpaqueStructs",
"UseSilkDSL",
"PrettifyNames",
"AddVTables"
Expand Down Expand Up @@ -91,6 +92,11 @@
"MinVersion": "2.0"
}
]
},
"AddOpaqueStructs": {
"Names": [
"GLsync"
]
}
}
}
Expand Down

0 comments on commit b4e71bf

Please sign in to comment.