forked from loongly/PureScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSCGenerater.cs
More file actions
186 lines (159 loc) · 6 KB
/
Copy pathCSCGenerater.cs
File metadata and controls
186 lines (159 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Generater
{
public class CSCGenerater
{
static string[] addtionFlag = new string[]
{
"-t:library",
"-unsafe",
};
static string[] addtionRef = new string[]
{
"mscorlib.dll",
"UnityEngine.CoreModule.dll",
//"PureScript.dll",
};
private static string CSCPath = "csc";
private static string[] AdapterSrc = new string[]
{
"glue/Binder.impl.cs",
"Tools/CustomBinder.cs",
"Tools/ObjectStore.cs",
};
private static string[] AdapterWrapperSrc = new string[]
{
"Tools/CustomBinder.cs",
"Tools/ObjectStore.wrapper.cs",
"Tools/ScriptEngine.cs",
};
public static CSCGenerater AdapterCompiler;
public static CSCGenerater AdapterWrapperCompiler;
private static string OutDir;
private static string DllRefDir;
private static string AdapterDir;
private static HashSet<string> IgnoreRefSet = new HashSet<string>();
private static Dictionary<string, CSCGenerater> WrapperDic = new Dictionary<string, CSCGenerater>();
public static void Init(string cscDir,string adapterDir, string outDir, HashSet<string> ignoreRefSet)
{
CSCPath = Path.Combine(cscDir, Utils.IsWin32() ? "csc.exe":"csc") ;
OutDir = outDir;
DllRefDir = outDir;
AdapterDir = adapterDir;
IgnoreRefSet = ignoreRefSet;
AdapterCompiler = new CSCGenerater(Path.Combine(outDir, "Adapter.gen.dll"));
foreach(var file in AdapterSrc)
AdapterCompiler.AddSource(Path.Combine(adapterDir,file));
AdapterCompiler.refSet.Add("PureScript.dll");
SetWrapper("Adapter.wrapper.dll");
foreach (var file in AdapterWrapperSrc)
AdapterWrapperCompiler.AddSource(Path.Combine(adapterDir, file));
}
public static void SetWrapper(string dllName)
{
if (!WrapperDic.TryGetValue(dllName,out AdapterWrapperCompiler))
{
AdapterWrapperCompiler = new CSCGenerater(Path.Combine(OutDir, dllName));
//foreach (var file in AdapterWrapperSrc)
// AdapterWrapperCompiler.AddSource(Path.Combine(AdapterDir, file));
AdapterWrapperCompiler.AddDefine("WRAPPER_SIDE");
if (!Utils.IsWin32())
AdapterWrapperCompiler.AddDefine("IOS");
WrapperDic[dllName] = AdapterWrapperCompiler;
if (dllName != "Adapter.wrapper.dll")
AdapterWrapperCompiler.AddReference("Adapter.wrapper.dll");
}
}
public static void End()
{
AdapterCompiler.Gen();
//AdapterWrapperCompiler.Gen();
var list = GetSortedList();
foreach (var wrapper in list)
{
wrapper.Gen();
}
}
private static List<CSCGenerater> GetSortedList()
{
foreach (var wrapper in WrapperDic.Values)
{
CountRef(wrapper);
}
var list = new List<CSCGenerater>(WrapperDic.Values);
list.Sort((a, b) => { return b.RefCount - a.RefCount; });
return list;
}
private static void CountRef(CSCGenerater gener)
{
foreach (var depend in gener.refSet)
{
if (WrapperDic.TryGetValue(depend, out var dependGener))
{
dependGener.RefCount++;
CountRef(dependGener);
}
}
}
public string outName { get; private set; }
public HashSet<string> refSet = new HashSet<string>();
public int RefCount = 0;
HashSet<string> srcSet = new HashSet<string>();
HashSet<string> defineSet = new HashSet<string>();
public CSCGenerater(string targetName)
{
outName = Path.GetFullPath(targetName);
}
public void AddReference(string target)
{
//if(!IgnoreRefSet.Contains(target))
refSet.Add(target);
}
public void RemoveReference(string target)
{
//if(!IgnoreRefSet.Contains(target))
refSet.Remove(target);
}
public void AddSource(string file)
{
var path = Path.GetFullPath(file);
srcSet.Add(path);
}
public void AddDefine(string define)
{
defineSet.Add(define);
}
public void Gen()
{
var fName = $"{Path.GetFileName(outName)}.txt";
using(var config = File.CreateText(fName))
{
config.WriteLine($"-out:{outName}");
foreach (var flag in addtionFlag)
config.WriteLine(flag);
foreach (var refFile in addtionRef)
config.WriteLine($"-r:{Path.Combine(DllRefDir, refFile)}");
foreach (var refFile in refSet)
config.WriteLine($"-r:{Path.Combine(DllRefDir, refFile)}");
foreach (var define in defineSet)
config.WriteLine($"-define:{define}");
var netstandFile = Path.Combine(OutDir, "netstandard.dll");
if(File.Exists(netstandFile))
config.WriteLine($"-r:{Path.Combine(DllRefDir, netstandFile)}");
foreach (var src in srcSet)
config.WriteLine(src);
}
int res = Utils.RunCMD(CSCPath, new string[] { $"@{fName}" });
if (res != 0)
throw new Exception($"Run CSC error,with: {CSCPath} @{fName}");
}
/*public void AddTypeForwardedTo(string typeName)
{
}*/
}
}