Skip to content

Commit

Permalink
Merge #45: Expand the amount of core-only
Browse files Browse the repository at this point in the history
32c87da Allow doing more things without alloc (John Ericson)

Pull request description:

ACKs for top commit:
  apoelstra:
    ACK 32c87da

Tree-SHA512: eac2103059fcd688690947d5817dbf758fcb99c82ec83354aa2e82113b6f5f2487e28864efc9ab2cdeb978d832d9dcbfe08208ec9f935bd356a430cff3536023
  • Loading branch information
apoelstra committed Mar 31, 2023
2 parents 1645683 + 32c87da commit d566ec4
Show file tree
Hide file tree
Showing 10 changed files with 452 additions and 111 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/rust.yml
Expand Up @@ -88,7 +88,14 @@ jobs:
steps:
- name: Checkout Crate
uses: actions/checkout@v3
- name: Set up QEMU
run: sudo apt update && sudo apt install -y qemu-system-arm gcc-arm-none-eabi
- name: Checkout Toolchain
uses: dtolnay/rust-toolchain@stable
uses: dtolnay/rust-toolchain@nightly
with:
targets: thumbv7m-none-eabi
- name: Run
run: cd embedded/no-allocator && cargo rustc -- -C link-arg=-nostartfiles
env:
RUSTFLAGS: "-C link-arg=-Tlink.x"
CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER: "qemu-system-arm -cpu cortex-m3 -machine mps2-an385 -nographic -semihosting-config enable=on,target=native -kernel"
run: cd embedded/no-allocator && cargo run --target thumbv7m-none-eabi
5 changes: 4 additions & 1 deletion Cargo.toml
Expand Up @@ -19,4 +19,7 @@ alloc = []
# Only for CI to make all warnings errors, do not activate otherwise (may break forward compatibility)
strict = []

[dependencies]
[dependencies.arrayvec]
version = "0.7.1"
default-features = false
optional = true
45 changes: 33 additions & 12 deletions contrib/test.sh
Expand Up @@ -4,28 +4,31 @@
#
# The "strict" feature is used to configure cargo to deny all warnings, always use it in test runs.

set -ex

FEATURES="std alloc"
set -eu
set -x

# Some tests require certain toolchain types.
NIGHTLY=false
MIN_VERSION=false
if cargo --version | grep nightly; then
NIGHTLY=true
fi
if cargo --version | grep 1.41; then
MIN_VERSION=true
fi

# Sanity, check tools exist.
cargo --version
rustc --version

# Run the linter if told to.
if [ "$DO_LINT" = true ]
if [ "${DO_LINT-false}" = true ]
then
cargo clippy --all-features --all-targets -- -D warnings
fi

# Run formatter if told to.
if [ "$DO_FMT" = true ]; then
if [ "${DO_FMT-false}" = true ]; then
if [ "$NIGHTLY" = false ]; then
echo "DO_FMT requires a nightly toolchain (consider using RUSTUP_TOOLCHAIN)"
exit 1
Expand All @@ -34,16 +37,34 @@ if [ "$DO_FMT" = true ]; then
cargo fmt --check
fi

check () {
cargo build --no-default-features --features="strict $1"
cargo test --no-default-features --features="strict $1"
}

# Check without features ("strict" is a CI feature only, see above).
cargo build --no-default-features --features="strict"
cargo test --no-default-features --features="strict"
check ""

# Check "std" feature (implies "alloc", so this is equivalent to --all-features).
cargo build --no-default-features --features="strict std"
cargo test --no-default-features --features="strict std"
# Check "arrayvec" feature alone.
if [ "$MIN_VERSION" != true ]; then
check "arrayvec"
fi

# Check "alloc" feature alone.
cargo build --no-default-features --features="strict alloc"
cargo test --no-default-features --features="strict alloc"
check "alloc"

# Check "alloc" & "arrayvec" features together.
if [ "$MIN_VERSION" != true ]; then
check "alloc arrayvec"
fi

# Check "std" feature (implies "alloc", so this is equivalent to --all-features).
cargo build --no-default-features --features="std"
cargo test --no-default-features --features="std"

# Check "std" & "arrayvec" features together.
if [ "$MIN_VERSION" != true ]; then
check "std arrayvec"
fi

exit 0
11 changes: 11 additions & 0 deletions embedded/README.md
@@ -0,0 +1,11 @@
## Embedded tests

These no-std tests are a bit peculiar.
They are cross compiled to ARM and run in an emulator.
Here's why:

- We want to build for an exotic platform to help make sure `std` doesn't sneak in by accident.

- We use an emulator and build something runnable,
rather than merely testing whether a library builds,
because we want to actually run our integration test.
16 changes: 13 additions & 3 deletions embedded/no-allocator/Cargo.toml
Expand Up @@ -5,11 +5,21 @@ readme = "README.md"
name = "no-allocator"
version = "0.1.0"

[dependencies]
cortex-m = "0.6.0"
cortex-m-rt = "0.6.10"
cortex-m-semihosting = "0.3.3"
panic-halt = "0.2.0"
arrayvec = { version = "0.7.1", default-features = false }
bech32 = { path = "../../", default-features = false, features = ["arrayvec"] }

[[bin]]
name = "no-allocator"
test = false
bench = false

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

[dependencies]
bech32 = { path = "../../", default_features = false }
10 changes: 0 additions & 10 deletions embedded/no-allocator/README.md

This file was deleted.

6 changes: 6 additions & 0 deletions embedded/no-allocator/memory.x
@@ -0,0 +1,6 @@
MEMORY
{

FLASH : ORIGIN = 0x00000000, LENGTH = 256K
RAM : ORIGIN = 0x20000000, LENGTH = 64K
}
50 changes: 39 additions & 11 deletions embedded/no-allocator/src/main.rs
Expand Up @@ -3,24 +3,52 @@
//! Build with: `cargo rustc -- -C link-arg=-nostartfiles`.
//!

#![no_std]
#![feature(alloc_error_handler)]
#![no_main]
#![no_std]

use panic_halt as _;

use core::panic::PanicInfo;
use arrayvec::{ArrayString, ArrayVec};
use bech32::{self, u5, ComboError, FromBase32, ToBase32, Variant};
use cortex_m_rt::entry;
use cortex_m_semihosting::{debug, hprintln};

// Note: `#[global_allocator]` is NOT set.

#[allow(unused_imports)]
use bech32;
#[entry]
fn main() -> ! {
let mut encoded = ArrayString::<30>::new();

let mut base32 = ArrayVec::<u5, 30>::new();

[0x00u8, 0x01, 0x02].write_base32(&mut base32).unwrap();

bech32::encode_to_fmt_anycase(&mut encoded, "bech32", &base32, Variant::Bech32)
.unwrap()
.unwrap();
test(&*encoded == "bech321qqqsyrhqy2a");

hprintln!("{}", encoded).unwrap();

let mut decoded = ArrayVec::<u5, 30>::new();

let mut scratch = ArrayVec::<u5, 30>::new();

let (hrp, data, variant) =
bech32::decode_lowercase::<ComboError, _, _>(&encoded, &mut decoded, &mut scratch).unwrap();
test(hrp == "bech32");
let res = ArrayVec::<u8, 30>::from_base32(&data).unwrap();
test(&res == [0x00, 0x01, 0x02].as_ref());
test(variant == Variant::Bech32);

debug::exit(debug::EXIT_SUCCESS);

/// This function is called on panic, defining this ensures build will fail if `std` is enabled
/// because `panic` will be defined twice.
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}

#[no_mangle]
pub extern "C" fn _start() -> ! {
loop {}
fn test(result: bool) {
if !result {
debug::exit(debug::EXIT_FAILURE);
}
}
2 changes: 1 addition & 1 deletion embedded/with-allocator/Cargo.toml
Expand Up @@ -11,7 +11,7 @@ cortex-m-rt = "0.6.10"
cortex-m-semihosting = "0.3.3"
panic-halt = "0.2.0"
alloc-cortex-m = "0.4.1"
bech32 = { path="../../", default-features = false, features = ["alloc"] }
bech32 = { path = "../../", default-features = false, features = ["alloc"] }

[[bin]]
name = "with-allocator"
Expand Down

0 comments on commit d566ec4

Please sign in to comment.