Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the cdylib no_std #59

Merged
merged 7 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 2 additions & 66 deletions .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -255,61 +255,6 @@ jobs:
# fi
# RUST_BACKTRACE=1 cargo fuzz run --no-default-features --features="$features" $target -- -max_total_time=10
# done
#
# link-c-dynamic-library:
# name: dynamic library
# strategy:
# matrix:
# include:
# - target: x86_64-unknown-linux-gnu
# features:
# - ''
# runs-on: ubuntu-latest
# steps:
# - name: Checkout sources
# uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
# with:
# persist-credentials: false
# submodules: true
# - name: Install rust toolchain
# uses: dtolnay/rust-toolchain@be73d7920c329f220ce78e0234b8f96b7ae60248
# with:
# toolchain: stable
# targets: ${{matrix.target}}
# - name: "cdylib: default settings"
# working-directory: libz-rs-sys-cdylib
# env:
# LD_LIBRARY_PATH: "target/${{matrix.target}}/release/deps"
# run: |
# cargo build --release --target ${{matrix.target}}
# cc -o zpipe zpipe.c target/${{matrix.target}}/release/deps/libz_rs.so
# ./zpipe < Cargo.toml | ./zpipe -d > out.txt
# cmp -s Cargo.toml out.txt
# - name: "cdylib: rust-allocator"
# env:
# LD_LIBRARY_PATH: "target/${{matrix.target}}/release/deps"
# working-directory: libz-rs-sys-cdylib
# run: |
# cargo build --release --target ${{matrix.target}} --no-default-features --features="rust-allocator"
# cc -o zpipe zpipe.c target/${{matrix.target}}/release/deps/libz_rs.so
# ./zpipe < Cargo.toml | ./zpipe -d > out.txt
# cmp -s Cargo.toml out.txt
# - name: "cdylib: no_std"
# env:
# LD_LIBRARY_PATH: "target/${{matrix.target}}/release/deps"
# working-directory: libz-rs-sys-cdylib
# run: |
# cargo build --release --target ${{matrix.target}} --no-default-features
# cc -o zpipe_no_std zpipe_no_std.c target/${{matrix.target}}/release/deps/libz_rs.so
# ./zpipe_no_std < Cargo.toml | ./zpipe_no_std -d > out.txt
# cmp -s Cargo.toml out.txt
# - name: "cdylib: custom-prefix"
# working-directory: libz-rs-sys-cdylib
# env:
# LIBZ_RS_SYS_PREFIX: "MY_CUSTOM_PREFIX_"
# run: |
# cargo build --release --target ${{matrix.target}} --features=custom-prefix
# objdump -tT target/${{matrix.target}}/release/deps/libz_rs.so | grep -q "MY_CUSTOM_PREFIX_uncompress" || (echo "symbol not found!" && exit 1)

wasm32:
name: "wasm32"
Expand Down Expand Up @@ -401,16 +346,7 @@ jobs:
cc -o bzpipe bzpipe.c target/${{matrix.target}}/release/deps/libbz2_rs.so -I ../
./bzpipe < Cargo.toml | ./bzpipe -d > out.txt
cmp -s Cargo.toml out.txt
- name: "cdylib: rust-allocator"
env:
LD_LIBRARY_PATH: "target/${{matrix.target}}/release/deps"
working-directory: libbzip2-rs-sys-cdylib
run: |
cargo build --release --target ${{matrix.target}} --no-default-features --features="rust-allocator"
cc -o bzpipe bzpipe.c target/${{matrix.target}}/release/deps/libbz2_rs.so -I ../
./bzpipe < Cargo.toml | ./bzpipe -d > out.txt
cmp -s Cargo.toml out.txt
- name: "cdylib: no_std"
- name: "cdylib: no stdio"
env:
LD_LIBRARY_PATH: "target/${{matrix.target}}/release/deps"
working-directory: libbzip2-rs-sys-cdylib
Expand All @@ -424,7 +360,7 @@ jobs:
LD_LIBRARY_PATH: "target/${{matrix.target}}/release/deps"
working-directory: libbzip2-rs-sys-cdylib
run: |
cargo build --release --target ${{matrix.target}} --no-default-features --features="c-allocator"
cargo build --release --target ${{matrix.target}} --no-default-features
cc -o bzpipe bzpipe.c target/${{matrix.target}}/release/deps/libbz2_rs.so -I ../
./bzpipe < Cargo.toml | ./bzpipe -d > out.txt
cmp -s Cargo.toml out.txt
Expand Down
1 change: 1 addition & 0 deletions libbzip2-rs-sys-cdylib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions libbzip2-rs-sys-cdylib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ rust-version = "1.82" # MSRV

[lib]
name = "bz2_rs" # turns into e.g. `libbz2_rs.so`
crate-type=["cdylib"]
crate-type = ["cdylib"]
test = false

[features]
default = ["c-allocator", "libbzip2-rs-sys/stdio", "libbzip2-rs-sys/std"] # when used as a cdylib crate, use the c allocator
c-allocator = ["libbzip2-rs-sys/c-allocator"] # by default, use malloc/free for memory allocation
rust-allocator = ["libbzip2-rs-sys/rust-allocator", "libbzip2-rs-sys/std"] # by default, use the rust global alloctor for memory allocation
default = ["stdio"]
stdio = ["libbzip2-rs-sys/stdio"]
custom-prefix = ["libbzip2-rs-sys/custom-prefix"] # use the LIBBZIP2_RS_SYS_PREFIX to prefix all exported symbols
capi = []

[dependencies]
libbzip2-rs-sys = { version = "0.0.0", path = "../libbzip2-rs-sys", default-features = false }
libbzip2-rs-sys = { version = "0.0.0", path = "../libbzip2-rs-sys", default-features = false, features = ["c-allocator"] }
libc = "0.2"

[package.metadata.capi.library]
version = "1.0.9" # the bzip2 api version we match
Expand All @@ -34,3 +35,10 @@ enabled = false
[package.metadata.capi.pkg_config]
name = "libbz2_rs"
filename = "libbz2_rs"

# no_std requires panic=abort
[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"
1 change: 1 addition & 0 deletions libbzip2-rs-sys-cdylib/bzpipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

extern void bz_internal_error(int errcode) {
fprintf(stderr, "bzip2 hit internal error code: %d\n", errcode);
exit(1);
}

#ifdef NO_STD
Expand Down
45 changes: 45 additions & 0 deletions libbzip2-rs-sys-cdylib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
#![no_std]

extern crate libbzip2_rs_sys;

use core::panic::PanicInfo;
pub use libbzip2_rs_sys::*;

#[cfg(feature = "stdio")]
struct StderrWritter;

#[cfg(feature = "stdio")]
impl core::fmt::Write for StderrWritter {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
use core::ffi::c_void;
use libc::write;

unsafe {
write(2, s.as_ptr() as *const c_void, s.len());
}
Ok(())
}
}

#[panic_handler]
fn panic_handler(_info: &PanicInfo) -> ! {
#[cfg(feature = "stdio")]
{
use core::fmt::Write;
use libc::exit;

let _ = StderrWritter.write_str("libbzip2-rs: internal error:\n");
let _ = StderrWritter.write_fmt(format_args!("{}", _info.message()));

unsafe {
exit(3);
}
}

#[cfg(not(feature = "stdio"))]
{
extern "C" {
fn bz_internal_error(errcode: core::ffi::c_int);
}

unsafe { bz_internal_error(-1) }
loop {}
}
}
2 changes: 1 addition & 1 deletion libbzip2-rs-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ readme = "README.md"
edition.workspace = true

[features]
default = ["std", "c-allocator", "stdio"]
default = ["std", "stdio"]
c-allocator = ["dep:libc"] # use a malloc-based C allocator (rust is picked over c if both are configured)
rust-allocator = [] # use the rust global allocator (rust is picked over c if both are configured)
std = ["rust-allocator"]
Expand Down
47 changes: 25 additions & 22 deletions libbzip2-rs-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,37 +84,40 @@
// --- assert failure logic

macro_rules! assert_h {
($condition:expr, $errcode:expr) => {{
#[cfg(feature = "stdio")]
($condition:expr, $errcode:expr) => {
if !$condition {
#[cfg(feature = "std")]
std::eprint!("{}", $crate::AssertFail($errcode));
#[cfg(feature = "std")]
std::process::exit(3);

#[cfg(not(feature = "std"))]
panic!("{}", $crate::AssertFail($errcode));
}

#[cfg(not(feature = "stdio"))]
if !$condition {
$crate::no_stdio_internal_error($errcode)
$crate::handle_assert_failure($errcode)
}
}};
};
}

#[cfg(not(feature = "stdio"))]
pub(crate) fn no_stdio_internal_error(errcode: c_int) {
extern "C" {
fn bz_internal_error(errcode: c_int);
#[cold]
fn handle_assert_failure(errcode: c_int) -> ! {
#[cfg(feature = "stdio")]
{
#[cfg(feature = "std")]
std::eprint!("{}", AssertFail(errcode));
#[cfg(feature = "std")]
std::process::exit(3);

Check warning on line 101 in libbzip2-rs-sys/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

libbzip2-rs-sys/src/lib.rs#L95-L101

Added lines #L95 - L101 were not covered by tests

#[cfg(not(feature = "std"))]
panic!("{}", AssertFail(errcode));
}

unsafe { bz_internal_error(errcode) }
#[cfg(not(feature = "stdio"))]
{
extern "C" {
fn bz_internal_error(errcode: c_int);
}

unsafe { bz_internal_error(errcode) }
loop {}
}
}

pub(crate) use assert_h;
use assert_h;

pub(crate) struct AssertFail(i32);
struct AssertFail(i32);

impl core::fmt::Display for AssertFail {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Expand Down
Loading