danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 1 | # Rust in Chromium |
| 2 | |
| 3 | [TOC] |
| 4 | |
| 5 | # Why? |
| 6 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 7 | Handling untrustworthy data in non-trivial ways is a major source of security |
| 8 | bugs, and it's therefore against Chromium's security policies |
| 9 | [to do it in the Browser or Gpu process](../docs/security/rule-of-2.md) unless |
| 10 | you are working in a memory-safe language. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 11 | |
| 12 | Rust provides a cross-platform memory-safe language so that all platforms can |
| 13 | handle untrustworthy data directly from a privileged process, without the |
| 14 | performance overheads and complexity of a utility process. |
| 15 | |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 16 | # Status |
| 17 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 18 | The Rust toolchain is enabled for and supports all platforms and development |
| 19 | environments that are supported by the Chromium project. The first milestone |
| 20 | to include full production-ready support was M119. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 21 | |
David Adrian | d891869 | 2024-12-12 22:02:50 | [diff] [blame^] | 22 | Rust can be used anywhere in the Chromium repository (not just `//third_party`) |
| 23 | subject to [current interop capabilities][interop-rust-doc], however it is |
| 24 | currently subject to a internal approval and FYI process. Googlers can view |
| 25 | go/chrome-rust for details. New usages of Rust are documented at |
| 26 | [`[email protected]`](https://groups.google.com/a/chromium.org/g/rust-fyi). |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 27 | |
| 28 | For questions or help, reach out to `[email protected]` or `#rust` on the |
| 29 | [Chromium Slack](https://www.chromium.org/developers/slack/). |
| 30 | |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 31 | If you use VSCode, we have [additional advice below](#using-vscode). |
| 32 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 33 | # Adding a third-party Rust library |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 34 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 35 | Third-party libraries are pulled from [crates.io](https://crates.io), but |
| 36 | Chromium does not use Cargo as a build system. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 37 | |
| 38 | ## Third-party review |
| 39 | |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 40 | All third-party crates need to go through third-party review. See |
| 41 | [//docs/adding_to_third_party.md](adding_to_third_party.md) for instructions on |
| 42 | how to have a library reviewed. |
| 43 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 44 | ## Importing a crate from crates.io |
| 45 | |
danakj | 0ec93d1 | 2023-11-17 16:12:23 | [diff] [blame] | 46 | The `//third_party/rust/chromium_crates_io/Cargo.toml` file defines the set of crates |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 47 | depended on from first-party code. Any transitive dependencies will be found |
danakj | 0ec93d1 | 2023-11-17 16:12:23 | [diff] [blame] | 48 | from those listed there. The file is a [standard `Cargo.toml` file]( |
| 49 | https://doc.rust-lang.org/cargo/reference/manifest.html), though the crate |
| 50 | itself is never built, it is only used to collect dependencies through the |
| 51 | `[dependencies]` section. |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 52 | |
danakj | 0ec93d1 | 2023-11-17 16:12:23 | [diff] [blame] | 53 | To use a third-party crate "bar" version 3 from first party code: |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 54 | 1. Change directory to the root `src/` dir of Chromium. |
| 55 | 1. Add the crate to `//third_party/rust/chromium_crates_io/Cargo.toml`: |
| 56 | * `vpython3 ./tools/crates/run_gnrt.py add foo` to add the latest version of `foo`. |
| 57 | * `vpython3 ./tools/crates/run_gnrt.py add [email protected]` to add a specific version of `foo`. |
| 58 | * Or, directly through (nightly) cargo: |
| 59 | `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt add foo` |
| 60 | * Or, edit the Cargo.toml by hand, finding the version you want from [crates.io](https://crates.io). |
| 61 | 1. Download the crate's files: |
| 62 | * `./tools/crates/run_gnrt.py vendor` to download the new crate. |
| 63 | * Or, directly through (nightly) cargo: |
danakj | 0ec93d1 | 2023-11-17 16:12:23 | [diff] [blame] | 64 | `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt vendor` |
danakj | f509caa | 2023-11-23 15:29:43 | [diff] [blame] | 65 | * This will also apply any patches in `//third_party/rust/chromium_crates_io/patches` |
| 66 | for the crates. If a patch can not apply, the crate's download will be cancelled and |
| 67 | an error will be printed. See [patching errors](#patching-errors) below for how to resolve |
| 68 | this. |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 69 | 1. (optional) If the crate is only to be used by tests and tooling, then |
| 70 | specify the `"test"` group in `//third_party/rust/chromium_crates_io/gnrt_config.toml`: |
| 71 | ``` |
| 72 | [crate.foo] |
| 73 | group = "test" |
| 74 | ``` |
| 75 | 1. Generate the `BUILD.gn` file for the new crate: |
| 76 | * `vpython3 ./tools/crates/run_gnrt.py gen` |
| 77 | * Or, directly through (nightly) cargo: |
| 78 | `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt gen` |
Lukasz Anforowicz | 652bf4a | 2024-03-20 16:48:29 | [diff] [blame] | 79 | 1. Verify if all new dependencies are already audited by running `cargo vet` |
Lukasz Anforowicz | df3d5af | 2024-03-20 18:21:16 | [diff] [blame] | 80 | See [`rust-unsafe.md#cargo-vet-policy`](rust-unsafe.md#cargo-vet-policy) for |
Lukasz Anforowicz | 652bf4a | 2024-03-20 16:48:29 | [diff] [blame] | 81 | more details. This boils down to: |
Lukasz Anforowicz | 11c3487 | 2024-01-25 17:15:46 | [diff] [blame] | 82 | * `./tools/crates/run_cargo_vet.py check` |
danakj | aed1daf | 2024-01-05 17:44:32 | [diff] [blame] | 83 | * If `check` fails, then there are missing audits, which need to be added to |
| 84 | `//third_party/rust/chromium_crates_io/supply-chain/audits.toml`. |
Lukasz Anforowicz | 057876d | 2024-06-05 19:07:58 | [diff] [blame] | 85 | 1. Add the new files to git: |
| 86 | * `git add -f third_party/rust/chromium_crates_io/vendor`. |
| 87 | (The `-f` is important, as files may be skipped otherwise from a |
| 88 | `.gitignore` inside the crate.) |
| 89 | * `git add third_party/rust` |
danakj | aed1daf | 2024-01-05 17:44:32 | [diff] [blame] | 90 | 1. Upload the CL. If there is any `unsafe` usage then Security experts will need to |
Lukasz Anforowicz | 652bf4a | 2024-03-20 16:48:29 | [diff] [blame] | 91 | audit the "ub-risk" level. See |
Lukasz Anforowicz | df3d5af | 2024-03-20 18:21:16 | [diff] [blame] | 92 | [`rust-unsafe.md#code-review-policy`](rust-unsafe.md#code-review-policy) for |
Lukasz Anforowicz | 652bf4a | 2024-03-20 16:48:29 | [diff] [blame] | 93 | more details. |
danakj | 0ec93d1 | 2023-11-17 16:12:23 | [diff] [blame] | 94 | |
| 95 | ### Cargo features |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 96 | |
| 97 | To enable a feature "spaceships" in the crate, change the entry in |
danakj | 0ec93d1 | 2023-11-17 16:12:23 | [diff] [blame] | 98 | `//third_party/rust/chromium_crates_io/Cargo.toml` to include the feature: |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 99 | ```toml |
| 100 | [dependencies] |
| 101 | bar = { version = "3", features = [ "spaceships" ] } |
| 102 | ``` |
| 103 | |
danakj | f509caa | 2023-11-23 15:29:43 | [diff] [blame] | 104 | ### Patching third-party crates |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 105 | |
| 106 | You may patch a crate in tree, but save any changes made into a diff file in |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 107 | a `//third_party/rust/chromium_crates_io/patches/` directory for the crate. |
| 108 | The diff file should be generated by `git-format-patch` each new patch numbered |
| 109 | consecutively so that they can be applied in order. For example, these files |
| 110 | might exist if the "foo" crate was patched with a couple of changes: |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 111 | |
| 112 | ``` |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 113 | //third_party/rust/chromium_crates_io/patches/foo/patches/0001-Edit-the-Cargo-toml.diff |
| 114 | //third_party/rust/chromium_crates_io/patches/foo/patches/0002-Other-changes.diff |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 115 | ``` |
| 116 | |
Adrian Taylor | 138cb9f | 2023-11-08 18:41:54 | [diff] [blame] | 117 | The recommended procedure to create such patches is: |
| 118 | |
| 119 | 1. Commit the plain new version of the crate to your local git branch |
| 120 | 2. Modify the crate as necessary |
| 121 | 3. Commit that modified version |
| 122 | 4. Use `git format-patch <unpatched version>` to generate the patch files |
| 123 | 5. Add the patch files in a new, third, commit |
| 124 | 6. Squash them, or rely on `git cl upload` doing so |
| 125 | |
danakj | f509caa | 2023-11-23 15:29:43 | [diff] [blame] | 126 | #### Patching errors |
| 127 | |
| 128 | If `gnrt vendor` fails to apply a patch for a crate, it will cancel the download of that |
| 129 | crate rather than leave it in a broken state. To recreate patches, first get a pristine |
| 130 | copy of the crate by using the `--no-patches` argument: |
| 131 | |
| 132 | 1. Download the crate without applying patches: |
| 133 | * `vpython3 ./tools/crates/run_gnrt.py vendor --no-patches=<CRATE_NAME>` |
| 134 | 2. Then recreate the patches as described in [Patching third-party crates]( |
| 135 | #patching-third_party-crates). |
| 136 | |
| 137 | To verify the patches work, remove the vendored crate directory in |
| 138 | `//third_party/rust/chromium_crates_io/vendor/`, named after the crate name |
| 139 | and version. Then run the `vendor` action without `--no-patches` which will |
| 140 | download the crate and apply the patches: |
| 141 | * `vpython3 ./tools/crates/run_gnrt.py vendor` |
| 142 | |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 143 | ## Security |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 144 | |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 145 | If a shipping library needs security review (has any `unsafe`), and the review |
| 146 | finds it's not satisfying the [rule of 2](../docs/security/rule-of-2.md), then |
| 147 | move it to the `"sandbox"` group in `//third_party/rust/chromium_crates_io/gnrt_config.toml` |
| 148 | to make it clear it can't be used in a privileged process: |
| 149 | ``` |
| 150 | [crate.foo] |
| 151 | group = "sandbox" |
| 152 | ``` |
| 153 | |
| 154 | If a transitive dependency moves from `"safe"` to `"sandbox"` and causes |
| 155 | a dependency chain across the groups, it will break the `gnrt vendor` step. |
| 156 | You will need to fix the new crate so that it's deemed safe in unsafe review, |
| 157 | or move the other dependent crates out of `"safe"` as well by setting their |
| 158 | group in `gnrt_config.toml`. |
| 159 | |
| 160 | # Updating existing third-party crates |
| 161 | |
Lukasz Anforowicz | 85528a6 | 2024-03-20 19:12:53 | [diff] [blame] | 162 | Third-party crates will get updated semi-automatically through the process |
| 163 | described in |
| 164 | [`../tools/crates/create_update_cl.md`](../tools/crates/create_update_cl.md). |
| 165 | If you nevertheless need to manually update a crate to its latest minor |
| 166 | version, then follow the steps below: |
| 167 | |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 168 | 1. Change directory to the root `src/` dir of Chromium. |
Dominik Röttsches | a07a553 | 2024-01-24 19:16:23 | [diff] [blame] | 169 | 1. Update the versions in `//third_party/rust/chromium_crates_io/Cargo.toml`. |
Lukasz Anforowicz | 85528a6 | 2024-03-20 19:12:53 | [diff] [blame] | 170 | * `vpython3 ./tools/crates/run_gnrt.py update <crate name>` |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 171 | * Or, directly through (nightly) cargo: |
Lukasz Anforowicz | 85528a6 | 2024-03-20 19:12:53 | [diff] [blame] | 172 | `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt update <crate name>` |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 173 | 1. Download any updated crate's files: |
| 174 | * `./tools/crates/run_gnrt.py vendor` |
Dominik Röttsches | a07a553 | 2024-01-24 19:16:23 | [diff] [blame] | 175 | * If you want to restrict the update to certain crates, add the crate names |
| 176 | as arguments to `vendor`, like: `./tools/crates/run_gnrt.py vendor |
| 177 | <crate-name>` |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 178 | * Or, directly through (nightly) cargo: |
| 179 | `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt vendor` |
Lukasz Anforowicz | 8452bd8d | 2023-11-28 23:31:55 | [diff] [blame] | 180 | 1. Add the downloaded files to git: |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 181 | * `git add -f third_party/rust/chromium_crates_io/vendor` |
| 182 | * The `-f` is important, as files may be skipped otherwise from a |
| 183 | `.gitignore` inside the crate. |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 184 | 1. Generate the `BUILD.gn` files |
| 185 | * `vpython3 ./tools/crates/run_gnrt.py gen` |
| 186 | * Or, directly through (nightly) cargo: |
| 187 | `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt gen` |
Lukasz Anforowicz | 8452bd8d | 2023-11-28 23:31:55 | [diff] [blame] | 188 | 1. Add the generated files to git: |
| 189 | * `git add -f third_party/rust` |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 190 | |
| 191 | ### Directory structure for third-party crates |
| 192 | |
| 193 | The directory structure for a crate "foo" version 3.4.2 is: |
| 194 | ``` |
| 195 | //third_party/ |
| 196 | rust/ |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 197 | foo/ (for the "foo" crate) |
Lukasz Anforowicz | 8452bd8d | 2023-11-28 23:31:55 | [diff] [blame] | 198 | v3/ (version 3.4.2 maps to the v3 epoch) |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 199 | BUILD.gn (generated by gnrt gen) |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 200 | README.chromium (generated by gnrt vendor) |
Lukasz Anforowicz | 8452bd8d | 2023-11-28 23:31:55 | [diff] [blame] | 201 | chromium_crates_io/ |
| 202 | vendor/ |
| 203 | foo-3.4.2 (crate sources downloaded from crates.io) |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 204 | patches/ |
Lukasz Anforowicz | 8452bd8d | 2023-11-28 23:31:55 | [diff] [blame] | 205 | foo/ (patches for the "foo" crate) |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 206 | 0001-Edit-the-Cargo-toml.diff |
| 207 | 0002-Other-changes.diff |
Lukasz Anforowicz | 8452bd8d | 2023-11-28 23:31:55 | [diff] [blame] | 208 | Cargo.toml |
| 209 | Cargo.lock |
| 210 | gnrt_config.toml |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 211 | ``` |
| 212 | |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 213 | ## Writing a wrapper for binding generation |
| 214 | |
| 215 | Most Rust libraries will need a more C++-friendly API written on top of them in |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 216 | order to generate C++ bindings to them. The wrapper library can be placed |
| 217 | in `//third_party/rust/<cratename>/<epoch>/wrapper` or at another single place |
| 218 | that all C++ goes through to access the library. The [CXX](https://cxx.rs) is |
| 219 | used to generate bindings between C++ and Rust. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 220 | |
| 221 | See |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 222 | [`//third_party/rust/serde_json_lenient/v0_1/wrapper/`]( |
| 223 | https://source.chromium.org/chromium/chromium/src/+/main:third_party/rust/serde_json_lenient/v0_1/wrapper/) |
| 224 | and |
| 225 | [`//components/qr_code_generator`]( |
| 226 | https://source.chromium.org/chromium/chromium/src/+/main:components/qr_code_generator/;l=1;drc=b185db5d502d4995627e09d62c6934590031a5f2) |
| 227 | for examples. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 228 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 229 | Rust libraries should use the |
| 230 | [`rust_static_library`]( |
| 231 | https://source.chromium.org/chromium/chromium/src/+/main:build/rust/rust_static_library.gni) |
| 232 | GN template (not the built-in `rust_library`) to integrate properly into the |
| 233 | mixed-language Chromium build and get the correct compiler options applied to |
| 234 | them. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 235 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 236 | The [CXX](https://cxx.rs) tool is used for generating C++ bindings to Rust |
| 237 | code. Since it requires explicit declarations in Rust, an wrapper shim around a |
| 238 | pure Rust library is needed. Add these Rust shims that contain the CXX |
| 239 | `bridge` macro to the `cxx_bindings` GN variable in the `rust_static_library` |
| 240 | to have CXX generate a C++ header for that file. To include the C++ header |
| 241 | file, rooted in the `gen` output directory, use |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 242 | ``` |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 243 | #include "the/path/to/the/rust/file.rs.h" |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 244 | ``` |
| 245 | |
danakj | 3d037ff | 2024-11-07 19:31:41 | [diff] [blame] | 246 | # Logging |
Adrian Taylor | 91eaa36 | 2024-02-09 14:17:03 | [diff] [blame] | 247 | |
danakj | 3d037ff | 2024-11-07 19:31:41 | [diff] [blame] | 248 | Use the [log](https://docs.rs/log) crate's macros in place of base `LOG` |
| 249 | macros from C++. They do the same things. The `debug!` macro maps to |
| 250 | `DLOG(INFO)`, the `info!` macro maps to `LOG(INFO)`, and `warn!` and `error!` |
| 251 | map to `LOG(WARNING)` and `LOG(ERROR)` respectively. The additional `trace!` |
| 252 | macro maps to `DLOG(INFO)` (but there is [WIP to map it to `DVLOG(INFO)`]( |
| 253 | https://chromium-review.googlesource.com/c/chromium/src/+/5996820)). |
| 254 | |
| 255 | Note that the standard library also includes a helpful |
| 256 | [`dbg!`](https://doc.rust-lang.org/std/macro.dbg.html) macro which writes |
| 257 | everything about a variable to `stderr`. |
| 258 | |
| 259 | Logging may not yet work in component builds: |
| 260 | [crbug.com/374023535](https://crbug.com/374023535). |
| 261 | |
| 262 | # Tracing |
| 263 | |
| 264 | TODO: [crbug.com/377915495](https://crbug.com/377915495). |
| 265 | |
| 266 | # Strings |
| 267 | |
| 268 | Prefer to use [`BString`](https://docs.rs/bstr/latest/bstr/struct.BString.html) |
| 269 | and [`BStr`](https://docs.rs/bstr/latest/bstr/struct.BStr.html) to work with |
| 270 | strings in first-party code instead of `std::String` and `str`. These types do |
| 271 | not require the strings to be valid UTF-8, and avoid error handling or panic |
| 272 | crashes when working with strings from C++ and/or from the web. Because the |
| 273 | web is not UTF-8 encoded, many strings in Chromium are also not. |
| 274 | |
| 275 | In cross-language bindings, `&[u8]` can be used to represent a string until |
| 276 | native support for `BStr` is available in our interop tooling. A `u8` slice |
| 277 | can be converted to `BStr` or treated as a string with |
| 278 | [`ByteSlice`](https://docs.rs/bstr/latest/bstr/trait.ByteSlice.html). |
Adrian Taylor | 91eaa36 | 2024-02-09 14:17:03 | [diff] [blame] | 279 | |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 280 | # Using VSCode |
| 281 | |
| 282 | 1. Ensure you're using the `rust-analyzer` extension for VSCode, rather than |
| 283 | earlier forms of Rust support. |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 284 | 2. Run `gn` with the `--export-rust-project` flag, such as: |
| 285 | `gn gen out/Release --export-rust-project`. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 286 | 3. `ln -s out/Release/rust-project.json rust-project.json` |
| 287 | 4. When you run VSCode, or any other IDE that uses |
| 288 | [rust-analyzer](https://rust-analyzer.github.io/) it should detect the |
| 289 | `rust-project.json` and use this to give you rich browsing, autocompletion, |
| 290 | type annotations etc. for all the Rust within the Chromium codebase. |
danakj | f3d7f37 | 2023-12-07 18:17:12 | [diff] [blame] | 291 | 5. Point rust-analyzer to the rust toolchain in Chromium. Otherwise you will |
| 292 | need to install Rustc in your system, and Chromium uses the nightly |
| 293 | compiler, so you would need that to match. Add the following to |
| 294 | `.vscode/settings.json` in the Chromium checkout: |
| 295 | ``` |
| 296 | { |
| 297 | // The rest of the settings... |
| 298 | |
| 299 | "rust-analyzer.cargo.extraEnv": { |
| 300 | "PATH": "../../third_party/rust-toolchain/bin:$PATH", |
| 301 | } |
| 302 | } |
| 303 | ``` |
| 304 | This assumes you are working with an output directory like `out/Debug` which |
| 305 | has two levels; adjust the number of `..` in the path according to your own |
| 306 | setup. |
Adrian Taylor | c5fbb57 | 2023-11-21 14:25:42 | [diff] [blame] | 307 | |
| 308 | # Using cargo |
| 309 | |
| 310 | If you are building a throwaway or experimental tool, you might like to use pure |
| 311 | `cargo` tooling rather than `gn` and `ninja`. Even then, you may choose |
| 312 | to restrict yourself to the toolchain and crates that are already approved for |
| 313 | use in Chromium. |
| 314 | |
| 315 | Here's how. |
| 316 | |
| 317 | ``` |
| 318 | export PATH_TO_CHROMIUM_SRC=~/chromium/src |
| 319 | mkdir my-rust-tool |
| 320 | cd my-rust-tool |
| 321 | mkdir .cargo |
| 322 | cat <<END > .cargo/config.toml |
| 323 | [source.crates-io] |
| 324 | replace-with = "vendored-sources" |
| 325 | |
| 326 | [source.vendored-sources] |
| 327 | directory = "$PATH_TO_CHROMIUM_SRC/third_party/rust/chromium_crates_io/vendor" |
| 328 | END |
| 329 | $PATH_TO_CHROMIUM_SRC/third_party/rust-toolchain/bin/cargo init --offline |
| 330 | $PATH_TO_CHROMIUM_SRC/third_party/rust-toolchain/bin/cargo run --offline |
| 331 | ``` |
| 332 | |
| 333 | Most `cargo` tooling works well with this setup; one exception is `cargo add`, |
| 334 | but you can still add dependencies manually to your `Cargo.toml`: |
| 335 | |
| 336 | ``` |
| 337 | [dependencies] |
| 338 | log = "0.4" |
| 339 | ``` |
David Adrian | d891869 | 2024-12-12 22:02:50 | [diff] [blame^] | 340 | |
| 341 | [interop-rust-doc]: https://docs.google.com/document/d/1kvgaVMB_isELyDQ4nbMJYWrqrmL3UZI4tDxnyxy9RTE/edit?tab=t.0#heading=h.fpqr6hf3c3j0 |