...
This commit is contained in:
@@ -1,20 +1,49 @@
|
||||
using CLI;
|
||||
using System.Reflection;
|
||||
using CLI;
|
||||
using Generation.QBE;
|
||||
using Syntax;
|
||||
using Syntax.Diagnostics;
|
||||
using Syntax.Parsing;
|
||||
using Syntax.Tokenization;
|
||||
using Syntax.Typing;
|
||||
using Binder = Syntax.Typing.Binder;
|
||||
|
||||
const string OUT_DIR = "bin-int";
|
||||
const string INT_DIR = "bin-int";
|
||||
var BUILTIN_DIR = Path.Join(INT_DIR, "builtin");
|
||||
var OBJECT_DIR = Path.Join(INT_DIR, "obj");
|
||||
var OUT_DIR = Path.Join(INT_DIR, "out");
|
||||
|
||||
if (Directory.Exists(INT_DIR))
|
||||
{
|
||||
Directory.Delete(INT_DIR, true);
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(BUILTIN_DIR);
|
||||
Directory.CreateDirectory(OBJECT_DIR);
|
||||
Directory.CreateDirectory(OUT_DIR);
|
||||
|
||||
var options = new Options();
|
||||
|
||||
var files = new List<string>();
|
||||
|
||||
foreach (var arg in args)
|
||||
for (var i = 0; i < args.Length; i++)
|
||||
{
|
||||
files.Add(arg);
|
||||
var arg = args[i];
|
||||
if (arg.StartsWith("-r") || arg.StartsWith("--runtime"))
|
||||
{
|
||||
i++;
|
||||
if (i >= args.Length - 1)
|
||||
{
|
||||
Console.Error.WriteLine($"{arg} must be followed by a value");
|
||||
return 1;
|
||||
}
|
||||
|
||||
options.CustomRuntime = args[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
files.Add(arg);
|
||||
}
|
||||
}
|
||||
|
||||
var diagnostics = new List<Diagnostic>();
|
||||
@@ -80,7 +109,7 @@ foreach (var file in files)
|
||||
return 1;
|
||||
}
|
||||
|
||||
var fileName = Path.GetTempFileName();
|
||||
var fileName = Path.Join(OBJECT_DIR, $"{HexString.CreateUnique(8)}_{Path.GetFileNameWithoutExtension(file)}.o");
|
||||
var asmSuccess = await GCC.Assemble(asm, fileName);
|
||||
if (!asmSuccess)
|
||||
{
|
||||
@@ -90,10 +119,57 @@ foreach (var file in files)
|
||||
objectFiles.Add(fileName);
|
||||
}
|
||||
|
||||
if (options.CustomRuntime == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
objectFiles.Add(await CreateBuiltinRuntime());
|
||||
}
|
||||
catch (RuntimeCreationException e)
|
||||
{
|
||||
Console.Error.WriteLine(e.Message);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!File.Exists(options.CustomRuntime))
|
||||
{
|
||||
Console.Error.WriteLine($"file '{options.CustomRuntime}' does not exist'");
|
||||
return 1;
|
||||
}
|
||||
|
||||
objectFiles.Add(options.CustomRuntime);
|
||||
}
|
||||
|
||||
var archiveResult = await Archive.Invoke(Path.Join(OUT_DIR, "out.a"), objectFiles);
|
||||
if (!archiveResult)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
|
||||
async Task<string> CreateBuiltinRuntime()
|
||||
{
|
||||
const string RUNTIME_NAME = "libruntime_x64.a";
|
||||
|
||||
var resources = Assembly.GetExecutingAssembly().GetManifestResourceNames();
|
||||
var runtime = resources.First(r => r.EndsWith(RUNTIME_NAME));
|
||||
|
||||
await using var reader = Assembly.GetExecutingAssembly().GetManifestResourceStream(runtime);
|
||||
|
||||
if (reader == null)
|
||||
{
|
||||
throw new RuntimeCreationException($"Cannot open stream to '{RUNTIME_NAME}'");
|
||||
}
|
||||
|
||||
var runtimePath = Path.Combine(BUILTIN_DIR, RUNTIME_NAME);
|
||||
await using var writer = new FileStream(runtimePath, FileMode.Create);
|
||||
|
||||
reader.CopyTo(writer);
|
||||
|
||||
return runtimePath;
|
||||
}
|
||||
|
||||
class RuntimeCreationException(string message) : Exception(message);
|
||||
Reference in New Issue
Block a user