diff --git a/README.rst b/README.rst index 690472578b..caa24342d9 100644 --- a/README.rst +++ b/README.rst @@ -60,18 +60,20 @@ installed. Building with `no_std` ---------------------- -To build cretonne without libstd, enable the `no_std` feature on `lib/cretonne`, -`lib/frontend`, `lib/native`, and `lib/wasm`. +To build cretonne without libstd, disable the `std` feature on `lib/cretonne`, +`lib/frontend`, `lib/native`, and `lib/wasm`, which is otherwise enabled by +default, and enable the `no_std` feature. For example, to build `cretonne`: cd lib/cretonne - cargo build --features no_std + cargo build --no-default-features --features no_std Or, when using `cretonne` as a dependency (in Cargo.toml): [dependency.cretonne] - path = "..." + ... + default-features = false features = ["no_std"] `no_std` is currently "best effort". We won't try to break it, and we'll diff --git a/cranelift/test-no_std.sh b/cranelift/test-no_std.sh index c9d3fe38f7..d3edbb921f 100755 --- a/cranelift/test-no_std.sh +++ b/cranelift/test-no_std.sh @@ -21,6 +21,7 @@ for LIB in $LIBS do banner "Rust unit tests in $LIB" cd "lib/$LIB" + cargo test --no-default-features --features no_std cargo test --features no_std cd "$topdir" done diff --git a/lib/cretonne/Cargo.toml b/lib/cretonne/Cargo.toml index 0cf3fffd9b..c1c666fa77 100644 --- a/lib/cretonne/Cargo.toml +++ b/lib/cretonne/Cargo.toml @@ -17,14 +17,19 @@ name = "cretonne" # Please don't add any unless they are essential to the task of creating binary # machine code. Integration tests that need external dependencies can be # accomodated in `tests`. + [dependencies.hashmap_core] version = "0.1.1" optional = true + [dependencies.error_core] version = "0.1.0" optional = true [features] -# Currently, the only feature is the `no_std` feature. -# Enabling this disables use of `stdlib`. -no_std = ["hashmap_core", "error_core"] \ No newline at end of file +# The "std" feature enables use of libstd. The "no_std" feature enables use +# of some minimal std-like replacement libraries. At least one of these two +# features to be enabled. +default = ["std"] +std = [] +no_std = ["hashmap_core", "error_core"] diff --git a/lib/cretonne/src/lib.rs b/lib/cretonne/src/lib.rs index 95868a6933..0952969bb1 100644 --- a/lib/cretonne/src/lib.rs +++ b/lib/cretonne/src/lib.rs @@ -1,16 +1,16 @@ //! Cretonne code generation library. -#![cfg_attr(feature = "no_std", no_std)] #![deny(missing_docs)] // Turns on alloc feature if no_std -#![cfg_attr(feature = "no_std", feature(alloc))] +#![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr(not(feature = "std"), feature(alloc))] // Include the `hashmap_core` crate if no_std #[cfg(feature = "no_std")] extern crate hashmap_core; #[cfg(feature = "no_std")] extern crate error_core; -#[cfg(feature = "no_std")] +#[cfg(not(feature = "std"))] #[macro_use] extern crate alloc; @@ -60,7 +60,7 @@ mod unreachable_code; mod write; /// This replaces `std` in builds with no_std. -#[cfg(feature = "no_std")] +#[cfg(not(feature = "std"))] mod std { pub use core::*; pub use alloc::{boxed, vec, string}; diff --git a/lib/frontend/Cargo.toml b/lib/frontend/Cargo.toml index 32c65e4ded..bccc84f6bc 100644 --- a/lib/frontend/Cargo.toml +++ b/lib/frontend/Cargo.toml @@ -12,7 +12,9 @@ readme = "README.md" name = "cton_frontend" [dependencies] -cretonne = { path = "../cretonne", version = "0.1.0" } +cretonne = { path = "../cretonne", version = "0.1.0", default-features = false } [features] +default = ["std"] +std = ["cretonne/std"] no_std = ["cretonne/no_std"] diff --git a/lib/frontend/src/lib.rs b/lib/frontend/src/lib.rs index 84af6f976b..52765b5a20 100644 --- a/lib/frontend/src/lib.rs +++ b/lib/frontend/src/lib.rs @@ -142,14 +142,14 @@ //! } //! ``` -#![cfg_attr(feature = "no_std", no_std)] #![deny(missing_docs)] -#![cfg_attr(feature = "no_std", feature(alloc))] +#![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr(not(feature = "std"), feature(alloc))] extern crate cretonne; -#[cfg(feature = "no_std")] +#[cfg(not(feature = "std"))] extern crate alloc; pub use frontend::{ILBuilder, FunctionBuilder}; @@ -157,7 +157,7 @@ pub use frontend::{ILBuilder, FunctionBuilder}; mod frontend; mod ssa; -#[cfg(feature = "no_std")] +#[cfg(not(feature = "std"))] mod std { pub use alloc::vec; pub use core::*; diff --git a/lib/native/Cargo.toml b/lib/native/Cargo.toml index 57a2d0bc3c..32417e8718 100644 --- a/lib/native/Cargo.toml +++ b/lib/native/Cargo.toml @@ -11,10 +11,12 @@ readme = "README.md" name = "cton_native" [dependencies] -cretonne = { path = "../cretonne", version = "0.1.0" } +cretonne = { path = "../cretonne", version = "0.1.0", default-features = false } [target.'cfg(any(target_arch = "x86", target_arch = "x86_64"))'.dependencies] raw-cpuid = "3.0.0" [features] +default = ["std"] +std = ["cretonne/std"] no_std = ["cretonne/no_std"] diff --git a/lib/native/src/lib.rs b/lib/native/src/lib.rs index 284366a65c..514e6df314 100644 --- a/lib/native/src/lib.rs +++ b/lib/native/src/lib.rs @@ -1,8 +1,9 @@ //! Performs autodetection of the host for the purposes of running //! Cretonne to generate code to run on the same machine. -#![cfg_attr(feature = "no_std", no_std)] #![deny(missing_docs)] +#![cfg_attr(not(feature = "std"), no_std)] + extern crate cretonne; #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] diff --git a/lib/wasm/Cargo.toml b/lib/wasm/Cargo.toml index 448166d568..3bd2b36aa8 100644 --- a/lib/wasm/Cargo.toml +++ b/lib/wasm/Cargo.toml @@ -12,12 +12,13 @@ name = "cton_wasm" [dependencies] wasmparser = "0.14.1" -cretonne = { path = "../cretonne", version = "0.1.0" } -cretonne-frontend = { path = "../frontend", version = "0.1.0" } +cretonne = { path = "../cretonne", version = "0.1.0", default_features = false } +cretonne-frontend = { path = "../frontend", version = "0.1.0", default_features = false } [dependencies.hashmap_core] version = "0.1.1" optional = true + [dependencies.error_core] version = "0.1.0" optional = true @@ -26,6 +27,6 @@ optional = true tempdir = "0.3.5" [features] -# Currently, the only feature is the `no_std` feature. -# Enabling this disables use of `stdlib`. -no_std = ["hashmap_core", "error_core", "cretonne/no_std", "cretonne-frontend/no_std"] \ No newline at end of file +default = ["std"] +std = ["cretonne/std", "cretonne-frontend/std"] +no_std = ["hashmap_core", "error_core", "cretonne/no_std", "cretonne-frontend/no_std"] diff --git a/lib/wasm/src/lib.rs b/lib/wasm/src/lib.rs index 9005e6cd42..76e69887d9 100644 --- a/lib/wasm/src/lib.rs +++ b/lib/wasm/src/lib.rs @@ -9,12 +9,12 @@ //! //! The main function of this module is [`translate_module`](fn.translate_module.html). -#![cfg_attr(feature = "no_std", no_std)] #![deny(missing_docs)] -#![cfg_attr(feature = "no_std", feature(alloc))] +#![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr(not(feature = "std"), feature(alloc))] -#[cfg(feature = "no_std")] +#[cfg(not(feature = "std"))] #[macro_use] extern crate alloc; @@ -42,7 +42,7 @@ pub use environ::{FuncEnvironment, ModuleEnvironment, DummyEnvironment, GlobalVa pub use translation_utils::{FunctionIndex, GlobalIndex, TableIndex, MemoryIndex, SignatureIndex, Global, GlobalInit, Table, Memory}; -#[cfg(feature = "no_std")] +#[cfg(not(feature = "std"))] mod std { pub use alloc::vec; pub use alloc::string;