Skip to content

Commit 08a574f

Browse files
committed
Add -c flag for imitating compilation into object file
Actual file is JSON with all command line args.
1 parent a4e494e commit 08a574f

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

Cesium.Compiler/Arguments.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public class Arguments
2323
[Value(0)]
2424
public IList<string> InputFilePaths { get; init; } = null!;
2525

26+
[Option('c', HelpText = "Compile only into Cessium Object Format (r) (RFC-8259)")]
27+
public bool CompileOnly { get; init; } = false;
28+
2629
[Option('o', "out")]
2730
public string OutputFilePath { get; init; } = null!;
2831

Cesium.Compiler/Compilation.cs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
// SPDX-License-Identifier: MIT
44

55
using System.Collections.Immutable;
6+
using System.Runtime.InteropServices.Marshalling;
67
using System.Text;
8+
using System.Text.Json;
9+
using System.Text.Json.Serialization.Metadata;
710
using Cesium.Ast;
811
using Cesium.CodeGen;
912
using Cesium.CodeGen.Contexts;
1013
using Cesium.Core;
1114
using Cesium.Parser;
1215
using Cesium.Preprocessor;
16+
using CommandLine.Text;
17+
using JetBrains.Annotations;
1318
using Mono.Cecil;
1419
using Yoakke.Streams;
1520
using Yoakke.SynKit.C.Syntax;
@@ -21,6 +26,69 @@ namespace Cesium.Compiler;
2126

2227
internal static class Compilation
2328
{
29+
public class CompiledObjectJson
30+
{
31+
public required IEnumerable<string> inputFilePaths;
32+
public required CompilationOptions compilationOptions;
33+
}
34+
35+
internal class CompiledObjectJsonTypeInfoResolver : IJsonTypeInfoResolver
36+
{
37+
38+
public JsonTypeInfo? GetTypeInfo(Type type, JsonSerializerOptions options)
39+
{
40+
if (type.Name == typeof(CompiledObjectJson).Name)
41+
{
42+
// return new JsonTypeInfo;
43+
}
44+
throw new NotImplementedException();
45+
}
46+
}
47+
48+
public static IJsonTypeInfoResolver GetResolver(this CompiledObjectJson json)
49+
{
50+
throw new NotImplementedException();
51+
}
52+
53+
public static bool IsObjectFileName(string filename)
54+
{
55+
return filename.EndsWith(".json.obj") || filename.EndsWith(".obj");
56+
}
57+
public static async Task<int> DumpToObjectJson(
58+
IEnumerable<string> inputFilePaths,
59+
string outputFilePath,
60+
CompilationOptions compilationOptions
61+
)
62+
{
63+
CompiledObjectJson compiledObjectJson = new CompiledObjectJson()
64+
{
65+
inputFilePaths = inputFilePaths,
66+
compilationOptions = compilationOptions
67+
};
68+
69+
if (!outputFilePath.EndsWith(".obj"))
70+
{
71+
outputFilePath += ".json.obj";
72+
}
73+
74+
StreamWriter outObjectWriter = new StreamWriter(outputFilePath);
75+
await outObjectWriter.WriteAsync(JsonSerializer.Serialize(compiledObjectJson));
76+
return 0;
77+
}
78+
79+
public static async Task<CompiledObjectJson> UndumpObjectJson(string inputObjectJsonFilePath)
80+
{
81+
StreamReader inObjectJsonReader = new StreamReader(inputObjectJsonFilePath);
82+
83+
var inObjectJsonStr = await inObjectJsonReader.ReadToEndAsync();
84+
var result = JsonSerializer.Deserialize<CompiledObjectJson>(inObjectJsonStr);
85+
if (result == null)
86+
{
87+
throw new Exception($"Invalid json from file {inputObjectJsonFilePath}");
88+
}
89+
return result;
90+
}
91+
2492
public static async Task<int> Compile(
2593
IEnumerable<string> inputFilePaths,
2694
string outputFilePath,
@@ -54,7 +122,21 @@ public static async Task<int> Compile(
54122

55123
foreach (var inputFilePath in inputFilePaths)
56124
{
57-
Console.WriteLine($"Processing input file \"{inputFilePath}\".");
125+
bool isObjectFile = IsObjectFileName(inputFilePath);
126+
Console.WriteLine($"Processing input {(isObjectFile ? "Cesium JSON object ": "")} file \"{inputFilePath}\".");
127+
if (isObjectFile)
128+
{
129+
var result = UndumpObjectJson(inputFilePath);
130+
if (result != null)
131+
{
132+
var newFiles = inputFilePaths.Intersect<string>(result.Result.inputFilePaths);
133+
foreach (var newFile in newFiles)
134+
{
135+
Console.WriteLine($"Processing input file (from Cesium JSON object file \"{inputFilePath}\") \"{newFile}\".");
136+
await GenerateCode(assemblyContext, newFile);
137+
}
138+
}
139+
}
58140
await GenerateCode(assemblyContext, inputFilePath);
59141
}
60142

Cesium.Compiler/Main.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static async Task<int> Main(string[] args)
2929
#pragma warning disable IL3000 // Automatic discovery of corelib is fallback option, if tooling do not pass that parameter
3030
var corelibAssembly = args.CoreLib ?? typeof(Math).Assembly.Location; // System.Runtime.dll
3131
#pragma warning restore IL3000
32-
var moduleKind = (args.ProducePreprocessedFile || args.DumpAst) ? ModuleKind.Console : args.ModuleKind ?? Path.GetExtension(args.OutputFilePath).ToLowerInvariant() switch
32+
var moduleKind = (args.ProducePreprocessedFile || args.DumpAst || args.CompileOnly) ? ModuleKind.Console : args.ModuleKind ?? Path.GetExtension(args.OutputFilePath).ToLowerInvariant() switch
3333
{
3434
".exe" => ModuleKind.Console,
3535
".dll" => ModuleKind.Dll,
@@ -48,6 +48,11 @@ public static async Task<int> Main(string[] args)
4848
args.IncludeDirectories.ToList(),
4949
args.ProducePreprocessedFile,
5050
args.DumpAst);
51+
52+
if (args.CompileOnly)
53+
{
54+
return await Compilation.DumpToObjectJson(args.InputFilePaths, args.OutputFilePath, compilationOptions);
55+
}
5156
return await Compilation.Compile(args.InputFilePaths, args.OutputFilePath, compilationOptions);
5257
});
5358
}

0 commit comments

Comments
 (0)