Skip to content

Commit

Permalink
Merge remote-tracking branch 'tcharding/07-28-alloc-feature' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed Sep 23, 2022
2 parents cc50e7d + e15eb28 commit a8a162e
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 20 deletions.
33 changes: 26 additions & 7 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

on: [pull_request]

name: Continuous Integration

jobs:
test:
Test:
name: Test Suite
runs-on: ubuntu-latest
strategy:
Expand All @@ -13,16 +14,17 @@ jobs:
- stable
- nightly
steps:
- name: Checkout Crate
- uses: actions/checkout@v2
- name: Checkout Toolchain
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- uses: actions-rs/cargo@v1
with:
command: test
args: --verbose --features strict
- name: Run Test Script
env: ${{ matrix.rust }}
run: ./contrib/test.sh

fmt:
name: Rustfmt
Expand Down Expand Up @@ -64,7 +66,8 @@ jobs:
command: clippy
args: -- -D warnings

Embedded:
EmbeddedWithAlloc:
name: no_std with alloc
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -83,4 +86,20 @@ jobs:
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 && cargo run --target thumbv7m-none-eabi
run: cd embedded/with-allocator && cargo run --target thumbv7m-none-eabi

EmbeddedNoAlloc:
name: no_std no alloc
runs-on: ubuntu-latest
strategy:
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: rustc
args: -- -C link-arg=-nostartfiles
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
*.o
/Cargo.lock

/embedded/Cargo.lock
/embedded/.cargo
/embedded/no-allocator/Cargo.lock
/embedded/no-allocator/.cargo
/embedded/with-allocator/Cargo.lock
/embedded/with-allocator/.cargo
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ edition = "2018"

[features]
default = ["std"]
std = []
std = ["alloc"]
alloc = []

# Only for CI to make all warnings errors, do not activate otherwise (may break forward compatibility)
strict = []

Expand Down
32 changes: 32 additions & 0 deletions contrib/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh
#
# CI test script for rust-bech32.
#
# The "strict" feature is used to configure cargo to deny all warnings, always use it in test runs.

set -ex

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

# Sanity, first check with default features.

cargo build
cargo test

# Sanity, build with no features.

cargo build --no-default-features --features="strict"

# Check "alloc" feature alone.

cargo build --no-default-features --features="strict std"
cargo test --no-default-features --features="strict std"

# Check "std" feature (implies "alloc").

cargo build --no-default-features --features="strict alloc"
cargo test --no-default-features --features="strict alloc"

exit 0
15 changes: 15 additions & 0 deletions embedded/no-allocator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
authors = ["Tobin C. Harding <me@tobin.cc>"]
edition = "2018"
readme = "README.md"
name = "no-allocator"
version = "0.1.0"

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

[dependencies]
bech32 = { path = "../../", default_features = false }
10 changes: 10 additions & 0 deletions embedded/no-allocator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# no_std test crate without an allocator

This crate is based on the blog post found at:

https://blog.dbrgn.ch/2019/12/24/testing-for-no-std-compatibility/

Its purpose is to test that the `rust-bech32` library can be built in a `no_std` environment without
a global allocator.

Build with: `cargo rustc -- -C link-arg=-nostartfiles`.
26 changes: 26 additions & 0 deletions embedded/no-allocator/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! Test `no_std` build of `bech32`.
//!
//! Build with: `cargo rustc -- -C link-arg=-nostartfiles`.
//!
#![no_std]
#![no_main]

use core::panic::PanicInfo;

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

#[allow(unused_imports)]
use bech32;

/// 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 {}
}
6 changes: 3 additions & 3 deletions embedded/Cargo.toml → embedded/with-allocator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Riccardo Casatta <riccardo@casatta.it>"]
edition = "2018"
readme = "README.md"
name = "embedded"
name = "with-allocator"
version = "0.1.0"

[dependencies]
Expand All @@ -11,10 +11,10 @@ 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 }
bech32 = { path="../../", default-features = false }

[[bin]]
name = "embedded"
name = "with-allocator"
test = false
bench = false

Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit a8a162e

Please sign in to comment.