MLLIF
a MLIR-based Language to Language Interoperability Flyover
Loading...
Searching...
No Matches
Program.cs
Go to the documentation of this file.
1using System.Text;
2using Microsoft.CodeAnalysis;
3using Microsoft.CodeAnalysis.MSBuild;
5using Humanizer;
6using Project = Microsoft.CodeAnalysis.Project;
7
9
10internal static class Program
11{
12 private static async Task<int> Main(string[] args)
13 {
14 using var msbuild = MSBuildWorkspace.Create();
15
16 if (args.Length < 2)
17 {
18 Console.Error.WriteLine(
19 "error: insufficient arguments\n" +
20 "usage: MLLIFCSharpFrontBuild <csproj-file> <output-dir>");
21 return -1;
22 }
23
24 var projectPath = args[0];
25 var outputDir = args[1];
26
27 Project project;
28 try
29 {
30 project = await msbuild.OpenProjectAsync(projectPath);
31 }
32 catch (FileNotFoundException)
33 {
34 Console.Error.WriteLine($"error: no such project '{projectPath}'");
35 return -1;
36 }
37
38 // Get compilation
39 Compilation? compilation;
40 {
41 var start = DateTime.Now;
42
43 compilation = await project.GetCompilationAsync();
44
45 var elapsed = DateTime.Now - start;
46 Console.Error.WriteLine($"info: {elapsed.Humanize()} taken to build");
47 }
48
49 if (compilation is null)
50 {
51 Console.Error.WriteLine("error: failed to build project");
52 return -1;
53 }
54
55 // Prepare resources for bindings
56 Directory.CreateDirectory(outputDir);
57 await using var hdr = new StreamWriter(File.Create(Path.Combine(outputDir, "library.h")), Encoding.UTF8);
58 await using var src = new StreamWriter(File.Create(Path.Combine(outputDir, "library.cxx")), Encoding.UTF8);
59 await using var hdrW = new CodeWriter(hdr, " ");
60 await using var srcW = new CodeWriter(src, " ");
61
62 // Write bindings
63 var stub = await Embedded.Resource("BridgeStub.cxx");
64 {
65 var start = DateTime.Now;
66
67 var ns = compilation.Assembly.GlobalNamespace;
68 var ctx = new CodeContext(project);
69
70 foreach (var diag in new NamespaceWriter(ns).WriteTo(hdrW, ctx))
71 Console.Error.WriteLine(diag);
72
73 srcW.WriteLine(stub);
74 foreach (var t in ctx)
75 foreach (var diag in new TypeDefWriter(t).WriteTo(srcW, ctx))
76 Console.Error.WriteLine(diag);
77
78 var elapsed = DateTime.Now - start;
79 Console.Error.WriteLine($"info: {elapsed.Humanize()} taken to generate");
80 }
81
82
83
84 // Infer runtime version
85 string? version = null;
86 foreach (var reference in compilation.References)
87 {
88 if (reference.Display is null)
89 continue;
90
91 var terms = reference.Display.Replace("\\", "/").Split('/', StringSplitOptions.RemoveEmptyEntries);
92
93 int i;
94 for (i = 0; i < terms.Length; i++)
95 if (terms[i] == "microsoft.netcore.app.ref")
96 break;
97 i++;
98 if (i >= terms.Length)
99 continue;
100
101 version = terms[i];
102 break;
103 }
104 if (version is null)
105 {
106 version = Environment.Version.ToString();
107 Console.Error.WriteLine(
108 $"warning: cannot infer .net runtime version; defaults to running .net runtime version ({version})");
109 }
110
111 // Serve CMakeLists.txt
112 var cmake = await Embedded.Resource("CMakeLists.txt");
113 cmake = cmake.Replace("{@}", version);
114 await File.WriteAllTextAsync(Path.Combine(outputDir, "CMakeLists.txt"), cmake);
115
116 return 0;
117 }
118}
Microsoft.CodeAnalysis.Project Project
Definition Program.cs:6