...
This commit is contained in:
71
src/compiler/NubLang.CLI/GCC.cs
Normal file
71
src/compiler/NubLang.CLI/GCC.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace NubLang.CLI;
|
||||
|
||||
public static class GCC
|
||||
{
|
||||
public static async Task<bool> Assemble(string asm, string objPath)
|
||||
{
|
||||
var dir = Path.GetDirectoryName(objPath);
|
||||
if (dir != null)
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
|
||||
using var process = new Process();
|
||||
process.StartInfo = new ProcessStartInfo("gcc", ["-xassembler", "-c", "-o", objPath, "-"])
|
||||
{
|
||||
UseShellExecute = false,
|
||||
RedirectStandardInput = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
|
||||
process.Start();
|
||||
|
||||
await process.StandardInput.WriteAsync(asm);
|
||||
process.StandardInput.Close();
|
||||
|
||||
await process.WaitForExitAsync();
|
||||
|
||||
var errors = await process.StandardError.ReadToEndAsync();
|
||||
if (!string.IsNullOrWhiteSpace(errors))
|
||||
{
|
||||
await Console.Error.WriteLineAsync("gcc error when assembling:\n" + errors);
|
||||
}
|
||||
|
||||
return process.ExitCode == 0;
|
||||
}
|
||||
|
||||
public static async Task<bool> Link(string executablePath, params IEnumerable<string> objectFiles)
|
||||
{
|
||||
var dir = Path.GetDirectoryName(executablePath);
|
||||
if (dir != null)
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
|
||||
using var process = new Process();
|
||||
process.StartInfo = new ProcessStartInfo("gcc", ["-nostartfiles", "-o", executablePath, ..objectFiles])
|
||||
{
|
||||
UseShellExecute = false,
|
||||
RedirectStandardInput = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
|
||||
process.Start();
|
||||
|
||||
await process.WaitForExitAsync();
|
||||
|
||||
var errors = await process.StandardError.ReadToEndAsync();
|
||||
if (!string.IsNullOrWhiteSpace(errors))
|
||||
{
|
||||
await Console.Error.WriteLineAsync("gcc error when linking:\n" + errors);
|
||||
}
|
||||
|
||||
return process.ExitCode == 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user