Stop using fatal errors

This commit is contained in:
Rune
2026-03-05 17:02:04 +01:00
parent 3590478b20
commit fa90624c84
4 changed files with 82 additions and 33 deletions

View File

@@ -16,24 +16,45 @@ static class Program
Console.WriteLine("""
Simple and fast build tool using clang and ninja.
Usage: $(Var CxxBuilderExe) <patterns...> -- src=<> target=<> builddir=<> output=<> [cflags=<>]
Example: $(Var CxxBuilderExe) **.c vk_*.cpp -- "src=$(ProjectDir)/MyLib/src" target=$(TargetTriple) "builddir=$(BuildDir)" output=MyLib cflags=-Isome/dir
Usage: $(Var CxxBuilderExe) [<options>] <patterns...> -- src=<> target=<> config=<> builddir=<> output=<> [cflags=<>]
Example: $(Var CxxBuilderExe) **.c vk_*.cpp -- "src=$(ProjectDir)/MyLib/src" target=$(TargetTriple) config=$(Configuration) "builddir=$(BuildDir)" output=MyLib cflags=-Isome/dir
src - The source dir, all patterns are rooted here
target - the llvm target triple passed to clang
builddir - contains build artifacts and ninja files
output - the name of the output object archive $builddir/$output.lib on windows and $builddir/$output.a elsewhere
cflags - additional flags passed to clang
--template - TODO
--cmake - builds $src/CMakeLists.txt to build, patterns and output are ignored
$src - The source dir, all patterns are rooted here
$target - the llvm target triple passed to clang
$config - the build configuration
$builddir - contains build artifacts and ninja files
$output - the name of the output object archive $builddir/$output.lib on windows and $builddir/$output.a elsewhere
$cflags - additional flags passed to clang
""");
return 1;
}
StringView src = null, target = null, builddir = null, output = null, cflags = null;
mixin Assert(bool condition, StringView str)
{
if (!condition)
{
Console.WriteLine(str);
return 1;
}
}
enum { Default, CMake, Template } mode = .Default;
StringView src = null, target = null, config = null, builddir = null, output = null, cflags = null;
var iter = args.GetEnumerator();
skipPatterns: do
{
for (let arg in iter)
{
if (arg == "--") break skipPatterns;
if (arg == "--cmake")
{
Assert!(mode == .Default, "Conflicting options");
mode = .CMake;
}
}
PrintUsage!();
}
for (let arg in iter)
@@ -50,20 +71,24 @@ static class Program
value.Trim();
switch (key)
{
case "src": Runtime.Assert(src.IsNull, "Duplicate var 'src'"); src = value;
case "target": Runtime.Assert(target.IsNull, "Duplicate var 'target'"); target = value;
case "builddir": Runtime.Assert(builddir.IsNull, "Duplicate var 'builddir'"); builddir = value;
case "output": Runtime.Assert(output.IsNull, "Duplicate var 'output'"); output = value;
case "cflags": Runtime.Assert(cflags.IsNull, "Duplicate var 'cflags'"); cflags = value;
case "src": Assert!(src.IsNull, "Duplicate var 'src'"); src = value;
case "target": Assert!(target.IsNull, "Duplicate var 'target'"); target = value;
case "config": Assert!(config.IsNull, "Duplicate var 'config'"); config = value;
case "builddir": Assert!(builddir.IsNull, "Duplicate var 'builddir'"); builddir = value;
case "output": Assert!(output.IsNull, "Duplicate var 'output'"); output = value;
case "cflags": Assert!(cflags.IsNull, "Duplicate var 'cflags'"); cflags = value;
default: PrintUsage!();
}
}
Runtime.Assert(!src.IsNull, "Missing var 'src'");
Runtime.Assert(!target.IsNull, "Missing var 'target'");
Runtime.Assert(!builddir.IsNull, "Missing var 'builddir'");
Runtime.Assert(!builddir.IsNull, "Missing var 'output'");
Assert!(!src.IsNull, "Missing var 'src'");
Assert!(!target.IsNull, "Missing var 'target'");
Assert!(!config.IsNull, "Missing var 'config'");
Assert!(!builddir.IsNull, "Missing var 'builddir'");
Assert!(!output.IsNull || mode == .CMake, "Missing var 'output'");
switch (mode)
{
case .Default:
StreamWriter writer = scope .()..Create(scope $"{builddir}/build.ninja");
{
String buffer = scope .(1024);
@@ -79,7 +104,7 @@ static class Program
WriteVarPath("builddir", builddir);
buffer.AppendF($"""
target = {target}
cflags = {cflags}
cflags = {(config == "Release") ? "-O2" : "-O2 -g"} {cflags}
""");
writer.Write(buffer);
}
@@ -121,9 +146,25 @@ static class Program
ar.Append(" $builddir/", match, ".o");
}
let abssrc = Path.GetAbsolutePath(src, Directory.GetCurrentDirectory(..scope .(128)), ..scope .(128));
FileMatcher.HandleMatches(patterns, abssrc, scope => HandleMatch);
if (FileMatcher.HandleMatches(patterns, abssrc, scope => HandleMatch) case .Err(let err))
{
Console.WriteLine($"Syntax Error in Pattern: {err}");
return 1;
}
writer.WriteLine(ar);
case .CMake:
#if !DEBUG
if (File.Exists(scope $"{builddir}/build.ninja"))
break;
#endif
String cmd = scope .(512);
cmd.Append("cmake -S \"", src, "\" -B \"", builddir, "\" -GNinja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ ");
cmd.Append("-DCMAKE_C_FLAGS=\"-target ", target, " ", cflags, "\" -DCMAKE_CXX_FLAGS=\"-target ", target, " ", cflags, "\" -DCMAKE_BUILD_TYPE=");
if (config == "Release") cmd.Append("Release");
else cmd.Append("RelWithDebInfo");
Assert!(system(cmd) == 0, "Build file generation failed");
case .Template: //TODO
}
let ret = system(scope $"ninja -C {builddir}");