ddnet/cmake/FindRust.cmake
heinrich5991 dcd76fd3e1 Add support for Rust code in DDNet
The glue is done using the [cxx crate](https://cxx.rs/) on the Rust
side.

As a proof-of-concept, only a small console command (`rust_version`)
printing the currently used Rust version was added.

You can generate and open the Rust documentation using
`DDNET_TEST_NO_LINK=1 cargo doc --open`.

You can run the Rust tests using `cmake --build <build dir> --target
run_rust_tests`, they're automatically included in the `run_tests`
target as well.

Rust tests don't work on Windows in debug mode on Windows because Rust
cannot currently link with the debug version of the C stdlib on Windows:
https://github.com/rust-lang/rust/issues/39016.

---

The stuff in `src/rust-bridge` is generated using
```
cxxbridge src/engine/shared/rust_version.rs --output src/rust-bridge/engine/shared/rust_version.cpp --output src/rust-bridge/engine/shared/rust_version.h
cxxbridge src/engine/console.rs --output src/rust-bridge/cpp/console.cpp --output src/rust-bridge/cpp/console.h
```
2022-10-19 23:46:06 +02:00

30 lines
937 B
CMake

find_program(RUST_RUSTC rustc)
find_program(RUST_CARGO cargo)
if(RUST_RUSTC)
execute_process(COMMAND ${RUST_RUSTC} --version --verbose OUTPUT_VARIABLE RUSTC_VERSION_OUTPUT)
string(REPLACE "\n" ";" RUSTC_VERSION_OUTPUT "${RUSTC_VERSION_OUTPUT}")
set(RUST_NIGHTLY OFF)
foreach(line ${RUSTC_VERSION_OUTPUT})
if(NOT DEFINED RUST_VERSION_STRING)
set(RUST_VERSION_STRING ${line})
endif()
if(line MATCHES "^([^:]+): (.*)$")
set(KEY ${CMAKE_MATCH_1})
set(VALUE ${CMAKE_MATCH_2})
if(KEY STREQUAL "release")
set(RUST_VERSION ${VALUE})
if(VALUE MATCHES "nightly")
set(RUST_NIGHTLY ON)
endif()
elseif(KEY STREQUAL "host")
set(RUST_TARGET_HOST ${VALUE})
endif()
endif()
endforeach()
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Rust DEFAULT_MSG RUST_RUSTC RUST_CARGO)
mark_as_advanced(RUST_RUSTC RUST_CARGO)