mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-04 01:49:05 +00:00
COMPILES TO EXE
This commit is contained in:
47
LX-Build/CommandProcess.cs
Normal file
47
LX-Build/CommandProcess.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LX_Build
|
||||
{
|
||||
internal class CommandProcess
|
||||
{
|
||||
private readonly string m_ErrorMessage;
|
||||
private readonly int m_ExitCode;
|
||||
|
||||
public int ExitCode() => m_ExitCode;
|
||||
|
||||
public string Error() => m_ErrorMessage;
|
||||
|
||||
public CommandProcess(string command)
|
||||
{
|
||||
// Creates a process to run the command //
|
||||
ProcessStartInfo info = new()
|
||||
{
|
||||
FileName = "cmd.exe",
|
||||
Arguments = $"/c {command}",
|
||||
RedirectStandardError = true,
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
};
|
||||
|
||||
// Starts the process //
|
||||
using Process process = Process.Start(info);
|
||||
|
||||
// Reads the streams of the output //
|
||||
string output = process.StandardOutput.ReadToEnd();
|
||||
string error = process.StandardError.ReadToEnd();
|
||||
|
||||
// Waits for the process to exit //
|
||||
process.WaitForExit();
|
||||
|
||||
// Assigns output of the error for external access //
|
||||
m_ExitCode = process.ExitCode;
|
||||
m_ErrorMessage = error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,8 @@ namespace LX_Build
|
||||
private static partial bool SetDllDirectory(string lpPathName);
|
||||
|
||||
// Imports the Frontend of the compiler //
|
||||
[LibraryImport("Generator.dll", StringMarshalling = StringMarshalling.Custom, StringMarshallingCustomType = typeof(System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller))]
|
||||
[LibraryImport ("Generator.dll", StringMarshalling = StringMarshalling.Custom,
|
||||
StringMarshallingCustomType = typeof(System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller))]
|
||||
[UnmanagedCallConv(CallConvs = new Type[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
|
||||
public static partial int GenIR(string arg1, string arg2, string? arg3);
|
||||
|
||||
|
||||
@@ -1,16 +1,57 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace LX_Build
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
static void CompileToObj(string inPath, string outPath)
|
||||
{
|
||||
// Initalises the CPP interface //
|
||||
// The command to compile the IR to .obj //
|
||||
string command = $"llc -filetype=obj -o \"{outPath}\" \"{inPath}\"";
|
||||
|
||||
// Runs the command //
|
||||
CommandProcess process = new(command);
|
||||
|
||||
if (process.ExitCode() == 0)
|
||||
{
|
||||
Console.WriteLine(inPath + " -> " + outPath);
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine(process.Error());
|
||||
}
|
||||
|
||||
static void LinkToExe(string objectFile)
|
||||
{
|
||||
// The command to link the object file to an .exe //
|
||||
string command = $"lld-link /OUT:example/Main.exe {objectFile} /ENTRY:main";
|
||||
|
||||
// Runs the command //
|
||||
CommandProcess process = new(command);
|
||||
|
||||
if (process.ExitCode() == 0)
|
||||
{
|
||||
Console.WriteLine(objectFile + " -> " + "Main.exe");
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine(process.Error());
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
// Initalises the CPP interface, MUST ALWAYS BE CALLED FIRST //
|
||||
LX_API.Init();
|
||||
|
||||
// Generates LLVM IR with the example files //
|
||||
_ = LX_API.GenIR("example/main.lx", "example/main.ll", "example/log");
|
||||
|
||||
// Compilers the LLVM IR to an object file using the command line //
|
||||
CompileToObj("example/main.ll", "example/main.obj");
|
||||
|
||||
// Links the object file to an .exe //
|
||||
LinkToExe("example/main.obj");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user