Skip to content

Commit cd87983

Browse files
committed
feat(engine-native) Store cross-compilation and proper Linker type inside NativeEngineInner.
1 parent 9fcbbbe commit cd87983

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed

lib/engine-native/src/artifact.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,7 @@ impl NativeArtifact {
256256
.map_err(to_compile_error)?
257257
};
258258

259-
let host_target = Triple::host();
260-
let is_cross_compiling = target_triple != &host_target;
259+
let is_cross_compiling = engine_inner.is_cross_compiling();
261260
let cross_compiling_args: Vec<String> = if is_cross_compiling {
262261
vec![
263262
format!("--target={}", target_triple),
@@ -276,15 +275,10 @@ impl NativeArtifact {
276275
trace!(
277276
"Compiling for target {} from host {}",
278277
target_triple.to_string(),
279-
host_target.to_string()
278+
Triple::host().to_string(),
280279
);
281280

282-
let linker = if is_cross_compiling {
283-
"clang-10"
284-
} else {
285-
"gcc"
286-
};
287-
281+
let linker: &'static str = engine_inner.linker().into();
288282
let output = Command::new(linker)
289283
.arg(&filepath)
290284
.arg("-o")

lib/engine-native/src/engine.rs

+52-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::sync::Arc;
77
use std::sync::Mutex;
88
#[cfg(feature = "compiler")]
99
use wasmer_compiler::Compiler;
10-
use wasmer_compiler::{CompileError, Target};
10+
use wasmer_compiler::{CompileError, Target, Triple};
1111
use wasmer_engine::{Artifact, DeserializeError, Engine, EngineId, Tunables};
1212
#[cfg(feature = "compiler")]
1313
use wasmer_types::Features;
@@ -29,9 +29,22 @@ impl NativeEngine {
2929
/// Create a new `NativeEngine` with the given config
3030
#[cfg(feature = "compiler")]
3131
pub fn new(compiler: Box<dyn Compiler + Send>, target: Target, features: Features) -> Self {
32-
if let Err(_) = which("gcc").or_else(|_| which("clang-10")) {
33-
panic!("gcc/clang is not found, it is required for using the `NativeEngine`");
34-
}
32+
let host_target = Triple::host();
33+
let is_cross_compiling = target.triple() != &host_target;
34+
35+
let linker = if is_cross_compiling {
36+
which(Into::<&'static str>::into(Linker::Clang10))
37+
.map(|_| Linker::Clang10)
38+
.or_else(|_| {
39+
which(Into::<&'static str>::into(Linker::Clang))
40+
.map(|_| Linker::Clang)
41+
})
42+
.expect("Nor `clang-10` or `clang` has been found, at least one of them is required for the `NativeEngine`")
43+
} else {
44+
which(Into::<&'static str>::into(Linker::Gcc))
45+
.map(|_| Linker::Gcc)
46+
.expect("`gcc` has not been found, it is required for the `NativeEngine`")
47+
};
3548

3649
Self {
3750
inner: Arc::new(Mutex::new(NativeEngineInner {
@@ -40,6 +53,8 @@ impl NativeEngine {
4053
signatures: SignatureRegistry::new(),
4154
prefixer: None,
4255
features,
56+
is_cross_compiling,
57+
linker,
4358
})),
4459
target: Arc::new(target),
4560
engine_id: EngineId::default(),
@@ -69,6 +84,8 @@ impl NativeEngine {
6984
trampolines: HashMap::new(),
7085
signatures: SignatureRegistry::new(),
7186
prefixer: None,
87+
is_cross_compiling: false,
88+
linker: Linker::None,
7289
})),
7390
target: Arc::new(Target::default()),
7491
engine_id: EngineId::default(),
@@ -178,6 +195,25 @@ impl Engine for NativeEngine {
178195
}
179196
}
180197

198+
#[derive(Clone, Copy)]
199+
pub(crate) enum Linker {
200+
None,
201+
Clang10,
202+
Clang,
203+
Gcc,
204+
}
205+
206+
impl Into<&'static str> for Linker {
207+
fn into(self) -> &'static str {
208+
match self {
209+
Self::None => "",
210+
Self::Clang10 => "clang-10",
211+
Self::Clang => "clang",
212+
Self::Gcc => "gcc",
213+
}
214+
}
215+
}
216+
181217
/// The inner contents of `NativeEngine`
182218
pub struct NativeEngineInner {
183219
/// The compiler
@@ -195,6 +231,10 @@ pub struct NativeEngineInner {
195231
/// the functions in the shared object generated by the `NativeEngine`,
196232
/// so we can assure no collisions.
197233
prefixer: Option<Box<dyn Fn(&[u8]) -> String + Send>>,
234+
/// Whether the native engine will cross-compile.
235+
is_cross_compiling: bool,
236+
/// The linker to use.
237+
linker: Linker,
198238
}
199239

200240
impl NativeEngineInner {
@@ -255,4 +295,12 @@ impl NativeEngineInner {
255295
// where they belong become unallocated.
256296
self.trampolines.insert(index, trampoline);
257297
}
298+
299+
pub(crate) fn is_cross_compiling(&self) -> bool {
300+
self.is_cross_compiling
301+
}
302+
303+
pub(crate) fn linker(&self) -> Linker {
304+
self.linker
305+
}
258306
}

0 commit comments

Comments
 (0)