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

create-exe: prefer libwasmer headless when cross-compiling #3103

Merged
merged 3 commits into from
Aug 12, 2022
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: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,6 @@ package-capi-headless: build-capi-headless
fi
if [ -f $(TARGET_DIR)/libwasmer.a ]; then \
cp $(TARGET_DIR)/libwasmer.a package/lib/libwasmer-headless.a ;\
strip package/lib/libwasmer-headless.a ;\
fi

package-docs: build-docs build-docs-capi
Expand Down
2 changes: 2 additions & 0 deletions lib/c-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ compiler = [
"wasmer-compiler/compiler",
]
compiler-headless = [
"wasmer-artifact-load",
"static-artifact-load",
"wasmer-api/compiler",
"wasmer-compiler/translator",
"wasmer-compiler/compiler",
Expand Down
21 changes: 19 additions & 2 deletions lib/cli/src/commands/create_exe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,30 @@ impl CreateExe {
} else {
"lib/libwasmer.a"
};
let libwasmer_headless_path = if self
.target_triple
.clone()
.unwrap_or(Triple::host())
.operating_system
== wasmer_types::OperatingSystem::Windows
{
"lib/wasmer.lib"
} else {
"lib/libwasmer-headless.a"
};
let filename = if let Some(local_tarball) = cross_subc.tarball {
let files = untar(local_tarball)?;
files.into_iter().find(|f| f.contains(libwasmer_path)).ok_or_else(|| {
files.clone().into_iter().find(|f| f.contains(libwasmer_headless_path)).or_else(||
files.into_iter().find(|f| f.contains(libwasmer_path))).ok_or_else(|| {
anyhow!("Could not find libwasmer for {} target in the provided tarball path.", target)})?
} else {
#[cfg(feature = "http")]
{
let release = http_fetch::get_latest_release()?;
let tarball = http_fetch::download_release(release, target.clone())?;
let files = untar(tarball)?;
files.into_iter().find(|f| f.contains(libwasmer_path)).ok_or_else(|| {
files.clone().into_iter().find(|f| f.contains(libwasmer_headless_path)).or_else(||
files.into_iter().find(|f| f.contains(libwasmer_path))).ok_or_else(|| {
anyhow!("Could not find libwasmer for {} target in the fetched release from Github: you can download it manually and specify its path with the --cross-compilation-library-path LIBRARY_PATH flag.", target)})?
}
#[cfg(not(feature = "http"))]
Expand Down Expand Up @@ -456,6 +469,10 @@ impl CreateExe {
let mut cmd = Command::new(zig_binary_path);
let mut cmd_mut: &mut Command = cmd
.arg("cc")
.arg("-w")
.arg("-fgnu-inline-asm")
.arg("-fsanitize=undefined")
.arg("-fsanitize-trap=undefined")
.arg("-target")
.arg(&zig_triple)
.arg(&format!("-L{}", libwasmer_path.display()))
Expand Down
37 changes: 19 additions & 18 deletions lib/compiler/src/engine/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,28 @@ use crate::{
use crate::{Compiler, FunctionBodyData, ModuleTranslationState};
use crate::{Engine, EngineInner};
use enumset::EnumSet;
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
use std::collections::BTreeMap;
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
use std::mem;
use std::sync::Arc;
use std::sync::Mutex;
#[cfg(feature = "static-artifact-create")]
use wasmer_object::{emit_compilation, emit_data, get_object_for_target, Object};
use wasmer_types::entity::{BoxedSlice, PrimaryMap};
use wasmer_types::MetadataHeader;
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
use wasmer_types::{
compilation::symbols::{ModuleMetadata, ModuleMetadataSymbolRegistry},
entity::EntityRef,
CompileModuleInfo, Target,
};
use wasmer_types::{
CompileError, CpuFeature, DataInitializer, DeserializeError, FunctionIndex, LocalFunctionIndex,
MemoryIndex, ModuleInfo, OwnedDataInitializer, SerializableModule, SerializeError,
SignatureIndex, TableIndex,
};
#[cfg(feature = "static-artifact-create")]
use wasmer_types::{CompileModuleInfo, Target};
use wasmer_vm::{FunctionBodyPtr, MemoryStyle, TableStyle, VMSharedSignatureIndex, VMTrampoline};
use wasmer_vm::{InstanceAllocator, InstanceHandle, StoreObjects, TrapHandlerFn, VMExtern};

Expand All @@ -44,7 +45,7 @@ pub enum Artifact {
Universal(UniversalArtifact),
/// Stores functions etc as symbols and data meant to be stored in object files and
/// executables.
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
Static(StaticArtifact),
}

Expand All @@ -64,7 +65,7 @@ pub type PrefixerFn = Box<dyn Fn(&[u8]) -> String + Send>;

/// Stores functions etc as symbols and data meant to be stored in object files and
/// executables.
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
pub struct StaticArtifact {
metadata: ModuleMetadata,
_module_bytes: Vec<u8>,
Expand Down Expand Up @@ -245,55 +246,55 @@ impl ArtifactCreate for Artifact {
fn create_module_info(&self) -> ModuleInfo {
match self {
Self::Universal(UniversalArtifact { artifact, .. }) => artifact.create_module_info(),
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
Self::Static(StaticArtifact { metadata, .. }) => metadata.compile_info.module.clone(),
}
}

fn features(&self) -> &Features {
match self {
Self::Universal(UniversalArtifact { artifact, .. }) => artifact.features(),
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
Self::Static(StaticArtifact { metadata, .. }) => &metadata.compile_info.features,
}
}

fn cpu_features(&self) -> EnumSet<CpuFeature> {
match self {
Self::Universal(_self) => _self.artifact.cpu_features(),
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
Self::Static(_self) => EnumSet::from_u64(_self.metadata.cpu_features),
}
}

fn data_initializers(&self) -> &[OwnedDataInitializer] {
match self {
Self::Universal(_self) => _self.artifact.data_initializers(),
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
Self::Static(_self) => &_self.metadata.data_initializers,
}
}

fn memory_styles(&self) -> &PrimaryMap<MemoryIndex, MemoryStyle> {
match self {
Self::Universal(_self) => _self.artifact.memory_styles(),
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
Self::Static(_self) => &_self.metadata.compile_info.memory_styles,
}
}

fn table_styles(&self) -> &PrimaryMap<TableIndex, TableStyle> {
match self {
Self::Universal(UniversalArtifact { artifact, .. }) => artifact.table_styles(),
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
Self::Static(StaticArtifact { metadata, .. }) => &metadata.compile_info.table_styles,
}
}

fn serialize(&self) -> Result<Vec<u8>, SerializeError> {
match self {
Self::Universal(UniversalArtifact { artifact, .. }) => artifact.serialize(),
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
Self::Static(StaticArtifact { .. }) => todo!(),
}
}
Expand Down Expand Up @@ -328,7 +329,7 @@ impl Artifact {
frame_infos.clone(),
);
}
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
Self::Static(_self) => {
// Do nothing for static artifact
}
Expand All @@ -340,7 +341,7 @@ impl Artifact {
pub fn finished_functions(&self) -> &BoxedSlice<LocalFunctionIndex, FunctionBodyPtr> {
match self {
Self::Universal(_self) => &_self.finished_functions,
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
Self::Static(_self) => &_self.finished_functions,
}
}
Expand All @@ -350,7 +351,7 @@ impl Artifact {
pub fn finished_function_call_trampolines(&self) -> &BoxedSlice<SignatureIndex, VMTrampoline> {
match self {
Self::Universal(_self) => &_self.finished_function_call_trampolines,
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
Self::Static(_self) => &_self.finished_function_call_trampolines,
}
}
Expand All @@ -362,7 +363,7 @@ impl Artifact {
) -> &BoxedSlice<FunctionIndex, FunctionBodyPtr> {
match self {
Self::Universal(_self) => &_self.finished_dynamic_function_trampolines,
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
Self::Static(_self) => &_self.finished_dynamic_function_trampolines,
}
}
Expand All @@ -371,7 +372,7 @@ impl Artifact {
pub fn signatures(&self) -> &BoxedSlice<SignatureIndex, VMSharedSignatureIndex> {
match self {
Self::Universal(_self) => &_self.signatures,
#[cfg(feature = "static-artifact-create")]
#[cfg(any(feature = "static-artifact-create", feature = "static-artifact-load"))]
Self::Static(_self) => &_self.signatures,
}
}
Expand Down