Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add Rust CI and update cssparser dep and lock #3175

Merged
merged 5 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/changed-files.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
linux:
- 'src/**'
- 'rustutils/**'
- 'test/**'
- 'render-test/**'
- 'expression-test/**'
Expand All @@ -12,6 +13,7 @@ linux:
- 'BUILD.bazel'
- '.bazelrc'
- '.bazelversion'
- '!**/*.md'
windows:
- '.github/workflows/windows-ci.yml'
- 'src/**'
Expand All @@ -24,11 +26,11 @@ windows:
- 'metrics/**'
- 'vendor/**'
- '.gitmodules'
- '!**/*.md'
- 'WORKSPACE'
- 'BUILD.bazel'
- '.bazelrc'
- '.bazelversion'
- '!**/*.md'
ios:
- 'platform/ios/**'
- 'platform/darwin/**'
Expand All @@ -44,12 +46,12 @@ ios:
- 'test/**'
- 'vendor/**'
- '.gitmodules'
- '!**/*.md'
- 'WORKSPACE'
- 'BUILD.bazel'
- '.bazelrc'
- '.bazelversion'
- 'pnpm-lock.yaml'
- '!**/*.md'
android:
- 'CMakeLists.txt'
- 'platform/android/**'
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/rust-ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: rust-ci

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:

defaults:
run:
shell: bash
working-directory: rustutils/

jobs:
test:
name: Rust tests
runs-on: ubuntu-latest
steps:
- uses: taiki-e/install-action@v2
with: { tool: just }
- uses: actions/checkout@v4
- run: just -v ci-test

msrv:
name: Rust tests MSRV (Minimal Supported Rust Version)
runs-on: ubuntu-latest
steps:
- uses: taiki-e/install-action@v2
with: { tool: just }
- uses: actions/checkout@v4
- name: Read crate metadata
id: metadata
run: echo "rust-version=$(sed -ne 's/rust-version *= *\"\(.*\)\"/\1/p' Cargo.toml)" >> "$GITHUB_OUTPUT"
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ steps.metadata.outputs.rust-version }}
- run: just ci-test-msrv
23 changes: 16 additions & 7 deletions docs/mdbook/src/rust.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Rust

We have added experimental support for intergrating Rust code into the source tree.
We have added experimental support for integrating Rust code into the source tree.

## Rust Bridge

Expand All @@ -16,15 +16,15 @@ When building with CMake, need to have the correct Rust toolchain(s) installed.

You can use `rustup` to manage toolchains. Which toolchain you needs depends on your host platform and for what platform you are trying to build. If your host and target platform are the same, you probably have the correct toolchain installed after installing Rust. For example when building for **Android** and building on a **x84 Linux** host you would use the following command:

```
```shell
rustup target add --toolchain stable-x86_64-unknown-linux-gnu aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android
```

See [Platform Support](https://doc.rust-lang.org/nightly/rustc/platform-support.html) in the Rust documentation for more details. You will get a descriptive error message when the correct toolchain is not available, so we don't list all possible combinations here.

You also need to have cxxbridge installed:

```
```shell
cargo install cxxbridge-cmd
```

Expand All @@ -36,26 +36,35 @@ Pass the `--//:use_rust` flag to Bazel commands.

Note that when [generating an Xcode project](./ios/README.md) you should not pass this option to Bazel directly, but as follows:

```
```shell
bazel run //platform/ios:xcodeproj --@rules_xcodeproj//xcodeproj:extra_common_flags="--//:renderer=metal --//:use_rust"
```

## Just

For the Rust subproject, we suggest installing [just](https://github.com/casey/just#readme), a modern alternative to `make`. The `justfile` in the root directory contains a number of useful commands for building and testing the Rust code. The same commands can be run directly, but `just` provides a more convenient interface.

* Install `just` with `cargo install just`
* Run `just` in the `/rustutils` dir to see a list of available commands
* Some common commands: `just check`, `just test`, `just fmt`
* To run the same steps as used by CI, run `just ci-test`

## Creating a new Module

To create a new module:

1. Add a new source file to `rustutils/src/example.rs`.
2. Implement it, see the [CXX documentation](https://cxx.rs/index.html) or see `rustutils/src/color.rs` for an example.
3. Create a C++ source file that will use the generated C++ header. See `src/mbgl/util/color.rs.cpp` for an example. Import the generated header with
```
```cpp
#include <rustutils/example.hpp>
```
4. Conditionally include either the `*.rs.cpp` file or the `*.cpp` file it replaces in CMake and Bazel. Here is what it looks like for CMake:
```
```cmake
${PROJECT_SOURCE_DIR}/src/mbgl/util/color$<IF:$<BOOL:${MLN_USE_RUST}>,.rs.cpp,.cpp>
```
And here for Bazel:
```
```bazel
select({
"//:rust": [
"src/mbgl/util/color.rs.cpp",
Expand Down
Loading
Loading