|
| 1 | +- Feature Name: `cfg-target-abi` |
| 2 | +- Start Date: 2020-09-27 |
| 3 | +- RFC PR: [rust-lang/rfcs#2992](https://github.com/rust-lang/rfcs/pull/2992) |
| 4 | +- Rust Issue: [rust-lang/rust#80970](https://github.com/rust-lang/rust/issues/80970) |
| 5 | + |
| 6 | +# Summary |
| 7 | +[summary]: #summary |
| 8 | + |
| 9 | +This proposes a new `cfg`: `target_abi`, which specifies certain aspects of the |
| 10 | +target's [Application Binary Interface (ABI)][abi]. This also adds a |
| 11 | +`CARGO_CFG_TARGET_ABI` environment variable for parity with other |
| 12 | +`CARGO_CFG_TARGET_*` variables. |
| 13 | + |
| 14 | +# Motivation |
| 15 | +[motivation]: #motivation |
| 16 | + |
| 17 | +Certain targets are only differentiated by their ABI. For example: the `ios` OS |
| 18 | +in combination with the `macabi` ABI denotes targeting Mac Catalyst (iOS on |
| 19 | +macOS). The non-`macabi` `x86_64-apple-ios` target is not for Mac Catalyst and |
| 20 | +instead is for the iOS simulator, which is a very different environment. |
| 21 | + |
| 22 | +It is not currently possible to `#[cfg]` against a certain target ABI without |
| 23 | +a `build.rs` script to emit a custom `cfg` based on the `TARGET` environment |
| 24 | +variable. This is not ideal because: |
| 25 | + |
| 26 | +- Adding a build script increases compile time and makes a crate incompatible |
| 27 | + with certain build systems. |
| 28 | + |
| 29 | +- Checking `TARGET` is error prone, mainly because the ABI often follows |
| 30 | + `target_env` without separation. |
| 31 | + |
| 32 | +# Guide-level explanation |
| 33 | +[guide-level-explanation]: #guide-level-explanation |
| 34 | + |
| 35 | +This would act like [existing `target_*` configurations][cfg-options]. |
| 36 | + |
| 37 | +For example: if one had a module with bindings to |
| 38 | +[Apple's AppKit](https://developer.apple.com/documentation/appkit), this feature |
| 39 | +could be used to ensure the module is available when targeting regular macOS and |
| 40 | +Mac Catalyst. |
| 41 | + |
| 42 | +```rust |
| 43 | +#[cfg(any( |
| 44 | + target_os = "macos", |
| 45 | + all( |
| 46 | + target_os = "ios", |
| 47 | + target_abi = "macabi", |
| 48 | + ), |
| 49 | +))] |
| 50 | +pub mod app_kit; |
| 51 | +``` |
| 52 | + |
| 53 | +This configuration option would also be usable as |
| 54 | +`#[cfg_attr(target_abi = "...", attr)]`. |
| 55 | + |
| 56 | +# Reference-level explanation |
| 57 | +[reference-level-explanation]: #reference-level-explanation |
| 58 | + |
| 59 | +`target_abi` is a key-value option set once with the target's ABI. The value is |
| 60 | +similar to the fourth element of the platform's target triple. It often comes |
| 61 | +after the `target_env` value. Embedded ABIs such as `gnueabihf` will define |
| 62 | +`target_env` as `"gnu"` and `target_abi` as `"eabihf"`. |
| 63 | + |
| 64 | +Example values: |
| 65 | + |
| 66 | +- `""` |
| 67 | +- `"abi64"` |
| 68 | +- `"eabi"` |
| 69 | +- `"eabihf"` |
| 70 | +- `"macabi"` |
| 71 | + |
| 72 | +# Drawbacks |
| 73 | +[drawbacks]: #drawbacks |
| 74 | + |
| 75 | +- Additional metadata for the compiler to keep track of. |
| 76 | + |
| 77 | +- Like other `cfg`s, this can be manipulated at build time to be a value that |
| 78 | + mismatches the actual target. |
| 79 | + |
| 80 | +# Rationale and alternatives |
| 81 | +[rationale-and-alternatives]: #rationale-and-alternatives |
| 82 | + |
| 83 | +We can keep the existing work-around of checking the `TARGET` environment |
| 84 | +variable in a `build.rs` script. However, this is not ideal because: |
| 85 | + |
| 86 | +- Adding a build script increases compile time and makes a crate incompatible |
| 87 | + with certain build systems. |
| 88 | + |
| 89 | +- Checking `TARGET` is error prone, mainly because the ABI often follows |
| 90 | + `target_env` without separation. |
| 91 | + |
| 92 | +# Prior art |
| 93 | +[prior-art]: #prior-art |
| 94 | + |
| 95 | +- [Target component configurations][cfg-options]: `target_arch`, |
| 96 | + `target_vendor`, `target_os`, and `target_env`. |
| 97 | + |
| 98 | +- `CARGO_CFG_TARGET_*` |
| 99 | + [environment variables for `build.rs`](https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts). |
| 100 | + |
| 101 | +# Unresolved questions |
| 102 | +[unresolved-questions]: #unresolved-questions |
| 103 | + |
| 104 | +None. |
| 105 | + |
| 106 | +# Future possibilities |
| 107 | +[future-possibilities]: #future-possibilities |
| 108 | + |
| 109 | +None. |
| 110 | + |
| 111 | +[abi]: https://en.wikipedia.org/wiki/Application_binary_interface |
| 112 | +[cfg-options]: https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options |
0 commit comments