Skip to content

Commit 5098593

Browse files
committed
MachineInfo and you.
Eval main.lua in users config scripts dir. Improved MachineInfo parsing by only returning known machines, host is added by default. UnixCCompiler to just generically use CommonUnixCCompiler using cc and c++ symlinks. CProject now has optimisation flags, right now It's pretty basic with no checks.
1 parent 2f33779 commit 5098593

20 files changed

+384
-97
lines changed

Borz.Cli/CmdUtils.cs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,4 @@ public static void LoadWorkspaceSettings(Options opt)
1515
}
1616
}
1717

18-
public static MachineInfo ParseMachine(string input)
19-
{
20-
var machineInfo = MachineInfo.Parse(input, MachineInfo.GetCurrentMachineInfo());
21-
22-
if (machineInfo == null)
23-
throw new Exception("Cant parse given machine info.");
24-
25-
var machinesPath = Path.Combine(IPlatform.Instance.GetUserConfigPath(), "borz", "machines.ako");
26-
if (!File.Exists(machinesPath))
27-
throw new Exception("Couldn't fine machines.ako");
28-
29-
var machines = Deserializer.FromString(File.ReadAllText(machinesPath));
30-
if (machines == null)
31-
throw new Exception("Failed to parse machines.ako");
32-
33-
if (!machines.TryGet(machineInfo.OS, out var osTable))
34-
{
35-
throw new Exception($"OS \"{machineInfo.OS}\" not in machines.ako");
36-
}
37-
38-
if (!osTable.TryGet(machineInfo.Arch, out var archTable))
39-
{
40-
throw new Exception($"Arch \"{machineInfo.Arch}\" not found in {machineInfo.OS}");
41-
}
42-
43-
if (archTable.TryGet("compilers", out var compilers))
44-
{
45-
foreach (var akoVar in compilers.TableValue)
46-
{
47-
machineInfo.Compilers.Add(akoVar.Key, akoVar.Value);
48-
}
49-
}
50-
51-
if (archTable.TryGet("bins", out var bins))
52-
{
53-
foreach (var var in bins.TableValue)
54-
{
55-
machineInfo.Binaries.Add(var.Key, var.Value);
56-
}
57-
}
58-
59-
return machineInfo;
60-
}
6118
}

Borz.Cli/Commands/CompileCommand.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,34 @@ public override int Execute([NotNull] CommandContext context, [NotNull] Settings
3434

3535
if (settings.Target != null)
3636
{
37-
var parsedTarget = CmdUtils.ParseMachine(settings.Target);
37+
var parsedTarget = MachineInfo.Parse(settings.Target);
38+
if (parsedTarget == null)
39+
{
40+
MugiLog.Error($"Unknown target: {settings.Target} known targets are:");
41+
foreach (var machine in MachineInfo.GetKnownMachines())
42+
{
43+
MugiLog.Error(machine.ToString());
44+
}
45+
46+
return 1;
47+
}
48+
3849
opt.Target = parsedTarget;
3950
}
4051

4152
CmdUtils.LoadWorkspaceSettings(opt);
4253

54+
if (settings.Config != null)
55+
{
56+
if (!opt.ValidConfigs.Contains(settings.Config))
57+
{
58+
MugiLog.Error($"{settings.Config} isn't valid.");
59+
return 1;
60+
}
61+
62+
opt.Config = settings.Config;
63+
}
64+
4365
var ws = new Workspace(".");
4466

4567
var script = ScriptRunner.CreateScript();

Borz/BinType.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using MoonSharp.Interpreter;
2-
31
namespace Borz;
42

53
public enum BinType : uint

Borz/Borz.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public static void Init()
3535

3636
((ScriptLoaderBase)Script.DefaultOptions.ScriptLoader).ModulePaths = new string[] { "./?", "./?.lua" };
3737
ScriptRunner.RegisterTypes();
38+
39+
//Call scripts in user dir
40+
if (Directory.Exists(Path.Combine(ConfigFolderPath, "scripts")))
41+
{
42+
var script = ScriptRunner.CreateScript();
43+
ScriptRunner.Eval(script,Path.Combine(ConfigFolderPath, "scripts", "main.lua"));
44+
}
45+
3846
}
3947

4048
public static void Shutdown()

Borz/CompilerFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Borz.Compilers;
2-
using Borz.Languages.C;
32

43
namespace Borz;
54

@@ -13,7 +12,9 @@ static CompilerFactory()
1312
{
1413
_knownCompilers = new Dictionary<string, CompilerCreationDelete>()
1514
{
16-
{ "gcc", opt => new GccCompiler(opt) }
15+
{ "unix", opt => new UnixCCompiler(opt) },
16+
{ "gcc", opt => new GccCompiler(opt) },
17+
{ "psxgcc", opt => new PsxCompiler(opt) },
1718
};
1819
}
1920

Borz/Compilers/CommonUnixCCompiler.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public override ProcUtil.RunOutput CompileObject(Project inPrj, string sourceFil
3131

3232
AddSymbols(project, ref cmdArgs);
3333
AddStdVersion(project, ref cmdArgs);
34+
AddOptimisation(project, ref cmdArgs);
3435

3536
if (GenerateSourceDependencies)
3637
cmdArgs.Add("-MMD");
@@ -85,6 +86,7 @@ public override ProcUtil.RunOutput LinkProject(Project inPrj, string[] objects)
8586
cmdArgs.Add(outputPath);
8687

8788
AddStdVersion(project, ref cmdArgs);
89+
AddOptimisation(project, ref cmdArgs);
8890

8991
cmdArgs.AddRange(objects);
9092

@@ -165,6 +167,7 @@ public override ProcUtil.RunOutput CompilePch(CProject project)
165167
AddPic(project, ref cmdArgs);
166168
AddSymbols(project, ref cmdArgs);
167169
AddStdVersion(project, ref cmdArgs);
170+
AddOptimisation(project, ref cmdArgs);
168171
cmdArgs.Add("-o");
169172
cmdArgs.Add(pchObj);
170173
cmdArgs.Add("-c");
@@ -245,4 +248,12 @@ public void AddLibraries(CProject project, ref List<string> args)
245248
{
246249
foreach (var library in project.GetLibraries(Opt)) args.Add($"-l{library}");
247250
}
251+
252+
public void AddOptimisation(CProject project, ref List<string> args)
253+
{
254+
if (project.Optimisation != String.Empty)
255+
{
256+
args.Add($"-O{project.Optimisation}");
257+
}
258+
}
248259
}

Borz/Compilers/GccCompiler.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using Borz.Languages.C;
2-
31
namespace Borz.Compilers;
42

53
public class GccCompiler : CommonUnixCCompiler
@@ -15,12 +13,23 @@ public GccCompiler(Options opt) : base(opt)
1513

1614
public override (bool supported, string reason) IsSupported()
1715
{
18-
var result = ProcUtil.RunCmd(CCompilerElf, "--version");
16+
var result = ProcUtil.RunCmd(CCompilerElf, "-dumpfullversion");
1917
if (result.Exitcode != 0)
2018
{
21-
return (false, $"Failed to run gcc --version: {result.Ouput}");
19+
return (false, $"Failed to run gcc -dumpfullversion: {result.Ouput}");
2220
}
2321

22+
int major, minor, patch;
23+
var version = result.Ouput.Split('.');
24+
major = int.Parse(version[0]);
25+
minor = int.Parse(version[1]);
26+
patch = int.Parse(version[2]);
27+
28+
if (major < 12)
29+
{
30+
return (false, $"Need major version 13+, current is: {major}.{minor}.{patch}");
31+
}
32+
2433
return (true, string.Empty);
2534
}
2635
}

Borz/Compilers/PsxCompiler.cs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
using Borz.Helpers;
2+
using Borz.Languages.C;
3+
using Microsoft.VisualBasic;
4+
5+
namespace Borz.Compilers;
6+
7+
public class PsxCompiler : CommonUnixCCompiler
8+
{
9+
private string _psxDir = "/home/drogonmar/.config/borz/third_party/";
10+
11+
private string[] _extraArgs = new[]
12+
{
13+
"-ffunction-sections",
14+
"-fdata-sections",
15+
"-mno-gpopt",
16+
"-fomit-frame-pointer",
17+
"-fno-builtin",
18+
"-fno-strict-aliasing",
19+
"-Wno-attributes",
20+
};
21+
22+
private string[] _commonArgs = new[]
23+
{
24+
"-march=mips1",
25+
"-mabi=32",
26+
"-EL",
27+
"-fno-pic",
28+
"-mno-shared",
29+
"-mno-abicalls",
30+
"-mfp32",
31+
"-mno-llsc",
32+
"-fno-stack-protector",
33+
"-nostdlib",
34+
"-ffreestanding"
35+
};
36+
37+
public PsxCompiler(Options opt) : base(opt)
38+
{
39+
CCompilerElf = Opt.GetTarget().GetBinaryPath("gcc", "gcc");
40+
CppCompilerElf = Opt.GetTarget().GetBinaryPath("g++", "g++");
41+
}
42+
43+
public override string Name => "PsxCompiler";
44+
public override (bool supported, string reason) IsSupported()
45+
{
46+
if (Opt.GetTarget().OS != "psx")
47+
{
48+
return (false, "Target's OS isn't psx");
49+
}
50+
51+
if (Opt.GetTarget().Arch != "mipsel")
52+
{
53+
return (false, "Target's arch isn't mipsel");
54+
}
55+
56+
return (true, String.Empty);
57+
}
58+
59+
public void CheckProject(CProject project)
60+
{
61+
if (project.UsePIC == true)
62+
throw new Exception("PIC isnt supported for PSX");
63+
}
64+
65+
public override ProcUtil.RunOutput CompileObject(Project inPrj, string sourceFile, string outputFile)
66+
{
67+
if(inPrj is not CProject project)
68+
throw new Exception("Project is not a CppProject or CProject");
69+
70+
CheckProject(project);
71+
72+
List<string> cmdArgs = new();
73+
cmdArgs.Add("-I" + Path.Combine(_psxDir, "psyq-iwyu", "include"));
74+
cmdArgs.AddRange(_extraArgs);
75+
cmdArgs.AddRange(_commonArgs);
76+
cmdArgs.Add("-I" + Path.Combine(_psxDir, "nugget"));
77+
AddSymbols(project, ref cmdArgs);
78+
AddOptimisation(project, ref cmdArgs);
79+
80+
cmdArgs.Add("-o");
81+
cmdArgs.Add(outputFile);
82+
83+
cmdArgs.Add("-c");
84+
cmdArgs.Add(sourceFile);
85+
86+
var compiler = sourceFile.EndsWith(".cpp") ? CppCompilerElf : CCompilerElf;
87+
88+
CompileDatabase?.Add(new CompileCommands.CompileCommand
89+
{
90+
Directory = project.Directory,
91+
Arguments = cmdArgs.ToArray(),
92+
Command = compiler + " " + Strings.Join(cmdArgs.ToArray(), " "),
93+
File = sourceFile,
94+
Output = outputFile
95+
});
96+
97+
return ProcUtil.RunCmdOptLog(Opt.GetTarget(), compiler,
98+
Strings.Join(cmdArgs.ToArray())!, project.Directory, Opt.JustPrint);
99+
}
100+
101+
public override ProcUtil.RunOutput LinkProject(Project inPrj, string[] objects)
102+
{
103+
if (inPrj is not CProject project)
104+
throw new Exception("Project is not a CppProject or CProject");
105+
106+
CheckProject(project);
107+
108+
var outputPath = project.GetOutputFilePath(Opt);
109+
110+
if(project.Type == BinType.StaticLib)
111+
return ProcUtil.RunCmdOptLog(Opt.GetTarget(), "ar", $"-rcs \"{outputPath}\" " + string.Join(" ", objects.ToArray()),
112+
project.Directory, Opt.JustPrint);
113+
114+
List<string> cmdArgs = new();
115+
116+
cmdArgs.Add("-o");
117+
cmdArgs.Add(outputPath);
118+
119+
cmdArgs.AddRange(objects);
120+
121+
AddOptimisation(project, ref cmdArgs);
122+
123+
if (GenerateSourceDependencies)
124+
cmdArgs.Add("-MMD");
125+
126+
cmdArgs.Add("-L" + Path.Combine(_psxDir, "psyq", "lib"));
127+
AddLibraryPaths(project, ref cmdArgs);
128+
129+
cmdArgs.Add("-Wl,--start-group");
130+
AddLibraries(project, ref cmdArgs);
131+
cmdArgs.Add("-Wl,--end-group");
132+
cmdArgs.Add($"-Wl,-Map={project.Name}.map");
133+
134+
cmdArgs.Add("-nostdlib");
135+
136+
cmdArgs.AddRange(new []
137+
{
138+
"-T" + Path.Combine(_psxDir, "nugget", "nooverlay.ld"),
139+
"-T" + Path.Combine(_psxDir, "nugget", "ps-exe.ld")
140+
});
141+
142+
cmdArgs.Add("-static");
143+
cmdArgs.Add("-Wl,--gc-sections");
144+
cmdArgs.AddRange(_commonArgs);
145+
cmdArgs.Add("-Wl,--oformat=elf32-littlemips");
146+
AddSymbols(project, ref cmdArgs);
147+
AddOptimisation(project, ref cmdArgs);
148+
149+
return ProcUtil.RunCmdOptLog(Opt.GetTarget(), project.Language == Lang.C ? CCompilerElf : CppCompilerElf,
150+
Strings.Join(cmdArgs.ToArray())!, project.Directory, Opt.JustPrint);
151+
}
152+
}

Borz/Compilers/UnixCCompiler.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Borz.Compilers;
2+
3+
public class UnixCCompiler : CommonUnixCCompiler
4+
{
5+
public override string Name => "unix";
6+
7+
public UnixCCompiler(Options opt) : base(opt)
8+
{
9+
CCompilerElf = Opt.GetTarget().GetBinaryPath("cc", "cc");
10+
CppCompilerElf = Opt.GetTarget().GetBinaryPath("c++", "c++");
11+
}
12+
13+
public override (bool supported, string reason) IsSupported()
14+
{
15+
//cant test this.
16+
return (true, string.Empty);
17+
}
18+
}

Borz/IPlatform.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Reflection;
2-
using AkoSharp;
32

43
namespace Borz;
54

0 commit comments

Comments
 (0)