Skip to content

Default includes #1002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 44 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
0d25e88
Enable OSX CI
HurricanKai Aug 5, 2022
886c84d
Add Function to resolve default includes
HurricanKai Aug 5, 2022
176eab8
Use Default Includes
HurricanKai Aug 5, 2022
e77251d
Add debug statements
HurricanKai Aug 5, 2022
8c6bd1d
Output to stderr
HurricanKai Aug 5, 2022
bdba9c0
Use exceptions for debugging :)
HurricanKai Aug 5, 2022
c8bf0fa
More cursed things
HurricanKai Aug 5, 2022
38bcb21
Just run the OSX check on UNIX
HurricanKai Aug 5, 2022
4e636ae
Improved Process handling
HurricanKai Aug 5, 2022
5139b9b
Only check output if exist code is zero
HurricanKai Aug 5, 2022
5832f80
Handle OSX failure
HurricanKai Aug 5, 2022
4a7c91b
Debug OSX Include
HurricanKai Aug 5, 2022
f9527c1
More Debug Exceptions
HurricanKai Aug 5, 2022
be3d92a
Use -isysroot instead on MacOS
HurricanKai Aug 5, 2022
a7fdf37
Specify using libc++
HurricanKai Aug 5, 2022
b631d78
Prepare for rebase
HurricanKai Aug 5, 2022
4274500
Merge origin/develop/3.0 into default-includes
HurricanKai Aug 5, 2022
63fff71
Add Configuration for ClangScraper, and allow specification of Xcode SDK
HurricanKai Aug 5, 2022
ee82b65
use Msvc & ucrt includes
HurricanKai Aug 5, 2022
70ca26f
Use Environment Variables with SILK_DOTNET_ prefix
HurricanKai Aug 5, 2022
9cec781
Handle missing XCode SDK version
HurricanKai Aug 5, 2022
a0ea824
Add Extra Logging
HurricanKai Aug 5, 2022
b6420f3
Correctly specify environment variable name
HurricanKai Aug 5, 2022
422eb60
Ignore 'Unsupported cursor: 'macro definition'. Generated bindings ma…
HurricanKai Aug 5, 2022
c2ad59d
Bind to correct configuration section
HurricanKai Aug 5, 2022
aca41ea
Will this work?
HurricanKai Aug 5, 2022
a2db0cc
/usr/include & /usr/local/include on non-windows
HurricanKai Aug 5, 2022
2d0e8af
Apparently -isysroot is not a thing
HurricanKai Aug 5, 2022
50b4ea1
Use relative paths
HurricanKai Aug 5, 2022
b51acf3
only parse one line from xcrun
HurricanKai Aug 5, 2022
f2de0bd
Warn about missing standard includes
HurricanKai Aug 5, 2022
270898c
Dump all available files
HurricanKai Aug 5, 2022
e202db7
Well.... 8h down the drain cause I'm stupid
HurricanKai Aug 5, 2022
a129a93
Remove file dumping
HurricanKai Aug 5, 2022
2d43729
Disable Integration Tests until proper typemap is in
HurricanKai Aug 5, 2022
e586abe
Add Trace output for xcrun
HurricanKai Aug 5, 2022
926db7d
Adjust osx11 RID
HurricanKai Aug 5, 2022
94a67d3
Trace Available SDKs if xcrun fails
HurricanKai Aug 5, 2022
f3b6e5a
Truncate xcrun output
HurricanKai Aug 5, 2022
026aecb
Use slightly less specific xcode SDKs
HurricanKai Aug 5, 2022
ac9718b
Also output XCode SDKs
HurricanKai Aug 5, 2022
e3fe46d
Add XCode Version to GH Actions setup
HurricanKai Aug 5, 2022
e52cc2b
Fix condition syntax
HurricanKai Aug 5, 2022
76b1bce
Disable unecessary clang warnings
HurricanKai Aug 5, 2022
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
21 changes: 15 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ jobs:
# os: ubuntu-18.04

# macos 10.15 is not tested as the runner image is deprecated.

# OSX is currently broken as standard headers (stdint.h) can't be found
# - rid: osx.12-x64
# os: macos-12
# - rid: osx.11.0-x64
# os: macos-11
# view https://github.com/actions/virtual-environments/blob/main/images/macos/ files for info on xcode sdk versions
- rid: osx.12-x64
os: macos-12
xcode_sdk: macosx12.3 # macos-12 image is currently (5th August 2022) on MacOS 12.5 but the latest sdk is macosx12.3
xcode_version: Xcode_13.4.1

- rid: osx.11.0-x64
os: macos-11
xcode_sdk: macosx11.3
xcode_version: Xcode_12.5.1
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand All @@ -55,5 +59,10 @@ jobs:
# Build can't be done in a separate step as building for a specific runtime & configuration is unsupported for some reason...
# https://github.com/dotnet/sdk/issues/14281
# doing the same (building for a specific configuration and runtime is supported via dotnet test)
- name: Switch XCode Version
if: matrix.xcode_version != 0
run: sudo xcode-select -switch /Applications/${{ matrix.xcode_version }}.app
- name: Test
run: dotnet test -c Release --no-restore --runtime ${{ matrix.rid }}
env:
SILK_DOTNET_Scraper__XcodeSdk: ${{ matrix.xcode_sdk }}
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="$(MsExtensionNugetVersion)" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="$(MsExtensionNugetVersion)" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="$(MsExtensionNugetVersion)" />
<PackageVersion Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="$(MsExtensionNugetVersion)" />
</ItemGroup>
</Project>
110 changes: 108 additions & 2 deletions src/generators/Silk.NET.SilkTouch.Scraper/ClangScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using ClangSharp;
using ClangSharp.Interop;
using Silk.NET.SilkTouch.Scraper.Subagent;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Silk.NET.SilkTouch.Symbols;

namespace Silk.NET.SilkTouch.Scraper;
Expand All @@ -19,7 +22,8 @@ namespace Silk.NET.SilkTouch.Scraper;
/// </summary>
public sealed class ClangScraper
{
private readonly ILoggerFactory _loggerFactory;
private readonly ILoggerFactory _loggerFactory;
private readonly IOptions<ClangScraperConfiguration> _options;
/// <summary>
/// Placeholder used in place of library paths
/// </summary>
Expand All @@ -34,9 +38,11 @@ public sealed class ClangScraper
/// Creates a ClangScraper given it's dependencies
/// </summary>
/// <param name="loggerFactory">A logger factory to create loggers from</param>
public ClangScraper(ILoggerFactory loggerFactory)
/// <param name="options">an <see cref="IOptions{TOptions}"/> instance used to retrieve configuration. See <see cref="ClangScraperConfiguration"/></param>
public ClangScraper(ILoggerFactory loggerFactory, IOptions<ClangScraperConfiguration> options)
{
_loggerFactory = loggerFactory;
_options = options;
}

/// <summary>
Expand All @@ -57,6 +63,101 @@ public IEnumerable<Symbol> ScrapeXML(XmlDocument document)
return visitor.Visit(bindings);
}

private string GetXCodeSdkPath()
{
var logger = _loggerFactory.CreateLogger("XCode SDK Resolver");
var sdk = _options.Value.XcodeSdk;
logger.LogTrace("Resolving XCode SDK using SDK {sdk}", sdk);
var process = new Process();
process.StartInfo = new ProcessStartInfo
("xcrun", "--show-sdk-path" + (sdk is null ? "" : $" --sdk {sdk}"))
{
RedirectStandardOutput = true
};
process.Start();
process.WaitForExit();
var output = process.StandardOutput.ReadToEnd();
logger.LogTrace("Got Response from xcrun: {response} {length}", output.ReplaceLineEndings("\\n"), output.Length);
var lines = output.Split('\n');
var path = lines.Length > 0 ? lines[0] : null;
if (string.IsNullOrWhiteSpace(path))
{
try
{
logger.LogTrace
(
"Available CommandLineTools SDKs appear to be: {versions}",
string.Join(", ", Directory.EnumerateDirectories("/Library/Developer/CommandLineTools/SDKs/"))
);
logger.LogTrace
(
"Available XCode SDK versions appear to be: {versions}",
string.Join
(
", ",
Directory.EnumerateDirectories
("/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/")
)
);
} catch { /* */ }
throw new InvalidOperationException("xcrun didn't return correct lines to stdout.");
}
logger.LogInformation("Resolved XCode SDK to {path}", path);
return path;
}

/// <summary>
/// Resolves platform specific standard includes
/// </summary>
/// <returns>An enumerable of directories to include</returns>
/// <remarks>
/// On Windows, this attempts to use the Visual Studio Resolver
/// On OSX, this attempts to run `xcrun --show-sdk-path` to get the SDK path, to then include /sdk path>/usr/include
/// On other UNIX platforms, this simply defaults to /usr/include
/// </remarks>
public IEnumerable<string> ResolveStandardIncludes()
{
var logger = _loggerFactory.CreateLogger("Standard includes");
var list = new List<string>();
if (OperatingSystem.IsWindows())
{
if (VisualStudioResolver.TryGetVisualStudioInfo(out var info))
{
logger.LogInformation("Successfully resolved VS to {path}", info.InstallationBaseFolder);
list.AddRange(info.MsvcToolsIncludes);
list.AddRange(info.UcrtIncludes);
}
else
{
logger.LogWarning("Failed to resolve VS, but OS is Windows!");
}
}
else
{
list.Add("/usr/include");
list.Add("/usr/local/include");

if (OperatingSystem.IsMacOS())
{
var sdkPath = GetXCodeSdkPath();
logger.LogTrace("Using SDK {sdk} as base", sdkPath);
var p1 = Path.Combine(sdkPath, "usr/include");
logger.LogTrace("Suggesting additional path {path}", p1);
list.Add(p1);
var p2 = Path.Combine(sdkPath, "usr/local/include");
logger.LogTrace("Suggesting additional path {path}", p2);
list.Add(p2);
}
}

foreach (var entry in list.Where(entry => !Directory.Exists(entry)))
{
logger.LogWarning("{entry} is a standard include, but does not exist!", entry);
}

return list;
}

/// <summary>
/// Calls into Clang to generate XML used to scrape symbols.
/// </summary>
Expand All @@ -70,6 +171,7 @@ public IEnumerable<Symbol> ScrapeXML(XmlDocument document)
public XmlDocument? GenerateXML(string headerFile, string[] includedNames, string[] excludedNames, string[] includeDirectories, string[] definedMacros)
{
var logger = _loggerFactory.CreateLogger("ClangScraper.ScrapeXML");

var opts = PInvokeGeneratorConfigurationOptions.None;
opts |= PInvokeGeneratorConfigurationOptions.NoDefaultRemappings;

Expand All @@ -94,6 +196,7 @@ Stream OutputStreamFactory(string fileName)
var commandLineArgs = new List<string>();
commandLineArgs.Add("--language=c++");
commandLineArgs.Add("--std=c++17");
commandLineArgs.Add("--stdlib=libc++");
commandLineArgs.Add("-Wno-pragma-once-outside-header");

for (int i = 0; i < definedMacros.Length; i++)
Expand Down Expand Up @@ -181,6 +284,9 @@ out var handle

foreach (var diagnostic in pinvokeGenerator.Diagnostics)
{
if (diagnostic.Message.StartsWith("Unsupported cursor:"))
continue;

logger.Log
(
diagnostic.Level switch
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Silk.NET.SilkTouch.Scraper;

/// <summary>
/// Represents configuration values for <see cref="ClangScraper"/>
/// </summary>
public sealed class ClangScraperConfiguration
{
/// <summary>
/// The Xcode SDK version. Only used when resolving SDK path on OSX. Passed to xcrun as --sdk if present.
/// </summary>
public string? XcodeSdk { get; set; } = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PackageReference Include="libClangSharp" />
<PackageReference Include="Verify.Xunit" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ typedef struct {
return Verifier.Verify(result);
}

[Fact,
[Fact(Skip = "TODO: Reenable after proper type support"),
Trait("Category", "Integration"),
Trait("Source Language", "C++"),
Trait("Target Language", "C#"),
Expand All @@ -44,7 +44,7 @@ struct vec4 : vec3 { float w; };");
return Verifier.Verify(result);
}

[Fact,
[Fact(Skip = "TODO: Reenable after proper type support"),
Trait("Category", "Integration"),
Trait("Source Language", "C++"),
Trait("Target Language", "C#"),
Expand All @@ -66,7 +66,7 @@ struct { long k, l; } w;
return Verifier.Verify(result);
}

[Fact,
[Fact(Skip = "TODO: Reenable after proper type support"),
Trait("Category", "Integration"),
Trait("Source Language", "C++"),
Trait("Target Language", "C#"),
Expand All @@ -82,7 +82,7 @@ struct y { struct x *q; /* ... */ };");
return Verifier.Verify(result);
}

[Fact,
[Fact(Skip = "TODO: Reenable after proper type support"),
Trait("Category", "Integration"),
Trait("Source Language", "C++"),
Trait("Target Language", "C#"),
Expand All @@ -104,7 +104,7 @@ union S
return Verifier.Verify(result);
}

[Fact,
[Fact(Skip = "TODO: Reenable after proper type support"),
Trait("Category", "Integration"),
Trait("Source Language", "C++"),
Trait("Target Language", "C#"),
Expand All @@ -128,7 +128,7 @@ union S
}


[Fact,
[Fact(Skip = "TODO: Reenable after proper type support"),
Trait("Category", "Integration"),
Trait("Source Language", "C++"),
Trait("Target Language", "C#"),
Expand Down Expand Up @@ -156,7 +156,7 @@ struct S
return Verifier.Verify(result);
}

[Fact,
[Fact(Skip = "TODO: Reenable after proper type support"),
Trait("Category", "Integration"),
Trait("Source Language", "C++"),
Trait("Target Language", "C#"),
Expand Down
21 changes: 16 additions & 5 deletions tests/Silk.NET.SilkTouch.IntegrationTests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Data;
using System.IO;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Silk.NET.SilkTouch.Emitter;
Expand All @@ -18,23 +19,33 @@ public static class TestHelper
public static string GetCSharpOutputFromCpp(string cpp)
{
var tempFile = Path.GetTempFileName();
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables(source => source.Prefix = "SILK_DOTNET_")
.Build();
var serviceProvider = new ServiceCollection()
.AddLogging(builder => builder.AddConsole())
.AddLogging(builder =>
{
builder.AddConsole();
builder.SetMinimumLevel(LogLevel.Trace);
}
)
.Configure<ClangScraperConfiguration>(configuration.GetSection("Scraper"))
.BuildServiceProvider();

File.WriteAllText(tempFile, "/* THIS IS A GENERATED FILE, PIPED TO CLANG FOR TESTING BY SILK.NET */" + cpp);

var scraper = new ClangScraper(serviceProvider.GetRequiredService<ILoggerFactory>());
var scraper = ActivatorUtilities.CreateInstance<ClangScraper>(serviceProvider);
var defaultIncludes = scraper.ResolveStandardIncludes().ToArray();
var xml = scraper.GenerateXML
(tempFile, Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>());

Assert.NotNull(xml);
(tempFile, Array.Empty<string>(), Array.Empty<string>(), defaultIncludes, Array.Empty<string>());

try
{
File.Delete(tempFile);
} catch { /* It's a Temporary File. We dont' care. */}

Assert.NotNull(xml);

var symbols = scraper.ScrapeXML(xml!);
var emitter = new CSharpEmitter();
var outputs = symbols.Select(x => emitter.Transform(x));
Expand Down
3 changes: 2 additions & 1 deletion tests/Silk.NET.SilkTouch.Scraper.Tests/BasicXMLTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ typedef struct {
} Test;");

var scraper = Helpers.CreateScraper();
var defaultIncludes = scraper.ResolveStandardIncludes().ToArray();
var xml = scraper.GenerateXML
(tempFile, Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>());
(tempFile, Array.Empty<string>(), Array.Empty<string>(), defaultIncludes, Array.Empty<string>());

/*
Next, Assert the XML looks something like this:
Expand Down
16 changes: 8 additions & 8 deletions tests/Silk.NET.SilkTouch.Scraper.Tests/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Xml;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Silk.NET.SilkTouch.Symbols;
Expand All @@ -12,22 +13,21 @@ namespace Silk.NET.SilkTouch.Scraper.Tests;

public static class Helpers
{
private static IServiceProvider CreateServiceProvider()
public static ClangScraper CreateScraper()
{
return new ServiceCollection()
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables(source => source.Prefix = "SILK_DOTNET_")
.Build();
var serviceProvider = new ServiceCollection()
.AddLogging(builder =>
{
builder.AddConsole();
builder.SetMinimumLevel(LogLevel.Trace);
}
)
.Configure<ClangScraperConfiguration>(configuration.GetSection("Scraper"))
.BuildServiceProvider();
}

public static ClangScraper CreateScraper()
{
var serviceProvider = CreateServiceProvider();

return new ClangScraper(serviceProvider.GetRequiredService<ILoggerFactory>());
return ActivatorUtilities.CreateInstance<ClangScraper>(serviceProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="libclang" />
<PackageReference Include="libClangSharp" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
</ItemGroup>

<ItemGroup>
Expand Down