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

Initial more managed #40

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 20 additions & 4 deletions src/cs/production/C2CS.Common/Terminal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ private static Process CreateProcess(string command, string? workingDirectory, s
var platform = Runtime.OperatingSystem;
if (platform == RuntimeOperatingSystem.Windows)
{
processStartInfo.FileName = "wsl";
processStartInfo.Arguments = command;
processStartInfo.FileName = "cmd";
processStartInfo.Arguments = $"/c {command}";
}
else
{
Expand Down Expand Up @@ -198,13 +198,29 @@ public static string DotNetPath()
var dotnetRuntimesStrings = dotnetRuntimesString.Split(new[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var x in dotnetRuntimesStrings)
{
var parse = x.Split(" ", StringSplitOptions.RemoveEmptyEntries);
var parse = x.Split(" ", StringSplitOptions.RemoveEmptyEntries);
//x format is Microsoft.AspNetCore.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
//so we should not break on spaces possible in path.
//this stupidity is for that!
for (int i = 3; i < parse.Length; i++)
{
parse[2] += " " + parse[i];
}
if (!parse[0].Contains("Microsoft.NETCore.App"))
{
continue;
}

var version = Version.Parse(parse[1]);
Version? version;
try
{
version = Version.Parse(parse[1]);
}
catch
{
continue;
}

if (version <= dotNetRuntimeVersion)
{
continue;
Expand Down
20 changes: 19 additions & 1 deletion src/cs/production/C2CS/Foundation/UseCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,25 @@ protected TOutput Step<TInput1, TInput2, TInput3, TInput4, TInput5, TInput6, TOu
return output;
}

private void BeginStep(int index, string stepName)
protected TOutput Step<TInput1, TInput2, TInput3, TInput4, TInput5, TInput6, TInput7, TOutput>(
string stepName,
TInput1 input1,
TInput2 input2,
TInput3 input3,
TInput4 input4,
TInput5 input5,
TInput6 input6,
TInput7 input7,
Func<TInput1, TInput2, TInput3, TInput4, TInput5, TInput6, TInput7, TOutput> func)
{
_stepIndex++;
BeginStep(_stepIndex, stepName);
var output = func(input1, input2, input3, input4, input5, input6, input7);
EndStep(_stepIndex, stepName);
return output;
}

private void BeginStep(int index, string stepName)
{
Console.WriteLine($"\tStarted step ({index}/{_stepCount}) '{stepName}'");
_stepStopwatch.Start();
Expand Down
19 changes: 15 additions & 4 deletions src/cs/production/C2CS/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Lucas Girouard-Stranks (https://github.com/lithiumtoast). All rights reserved.
// Copyright (c) Lucas Girouard-Stranks (https://github.com/lithiumtoast). All rights reserved.
// Licensed under the MIT license. See LICENSE file in the Git repository root directory for full license information.

using System;
Expand Down Expand Up @@ -193,14 +193,23 @@ private static Command CommandBindgenCSharp()
};
command.AddOption(namespacesOption);

var moreManagedOption = new Option<bool>(
new[] { "--moremanaged", "-mm" },
"Try to generate more safe managed bindings.")
{
IsRequired = false
};
command.AddOption(moreManagedOption);

command.Handler = CommandHandler.Create<
string,
string,
IEnumerable<string?>?,
string,
string?,
string?,
IEnumerable<string?>?
IEnumerable<string?>?,
bool
>(BindgenCSharp);
return command;
}
Expand Down Expand Up @@ -297,7 +306,8 @@ private static void BindgenCSharp(
string ignoredNamesFile,
string? libraryName,
string? className,
IEnumerable<string?>? namespaces)
IEnumerable<string?>? namespaces,
bool moremanaged)
{
var libraryName2 = string.IsNullOrEmpty(libraryName) ? string.Empty : libraryName;
var className2 = string.IsNullOrEmpty(className) ? string.Empty : className;
Expand All @@ -309,7 +319,8 @@ private static void BindgenCSharp(
ignoredNamesFile,
libraryName2,
className2,
namespaces);
namespaces,
moremanaged);
var useCase = new UseCases.BindgenCSharp.UseCase();
var response = useCase.Execute(request);
if (response.Status == UseCaseOutputStatus.Failure)
Expand Down
32 changes: 24 additions & 8 deletions src/cs/production/C2CS/UseCases/BindgenCSharp/Data/CSharpType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,31 @@

namespace C2CS.UseCases.BindgenCSharp
{
public readonly struct CSharpType
{
public readonly string Name;
public readonly string OriginalName;
public readonly int SizeOf;
public readonly int AlignOf;
public readonly int ArraySize;
public readonly struct CSharpType
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a tab vs spaces thing going on here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably. I work on Windows and couldn't figure out how to work with analyzers and fix indents.

{
public readonly string Name;
public readonly string OriginalName;
public readonly int SizeOf;
public readonly int AlignOf;
public readonly int ArraySize;

public bool IsArray => ArraySize > 0;
public bool IsArray => ArraySize > 0;

public bool IsBlittable => Name switch
{
"byte" => true,
"sbyte" => true,
"short" => true,
"ushort" => true,
"int" => true,
"uint" => true,
"long" => true,
"ulong" => true,
"single" => true,
"double" => true,
_ => false,
};


public CSharpType(
string name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@

namespace C2CS.UseCases.BindgenCSharp
{
public class CSharpCodeGenerator
public interface ICSharpCodeGenerator
{
string EmitCode(CSharpAbstractSyntaxTree abstractSyntaxTree);
}

public class CSharpCodeGenerator : ICSharpCodeGenerator
{
private readonly string _className;
private readonly string _libraryName;
Expand Down
Loading