-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathcsharp.rs
More file actions
149 lines (121 loc) · 4.29 KB
/
csharp.rs
File metadata and controls
149 lines (121 loc) · 4.29 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
use crate::{Compile, LanguageMethods, Runner, Verify};
use anyhow::Result;
use heck::*;
use std::env;
use std::fs;
use std::path::Path;
use std::process::Command;
pub struct Csharp;
fn dotnet() -> Command {
let dotnet_cmd = match env::var("DOTNET_ROOT") {
Ok(val) => Path::new(&val).join("dotnet"),
Err(_e) => "dotnet".into(),
};
Command::new(dotnet_cmd)
}
impl LanguageMethods for Csharp {
fn display(&self) -> &str {
"csharp"
}
fn comment_prefix_for_test_config(&self) -> Option<&str> {
Some("//@")
}
fn default_bindgen_args(&self) -> &[&str] {
&["--runtime=native-aot"]
}
fn default_bindgen_args_for_codegen(&self) -> &[&str] {
&["--generate-stub"]
}
fn should_fail_verify(
&self,
name: &str,
_config: &crate::config::WitConfig,
_args: &[String],
) -> bool {
// TODO: remove these exclusions as support is created
matches!(
name,
"error-context.wit"
| "resource-fallible-constructor.wit"
| "async-resource-func.wit"
| "import-export-resource.wit"
| "issue-1433.wit"
| "named-fixed-length-list.wit"
)
}
fn prepare(&self, runner: &mut Runner) -> Result<()> {
runner.run_command(dotnet().arg("--version"))?;
Ok(())
}
fn compile(&self, runner: &Runner, compile: &Compile<'_>) -> Result<()> {
let world_name = &compile.component.bindgen.world;
let path = &compile.component.path;
let test_dir = &compile.bindings_dir;
let new_path = test_dir.join("testcase.cs");
fs::copy(path, &new_path)?;
let camel = format!("{}World", world_name.to_upper_camel_case());
let assembly_name = "csharp-testcase";
let out_wasm = test_dir.join(&assembly_name);
let mut csproj =
wit_bindgen_csharp::CSProject::new(test_dir.to_path_buf(), &assembly_name, world_name);
csproj.aot();
csproj.generate()?;
let mut cmd = dotnet();
let mut wasm_filename = out_wasm.join(assembly_name);
wasm_filename.set_extension("wasm");
cmd.current_dir(test_dir)
.arg("publish")
.arg(test_dir.join(format!("{camel}.csproj")))
.arg("-r")
.arg("wasi-wasm")
.arg("-c")
.arg("Debug")
.arg("/p:PlatformTarget=AnyCPU")
.arg("/p:MSBuildEnableWorkloadResolver=false")
.arg("--self-contained")
.arg("/p:UseAppHost=false")
// .arg("/bl") // to diagnose dotnet build problems
.arg("-o")
.arg(&out_wasm);
let os = match std::env::consts::OS {
"windows" => "win",
"linux" => std::env::consts::OS,
"macos" => "osx",
other => todo!("OS {} not supported", other),
};
// TODO: Workaround for no aarch64(arm64 in dotnet parlance) packages on Windows
if os == "win" && std::env::consts::ARCH == "aarch64" {
cmd.arg("/p:_hostArchitecture=x64");
}
runner.run_command(&mut cmd)?;
fs::copy(&wasm_filename, &compile.output)?;
Ok(())
}
fn verify(&self, runner: &Runner, verify: &Verify<'_>) -> Result<()> {
let dir = verify.bindings_dir;
let name = verify.world;
let mut project = wit_bindgen_csharp::CSProject::new(dir.to_path_buf(), &name, "the_world");
project.aot();
project.clean();
project.generate().unwrap();
let mut cmd = dotnet();
cmd.current_dir(&dir);
let mut wasm_filename = dir.join(name);
wasm_filename.set_extension("wasm");
cmd.arg("build")
.arg(dir.join(format!("TheWorldWorld.csproj")))
.arg("-r")
.arg("wasi-wasm")
.arg("-c")
.arg("Debug")
.arg("/p:PlatformTarget=AnyCPU")
.arg("/p:MSBuildEnableWorkloadResolver=false")
.arg("--self-contained")
.arg("/p:UseAppHost=false")
// .arg("/bl") // to diagnose dotnet build problems
.arg("-o")
.arg(&wasm_filename);
runner.run_command(&mut cmd)?;
runner.run_command(dotnet().current_dir(&dir).arg("clean"))
}
}