From 806af289081e290546bf05ce15552a6b0ea9b39a Mon Sep 17 00:00:00 2001 From: Martynas Gurskas Date: Tue, 5 Mar 2024 16:27:25 +0200 Subject: [PATCH 1/7] Fix compilation warnings on MSVC, fix coverall test compilation issue on MSVC Signed-off-by: Martynas Gurskas --- bindgen/src/bindings/cpp/templates/arith_conv.cpp | 2 +- bindgen/src/bindings/cpp/templates/bytes_conv.cpp | 2 +- .../bindings/cpp/templates/callback_iface_tmpl.cpp | 2 +- .../src/bindings/cpp/templates/duration_helper.cpp | 2 +- bindgen/src/bindings/cpp/templates/enum_tmpl.cpp | 4 ++-- bindgen/src/bindings/cpp/templates/err_tmpl.cpp | 6 +++--- .../bindings/cpp/templates/scaffolding/callback.cpp | 2 +- .../src/bindings/cpp/templates/scaffolding/err.cpp | 2 +- bindgen/src/bindings/cpp/templates/str_conv.cpp | 2 +- .../src/bindings/cpp/templates/timestamp_helper.cpp | 2 +- .../scaffolding_tests/callbacks/lib_callbacks.cpp | 2 +- cpp-tests/scaffolding_tests/coverall/lib_coverall.cpp | 11 +++++------ .../fixture_callbacks/lib_fixture_callbacks.cpp | 2 +- 13 files changed, 20 insertions(+), 21 deletions(-) diff --git a/bindgen/src/bindings/cpp/templates/arith_conv.cpp b/bindgen/src/bindings/cpp/templates/arith_conv.cpp index e5243ca..66bcc25 100644 --- a/bindgen/src/bindings/cpp/templates/arith_conv.cpp +++ b/bindgen/src/bindings/cpp/templates/arith_conv.cpp @@ -18,5 +18,5 @@ void {{ ffi_converter_name }}::write(RustStream &stream, {{ type_name }} val) { } int32_t {{ ffi_converter_name }}::allocation_size({{ type_name }}) { - return sizeof({{ type_name }}); + return static_cast(sizeof({{ type_name }})); } \ No newline at end of file diff --git a/bindgen/src/bindings/cpp/templates/bytes_conv.cpp b/bindgen/src/bindings/cpp/templates/bytes_conv.cpp index e05f795..07af620 100644 --- a/bindgen/src/bindings/cpp/templates/bytes_conv.cpp +++ b/bindgen/src/bindings/cpp/templates/bytes_conv.cpp @@ -42,5 +42,5 @@ void {{ ffi_converter_name }}::write(RustStream &stream, const {{ type_name }} & } int32_t {{ ffi_converter_name }}::allocation_size(const {{ type_name }} &val) { - return sizeof(int32_t) + sizeof(uint8_t) * val.size(); + return static_cast(sizeof(int32_t) + sizeof(uint8_t) * val.size()); } diff --git a/bindgen/src/bindings/cpp/templates/callback_iface_tmpl.cpp b/bindgen/src/bindings/cpp/templates/callback_iface_tmpl.cpp index f1bd2ac..c95894c 100644 --- a/bindgen/src/bindings/cpp/templates/callback_iface_tmpl.cpp +++ b/bindgen/src/bindings/cpp/templates/callback_iface_tmpl.cpp @@ -34,7 +34,7 @@ void uniffi::{{ class_name }}::write(RustStream &stream, const {{ type_name }} & } int32_t uniffi::{{ class_name }}::allocation_size(const {{ type_name }} &impl) { - return sizeof(uint64_t); + return static_cast(sizeof(uint64_t)); } int {{ class_name }}::callback_stub(uint64_t handle, uint32_t method, uint8_t *args_data, int32_t args_len, RustBuffer *buf_ptr) { diff --git a/bindgen/src/bindings/cpp/templates/duration_helper.cpp b/bindgen/src/bindings/cpp/templates/duration_helper.cpp index 296fd0c..8125457 100644 --- a/bindgen/src/bindings/cpp/templates/duration_helper.cpp +++ b/bindgen/src/bindings/cpp/templates/duration_helper.cpp @@ -35,5 +35,5 @@ void {{ ffi_converter_name }}::write(RustStream &stream, const {{ type_name }} & } int32_t {{ ffi_converter_name }}::allocation_size(const {{ type_name }} &) { - return sizeof(uint64_t) + sizeof(uint32_t); + return static_cast(sizeof(uint64_t) + sizeof(uint32_t)); } \ No newline at end of file diff --git a/bindgen/src/bindings/cpp/templates/enum_tmpl.cpp b/bindgen/src/bindings/cpp/templates/enum_tmpl.cpp index 3e4233d..f01153d 100644 --- a/bindgen/src/bindings/cpp/templates/enum_tmpl.cpp +++ b/bindgen/src/bindings/cpp/templates/enum_tmpl.cpp @@ -44,7 +44,7 @@ void {{ ffi_converter_name }}::write(RustStream &stream, const {{ type_name }} & } int32_t {{ ffi_converter_name }}::allocation_size(const {{ type_name|class_name }} &) { - return sizeof(int32_t); + return static_cast(sizeof(int32_t)); } {%- else %} {{ type_name }} {{ ffi_converter_name }}::lift(RustBuffer buf) { @@ -84,7 +84,7 @@ RustBuffer {{ ffi_converter_name }}::lower(const {{ type_name }} &val) { } void {{ ffi_converter_name }}::write(RustStream &stream, const {{ type_name }} &val) { - int32_t variant_id = val.variant.index() + 1; + int32_t variant_id = static_cast(val.variant.index() + 1); stream << variant_id; diff --git a/bindgen/src/bindings/cpp/templates/err_tmpl.cpp b/bindgen/src/bindings/cpp/templates/err_tmpl.cpp index 3ab0619..7773ded 100644 --- a/bindgen/src/bindings/cpp/templates/err_tmpl.cpp +++ b/bindgen/src/bindings/cpp/templates/err_tmpl.cpp @@ -66,17 +66,17 @@ void {{ ffi_converter_name }}::write(RustStream &stream, const {{ class_name }} int32_t {{ ffi_converter_name }}::allocation_size(const {{ class_name }} &val) { {%- if e.is_flat() %} - return sizeof(int32_t); + return static_cast(sizeof(int32_t)); {%- else %} switch (val.get_variant_idx()) { {%- for variant in e.variants() %} case {{ loop.index }}: { auto& var = static_cast(val); - return sizeof(int32_t) + return static_cast(sizeof(int32_t) {%- for field in variant.fields() %} + {{ field|allocation_size_fn }}(var.{{ field.name()|var_name }}) - {%- endfor %}; + {%- endfor %}); } {%- endfor %} default: diff --git a/bindgen/src/bindings/cpp/templates/scaffolding/callback.cpp b/bindgen/src/bindings/cpp/templates/scaffolding/callback.cpp index 20631eb..8dd3fd7 100644 --- a/bindgen/src/bindings/cpp/templates/scaffolding/callback.cpp +++ b/bindgen/src/bindings/cpp/templates/scaffolding/callback.cpp @@ -127,7 +127,7 @@ void {{ ffi_converter_name|class_name }}::write(RustStream &stream, const {{ typ } int32_t {{ ffi_converter_name|class_name }}::allocation_size(const {{ type_name }} &obj) { - return sizeof(uint64_t); + return static_cast(sizeof(uint64_t)); } std::atomic {{ ffi_converter_name|class_name }}::fn_handle = 0; diff --git a/bindgen/src/bindings/cpp/templates/scaffolding/err.cpp b/bindgen/src/bindings/cpp/templates/scaffolding/err.cpp index 21deecb..2bd4dd3 100644 --- a/bindgen/src/bindings/cpp/templates/scaffolding/err.cpp +++ b/bindgen/src/bindings/cpp/templates/scaffolding/err.cpp @@ -50,7 +50,7 @@ void {{ ffi_converter_name }}{{ variant.name() }}::write(RustStream &stream, con } int32_t {{ ffi_converter_name }}{{ variant.name() }}::allocation_size(const {{ namespace }}::{{ variant.name() }} &val) { - auto size = sizeof(int32_t); + int32_t size = sizeof(int32_t); {%- if e.is_flat() %} size += {{ Type::String.borrow()|allocation_size_fn }}(val.what()); {%- else %} diff --git a/bindgen/src/bindings/cpp/templates/str_conv.cpp b/bindgen/src/bindings/cpp/templates/str_conv.cpp index bb23c24..a0f715e 100644 --- a/bindgen/src/bindings/cpp/templates/str_conv.cpp +++ b/bindgen/src/bindings/cpp/templates/str_conv.cpp @@ -31,5 +31,5 @@ void {{ ffi_converter_name }}::write(RustStream &stream, const std::string &val) } int32_t {{ ffi_converter_name }}::allocation_size(const std::string &val) { - return sizeof(int32_t) + val.length(); + return static_cast(sizeof(int32_t) + val.length()); } diff --git a/bindgen/src/bindings/cpp/templates/timestamp_helper.cpp b/bindgen/src/bindings/cpp/templates/timestamp_helper.cpp index b639037..00e270a 100644 --- a/bindgen/src/bindings/cpp/templates/timestamp_helper.cpp +++ b/bindgen/src/bindings/cpp/templates/timestamp_helper.cpp @@ -42,5 +42,5 @@ void {{ ffi_converter_name }}::write(RustStream &stream, const {{ type_name }} & } int32_t {{ ffi_converter_name }}::allocation_size(const {{ type_name }} &) { - return sizeof(int64_t) + sizeof(uint32_t); + return static_cast(sizeof(int64_t) + sizeof(uint32_t)); } diff --git a/cpp-tests/scaffolding_tests/callbacks/lib_callbacks.cpp b/cpp-tests/scaffolding_tests/callbacks/lib_callbacks.cpp index dc0f8bb..cf55317 100644 --- a/cpp-tests/scaffolding_tests/callbacks/lib_callbacks.cpp +++ b/cpp-tests/scaffolding_tests/callbacks/lib_callbacks.cpp @@ -5,7 +5,7 @@ std::string callbacks::Telephone::call(std::shared_ptr answerer) return answerer->answer(); } catch (telephone_error::Busy& e) { throw e; - } catch (std::runtime_error& e) { + } catch (std::runtime_error&) { throw telephone_error::InternalTelephoneError(); } } diff --git a/cpp-tests/scaffolding_tests/coverall/lib_coverall.cpp b/cpp-tests/scaffolding_tests/coverall/lib_coverall.cpp index c15c0b3..11804d0 100644 --- a/cpp-tests/scaffolding_tests/coverall/lib_coverall.cpp +++ b/cpp-tests/scaffolding_tests/coverall/lib_coverall.cpp @@ -160,10 +160,9 @@ std::unordered_map coverall::Coveralls::get_dict3(uint32_t k } void coverall::Coveralls::add_patch(std::shared_ptr patch) { - auto repair = Repair { - .when = std::chrono::high_resolution_clock::now(), - .patch = patch - }; + auto repair = Repair(); + repair.when = std::chrono::system_clock::now(); + repair.patch = patch; std::lock_guard lock(this->repairs_mutex); this->repairs.push_back(repair); @@ -205,8 +204,8 @@ coverall::SimpleDict coverall::create_some_dict() { .maybe_signed8 = 0, .signed64 = std::numeric_limits::max(), .maybe_signed64 = 0, - .float32 = 1.2345, - .maybe_float32 = 22.0 / 7.0, + .float32 = 1.2345f, + .maybe_float32 = 22.0f / 7.0f, .float64 = 0.0, .maybe_float64 = 1.0, .coveralls = std::make_shared("some_dict"), diff --git a/cpp-tests/scaffolding_tests/fixture_callbacks/lib_fixture_callbacks.cpp b/cpp-tests/scaffolding_tests/fixture_callbacks/lib_fixture_callbacks.cpp index 2d23ea1..bd4562b 100644 --- a/cpp-tests/scaffolding_tests/fixture_callbacks/lib_fixture_callbacks.cpp +++ b/cpp-tests/scaffolding_tests/fixture_callbacks/lib_fixture_callbacks.cpp @@ -36,7 +36,7 @@ std::optional fixture_callbacks::RustGetters::get_string_optional_c return foreign->get_string(v, argument_two); } catch (const fixture_callbacks::simple_error::BadArgument &e) { throw e; - } catch (const std::runtime_error &e) { + } catch (const std::runtime_error&) { throw fixture_callbacks::simple_error::UnexpectedError(); } } From 7541f170c58839f0c9f76f35d605183bc1e05de0 Mon Sep 17 00:00:00 2001 From: Martynas Gurskas Date: Tue, 5 Mar 2024 16:52:31 +0200 Subject: [PATCH 2/7] Modify CMakeLists to support Windows builds Signed-off-by: Martynas Gurskas --- cpp-tests/CMakeLists.txt | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/cpp-tests/CMakeLists.txt b/cpp-tests/CMakeLists.txt index 70c96c1..effd992 100644 --- a/cpp-tests/CMakeLists.txt +++ b/cpp-tests/CMakeLists.txt @@ -12,14 +12,25 @@ find_package(Threads REQUIRED) set(BINDINGS_BUILD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../target/debug) set(BINDINGS_SRC_DIR ${BINDINGS_BUILD_DIR}/bindings) +if (WIN32) + set(UNIFFI_FIXTURES_LIB uniffi_bindgen_cpp_fixtures.dll) + set(ADDITIONAL_LIBS ws2_32 userenv advapi32 ntdll crypt32 Bcrypt) +else() + set(UNIFFI_FIXTURES_LIB libuniffi_bindgen_cpp_fixtures.so) + set(ADDITIONAL_LIBS) +endif() + +find_program(VALGRIND "valgrind") macro(memcheck_test TEST_NAME) -add_test(NAME ${TEST_NAME}-test-memcheck - COMMAND valgrind - --error-exitcode=1 - --tool=memcheck - --leak-check=full - --errors-for-leak-kinds=definite - --show-leak-kinds=definite $) +if (VALGRIND) + add_test(NAME ${TEST_NAME}-test-memcheck + COMMAND valgrind + --error-exitcode=1 + --tool=memcheck + --leak-check=full + --errors-for-leak-kinds=definite + --show-leak-kinds=definite $) +endif() endmacro(memcheck_test) # Add a bindings test case @@ -28,7 +39,7 @@ add_executable(${TEST_NAME}-test tests/${TEST_NAME}/main.cpp ${BINDINGS_SRC_DIR} target_include_directories(${TEST_NAME}-test PRIVATE ${BINDINGS_SRC_DIR} include) target_link_directories(${TEST_NAME}-test PRIVATE ${BINDINGS_BUILD_DIR}) -target_link_libraries(${TEST_NAME}-test uniffi_bindgen_cpp_fixtures Threads::Threads) +target_link_libraries(${TEST_NAME}-test uniffi_bindgen_cpp_fixtures Threads::Threads ${ADDITIONAL_LIBS}) target_compile_definitions(${TEST_NAME}-test PRIVATE UNIFFI_BINDING_DIR="${BINDINGS_SRC_DIR}") add_test(NAME ${TEST_NAME}-test COMMAND ${TEST_NAME}-test) @@ -101,7 +112,7 @@ add_custom_target(libs ALL add_custom_target(bindings ALL DEPENDS libs - COMMAND ./uniffi-bindgen-cpp --library libuniffi_bindgen_cpp_fixtures.so --out-dir bindings + COMMAND ./uniffi-bindgen-cpp --library ${UNIFFI_FIXTURES_LIB} --out-dir bindings WORKING_DIRECTORY ${BINDINGS_BUILD_DIR} COMMENT "Generating bindings" ) @@ -109,7 +120,7 @@ add_custom_target(bindings ALL add_custom_target(scaffolding ALL BYPRODUCTS ${SCAFFOLDING_FILES} DEPENDS libs - COMMAND ./uniffi-bindgen-cpp --scaffolding --library libuniffi_bindgen_cpp_fixtures.so --out-dir bindings + COMMAND ./uniffi-bindgen-cpp --scaffolding --library ${UNIFFI_FIXTURES_LIB} --out-dir bindings WORKING_DIRECTORY ${BINDINGS_BUILD_DIR} COMMENT "Generating scaffolding" ) From 496ddf2d8d43d334c0e832ad36e0f2259303c80d Mon Sep 17 00:00:00 2001 From: Martynas Gurskas Date: Thu, 21 Mar 2024 16:27:05 +0200 Subject: [PATCH 3/7] Simplify CustomTypesBuiltin construction This was causing the MSVC compiler to fail Signed-off-by: Martynas Gurskas --- .../lib_custom_types_builtin.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/cpp-tests/scaffolding_tests/custom_types_builtin/lib_custom_types_builtin.cpp b/cpp-tests/scaffolding_tests/custom_types_builtin/lib_custom_types_builtin.cpp index 055e786..6a59435 100644 --- a/cpp-tests/scaffolding_tests/custom_types_builtin/lib_custom_types_builtin.cpp +++ b/cpp-tests/scaffolding_tests/custom_types_builtin/lib_custom_types_builtin.cpp @@ -3,12 +3,17 @@ #include custom_types_builtin::CustomTypesBuiltin custom_types_builtin::get_custom_types_builtin() { - return custom_types_builtin::CustomTypesBuiltin{ + auto custom_string = custom_types_builtin::CustomString{"Custom string"}; + auto array = std::vector{"Hello, world!"}; + auto bytes = std::vector{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + auto table = std::unordered_map{{"hello", "world"}}; + + auto result = custom_types_builtin::CustomTypesBuiltin{ .string = "Hello, world!", - .custom_string = custom_types_builtin::CustomString{"Custom string"}, - .array = std::vector{"Hello, world!"}, - .bytes = std::vector{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, - .table = std::unordered_map{{"hello", "world"}}, + .custom_string = custom_string, + .array = array, + .bytes = bytes, + .table = table, .boolean = true, .int8 = std::numeric_limits::max(), .int16 = std::numeric_limits::max(), @@ -19,8 +24,10 @@ custom_types_builtin::CustomTypesBuiltin custom_types_builtin::get_custom_types_ .uint32 = std::numeric_limits::max(), .uint64 = std::numeric_limits::max(), .flt = std::numeric_limits::max(), - .dbl = std::numeric_limits::max(), + .dbl = std::numeric_limits::max() }; + + return result; } custom_types_builtin::CustomTypesBuiltin custom_types_builtin::return_custom_types_builtin(custom_types_builtin::CustomTypesBuiltin type) { From f4fc89be811a8ff06b11e32c9af1033785e23170 Mon Sep 17 00:00:00 2001 From: Martynas Gurskas Date: Thu, 21 Mar 2024 16:28:47 +0200 Subject: [PATCH 4/7] Specify duration type for std::chrono::time_point To make sure that all of the platforms use the same representation, tests modified accordingly Signed-off-by: Martynas Gurskas --- .../src/bindings/cpp/gen_cpp/miscellany.rs | 2 +- .../chronological/lib_chronological.cpp | 3 +- .../chronological/lib_chronological.hpp | 2 +- .../coverall/lib_coverall.hpp | 2 +- cpp-tests/tests/chronological/main.cpp | 57 ++++++++++++++++--- 5 files changed, 55 insertions(+), 11 deletions(-) diff --git a/bindgen/src/bindings/cpp/gen_cpp/miscellany.rs b/bindgen/src/bindings/cpp/gen_cpp/miscellany.rs index 130c374..983957b 100644 --- a/bindgen/src/bindings/cpp/gen_cpp/miscellany.rs +++ b/bindgen/src/bindings/cpp/gen_cpp/miscellany.rs @@ -26,7 +26,7 @@ macro_rules! impl_code_type_for_miscellany { impl_code_type_for_miscellany!( TimestampCodeType, - "std::chrono::time_point", + "std::chrono::time_point", "Timestamp" ); impl_code_type_for_miscellany!( diff --git a/cpp-tests/scaffolding_tests/chronological/lib_chronological.cpp b/cpp-tests/scaffolding_tests/chronological/lib_chronological.cpp index 1604699..e2e7cc0 100644 --- a/cpp-tests/scaffolding_tests/chronological/lib_chronological.cpp +++ b/cpp-tests/scaffolding_tests/chronological/lib_chronological.cpp @@ -13,7 +13,8 @@ chronological::duration chronological::return_duration(chronological::duration a } std::string chronological::to_string_timestamp(chronological::timestamp a) { - std::time_t time = std::chrono::system_clock::to_time_t(a); + using time_point = std::chrono::system_clock::time_point; + std::time_t time = std::chrono::system_clock::to_time_t(time_point {std::chrono::duration_cast(a.time_since_epoch())}); auto ns = std::chrono::duration_cast(a.time_since_epoch()).count() % 1000000000; if (ns < 0) { ns += 1000000000; diff --git a/cpp-tests/scaffolding_tests/chronological/lib_chronological.hpp b/cpp-tests/scaffolding_tests/chronological/lib_chronological.hpp index db30bf7..6e5af81 100644 --- a/cpp-tests/scaffolding_tests/chronological/lib_chronological.hpp +++ b/cpp-tests/scaffolding_tests/chronological/lib_chronological.hpp @@ -5,7 +5,7 @@ namespace { namespace chronological { - typedef std::chrono::time_point timestamp; + typedef std::chrono::time_point timestamp; typedef std::chrono::duration duration; namespace chronological_error { diff --git a/cpp-tests/scaffolding_tests/coverall/lib_coverall.hpp b/cpp-tests/scaffolding_tests/coverall/lib_coverall.hpp index 542e014..042bddc 100644 --- a/cpp-tests/scaffolding_tests/coverall/lib_coverall.hpp +++ b/cpp-tests/scaffolding_tests/coverall/lib_coverall.hpp @@ -12,7 +12,7 @@ namespace { namespace coverall { - typedef std::chrono::time_point timestamp; + typedef std::chrono::time_point timestamp; class IFirst { public: diff --git a/cpp-tests/tests/chronological/main.cpp b/cpp-tests/tests/chronological/main.cpp index ab8bc81..744443c 100644 --- a/cpp-tests/tests/chronological/main.cpp +++ b/cpp-tests/tests/chronological/main.cpp @@ -14,7 +14,7 @@ std::chrono::time_point epo std::chrono::duration time_span_seconds(int seconds, int nanoseconds) { - return std::chrono::nanoseconds(std::chrono::seconds(seconds) + std::chrono::nanoseconds(nanoseconds)); + return std::chrono::seconds(seconds) + std::chrono::nanoseconds(nanoseconds); } std::chrono::time_point time_from_string(const std::string& time) { @@ -22,10 +22,47 @@ std::chrono::time_point time_from_string(const std::s auto ss = std::stringstream(time); ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S"); +#ifdef _WIN32 + return std::chrono::system_clock::from_time_t(_mkgmtime(&tm)); +#else return std::chrono::system_clock::from_time_t(timegm(&tm)); +#endif } void test_string_timestamps() { + // Test post-epoch timestamps + { + auto time_str = "1970-12-12T00:00:00.000000000Z"; + auto time = time_from_string(time_str); + + ASSERT_EQ(time_str, chronological::to_string_timestamp(time)); + } + + { + // get_time doesn't support nanoseconds, so we have to add them manually + auto time_str = "1970-12-31T23:59:58.999999900Z"; + auto time = time_from_string(time_str) + 999999900ns; + + ASSERT_EQ(time_str, chronological::to_string_timestamp(time)); + } + + { + // get_time doesn't support nanoseconds, so we have to add them manually + auto time = time_from_string("2024-11-05T00:06:01.283000200Z") + 283000200ns; + auto time2 = time_from_string("2024-11-05T00:06:00.283000100Z") + 283000100ns; + + ASSERT_EQ( + time, + chronological::add(time2, time_span_seconds(1, 100)) + ); + } + +#ifdef _WIN32 + // _mkgmtime doesn't support dates before 1970, so we skip these tests + return; +#endif + + // Test pre-epoch timestamps { auto time_str = "1969-12-12T00:00:00.000000000Z"; auto time = time_from_string(time_str); @@ -55,8 +92,8 @@ void test_string_timestamps() { int main() { ASSERT_EQ( - epoch_second(101, 110), - chronological::add(epoch_second(100, 100), time_span_seconds(1, 10)) + epoch_second(101, 200), + chronological::add(epoch_second(100, 100), time_span_seconds(1, 100)) ); ASSERT_EQ( @@ -69,14 +106,20 @@ int main() { chronological::ChronologicalError ); + + // When testing the min and max values, we need to round them to the nearest 100 ns, as on Windows one tick is 100 ns + auto min = std::chrono::time_point::min().time_since_epoch().count() / 100 * 100; + auto min_timepoint = std::chrono::time_point(std::chrono::nanoseconds(min)); ASSERT_EQ( - std::chrono::time_point::min(), - chronological::return_timestamp(std::chrono::time_point::min()) + min_timepoint, + chronological::return_timestamp(min_timepoint) ); + auto max = std::chrono::time_point::max().time_since_epoch().count() / 100 * 100; + auto max_timepoint = std::chrono::time_point(std::chrono::nanoseconds(max)); ASSERT_EQ( - std::chrono::time_point::max(), - chronological::return_timestamp(std::chrono::time_point::max()) + max_timepoint, + chronological::return_timestamp(max_timepoint) ); ASSERT_EQ( From dfc8ac1b2ce122118cad697627de3bef63608c4c Mon Sep 17 00:00:00 2001 From: Martynas Gurskas Date: Fri, 22 Mar 2024 11:17:39 +0200 Subject: [PATCH 5/7] Turn test job into matrix for Linux and Windows Signed-off-by: Martynas Gurskas --- .github/workflows/cpp.yml | 77 ++++++++++++++----- build_bindgen.sh | 4 - ...scaffolding.sh => build_scaffolding_lib.sh | 2 + fixtures/Cargo.toml | 2 +- 4 files changed, 59 insertions(+), 26 deletions(-) delete mode 100755 build_bindgen.sh rename build_scaffolding.sh => build_scaffolding_lib.sh (71%) diff --git a/.github/workflows/cpp.yml b/.github/workflows/cpp.yml index c63be02..4c001a0 100644 --- a/.github/workflows/cpp.yml +++ b/.github/workflows/cpp.yml @@ -10,13 +10,64 @@ env: CARGO_TERM_COLOR: always jobs: - build-scaffolding: + run-tests: + runs-on: ${{ matrix.os }} + strategy: + # TODO: remove this when the workflow is stable + fail-fast: false + + matrix: + # Ubuntu-20.04 is being used here on purpose instead of ubuntu-latest due to a bug with clang and libstdc++ + # https://github.com/actions/runner-images/issues/8659 + os: [ubuntu-20.04, windows-latest] + build_type: [Release] + cpp_compiler: [g++, clang++, cl] + exclude: + - os: windows-latest + cpp_compiler: g++ + - os: windows-latest + cpp_compiler: clang++ + - os: ubuntu-20.04 + cpp_compiler: cl + + steps: + - uses: actions/checkout@v3 + + - name: Set reusable strings + id: strings + shell: bash + run: | + echo "build-output-dir=${{ github.workspace }}/cpp-tests/build" >> "$GITHUB_OUTPUT" + echo "build-source-dir=${{ github.workspace }}/cpp-tests" >> "$GITHUB_OUTPUT" + + - name: Install additional dependencies + shell: bash + run: | + if [ "$RUNNER_OS" == "Linux" ]; then + sudo apt update && sudo apt install -y valgrind + fi + + - name: Configure CMake + run: > + cmake -B ${{ steps.strings.outputs.build-output-dir }} + -DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }} + -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} + -S ${{ steps.strings.outputs.build-source-dir }} + - name: Build + run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }} + + - name: Run tests + working-directory: ${{ steps.strings.outputs.build-output-dir }} + run: ctest --build-config ${{ matrix.build_type }} + + build-scaffolding-lib: runs-on: ubuntu-latest container: image: ghcr.io/nordsecurity/uniffi-bindgen-cpp-test-runner:v0.3.0 steps: - uses: actions/checkout@v3 - - name: Build scaffolding + + - name: Build scaffolding library shell: bash env: # Github sets HOME to /github/home and breaks dependencies in Docker image.. @@ -24,33 +75,17 @@ jobs: HOME: /root run: | source ~/.bashrc - ./build_bindgen.sh - ./build_scaffolding.sh + ./build_scaffolding_lib.sh - uses: actions/upload-artifact@v3 with: name: scaffolding_lib path: cpp-tests/build/libuniffi_fixtures.so - run-tests: - runs-on: ubuntu-latest - container: - image: ghcr.io/nordsecurity/uniffi-bindgen-cpp-test-runner:v0.3.0 - steps: - - uses: actions/checkout@v3 - - name: Run tests - shell: bash - env: - HOME: /root - run: | - source ~/.bashrc - ./build_bindgen.sh - ./test_bindings.sh - test-scaffolding-go: runs-on: ubuntu-latest container: image: ghcr.io/nordsecurity/uniffi-bindgen-cpp-test-runner:v0.3.0 - needs: build-scaffolding + needs: build-scaffolding-lib steps: - uses: actions/checkout@v3 with: @@ -71,7 +106,7 @@ jobs: runs-on: ubuntu-latest container: image: ghcr.io/nordsecurity/uniffi-bindgen-cs-test-runner:v0.1.0 - needs: build-scaffolding + needs: build-scaffolding-lib steps: - uses: actions/checkout@v3 with: diff --git a/build_bindgen.sh b/build_bindgen.sh deleted file mode 100755 index 1fa5027..0000000 --- a/build_bindgen.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -set -euxo pipefail - -cargo build --release --package uniffi-bindgen-cpp diff --git a/build_scaffolding.sh b/build_scaffolding_lib.sh similarity index 71% rename from build_scaffolding.sh rename to build_scaffolding_lib.sh index c58f93d..68f010a 100755 --- a/build_scaffolding.sh +++ b/build_scaffolding_lib.sh @@ -1,6 +1,8 @@ #!/bin/bash set -euxo pipefail +cargo build --release --package uniffi-bindgen-cpp + mkdir -p cpp-tests/build cd cpp-tests/build cmake -DCMAKE_BUILD_TYPE=Debug .. diff --git a/fixtures/Cargo.toml b/fixtures/Cargo.toml index 097ed1d..958d813 100644 --- a/fixtures/Cargo.toml +++ b/fixtures/Cargo.toml @@ -4,7 +4,7 @@ version = "1.0.0" edition = "2021" [lib] -crate-type = ["cdylib", "lib"] +crate-type = ["cdylib", "lib", "staticlib"] [dependencies] uniffi-example-arithmetic = { git = "https://github.com/NordSecurity/uniffi-rs.git", tag = "v0.3.1+v0.25.0" } From 719fa1125455d033a801e49c39df265c3106c855 Mon Sep 17 00:00:00 2001 From: Martynas Gurskas Date: Fri, 22 Mar 2024 13:15:09 +0200 Subject: [PATCH 6/7] Add macos-13 to the bulid matrix Signed-off-by: Martynas Gurskas --- .github/workflows/cpp.yml | 12 ++++++------ cpp-tests/CMakeLists.txt | 5 ++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/.github/workflows/cpp.yml b/.github/workflows/cpp.yml index 4c001a0..0a7cd24 100644 --- a/.github/workflows/cpp.yml +++ b/.github/workflows/cpp.yml @@ -13,22 +13,21 @@ jobs: run-tests: runs-on: ${{ matrix.os }} strategy: - # TODO: remove this when the workflow is stable - fail-fast: false - matrix: # Ubuntu-20.04 is being used here on purpose instead of ubuntu-latest due to a bug with clang and libstdc++ # https://github.com/actions/runner-images/issues/8659 - os: [ubuntu-20.04, windows-latest] + os: [ubuntu-20.04, windows-latest, macos-13] build_type: [Release] cpp_compiler: [g++, clang++, cl] exclude: - - os: windows-latest - cpp_compiler: g++ - os: windows-latest cpp_compiler: clang++ + - os: windows-latest + cpp_compiler: g++ - os: ubuntu-20.04 cpp_compiler: cl + - os: macos-13 + cpp_compiler: cl steps: - uses: actions/checkout@v3 @@ -53,6 +52,7 @@ jobs: -DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }} -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -S ${{ steps.strings.outputs.build-source-dir }} + - name: Build run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }} diff --git a/cpp-tests/CMakeLists.txt b/cpp-tests/CMakeLists.txt index effd992..6f3ccd3 100644 --- a/cpp-tests/CMakeLists.txt +++ b/cpp-tests/CMakeLists.txt @@ -15,9 +15,12 @@ set(BINDINGS_SRC_DIR ${BINDINGS_BUILD_DIR}/bindings) if (WIN32) set(UNIFFI_FIXTURES_LIB uniffi_bindgen_cpp_fixtures.dll) set(ADDITIONAL_LIBS ws2_32 userenv advapi32 ntdll crypt32 Bcrypt) -else() +elseif(UNIX AND NOT APPLE) set(UNIFFI_FIXTURES_LIB libuniffi_bindgen_cpp_fixtures.so) set(ADDITIONAL_LIBS) +else() + set(UNIFFI_FIXTURES_LIB libuniffi_bindgen_cpp_fixtures.dylib) + set(ADDITIONAL_LIBS) endif() find_program(VALGRIND "valgrind") From e6232f805f046451546197975c6e6b46ac64e3a7 Mon Sep 17 00:00:00 2001 From: Martynas Gurskas Date: Fri, 22 Mar 2024 16:40:43 +0200 Subject: [PATCH 7/7] Update changelog and docs Signed-off-by: Martynas Gurskas --- CHANGELOG.md | 7 +++++++ README.md | 6 +++--- docs/SCAFFOLDING.md | 34 +++++++++++++++++----------------- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74d7e62..18df8b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +#### v0.5.0+v0.25.0 + +---- + +- Core: **POTENTIALLY BREAKING** changed `timestamp` type from `time_point`, to `time_point` + + #### v0.4.2+v0.25.0 ---- diff --git a/README.md b/README.md index 8654bda..7557de9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# uniffi-bindgen-cpp - UniFFI C++ bindings generator +# uniffi-bindgen-cpp - UniFFI C++ bindings/scaffolding generator Generate [UniFFI](https://github.com/mozilla/uniffi-rs) bindings for C++. `uniffi-bindgen-cpp` lives as a separate project from `uniffi-rs`, as per @@ -56,9 +56,9 @@ Note: configuration is not supported when using library mode uniffi-bindgen-cpp path/to/definitions.udl --config path/to/uniffi.toml ``` -# EXPERIMENTAL: C++ Scaffolding +# C++ Scaffolding -It is possible to generate experimental C++ uniffi scaffolding that allows bridging C++ code with any other uniffi supported language (except Rust). +It is possible to generate C++ uniffi scaffolding that allows bridging C++ code with any other uniffi supported language (except Rust). More documentation and limitations can be found in the [scaffolding documentation](docs/SCAFFOLDING.md) # Versioning diff --git a/docs/SCAFFOLDING.md b/docs/SCAFFOLDING.md index c2284a0..6c79b83 100644 --- a/docs/SCAFFOLDING.md +++ b/docs/SCAFFOLDING.md @@ -80,23 +80,23 @@ Additionally, for examples of how to use the C++ scaffolding, refer to the [C++ ## UDL to C++ type mapping table -| UDL Type | C++ Type | -|--------------------------------|------------------------------------------------------| -| `i8/i16/i32/i64` | `int8_t/int16_t/int32_t/int64_t` | -| `u8/u16/u32/u64` | `uint8_t/uint16_t/uint32_t/uint64_t` | -| `f32/float` | `float` | -| `f64/double` | `double` | -| `boolean` | `bool` | -| `string` | `std::string` | -| `bytes` | `std::vector` | -| `timestamp` | `std::chrono::time_point` | -| `duration` | `std::chrono::duration` | -| `T?` | `std::optional` | -| `sequence` | `std::vector` | -| `record` | `std::unordered_map` | -| `enum` | `enum class` | -| `dictionary` | `struct/class` | -| `interface` | `std::shared_ptr` | +| UDL Type | C++ Type | +|--------------------------------|--------------------------------------------------------------------------------| +| `i8/i16/i32/i64` | `int8_t/int16_t/int32_t/int64_t` | +| `u8/u16/u32/u64` | `uint8_t/uint16_t/uint32_t/uint64_t` | +| `f32/float` | `float` | +| `f64/double` | `double` | +| `boolean` | `bool` | +| `string` | `std::string` | +| `bytes` | `std::vector` | +| `timestamp` | `std::chrono::time_point` | +| `duration` | `std::chrono::duration` | +| `T?` | `std::optional` | +| `sequence` | `std::vector` | +| `record` | `std::unordered_map` | +| `enum` | `enum class` | +| `dictionary` | `struct/class` | +| `interface` | `std::shared_ptr` | ## Unsupported features