Skip to content

Commit

Permalink
Add support for Rust code in DDNet
Browse files Browse the repository at this point in the history
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:
rust-lang/rust#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
```
  • Loading branch information
heinrich5991 committed Oct 19, 2022
1 parent 1b114b2 commit e093e94
Show file tree
Hide file tree
Showing 48 changed files with 1,916 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.'cfg(target_env = "msvc")']
rustflags = ["-C", "target-feature=+crt-static"]
21 changes: 6 additions & 15 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ jobs:
cmake-path: /usr/bin/
cmake-args: -G Ninja -DTEST_MYSQL=ON
cmake-init-env: CXXFLAGS=-Werror
gtest-env: GTEST_FILTER=-*SQLite*
package-file: "*-linux_x86_64.tar.xz"
fancy: false
env:
GTEST_FILTER: -*SQLite*
- os: macOS-latest
cmake-args: -G Ninja
cmake-init-env: CXXFLAGS=-Werror
Expand All @@ -55,6 +54,7 @@ jobs:
- name: Prepare Linux (non-fancy)
if: ${{ contains(matrix.os, 'ubuntu') && !matrix.fancy }}
run: |
rustup default 1.48.0
sudo rm -rf /var/lib/mysql/ /var/run/mysqld
sudo mkdir /var/lib/mysql/ /var/run/mysqld/
sudo chown mysql:mysql /var/lib/mysql/ /var/run/mysqld/
Expand Down Expand Up @@ -93,48 +93,41 @@ jobs:
sudo rm -rf /Library/Developer/CommandLineTools
- name: Build in debug mode
env: ${{ matrix.env }}
run: |
mkdir debug
cd debug
${{ matrix.cmake-path }}cmake -E env ${{ matrix.cmake-init-env }} ${{ matrix.cmake-path }}cmake ${{ matrix.cmake-args }} -DCMAKE_BUILD_TYPE=Debug -Werror=dev -DDOWNLOAD_GTEST=ON -DDEV=ON -DCMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG=. ..
${{ matrix.cmake-path }}cmake --build . --config Debug --target everything ${{ matrix.build-args }}
- name: Test debug
env: ${{ matrix.env }}
run: |
cd debug
${{ matrix.cmake-path }}cmake --build . --config Debug --target run_tests ${{ matrix.build-args }}
${{ matrix.cmake-path }}cmake -E env ${{ matrix.gtest-env }} ${{ matrix.cmake-path }}cmake --build . --config Debug --target run_tests ${{ matrix.build-args }}
- name: Run debug server
env: ${{ matrix.env }}
run: |
cd debug
./DDNet-Server shutdown
- name: Build in release mode
env: ${{ matrix.env }}
run: |
mkdir release
cd release
${{ matrix.cmake-path }}cmake -E env ${{ matrix.cmake-init-env }} ${{ matrix.cmake-path }}cmake ${{ matrix.cmake-args }} -DCMAKE_BUILD_TYPE=Release -Werror=dev -DDOWNLOAD_GTEST=ON -DCMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE=. ..
${{ matrix.cmake-path }}cmake --build . --config Release --target everything ${{ matrix.build-args }}
- name: Test release
env: ${{ matrix.env }}
run: |
cd release
${{ matrix.cmake-path }}cmake --build . --config Release --target run_tests ${{ matrix.build-args }}
${{ matrix.cmake-path }}cmake -E env ${{ matrix.gtest-env }} ${{ matrix.cmake-path }}cmake --build . --config Release --target run_tests ${{ matrix.build-args }}
- name: Run release server
env: ${{ matrix.env }}
run: |
cd release
./DDNet-Server shutdown
- name: Build headless client
if: contains(matrix.os, 'ubuntu-latest')
env: ${{ matrix.env }}
run: |
mkdir headless
cd headless
Expand Down Expand Up @@ -178,7 +171,6 @@ jobs:

- name: Build in release mode with debug info and all features on
if: matrix.fancy
env: ${{ matrix.env }}
run: |
mkdir fancy
cd fancy
Expand All @@ -187,14 +179,13 @@ jobs:
- name: Test fancy
if: matrix.fancy
env: ${{ matrix.env }}
run: |
find /usr/lib/ -name '*libwebsockets*'
cd fancy
${{ matrix.cmake-path }}cmake --build . --config RelWithDebInfo --target run_tests ${{ matrix.build-args }}
${{ matrix.cmake-path }}cmake -E env ${{ matrix.gtest-env }} ${{ matrix.cmake-path }}cmake --build . --config RelWithDebInfo --target run_tests ${{ matrix.build-args }}
- name: Run fancy server
if: matrix.fancy
env: ${{ matrix.env }}
run: |
cd fancy
./DDNet-Server shutdown
Expand Down
42 changes: 42 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Check Rust

on:
push:
branches-ignore:
- staging.tmp
- trying.tmp
- staging-squash-merge.tmp
pull_request:

jobs:
rustdoc:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run Rustdoc
run: |
RUSTDOCFLAGS=-Dwarnings DDNET_TEST_NO_LINK=1 cargo doc
rustfmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run Rustfmt
run:
cargo fmt -- --check

cargo-deny:
runs-on: ubuntu-latest
strategy:
matrix:
checks:
- advisories
- bans licenses sources

continue-on-error: ${{ matrix.checks == 'advisories' }}

steps:
- uses: actions/checkout@v2
- uses: EmbarkStudios/cargo-deny-action@v1
with:
command: check ${{ matrix.checks }}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ bundle/
.DS_Store
.ninja_deps
.ninja_log
CACHEDIR.TAG
CMakeCache.txt
CMakeFiles
CMakeSettings*
Expand All @@ -20,10 +21,12 @@ CTestTestfile.cmake
Debug
Makefile
Release
SAN.*
_CPack_Packages/
build.ninja
checksummed_*
cmake_install.cmake
debug
gmock.pc
gmock_main.pc
googletest-build/
Expand All @@ -34,8 +37,8 @@ gtest_main.pc
install_manifest*.txt
ninja_package
pack_*/
release
rules.ninja
SAN.*
testrunner\[1\]_include.cmake
vulkan_shaders_sha256.txt

Expand Down
Loading

0 comments on commit e093e94

Please sign in to comment.