-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathcpp.rs
More file actions
207 lines (187 loc) · 6.34 KB
/
cpp.rs
File metadata and controls
207 lines (187 loc) · 6.34 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use crate::config::StringList;
use crate::{LanguageMethods, Runner};
use anyhow::Context;
use heck::ToSnakeCase;
use serde::Deserialize;
use std::path::PathBuf;
use std::process::Command;
// option wasi_sdk_path is inherited from C
pub struct Cpp;
/// C/C++-specific configuration of component files
#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct LangConfig {
/// Space-separated list or array of compiler flags to pass.
#[serde(default)]
cflags: StringList,
}
fn clangpp(runner: &Runner) -> PathBuf {
match &runner.opts.c.wasi_sdk_path {
Some(path) => path.join("bin/wasm32-wasip2-clang++"),
None => "wasm32-wasip2-clang++".into(),
}
}
impl LanguageMethods for Cpp {
fn display(&self) -> &str {
"cpp"
}
fn comment_prefix_for_test_config(&self) -> Option<&str> {
Some("//@")
}
fn bindgen_name(&self) -> Option<&str> {
Some("cpp")
}
fn should_fail_verify(
&self,
name: &str,
config: &crate::config::WitConfig,
_args: &[String],
) -> bool {
return match name {
"issue1514-6.wit" | "named-fixed-length-list.wit" => true,
_ => false,
} || config.async_;
}
fn prepare(&self, runner: &mut Runner) -> anyhow::Result<()> {
let compiler = clangpp(runner);
let cwd = std::env::current_dir()?;
let dir = cwd.join(&runner.opts.artifacts).join("cpp");
super::write_if_different(&dir.join("test.cpp"), "int main() { return 0; }")?;
println!("Testing if `{}` works...", compiler.display());
runner
.run_command(Command::new(&compiler).current_dir(&dir).arg("test.cpp"))
.inspect_err(|_| {
eprintln!(
"Error: failed to find `{}`. Hint: pass `--wasi-sdk-path` or set `WASI_SDK_PATH`",
compiler.display()
);
})?;
Ok(())
}
fn generate_bindings_prepare(
&self,
_runner: &Runner,
bindgen: &crate::Bindgen,
dir: &std::path::Path,
) -> anyhow::Result<()> {
let mut export_header_dir = bindgen.wit_path.clone();
export_header_dir.pop();
export_header_dir.push("cpp");
// copy resource implementation in header files to target dir
if export_header_dir.is_dir() {
if !dir.exists() {
std::fs::create_dir_all(dir).context("failed to create bindings dir")?;
}
for entry in export_header_dir
.read_dir()
.context("failed to read test header directory")?
{
let entry = entry.context("failed to read test header directory entry")?;
let path = entry.path();
let mut dest = PathBuf::from(dir);
dest.push(path.file_name().unwrap());
std::fs::copy(path, dest).context("failed to copy header file")?;
}
}
Ok(())
}
fn compile(&self, runner: &Runner, compile: &crate::Compile) -> anyhow::Result<()> {
let compiler = clangpp(runner);
let config = compile.component.deserialize_lang_config::<LangConfig>()?;
let cwd = std::env::current_dir()?;
let mut helper_dir = cwd.clone();
helper_dir.push("crates");
helper_dir.push("cpp");
helper_dir.push("helper-types");
// for expected
let mut helper_dir2 = cwd;
helper_dir2.push("crates");
helper_dir2.push("cpp");
helper_dir2.push("test_headers");
// Compile the C-based bindings to an object file.
let bindings_object = compile.output.with_extension("bindings.o");
let mut cmd = Command::new(clangpp(runner));
cmd.arg(
compile
.bindings_dir
.join(format!("{}.cpp", compile.component.bindgen.world)),
)
.arg("-I")
.arg(&compile.bindings_dir)
.arg("-I")
.arg(helper_dir.to_str().unwrap())
.arg("-I")
.arg(helper_dir2.to_str().unwrap())
.arg("-fno-exceptions")
.arg("-Wall")
.arg("-Wextra")
.arg("-Werror")
.arg("-Wno-unused-parameter")
.arg("-std=c++20")
.arg("-c")
.arg("-g")
.arg("-o")
.arg(&bindings_object);
runner.run_command(&mut cmd)?;
// Now compile the runner's source code to with the above object and the
// component-type object into a final component.
let mut cmd = Command::new(compiler);
cmd.arg(&compile.component.path)
.arg(&bindings_object)
.arg(compile.bindings_dir.join(format!(
"{}_component_type.o",
compile.component.bindgen.world
)))
.arg("-I")
.arg(&compile.bindings_dir)
.arg("-I")
.arg(helper_dir.to_str().unwrap())
.arg("-I")
.arg(helper_dir2.to_str().unwrap())
.arg("-fno-exceptions")
.arg("-Wall")
.arg("-Wextra")
.arg("-Werror")
.arg("-Wc++-compat")
.arg("-Wno-unused-parameter")
.arg("-std=c++20")
.arg("-g")
.arg("-o")
.arg(&compile.output);
for flag in Vec::from(config.cflags) {
cmd.arg(flag);
}
cmd.arg("-mexec-model=reactor");
runner.run_command(&mut cmd)?;
Ok(())
}
fn verify(&self, runner: &Runner, verify: &crate::Verify) -> anyhow::Result<()> {
// for expected
let cwd = std::env::current_dir()?;
let mut helper_dir2 = cwd;
helper_dir2.push("crates");
helper_dir2.push("cpp");
helper_dir2.push("test_headers");
let compiler = clangpp(runner);
let mut cmd = Command::new(compiler);
cmd.arg(
verify
.bindings_dir
.join(format!("{}.cpp", verify.world.to_snake_case())),
)
.arg("-I")
.arg(&verify.bindings_dir)
.arg("-I")
.arg(helper_dir2.to_str().unwrap())
.arg("-Wall")
.arg("-Wextra")
.arg("-Werror")
.arg("-Wc++-compat")
.arg("-Wno-unused-parameter")
.arg("-std=c++20")
.arg("-c")
.arg("-o")
.arg(verify.artifacts_dir.join("tmp.o"));
runner.run_command(&mut cmd)
}
}