Skip to content

Commit

Permalink
Add no_std support with a "std" feature.
Browse files Browse the repository at this point in the history
The "std" feature is enabled by default; disabling it enables no_std
mode.
  • Loading branch information
sunfishcode committed Nov 20, 2023
1 parent 428123d commit cc0503c
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 12 deletions.
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exclude = ["/.github", "ci"]
keywords = ["linux"]

[dependencies]
c-gull = { version = "0.15.22", default-features = false, features = ["eyra"] }
c-gull = { version = "0.15.25", default-features = false, features = ["eyra"] }

[dev-dependencies]
assert_cmd = "2.0.12"
Expand All @@ -35,6 +35,9 @@ core_simd = { git = "https://github.com/rust-lang/portable-simd" }
[features]
default = ["be-std", "threadsafe-setenv", "use-compiler-builtins"]

# Enable features that depend on std. Disable this for `no_std`.
std = ["c-gull/std"]

# This makes `setenv` and friends thread-safe by leaking memory.
threadsafe-setenv = ["c-gull/threadsafe-setenv"]

Expand All @@ -45,7 +48,7 @@ log = ["c-gull/log"]
atomic-dbg-logger = ["c-gull/atomic-dbg-logger"]

# Install the `env_logger` crate as a logger.
env_logger = ["c-gull/env_logger"]
env_logger = ["c-gull/env_logger", "std"]

# Disable logging.
max_level_off = ["c-gull/max_level_off"]
Expand All @@ -55,7 +58,7 @@ max_level_off = ["c-gull/max_level_off"]
experimental-relocate = ["c-gull/experimental-relocate"]

# Have eyra do `use std::*;` so that it can be used as `std`.
be-std = []
be-std = ["std"]

# Should c-scape's `memcpy` etc. use compiler-builtins?
use-compiler-builtins = ["c-gull/use-compiler-builtins"]
Expand Down
2 changes: 1 addition & 1 deletion example-crates/all-from-source/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ eyra = { path = "../..", default-features = false, features = ["be-std", "thread
lto = true
codegen-units = 1

# This is just an example crate, and not part of the c-ward workspace.
# This is just an example crate, and not part of the eyra workspace.
[workspace]
2 changes: 1 addition & 1 deletion example-crates/extern-crate-hello-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ publish = false
[dependencies]
eyra = { path = "../.." }

# This is just an example crate, and not part of the c-ward workspace.
# This is just an example crate, and not part of the eyra workspace.
[workspace]
2 changes: 1 addition & 1 deletion example-crates/eyra-panic-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ publish = false
[dependencies]
std = { package = "eyra", path = "../.." }

# This is just an example crate, and not part of the c-ward workspace.
# This is just an example crate, and not part of the eyra workspace.
[workspace]
2 changes: 1 addition & 1 deletion example-crates/hello-world-lto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ std = { package = "eyra", path = "../.." }
[profile.release]
lto = true

# This is just an example crate, and not part of the c-ward workspace.
# This is just an example crate, and not part of the eyra workspace.
[workspace]
2 changes: 1 addition & 1 deletion example-crates/hello-world-small/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ lto = true
codegen-units = 1
panic = "abort"

# This is just an example crate, and not part of the c-ward workspace.
# This is just an example crate, and not part of the eyra workspace.
[workspace]
2 changes: 1 addition & 1 deletion example-crates/hello-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ publish = false
[dependencies]
std = { package = "eyra", path = "../.." }

# This is just an example crate, and not part of the c-ward workspace.
# This is just an example crate, and not part of the eyra workspace.
[workspace]
2 changes: 2 additions & 0 deletions example-crates/no-std/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
12 changes: 12 additions & 0 deletions example-crates/no-std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "no-std"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies]
eyra = { path = "../..", default-features = false }
rustix-dlmalloc = { version = "0.1.0", features = ["global"] }

# This is just an example crate, and not part of the eyra workspace.
[workspace]
3 changes: 3 additions & 0 deletions example-crates/no-std/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This crate demonstrates the use of Eyra in no-std mode.

This mode supports fewer features, but supports `no_std`.
4 changes: 4 additions & 0 deletions example-crates/no-std/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
// Pass -nostartfiles to the linker.
println!("cargo:rustc-link-arg=-nostartfiles");
}
22 changes: 22 additions & 0 deletions example-crates/no-std/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![no_std]
#![no_main]
#![feature(rustc_private)]
#![feature(lang_items)]
#![allow(internal_features)]

extern crate libc;
extern crate eyra;

#[no_mangle]
pub extern "C" fn main(_argc: i32, _argv: *const *const u8) -> i32 {
0
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! { loop {} }

#[global_allocator]
static GLOBAL_ALLOCATOR: rustix_dlmalloc::GlobalDlmalloc = rustix_dlmalloc::GlobalDlmalloc;

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#![doc = include_str!("../README.md")]
#![no_std]
#![cfg_attr(not(feature = "std"), no_std)]

// If enabled, re-export `std` so that we can be used as `std` to avoid the
// `extern crate eyra;`.
#[cfg(feature = "be-std")]
extern crate std;
#[cfg(feature = "be-std")]
#[doc(hidden)]
pub use std::*;

Expand Down
5 changes: 5 additions & 0 deletions tests/example_crates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,8 @@ fn example_crate_extern_crate_eyra_optional_example() {
None,
);
}

#[test]
fn example_crate_no_std() {
test_crate("no-std", &[], &[], "", "", None);
}
1 change: 1 addition & 0 deletions tests/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![feature(io_error_uncategorized)]
#![feature(read_buf)]
#![feature(maybe_uninit_uninit_array)]
#![feature(core_io_borrowed_buf)]

extern crate eyra;

Expand Down
1 change: 1 addition & 0 deletions tests/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![feature(io_error_uncategorized)]
#![feature(read_buf)]
#![feature(maybe_uninit_uninit_array)]
#![feature(core_io_borrowed_buf)]

extern crate eyra;

Expand Down

0 comments on commit cc0503c

Please sign in to comment.