Skip to content
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

feat: add AllowUnsafeBlocks supports for source code based metadata generation #10175

Merged
Merged
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
7 changes: 7 additions & 0 deletions docs/reference/docfx-json-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,13 @@ Specifies an optional set of MSBuild properties used when interpreting project f
> [!Note]
> Make sure to specify `"TargetFramework": <one of the frameworks>` in your docfx.json when the project is targeting for multiple platforms.

> [!Note]
> When generating metadata from source code files.
> Supported properties are limited to the following.
> - `DefineConstants`
> - `AllowUnsafeBlocks`
> If other properties are specified. These properties are ignored silently.

### `noRestore`

Do not run `dotnet restore` before building the projects.
Expand Down
30 changes: 26 additions & 4 deletions src/Docfx.Dotnet/CompilationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static Compilation CreateCompilationFromCSharpFiles(IEnumerable<string> f

return CS.CSharpCompilation.Create(
assemblyName: null,
options: new CS.CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, xmlReferenceResolver: XmlFileResolver.Default),
options: GetCSharpCompilationOptions(msbuildProperties),
syntaxTrees: syntaxTrees,
references: GetDefaultMetadataReferences("C#").Concat(references));
}
Expand All @@ -74,7 +74,7 @@ public static Compilation CreateCompilationFromCSharpCode(string code, IDictiona

return CS.CSharpCompilation.Create(
name,
options: new CS.CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, xmlReferenceResolver: XmlFileResolver.Default),
options: GetCSharpCompilationOptions(msbuildProperties),
syntaxTrees: [syntaxTree],
references: GetDefaultMetadataReferences("C#").Concat(references ?? []));
}
Expand All @@ -86,7 +86,7 @@ public static Compilation CreateCompilationFromVBFiles(IEnumerable<string> files

return VB.VisualBasicCompilation.Create(
assemblyName: null,
options: new VB.VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary, globalImports: GetVBGlobalImports(), xmlReferenceResolver: XmlFileResolver.Default),
options: GetVisualBasicCompilationOptions(msbuildProperties),
syntaxTrees: syntaxTrees,
references: GetDefaultMetadataReferences("VB").Concat(references));
}
Expand All @@ -98,7 +98,7 @@ public static Compilation CreateCompilationFromVBCode(string code, IDictionary<s

return VB.VisualBasicCompilation.Create(
name,
options: new VB.VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary, globalImports: GetVBGlobalImports(), xmlReferenceResolver: XmlFileResolver.Default),
options: GetVisualBasicCompilationOptions(msbuildProperties),
syntaxTrees: [syntaxTree],
references: GetDefaultMetadataReferences("VB").Concat(references ?? []));
}
Expand Down Expand Up @@ -231,4 +231,26 @@ private static VB.VisualBasicParseOptions GetVisualBasicParseOptions(IDictionary

return new VB.VisualBasicParseOptions(preprocessorSymbols: preprocessorSymbols);
}

private static CS.CSharpCompilationOptions GetCSharpCompilationOptions(IDictionary<string, string> msbuildProperties)
{
var options = new CS.CSharpCompilationOptions(
OutputKind.DynamicallyLinkedLibrary,
xmlReferenceResolver: XmlFileResolver.Default);

if (msbuildProperties.TryGetValue("AllowUnsafeBlocks", out var valueText) && bool.TryParse(valueText, out var allowUnsafe))
{
options = options.WithAllowUnsafe(allowUnsafe);
}

return options;
}

private static VB.VisualBasicCompilationOptions GetVisualBasicCompilationOptions(IDictionary<string, string> msbuildProperties)
{
return new VB.VisualBasicCompilationOptions(
OutputKind.DynamicallyLinkedLibrary,
globalImports: GetVBGlobalImports(),
xmlReferenceResolver: XmlFileResolver.Default);
}
}