-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathcsproj.rs
273 lines (233 loc) · 9.18 KB
/
csproj.rs
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use anyhow::Result;
use std::{fs, path::PathBuf};
use heck::ToUpperCamelCase;
pub struct CSProject;
pub struct CSProjectLLVMBuilder {
name: String,
dir: PathBuf,
aot: bool,
clean_targets: bool,
world_name: String,
}
pub struct CSProjectMonoBuilder {
name: String,
dir: PathBuf,
aot: bool,
clean_targets: bool,
world_name: String,
}
impl CSProject {
pub fn new(dir: PathBuf, name: &str, world_name: &str) -> CSProjectLLVMBuilder {
CSProjectLLVMBuilder {
name: name.to_string(),
dir,
aot: false,
clean_targets: false,
world_name: world_name.to_string(),
}
}
pub fn new_mono(dir: PathBuf, name: &str, world_name: &str) -> CSProjectMonoBuilder {
CSProjectMonoBuilder {
name: name.to_string(),
dir,
aot: false,
clean_targets: false,
world_name: world_name.to_string(),
}
}
}
impl CSProjectLLVMBuilder {
pub fn generate(&self) -> Result<()> {
let name = &self.name;
let world = &self.world_name.replace("-", "_");
let camel = format!("{}World", world.to_upper_camel_case());
fs::write(
self.dir.join("rd.xml"),
format!(
r#"<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Application>
<Assembly Name="{name}">
</Assembly>
</Application>
</Directives>"#
),
)?;
let mut csproj = format!(
"<Project Sdk=\"Microsoft.NET.Sdk\">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>preview</LangVersion>
<RootNamespace>{name}</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup>
<PublishTrimmed>true</PublishTrimmed>
<AssemblyName>{name}</AssemblyName>
</PropertyGroup>
<ItemGroup>
<NativeLibrary Include=\"{camel}_component_type.o\" />
<NativeLibrary Include=\"$(MSBuildProjectDirectory)/{camel}_cabi_realloc.o\" />
</ItemGroup>
<ItemGroup>
<RdXmlFile Include=\"rd.xml\" />
</ItemGroup>
"
);
if self.aot {
//TODO: Is this handled by the source generator? (Temporary just to test with numbers and strings)
csproj.push_str(
r#"
<ItemGroup>
<CustomLinkerArg Include="-Wl,--export,_initialize" />
<CustomLinkerArg Include="-Wl,--no-entry" />
<CustomLinkerArg Include="-mexec-model=reactor" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.DotNet.ILCompiler.LLVM" Version="9.0.0-*" />
<PackageReference Include="runtime.win-x64.Microsoft.DotNet.ILCompiler.LLVM" Version="9.0.0-*" />
</ItemGroup>
<Target Name="CheckWasmSdks">
<Error Text="Emscripten not found, not compiling to WebAssembly. To enable WebAssembly compilation, install Emscripten and ensure the EMSDK environment variable points to the directory containing upstream/emscripten/emcc.bat"
Condition="'$(EMSDK)' == ''" />
</Target>
"#,
);
csproj.push_str(&format!("
<Target Name=\"CompileCabiRealloc\" BeforeTargets=\"IlcCompile\" DependsOnTargets=\"CheckWasmSdks\"
Inputs=\"$(MSBuildProjectDirectory)/{camel}_cabi_realloc.c\"
Outputs=\"$(MSBuildProjectDirectory)/{camel}_cabi_realloc.o\"
>
<Exec Command=\"emcc.bat "$(MSBuildProjectDirectory)/{camel}_cabi_realloc.c" -c -o "$(MSBuildProjectDirectory)/{camel}_cabi_realloc.o"\"/>
</Target>
"
));
fs::write(
self.dir.join("nuget.config"),
r#"<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value=".packages" />
</config>
<packageSources>
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
<clear />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
<add key="dotnet-experimental" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-experimental/nuget/v3/index.json" />
<!--<add key="dotnet-experimental" value="C:\github\runtimelab\artifacts\packages\Debug\Shipping" />-->
</packageSources>
</configuration>"#,
)?;
}
if self.clean_targets {
let mut wasm_filename = self.dir.join(name);
wasm_filename.set_extension("wasm");
// In CI we run out of disk space if we don't clean up the files, we don't need to keep any of it around.
csproj.push_str(&format!(
"<Target Name=\"CleanAndDelete\" AfterTargets=\"Clean\">
<!-- Remove obj folder -->
<RemoveDir Directories=\"$(BaseIntermediateOutputPath)\" />
<!-- Remove bin folder -->
<RemoveDir Directories=\"$(BaseOutputPath)\" />
<RemoveDir Directories=\"{}\" />
<RemoveDir Directories=\".packages\" />
</Target>",
wasm_filename.display()
));
}
csproj.push_str(
r#"</Project>
"#,
);
fs::write(self.dir.join(format!("{camel}.csproj")), csproj)?;
Ok(())
}
pub fn aot(&mut self) {
self.aot = true;
}
pub fn clean(&mut self) -> &mut Self {
self.clean_targets = true;
self
}
}
impl CSProjectMonoBuilder {
pub fn generate(&self) -> Result<()> {
let name = &self.name;
let world = &self.world_name.replace("-", "_");
let camel = format!("{}World", world.to_upper_camel_case());
let aot = self.aot;
let maybe_aot = match aot {
true => format!("<WasmBuildNative>{aot}</WasmBuildNative>"),
false => String::new(),
};
let mut csproj = format!(
"<Project Sdk=\"Microsoft.NET.Sdk\">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
<OutputType>Library</OutputType>
{maybe_aot}
<RunAOTCompilation>{aot}</RunAOTCompilation>
<WasmNativeStrip>false</WasmNativeStrip>
<WasmSingleFileBundle>true</WasmSingleFileBundle>
<RootNamespace>{name}</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup>
<PublishTrimmed>true</PublishTrimmed>
<AssemblyName>{name}</AssemblyName>
</PropertyGroup>
<ItemGroup>
<NativeFileReference Include=\"{camel}_component_type.o\" Condition=\"Exists('{camel}_component_type.o')\"/>
</ItemGroup>
"
);
fs::write(
self.dir.join("nuget.config"),
r#"<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value=".packages" />
</config>
<packageSources>
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
<clear />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
<add key="dotnet9" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet9/nuget/v3/index.json" />
</packageSources>
</configuration>"#,
)?;
if self.clean_targets {
let mut wasm_filename = self.dir.join(name);
wasm_filename.set_extension("wasm");
// In CI we run out of disk space if we don't clean up the files, we don't need to keep any of it around.
csproj.push_str(&format!(
"<Target Name=\"CleanAndDelete\" AfterTargets=\"Clean\">
<!-- Remove obj folder -->
<RemoveDir Directories=\"$(BaseIntermediateOutputPath)\" />
<!-- Remove bin folder -->
<RemoveDir Directories=\"$(BaseOutputPath)\" />
<RemoveDir Directories=\"{}\" />
<RemoveDir Directories=\".packages\" />
</Target>",
wasm_filename.display()
));
}
csproj.push_str(
r#"</Project>
"#,
);
fs::write(self.dir.join(format!("{camel}.csproj")), csproj)?;
Ok(())
}
pub fn aot(&mut self) {
self.aot = true;
}
pub fn clean(&mut self) -> &mut Self {
self.clean_targets = true;
self
}
}