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

feat(native-engine) Panic if gcc/clang-10 is missing #1619

Merged
merged 2 commits into from
Sep 14, 2020
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
1 change: 1 addition & 0 deletions lib/engine-native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ bincode = "1.3"
leb128 = "0.2"
libloading = "0.6"
tempfile = "3.1"
which = "4.0"

[features]
# Enable the `compiler` feature if you want the engine to compile
Expand Down
12 changes: 3 additions & 9 deletions lib/engine-native/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ impl NativeArtifact {
.map_err(to_compile_error)?
};

let host_target = Triple::host();
let is_cross_compiling = target_triple != &host_target;
let is_cross_compiling = engine_inner.is_cross_compiling();
let cross_compiling_args: Vec<String> = if is_cross_compiling {
vec![
format!("--target={}", target_triple),
Expand All @@ -276,15 +275,10 @@ impl NativeArtifact {
trace!(
"Compiling for target {} from host {}",
target_triple.to_string(),
host_target.to_string()
Triple::host().to_string(),
);

let linker = if is_cross_compiling {
"clang-10"
} else {
"gcc"
};

let linker: &'static str = engine_inner.linker().into();
let output = Command::new(linker)
.arg(&filepath)
.arg("-o")
Expand Down
56 changes: 55 additions & 1 deletion lib/engine-native/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use std::sync::Arc;
use std::sync::Mutex;
#[cfg(feature = "compiler")]
use wasmer_compiler::Compiler;
use wasmer_compiler::{CompileError, Target};
use wasmer_compiler::{CompileError, Target, Triple};
use wasmer_engine::{Artifact, DeserializeError, Engine, EngineId, Tunables};
#[cfg(feature = "compiler")]
use wasmer_types::Features;
use wasmer_types::FunctionType;
use wasmer_vm::{SignatureRegistry, VMSharedSignatureIndex, VMTrampoline};
#[cfg(feature = "compiler")]
use which::which;

/// A WebAssembly `Native` Engine.
#[derive(Clone)]
Expand All @@ -27,13 +29,32 @@ impl NativeEngine {
/// Create a new `NativeEngine` with the given config
#[cfg(feature = "compiler")]
pub fn new(compiler: Box<dyn Compiler + Send>, target: Target, features: Features) -> Self {
let host_target = Triple::host();
let is_cross_compiling = target.triple() != &host_target;

let linker = if is_cross_compiling {
which(Into::<&'static str>::into(Linker::Clang10))
.map(|_| Linker::Clang10)
.or_else(|_| {
which(Into::<&'static str>::into(Linker::Clang))
.map(|_| Linker::Clang)
})
.expect("Nor `clang-10` or `clang` has been found, at least one of them is required for the `NativeEngine`")
} else {
which(Into::<&'static str>::into(Linker::Gcc))
.map(|_| Linker::Gcc)
.expect("`gcc` has not been found, it is required for the `NativeEngine`")
};

Self {
inner: Arc::new(Mutex::new(NativeEngineInner {
compiler: Some(compiler),
trampolines: HashMap::new(),
signatures: SignatureRegistry::new(),
prefixer: None,
features,
is_cross_compiling,
linker,
})),
target: Arc::new(target),
engine_id: EngineId::default(),
Expand Down Expand Up @@ -63,6 +84,8 @@ impl NativeEngine {
trampolines: HashMap::new(),
signatures: SignatureRegistry::new(),
prefixer: None,
is_cross_compiling: false,
linker: Linker::None,
})),
target: Arc::new(Target::default()),
engine_id: EngineId::default(),
Expand Down Expand Up @@ -172,6 +195,25 @@ impl Engine for NativeEngine {
}
}

#[derive(Clone, Copy)]
pub(crate) enum Linker {
None,
Clang10,
Clang,
Gcc,
}

impl Into<&'static str> for Linker {
fn into(self) -> &'static str {
match self {
Self::None => "",
Self::Clang10 => "clang-10",
Self::Clang => "clang",
Self::Gcc => "gcc",
}
}
}

/// The inner contents of `NativeEngine`
pub struct NativeEngineInner {
/// The compiler
Expand All @@ -189,6 +231,10 @@ pub struct NativeEngineInner {
/// the functions in the shared object generated by the `NativeEngine`,
/// so we can assure no collisions.
prefixer: Option<Box<dyn Fn(&[u8]) -> String + Send>>,
/// Whether the native engine will cross-compile.
is_cross_compiling: bool,
/// The linker to use.
linker: Linker,
}

impl NativeEngineInner {
Expand Down Expand Up @@ -249,4 +295,12 @@ impl NativeEngineInner {
// where they belong become unallocated.
self.trampolines.insert(index, trampoline);
}

pub(crate) fn is_cross_compiling(&self) -> bool {
self.is_cross_compiling
}

pub(crate) fn linker(&self) -> Linker {
self.linker
}
}