-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80b12fc
commit d520a26
Showing
24 changed files
with
120 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 24 additions & 82 deletions
106
src/cs/production/c2ffi.Tool/Commands/Extract/ExtractFfiTool.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,67 @@ | ||
// Copyright (c) Bottlenose Labs Inc. (https://github.com/bottlenoselabs). All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the Git repository root directory for full license information. | ||
|
||
using System.Collections.Immutable; | ||
using System.IO.Abstractions; | ||
using c2ffi.Data; | ||
using bottlenoselabs.Common.Tools; | ||
using c2ffi.Data.Serialization; | ||
using c2ffi.Tool.Commands.Extract.Domain.Explore; | ||
using c2ffi.Tool.Commands.Extract.Domain.Parse; | ||
using c2ffi.Tool.Commands.Extract.Input; | ||
using c2ffi.Tool.Commands.Extract.Input.Sanitized; | ||
using c2ffi.Tool.Commands.Extract.Input.Unsanitized; | ||
using c2ffi.Tool.Commands.Extract.Output; | ||
using Microsoft.Extensions.Logging; | ||
using ClangInstaller = c2ffi.Tool.Commands.Extract.Domain.Parse.ClangInstaller; | ||
using ExtractOptions = c2ffi.Tool.Commands.Extract.Input.ExtractOptions; | ||
|
||
namespace c2ffi.Tool.Commands.Extract; | ||
|
||
public sealed partial class ExtractFfiTool | ||
public sealed class ExtractFfiTool : Tool<UnsanitizedExtractInput, ExtractInput, ExtractOutput> | ||
{ | ||
private readonly ILogger<ExtractFfiTool> _logger; | ||
|
||
private readonly IFileSystem _fileSystem; | ||
private readonly ExtractInputSanitizer _inputSanitizer; | ||
private readonly ClangInstaller _clangInstaller; | ||
private readonly Explorer _explorer; | ||
|
||
private string? _clangFilePath; | ||
|
||
public ExtractFfiTool( | ||
ILogger<ExtractFfiTool> logger, | ||
IFileSystem fileSystem, | ||
ExtractInputSanitizer inputSanitizer, | ||
ClangInstaller clangInstaller, | ||
Explorer explorer) | ||
: base(logger, inputSanitizer, fileSystem) | ||
{ | ||
_logger = logger; | ||
_fileSystem = fileSystem; | ||
_inputSanitizer = inputSanitizer; | ||
_clangInstaller = clangInstaller; | ||
_explorer = explorer; | ||
} | ||
|
||
public void Run(string configurationFilePath, string? clangFilePath = null) | ||
{ | ||
if (!TryInstallClang(clangFilePath)) | ||
{ | ||
return; | ||
} | ||
|
||
var options = GetOptions(configurationFilePath); | ||
var targetPlatforms = ExtractFfis(options); | ||
if (targetPlatforms.IsDefaultOrEmpty) | ||
{ | ||
LogFailure(); | ||
} | ||
else | ||
{ | ||
LogSuccess(targetPlatforms); | ||
} | ||
_clangFilePath = clangFilePath; | ||
base.Run(configurationFilePath); | ||
} | ||
|
||
private bool TryInstallClang(string? clangFilePath = null) | ||
protected override void Execute(ExtractInput input, ExtractOutput output) | ||
{ | ||
return _clangInstaller.TryInstall(clangFilePath); | ||
} | ||
BeginStep("Install libclang"); | ||
var libClangIsInstalled = _clangInstaller.TryInstall(_clangFilePath); | ||
EndStep(); | ||
|
||
private ExtractOptions GetOptions(string configurationFilePath) | ||
{ | ||
return _inputSanitizer.SanitizeFromFile(configurationFilePath); | ||
} | ||
|
||
private ImmutableArray<TargetPlatform> ExtractFfis(ExtractOptions options) | ||
{ | ||
var builder = ImmutableArray.CreateBuilder<TargetPlatform>(); | ||
|
||
foreach (var targetPlatformOptions in options.TargetPlatformsOptions) | ||
if (!libClangIsInstalled) | ||
{ | ||
var targetPlatform = ExtractFfi(options, targetPlatformOptions); | ||
if (targetPlatform != null) | ||
{ | ||
builder.Add(targetPlatform.Value); | ||
} | ||
return; | ||
} | ||
|
||
return builder.ToImmutable(); | ||
} | ||
|
||
private TargetPlatform? ExtractFfi( | ||
ExtractOptions options, | ||
ExtractTargetPlatformOptions targetPlatformOptions) | ||
{ | ||
try | ||
foreach (var targetPlatformInput in input.TargetPlatformInputs) | ||
{ | ||
BeginStep($"Extracting FFI {targetPlatformInput.TargetPlatform}"); | ||
|
||
var ffi = _explorer.ExtractFfi( | ||
options.InputFilePath, | ||
targetPlatformOptions); | ||
Json.WriteFfiTargetPlatform(_fileSystem, targetPlatformOptions.OutputFilePath, ffi); | ||
} | ||
#pragma warning disable CA1031 | ||
catch (Exception e) | ||
#pragma warning restore CA1031 | ||
{ | ||
LogWriteFfiTargetPlatformFailure(e, targetPlatformOptions.TargetPlatform, targetPlatformOptions.OutputFilePath); | ||
return null; | ||
} | ||
input.InputFilePath, | ||
targetPlatformInput); | ||
Json.WriteFfiTargetPlatform(_fileSystem, targetPlatformInput.OutputFilePath, ffi); | ||
|
||
LogWriteFfiTargetPlatformSuccess(targetPlatformOptions.TargetPlatform, targetPlatformOptions.OutputFilePath); | ||
return targetPlatformOptions.TargetPlatform; | ||
EndStep(); | ||
} | ||
} | ||
|
||
[LoggerMessage(0, LogLevel.Information, "Success. Extracted FFI for the target platform '{TargetPlatform}': {FilePath}")] | ||
private partial void LogWriteFfiTargetPlatformSuccess( | ||
TargetPlatform targetPlatform, | ||
string filePath); | ||
|
||
[LoggerMessage(1, LogLevel.Error, "Failed to extract FFI for the target platform '{TargetPlatform}': {FilePath}")] | ||
private partial void LogWriteFfiTargetPlatformFailure( | ||
Exception exception, | ||
TargetPlatform targetPlatform, | ||
string filePath); | ||
|
||
[LoggerMessage(2, LogLevel.Information, "Success. Extracted FFIs for the target platforms '{TargetPlatforms}'.")] | ||
private partial void LogSuccess( | ||
ImmutableArray<TargetPlatform> targetPlatforms); | ||
|
||
[LoggerMessage(3, LogLevel.Error, "Failure.")] | ||
private partial void LogFailure(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
src/cs/production/c2ffi.Tool/Commands/Extract/Input/ExtractOptions.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.