Skip to content

Commit 7b2fabf

Browse files
committed
Auto merge of #11825 - ehuss:cross-test-docs, r=weihanglo
Add some documentation on writing cross-compilation tests This adds an overview of what you need to know to write tests that do cross-compilation.
2 parents 595cb11 + 9e6d0c0 commit 7b2fabf

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/doc/contrib/src/tests/writing.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,48 @@ If you need to test with registry dependencies, see
154154
If you need to test git dependencies, see [`support::git`] to create a git
155155
dependency.
156156

157+
#### Cross compilation
158+
159+
There are some utilities to help support tests that need to work against a
160+
target other than the host. See [Running cross
161+
tests](running.md#running-cross-tests) for more an introduction on cross
162+
compilation tests.
163+
164+
Tests that need to do cross-compilation should include this at the top of the
165+
test to disable it in scenarios where cross compilation isn't available:
166+
167+
```rust,ignore
168+
if cargo_test_support::cross_compile::disabled() {
169+
return;
170+
}
171+
```
172+
173+
The name of the target can be fetched with the [`cross_compile::alternate()`]
174+
function. The name of the host target can be fetched with
175+
[`cargo_test_support::rustc_host()`].
176+
177+
The cross-tests need to distinguish between targets which can *build* versus
178+
those which can actually *run* the resulting executable. Unfortunately, macOS is
179+
currently unable to run an alternate target (Apple removed 32-bit support a
180+
long time ago). For building, `x86_64-apple-darwin` will target
181+
`x86_64-apple-ios` as its alternate. However, the iOS target can only execute
182+
binaries if the iOS simulator is installed and configured. The simulator is
183+
not available in CI, so all tests that need to run cross-compiled binaries are
184+
disabled on CI. If you are running on macOS locally, and have the simulator
185+
installed, then it should be able to run them.
186+
187+
If the test needs to run the cross-compiled binary, then it should have
188+
something like this to exit the test before doing so:
189+
190+
```rust,ignore
191+
if cargo_test_support::cross_compile::can_run_on_host() {
192+
return;
193+
}
194+
```
195+
196+
[`cross_compile::alternate()`]: https://github.com/rust-lang/cargo/blob/d58902e22e148426193cf3b8c4449fd3c05c0afd/crates/cargo-test-support/src/cross_compile.rs#L208-L225
197+
[`cargo_test_support::rustc_host()`]: https://github.com/rust-lang/cargo/blob/d58902e22e148426193cf3b8c4449fd3c05c0afd/crates/cargo-test-support/src/lib.rs#L1137-L1140
198+
157199
### UI Tests
158200

159201
UI Tests are a bit more spread out and generally look like:

0 commit comments

Comments
 (0)