Skip to content

Commit

Permalink
feat: add AllowUnsafeBlocks supports for source code based metadata…
Browse files Browse the repository at this point in the history
… generation (#10175)

feat: add AllowUnsafeBlocks supports for source based metadata generation
  • Loading branch information
filzrev authored Sep 14, 2024
1 parent 3d477be commit 3ef4172
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
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);
}
}

0 comments on commit 3ef4172

Please sign in to comment.