diff --git a/cpp/justfile b/cpp/justfile index a12b5a6b22e..7e38358f1da 100644 --- a/cpp/justfile +++ b/cpp/justfile @@ -13,7 +13,8 @@ default: # on Unix; Windows uses MSVC. # Build and install cpp/moq, then build the probe against the installed package at C++17 and -# C++23 through CMake, and at C++17 through pkg-config, and run each. Also compiles the doc samples. +# C++23 through CMake (C++23 also the documented in-project way), and at C++17 through +# pkg-config, and run each. Also compiles the doc samples. check $FILES="": #!/usr/bin/env bash set -euo pipefail @@ -54,6 +55,12 @@ check $FILES="": run "$build/test-$std" done + # The setup doc/lib/cpp documents: the standard set in the project, and also required on + # the app's own target. + cmake -S moq/test -B "$build/test-target-23" -DCMAKE_BUILD_TYPE=Debug -DPROBE_CXX_STANDARD=23 -DCMAKE_PREFIX_PATH="$prefix" + cmake --build "$build/test-target-23" --config Release + run "$build/test-target-23" + # The samples in doc/lib/cpp compile against the installed headers. bash ../doc/lib/samples.sh cpp ../doc/lib/cpp/index.md >"$build/doc-samples.cpp" if [[ "$(uname -s)" != MINGW* && "$(uname -s)" != MSYS* ]]; then diff --git a/cpp/moq/CMakeLists.txt b/cpp/moq/CMakeLists.txt index ae9209d71dc..88e68390127 100644 --- a/cpp/moq/CMakeLists.txt +++ b/cpp/moq/CMakeLists.txt @@ -73,7 +73,9 @@ foreach(_line IN LISTS _lines) endif() endforeach() -# The bindings compile here with the including project's standard and flags; see src/moq.cpp. +# The bindings compile here at the including project's CMAKE_CXX_STANDARD (else the compiler's +# default, at least C++17) and with its flags; see src/moq.cpp. A standard raised only on a +# consumer's own target does not reach this library. # Named moq-cpp so an in-tree build can sit beside libmoq's `moq` target. add_library(moq-cpp STATIC src/moq.cpp) add_library(moq::cpp ALIAS moq-cpp) diff --git a/cpp/moq/README.md b/cpp/moq/README.md index 28fb95498b9..598ed7e2c2a 100644 --- a/cpp/moq/README.md +++ b/cpp/moq/README.md @@ -33,6 +33,10 @@ Every generated type lives in `namespace moq`. Objects are `std::shared_ptr`; re - `moq::Error` is a value holding a `std::variant` of its cases, and `to_string()` gives Rust's `Display` message. Nothing throws. A Rust panic or a misused future aborts with a message on stderr. - Futures are polled and continuations run on one process-wide executor thread unless the application installs its own with `moq::set_executor` before the first async call. `moq::shutdown()` stops the moq-ffi runtime thread (`moq_ffi_shutdown`), then the executor, before unloading the code a continuation could call into. +## C++ standard + +The installed package compiles `moq.cpp` inside the consumer's project, at its `CMAKE_CXX_STANDARD` (else the compiler's default, at least C++17). To use C++23, set `CMAKE_CXX_STANDARD 23` before `find_package(moq-cpp)`, or pass `-DCMAKE_CXX_STANDARD=23`. `target_compile_features(app PRIVATE cxx_std_23)` alone does not reach the separate `moq-cpp` library, so `` and the bindings pick different `expected` types and the link fails on `moq_abi_std_expected` or `moq_abi_tl_expected`. `just cpp check` builds the probe at C++23 both ways. + ## Cancellation Cancelling a future (`cancel()`, destroying it, destroying the `moq::Continuation` returned by `then`, or destroying a coroutine suspended on it) drops the Rust future. Native moq-ffi runs each async call as a spawned task that holds an `AbortOnDrop` on it (`rs/moq-ffi/src/ffi.rs`), so dropping the future aborts the work at its next await point instead of letting it finish unobserved. The continuation of a cancelled future never runs, and `get()` on it aborts. diff --git a/cpp/moq/cmake/moq-cpp-config.cmake.in b/cpp/moq/cmake/moq-cpp-config.cmake.in index d4ed0477fbf..e49d90440af 100644 --- a/cpp/moq/cmake/moq-cpp-config.cmake.in +++ b/cpp/moq/cmake/moq-cpp-config.cmake.in @@ -14,8 +14,10 @@ if(NOT TARGET moq::cpp) INTERFACE_LINK_LIBRARIES "@MOQ_NATIVE_LIBS@;Threads::Threads" ) - # The generated bindings compile here, in the consumer's project, so they see the same - # standard and flags as the code that includes . + # The generated bindings compile here, in the consumer's project, at its CMAKE_CXX_STANDARD + # (else the compiler's default, at least C++17) and with its flags. A standard raised only + # on the consumer's own target does not reach this library, so and the + # bindings disagree on uniffi::expected and the link fails: set CMAKE_CXX_STANDARD instead. add_library(moq-cpp STATIC "@PACKAGE_CMAKE_INSTALL_DATADIR@/moq-cpp/moq.cpp") target_compile_features(moq-cpp PUBLIC cxx_std_17) target_include_directories(moq-cpp PUBLIC "@PACKAGE_CMAKE_INSTALL_INCLUDEDIR@") diff --git a/cpp/moq/test/CMakeLists.txt b/cpp/moq/test/CMakeLists.txt index c7c7d16e9de..440d6e93b0b 100644 --- a/cpp/moq/test/CMakeLists.txt +++ b/cpp/moq/test/CMakeLists.txt @@ -1,8 +1,13 @@ # A consumer of the installed package: `just cpp check` installs cpp/moq to a prefix, then -# builds this with find_package(moq-cpp) at C++17 and C++23. +# builds this with find_package(moq-cpp) at C++17 and C++23, the standard given as +# -DCMAKE_CXX_STANDARD or as -DPROBE_CXX_STANDARD, which sets it in the project and also +# requires it on the app's own target, the way a consumer following doc/lib/cpp would. cmake_minimum_required(VERSION 3.21) project(moq-probe LANGUAGES CXX) +if(PROBE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD ${PROBE_CXX_STANDARD}) +endif() if(NOT CMAKE_CXX_STANDARD) set(CMAKE_CXX_STANDARD 17) endif() @@ -22,3 +27,6 @@ find_package(moq-cpp REQUIRED) add_executable(probe probe.cpp) target_link_libraries(probe PRIVATE moq::cpp) +if(PROBE_CXX_STANDARD) + target_compile_features(probe PRIVATE cxx_std_${PROBE_CXX_STANDARD}) +endif() diff --git a/doc/lib/cpp/index.md b/doc/lib/cpp/index.md index 807392c4a7d..d258337f706 100644 --- a/doc/lib/cpp/index.md +++ b/doc/lib/cpp/index.md @@ -25,6 +25,7 @@ and `moq-cpp.pc`. Targets: Linux x86\_64 and aarch64, macOS arm64, Windows x64. No Rust toolchain is needed to consume it. ```cmake ignore +set(CMAKE_CXX_STANDARD 23) # optional; see "C++ standard" below find_package(moq-cpp REQUIRED) # with CMAKE_PREFIX_PATH at the unpacked archive target_link_libraries(app PRIVATE moq::cpp) ``` @@ -36,10 +37,20 @@ c++ -std=c++17 app.cpp $(pkg-config --variable=sources moq-cpp) $(pkg-config --c The generated bindings ship as source (`share/moq-cpp/moq.cpp`) and compile inside your build, because `uniffi::expected` is `std::expected` or a bundled -`tl::expected` depending on the standard. Compile it with the same standard -as the code that includes ``; `find_package` does this for you, -and a mismatch fails to link rather than corrupting memory. On MSVC, link the -release runtime (`/MD`), which the Rust library uses in every configuration. +`tl::expected` depending on the standard. On MSVC, link the release runtime +(`/MD`), which the Rust library uses in every configuration. + +### C++ standard + +The bindings compile at your project's `CMAKE_CXX_STANDARD`, or the compiler's +default (at least C++17) when it is unset, and must match the code that +includes ``. To use C++23, set it before `find_package`, or pass +`-DCMAKE_CXX_STANDARD=23`. Raising the standard only on your own target +(`target_compile_features(app PRIVATE cxx_std_23)`) does not reach the +bindings, so the two disagree on `moq::expected` and the link fails with an +undefined `moq_abi_std_expected` or `moq_abi_tl_expected` symbol rather than +corrupting memory. With pkg-config, pass the same `-std` to the bindings +source and to your code. From source, `add_subdirectory(cpp/moq)` in a checkout builds `moq-ffi` with cargo and renders the bindings with the pinned `uniffi-bindgen-cpp` (see diff --git a/quest/m1/cpp/README.md b/quest/m1/cpp/README.md index 6722b039c8f..40f9e798698 100644 --- a/quest/m1/cpp/README.md +++ b/quest/m1/cpp/README.md @@ -69,7 +69,6 @@ Confirmed in [#4100](https://github.com/moq-dev/moq/pull/4100): - [Client settings parity](/quest/m1/cpp/client-config.md) - moq-ffi offers libmoq's client knobs, and the OBS advanced settings get back the ones the migration dropped - [Session report parity](/quest/m1/cpp/session-report.md) - a session reports its negotiated draft and reconnect failures, so the OBS dock shows them again - [Cancel](/quest/m1/cpp/cancel.md) - a cancelled or consumed future reports `valid() == false`, like `std::future`, and a read of it aborts with a message naming the misuse -- [C++ standard](/quest/m1/cpp/cxx-standard.md) - a consumer that sets C++23 only on its own target links the package ## Related diff --git a/quest/m1/cpp/cxx-standard.md b/quest/m1/cpp/cxx-standard.md deleted file mode 100644 index e051fadd9f9..00000000000 --- a/quest/m1/cpp/cxx-standard.md +++ /dev/null @@ -1,29 +0,0 @@ -# [XS] A C++23 consumer links the package - -## Goal - -A consumer that raises the standard only on its own target -(`target_compile_features(app PRIVATE cxx_std_23)`) either links against the -`moq-cpp` package or is told clearly how to configure it. Today it gets link -errors: that requirement does not flow backward into the separate `moq-cpp` -static library, which compiles `moq.cpp` at `cxx_std_17` and so picks -`tl::expected` while the app's `` picks `std::expected`. The ABI -guard turning that into a link error is correct; the surprise is not. Codex -found it on [#4187](https://github.com/moq-dev/moq/pull/4187). - -## Plan - -- Fix the misleading comment in `cpp/moq/cmake/moq-cpp-config.cmake.in` - (and the matching one in `cpp/moq/CMakeLists.txt`): the bindings compile - with the project's standard (`CMAKE_CXX_STANDARD`) or C++17, not with - whatever the including target asks for. -- Then choose the smallest fix that makes the C++23 path work: document - `CMAKE_CXX_STANDARD` (what the probe does) in `cpp/moq/README.md` and the - C++ docs page, or add a package option that sets the standard `moq-cpp` - compiles with. Prefer documentation unless a consumer (OBS, Unreal) needs - the option. -- Test the documented configuration: the package test already builds at - several standards; add one that builds the app at C++23 the documented - way. - -Public API: none, or one CMake option. Wire: none.