|
| 1 | +This is a verification package for the CI (Continuous Integration) of |
| 2 | +crates that support `#![no_std]` environments. It works by statically |
| 3 | +linking packages that need to be checked for `std` library dependencies |
| 4 | +and detecting whether duplicate implementations of `panic_impl` exist. |
| 5 | + |
| 6 | +Since `panic_impl` is provided by the std package and multiple |
| 7 | +implementations cannot coexist, the compilation will fail with a duplicate |
| 8 | +implementation error if even a single package depends on std. If this |
| 9 | +package builds successfully with the std feature disabled, it guarantees |
| 10 | +that the given feature set supports the `#![no_std]` environment. |
| 11 | + |
| 12 | +Another way to verify this is by checking if the successfully compiled |
| 13 | +binary is statically linked. In environments like Linux, you can use the |
| 14 | +`ldd` command to ensure that no dynamic libraries are linked. |
| 15 | + |
| 16 | +## Key Technical Terms Used |
| 17 | + |
| 18 | +- `#![no_std]`: The attribute used to indicate the crate does not link the |
| 19 | + standard library. |
| 20 | +- Static Linking: Combining all necessary libraries into the executable at |
| 21 | + compile time. |
| 22 | +- `panic_impl`: The language item used to define the strategy for handling |
| 23 | + panics. |
| 24 | +- Feature Set: The specific combination of Cargo features enabled during |
| 25 | + compilation. |
| 26 | + |
| 27 | +## Why this works |
| 28 | + |
| 29 | +In Rust, the `std` crate provides a default panic handler. If you are |
| 30 | +building for a bare-metal or restricted environment (no_std), you must |
| 31 | +provide your own `#[panic_handler]`. By attempting to link both, you |
| 32 | +create a conflict. This "conflict by design" is a simple way to use the |
| 33 | +compiler as a gatekeeper to ensure no hidden std dependencies creep into |
| 34 | +your embedded or kernel-level code. |
| 35 | + |
| 36 | +## How to use |
| 37 | + |
| 38 | +Since unwinding panics are not supported in a `#![no_std]` environment and |
| 39 | +we don't need to worry about panic behavior, you should disable them by |
| 40 | +setting the environment variable `CARGO_PROFILE_DEV_PANIC=abort`. |
| 41 | + |
| 42 | +```bash |
| 43 | +# Compile |
| 44 | +export CARGO_PROFILE_DEV_PANIC='abort' |
| 45 | +cargo build -p nostd-tests |
| 46 | + |
| 47 | +# Validate |
| 48 | +ldd ./target/debug/libnostd_tests.so | |
| 49 | + grep -Posq '^\tstatically linked$' |
| 50 | +``` |
0 commit comments