JohnTitor / ctest2

Automatic testing of FFI bindings for Rust
https://crates.io/crates/ctest2
Apache License 2.0
22 stars 20 forks source link

Specify linkage for `__test_fn...()` #33

Closed jessicah closed 2 years ago

jessicah commented 2 years ago

When using Lang::CXX, these ended up with C++ linkage, causing linking to fail.

JohnTitor commented 2 years ago

Could you add a testcase? Also, please do not increment the version number in this PR.

jessicah commented 2 years ago

I'm having trouble running the tests; is cd testcrate && cargo test supposed to work? I get multiple definition errors on both Linux and Haiku.

/usr/bin/ld: .../libtestcrate.rlib(t1gen.o):/.../testcrate/src/t1.h:65: multiple definition of `T1_static_mut_u8'; .../libtestcrate.rlib(t1.o):/.../testcrate/src/t1.h:65: first defined here
/usr/bin/ld: .../libtestcrate.rlib(t1gen.o):/.../testcrate/src/t1.h:66: multiple definition of `T1_static_mut_fn_ptr'; .../libtestcrate.rlib(t1.o):/.../testcrate/src/t1.h:66: first defined here
/usr/bin/ld: .../libtestcrate.rlib(t1gen.o):/.../testcrate/src/t1.h:72: multiple definition of `T1_static_right2'; .../libtestcrate.rlib(t1.o):/.../testcrate/src/t1.h:72: first defined here
/usr/bin/ld: .../libtestcrate.rlib(t1gen.o):/.../testcrate/src/t1.h:77: multiple definition of `T1_fn_ptr_s'; .../libtestcrate.rlib(t1.o):/.../testcrate/src/t1.h:77: first defined here
/usr/bin/ld: .../libtestcrate.rlib(t1gen.o):/.../testcrate/src/t1.h:82: multiple definition of `T1_fn_ptr_s2'; .../libtestcrate.rlib(t1.o):/.../testcrate/src/t1.h:82: first defined here
/usr/bin/ld: .../libtestcrate.rlib(t1gen.o):/.../testcrate/src/t1.h:101: multiple definition of `T1_opt_fn2'; .../libtestcrate.rlib(t1.o):/.../testcrate/src/t1.h:101: first defined here
/usr/bin/ld: .../libtestcrate.rlib(t1gen.o):/.../testcrate/src/t1.h:102: multiple definition of `T1_opt_fn3'; .../libtestcrate.rlib(t1.o):/.../testcrate/src/t1.h:102: first defined here
/usr/bin/ld: .../libtestcrate.rlib(t1gen.o):/.../testcrate/src/t1.h:157: multiple definition of `T1_fn_ptr_vol'; .../libtestcrate.rlib(t1.o):/.../testcrate/src/t1.h:157: first defined here
collect2: error: ld returned 1 exit status

Running cargo test in the root appears to only run the no_run test code from src/lib.rs? Sorry, I'm not too familiar with the whole cargo test stuff.

JohnTitor commented 2 years ago

Well, I can reproduce that failure too, what's weird is it doesn't reproduce on CI (GCC version difference?). I don't have time to investigate right now, could you use CI for testing?

jessicah commented 2 years ago

Oh, I see, the tests in testcrate have been explicitly disabled. I ran the CI test on top of current tip with the above patch, and no test failures, so not sure what my next step is for adding a test, given that testcrate tests don't work properly.

And I have already used this patch on Haiku, as it was needed for testing my changes to the libc crate, similar to what your CI already does.

jessicah commented 2 years ago

As this change is for the generated __test_fn declarations, I can't see why you'd ever want non-C linkage. Even when using c++, surely Rust is expecting C linkage? It also matches other generated test wrappers that using C linkage, e.g. __test_offset, __test_fsize, __test_size, __test_align, __test_signed, __test_const, and __test_roundtrip all use C linkage.

JohnTitor commented 2 years ago

Fair enough