Skip to content

Commit 454d688

Browse files
cpetigalexcrichton
andauthored
Test the C++ generator in CI (2nd version: with more fixes included and more upstream changes) (bytecodealliance#1340)
* Test the C++ generator in CI Updating CI configuration to ensure that `cpp` is listed under the languages tested. * Fix all runtime and most codegen tests (#3) * fix runtime tests * fix the majority of codegen tests * fix and simplify the newly added variant-with-data test * this looks ugly but seems right, it for sure fixes the test * recognize expected failures * cargo fmt (missing comma) * mark the wasi tests as expected failures for now --------- Co-authored-by: Alex Crichton <[email protected]>
1 parent 24eebf7 commit 454d688

File tree

6 files changed

+64
-12
lines changed

6 files changed

+64
-12
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
matrix:
6060
os: [ubuntu-latest, macos-latest, windows-latest]
6161
# moonbit removed from language matrix for now - causing CI failures
62-
lang: [c, rust, csharp]
62+
lang: [c, rust, csharp, cpp]
6363
exclude:
6464
# For now csharp doesn't work on macos, so exclude it from testing.
6565
- os: macos-latest

crates/test/src/cpp.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,43 @@ impl LanguageMethods for Cpp {
4141

4242
fn should_fail_verify(
4343
&self,
44-
_name: &str,
44+
name: &str,
4545
_config: &crate::config::WitConfig,
4646
_args: &[String],
4747
) -> bool {
48-
false
48+
match name {
49+
"async-trait-function.wit"
50+
| "error-context.wit"
51+
| "futures.wit"
52+
| "import_export_func.wit"
53+
| "import-func.wit"
54+
| "issue573.wit"
55+
| "issue929-no-export.wit"
56+
| "keywords.wit"
57+
| "lift-lower-foreign.wit"
58+
| "lists.wit"
59+
| "multiversion"
60+
| "resource-alias.wit"
61+
| "resource-borrow-in-record.wit"
62+
| "resources.wit"
63+
| "resources-in-aggregates.wit"
64+
| "resources-with-futures.wit"
65+
| "resources-with-streams.wit"
66+
| "ret-areas.wit"
67+
| "return-resource-from-export.wit"
68+
| "same-names1.wit"
69+
| "same-names5.wit"
70+
| "simple-http.wit"
71+
| "variants.wit"
72+
| "variants-unioning-types.wit"
73+
| "wasi-cli"
74+
| "wasi-filesystem"
75+
| "wasi-http"
76+
| "wasi-io"
77+
| "worlds-with-types.wit"
78+
| "streams.wit" => true,
79+
_ => false,
80+
}
4981
}
5082

5183
fn prepare(&self, runner: &mut crate::Runner<'_>) -> anyhow::Result<()> {
@@ -177,6 +209,13 @@ impl LanguageMethods for Cpp {
177209
}
178210

179211
fn verify(&self, runner: &crate::Runner<'_>, verify: &crate::Verify) -> anyhow::Result<()> {
212+
// for expected
213+
let cwd = std::env::current_dir()?;
214+
let mut helper_dir2 = cwd;
215+
helper_dir2.push("crates");
216+
helper_dir2.push("cpp");
217+
helper_dir2.push("test_headers");
218+
180219
let compiler = clangpp(runner);
181220
let mut cmd = Command::new(compiler);
182221
cmd.arg(
@@ -186,11 +225,14 @@ impl LanguageMethods for Cpp {
186225
)
187226
.arg("-I")
188227
.arg(&verify.bindings_dir)
228+
.arg("-I")
229+
.arg(helper_dir2.to_str().unwrap().to_string())
189230
.arg("-Wall")
190231
.arg("-Wextra")
191232
.arg("-Werror")
192233
.arg("-Wc++-compat")
193234
.arg("-Wno-unused-parameter")
235+
.arg("-std=c++20")
194236
.arg("-c")
195237
.arg("-o")
196238
.arg(verify.artifacts_dir.join("tmp.o"));

tests/runtime/cpp/variant-with-data/runner.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <assert.h>
22
#include <runner_cpp.h>
33

4+
using ::test::variant_with_data::to_test::DataVariant;
5+
46
template <class T>
57
static bool equal(T const& a, T const& b) {
68
return a == b;
@@ -19,12 +21,12 @@ static bool equal(wit::string const& a, wit::string const& b) {
1921
return a.get_view() == b.get_view();
2022
}
2123

22-
static bool equal(::test::variant_with_data::to_test::DataVariant const& a, ::test::variant_with_data::to_test::DataVariant const& b) {
24+
static bool equal(DataVariant const& a, DataVariant const& b) {
2325
if (a.variants.index() != b.variants.index()) return false;
2426
switch (a.variants.index()) {
25-
case 0: return equal(std::get<::test::variant_with_data::to_test::DataVariant::Bytes>(a.variants).value, std::get<::test::variant_with_data::to_test::DataVariant::Bytes>(b.variants).value);
26-
case 1: return equal(std::get<::test::variant_with_data::to_test::DataVariant::Number>(a.variants).value, std::get<::test::variant_with_data::to_test::DataVariant::Number>(b.variants).value);
27-
case 2: return equal(std::get<::test::variant_with_data::to_test::DataVariant::Text>(a.variants).value, std::get<::test::variant_with_data::to_test::DataVariant::Text>(b.variants).value);
27+
case 0: return equal(std::get<DataVariant::Bytes>(a.variants).value, std::get<DataVariant::Bytes>(b.variants).value);
28+
case 1: return equal(std::get<DataVariant::Number>(a.variants).value, std::get<DataVariant::Number>(b.variants).value);
29+
case 2: return equal(std::get<DataVariant::Text>(a.variants).value, std::get<DataVariant::Text>(b.variants).value);
2830
}
2931
return false;
3032
}

tests/runtime/cpp/variant-with-data/test.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22
#include <vector>
33
#include <test_cpp.h>
44

5-
using ::test::variant_with_data::to_test::DataVariant;
5+
using exports::test::variant_with_data::to_test::DataVariant;
66

77
DataVariant exports::test::variant_with_data::to_test::GetData(uint8_t num) {
88
DataVariant variant;
99
switch (num) {
1010
case 0: {
1111
uint8_t bytes[]{0x01, 0x02, 0x03, 0x04, 0x05};
1212
variant.variants = DataVariant::Bytes(wit::vector<uint8_t>::from_view(std::span<uint8_t>(bytes)));
13+
break;
1314
}
1415
case 1:
1516
variant.variants = DataVariant::Number(42);
17+
break;
1618
case 2:
1719
variant.variants = DataVariant::Text(wit::string::from_view("hello"));
20+
break;
1821
default:
1922
variant.variants = DataVariant::Number(0);
23+
break;
2024
}
2125
auto result = std::move(variant);
2226
return result;

tests/runtime/resource_borrow_in_record/cpp/exports-test-resource_borrow_in_record-to_test-Thing.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ class Thing : public wit::ResourceExportBase<Thing> {
1616
public:
1717
static void Dtor(to_test::Thing *self) { delete self; }
1818
Thing(wit::string s) : contents(s.to_string()) {}
19-
static Owned New(wit::string s) { return Owned(new Thing(std::move(s))); }
20-
wit::string Get() const { return wit::string::from_view(std::string_view(contents)); }
19+
static Owned New(wit::string s) { return Owned(new Thing(wit::string::from_view(std::string_view(s.to_string() + " new")))); }
20+
wit::string Get() const { return wit::string::from_view(std::string_view(contents + " get")); }
2121
static int32_t ResourceNew(to_test::Thing *self);
2222
static Thing *ResourceRep(int32_t id);
2323
static void ResourceDrop(int32_t id);
2424

25+
// this is ugly but exactly how the Rust test is designed
26+
static Owned new_internal(wit::string s) { return Owned(new Thing(std::move(s))); }
27+
std::string const& get_internal() const { return contents; }
28+
2529
private:
2630
std::string contents;
2731
};

tests/runtime/resource_borrow_in_record/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace test_exports = ::exports::test::resource_borrow_in_record::to_test;
55
wit::vector<test_exports::Thing::Owned> test_exports::Test(wit::vector<test_exports::Foo> list) {
66
auto result = wit::vector<test_exports::Thing::Owned>::allocate(list.size());
77
for (size_t i = 0; i < list.size(); ++i) {
8-
auto str = wit::string::from_view(std::string_view(list[i].thing.get().Get().to_string() + " test"));
9-
result.initialize(i, test_exports::Thing::New(std::move(str)));
8+
auto str = wit::string::from_view(std::string_view(list[i].thing.get().get_internal() + " test"));
9+
result.initialize(i, test_exports::Thing::new_internal(std::move(str)));
1010
}
1111
return result;
1212
}

0 commit comments

Comments
 (0)