Skip to content

Commit

Permalink
Added using the Clang resource directory provided by ClangSharp.Patho…
Browse files Browse the repository at this point in the history
…gen.Runtime.

Fixes #201
  • Loading branch information
PathogenDavid committed Aug 26, 2021
1 parent cc90cb1 commit d10aa1b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Biohazrd/Biohazrd.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="ClangSharp" Version="10.0.0-beta" />
<PackageReference Include="ClangSharp.Pathogen" Version="[0.0.0-ci41]" />
<PackageReference Include="ClangSharp.Pathogen" Version="[0.0.0-ci43]" />
<!--
-->

Expand Down
18 changes: 17 additions & 1 deletion Biohazrd/TranslatedLibraryBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ClangSharp;
using ClangSharp;
using ClangSharp.Interop;
using ClangSharp.Pathogen;
using System;
Expand All @@ -18,6 +18,22 @@ public sealed partial class TranslatedLibraryBuilder

public TranslationOptions Options { get; set; } = new();

public TranslatedLibraryBuilder()
{
// On non-Windows platforms we need to provide the Clang resource directory.
// This specifies the version copied to our output directory by ClangSharp.Pathogen.Runtime.
// (One Windows the same files come from the UCRT instead.)
// See https://github.com/InfectedLibraries/Biohazrd/issues/201 for more details
string resourceDirectoryPath = Path.Combine(AppContext.BaseDirectory, "clang-resources");
if (!OperatingSystem.IsWindows())
{
if (!Directory.Exists(resourceDirectoryPath) || !File.Exists(Path.Combine(resourceDirectoryPath, "include", "stddef.h")))
{ throw new DirectoryNotFoundException("Clang resources directory not found."); }

AddCommandLineArguments("-resource-dir", resourceDirectoryPath);
}
}

public void AddFile(SourceFile sourceFile)
{
Debug.Assert(Path.IsPathFullyQualified(sourceFile.FilePath), "File paths should always be fully qualified.");
Expand Down
5 changes: 0 additions & 5 deletions Tests/Biohazrd.Tests.Common/BiohazrdTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ protected TranslatedLibraryBuilder CreateLibraryBuilder(string cppCode, string?
Contents = cppCode
});

//TODO: Figure out a better way to deal with this
// https://github.com/InfectedLibraries/Biohazrd/issues/201
if (OperatingSystem.IsLinux())
{ builder.AddCommandLineArguments("-isystem/usr/lib/llvm-10/lib/clang/10.0.0/include/"); }

if (targetTriple is not null)
{ builder.AddCommandLineArgument($"--target={targetTriple}"); }

Expand Down
16 changes: 16 additions & 0 deletions Tests/Biohazrd.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,21 @@ public void TranslateEvenWithParsingErrorsTrue()
Assert.Contains(library.ParsingDiagnostics, d => d.IsError && d.IsFromClang);
Assert.NotEmpty(library.Declarations);
}

[Fact]
[RelatedIssue("https://github.com/InfectedLibraries/Biohazrd/issues/201")]
public void ClangFindsSystemIncludes()
{
// On Windows stddef.h will be found from the UCRT.
// On Linux it needs to come from the Clang resource directory.
TranslatedLibrary library = CreateLibrary
(
@"
#include <stddef.h>
size_t Test();
"
);
library.FindDeclaration<TranslatedFunction>("Test");
}
}
}

0 comments on commit d10aa1b

Please sign in to comment.