From b2b20a95a10e414be9c66fc931801d15ee295c69 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 7 Jun 2018 11:26:09 -0700 Subject: [PATCH] Fix missing no_std support in cretonne-module. And, tidy up the extern crate declarations in the std replacement modules. --- lib/codegen/src/lib.rs | 11 +++++------ lib/frontend/src/lib.rs | 7 +++---- lib/module/src/data_context.rs | 2 ++ lib/module/src/lib.rs | 24 +++++++++++++++++++++++- lib/module/src/module.rs | 2 ++ lib/wasm/src/lib.rs | 18 ++++++++---------- 6 files changed, 43 insertions(+), 21 deletions(-) diff --git a/lib/codegen/src/lib.rs b/lib/codegen/src/lib.rs index cfcae5d344..dce082e02a 100644 --- a/lib/codegen/src/lib.rs +++ b/lib/codegen/src/lib.rs @@ -42,10 +42,6 @@ #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(feature = "std"), feature(alloc))] -// Include the `hashmap_core` crate if std is not available. -#[allow(unused_extern_crates)] -#[cfg(not(feature = "std"))] -extern crate hashmap_core; #[cfg(not(feature = "std"))] #[macro_use] extern crate alloc; @@ -119,8 +115,11 @@ mod std { pub use alloc::{boxed, string, vec}; pub use core::*; pub mod collections { + #[allow(unused_extern_crates)] + extern crate hashmap_core; + + pub use self::hashmap_core::map as hash_map; + pub use self::hashmap_core::{HashMap, HashSet}; pub use alloc::BTreeSet; - pub use hashmap_core::map as hash_map; - pub use hashmap_core::{HashMap, HashSet}; } } diff --git a/lib/frontend/src/lib.rs b/lib/frontend/src/lib.rs index 8b25becb33..8a414dd849 100644 --- a/lib/frontend/src/lib.rs +++ b/lib/frontend/src/lib.rs @@ -143,9 +143,6 @@ extern crate cretonne_codegen; -#[cfg(not(feature = "std"))] -extern crate alloc; - pub use frontend::{FunctionBuilder, FunctionBuilderContext}; pub use variable::Variable; @@ -155,6 +152,8 @@ mod variable; #[cfg(not(feature = "std"))] mod std { - pub use alloc::vec; + extern crate alloc; + + pub use self::alloc::vec; pub use core::*; } diff --git a/lib/module/src/data_context.rs b/lib/module/src/data_context.rs index ef327fce88..0fef3f6400 100644 --- a/lib/module/src/data_context.rs +++ b/lib/module/src/data_context.rs @@ -3,6 +3,8 @@ use cretonne_codegen::binemit::{Addend, CodeOffset}; use cretonne_codegen::entity::PrimaryMap; use cretonne_codegen::ir; +use std::boxed::Box; +use std::vec::Vec; /// This specifies how data is to be initialized. #[derive(PartialEq, Eq, Debug)] diff --git a/lib/module/src/lib.rs b/lib/module/src/lib.rs index 5c1091dd3f..445b588c40 100644 --- a/lib/module/src/lib.rs +++ b/lib/module/src/lib.rs @@ -1,7 +1,8 @@ //! Top-level lib.rs for `cretonne_module`. #![deny(missing_docs, trivial_numeric_casts, unused_extern_crates)] -#![warn(unused_import_braces, unstable_features)] +#![warn(unused_import_braces)] +#![cfg_attr(feature = "std", warn(unstable_features))] #![cfg_attr(feature = "clippy", plugin(clippy(conf_file = "../../clippy.toml")))] #![cfg_attr(feature = "cargo-clippy", allow(new_without_default, new_without_default_derive))] #![cfg_attr( @@ -11,6 +12,13 @@ print_stdout, unicode_not_nfc, use_self ) )] +// Turns on no_std and alloc features if std is not available. +#![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr(not(feature = "std"), feature(alloc))] + +#[cfg(not(feature = "std"))] +#[cfg_attr(test, macro_use)] +extern crate alloc; #[macro_use] extern crate cretonne_codegen; @@ -26,3 +34,17 @@ mod module; pub use backend::Backend; pub use data_context::{DataContext, DataDescription, Init, Writability}; pub use module::{DataId, FuncId, FuncOrDataId, Linkage, Module, ModuleError, ModuleNamespace}; + +/// This replaces `std` in builds with `core`. +#[cfg(not(feature = "std"))] +mod std { + pub use alloc::{borrow, boxed, string, vec}; + pub use core::*; + pub mod collections { + #[allow(unused_extern_crates)] + extern crate hashmap_core; + + pub use self::hashmap_core::map as hash_map; + pub use self::hashmap_core::{HashMap, HashSet}; + } +} diff --git a/lib/module/src/module.rs b/lib/module/src/module.rs index ee6943432c..23b8328730 100644 --- a/lib/module/src/module.rs +++ b/lib/module/src/module.rs @@ -9,7 +9,9 @@ use cretonne_codegen::entity::{EntityRef, PrimaryMap}; use cretonne_codegen::result::CtonError; use cretonne_codegen::{binemit, ir, Context}; use data_context::DataContext; +use std::borrow::ToOwned; use std::collections::HashMap; +use std::string::String; use Backend; /// A function identifier for use in the `Module` interface. diff --git a/lib/wasm/src/lib.rs b/lib/wasm/src/lib.rs index a7ceeee389..35256c924c 100644 --- a/lib/wasm/src/lib.rs +++ b/lib/wasm/src/lib.rs @@ -24,13 +24,6 @@ #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(feature = "std"), feature(alloc))] -#[cfg(not(feature = "std"))] -extern crate alloc; - -#[allow(unused_extern_crates)] -#[cfg(not(feature = "std"))] -extern crate hashmap_core; - #[macro_use(dbg)] extern crate cretonne_codegen; extern crate cretonne_frontend; @@ -58,12 +51,17 @@ pub use translation_utils::{FunctionIndex, Global, GlobalIndex, GlobalInit, Memo #[cfg(not(feature = "std"))] mod std { - pub use alloc::string; - pub use alloc::vec; + extern crate alloc; + + pub use self::alloc::string; + pub use self::alloc::vec; pub use core::fmt; pub use core::option; pub use core::{cmp, i32, str, u32}; pub mod collections { - pub use hashmap_core::{map as hash_map, HashMap}; + #[allow(unused_extern_crates)] + extern crate hashmap_core; + + pub use self::hashmap_core::{map as hash_map, HashMap}; } }