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

fix(c-api) Rename lib's name to wasmer #2457

Merged
merged 2 commits into from
Jul 8, 2021
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
6 changes: 3 additions & 3 deletions .github/workflows/documentation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up dependencies
run: |
sudo apt-get update -y
sudo apt-get install -y libxkbcommon-dev doxygen
sudo apt-get install -y libxkbcommon-dev
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
Expand All @@ -26,9 +26,9 @@ jobs:
mkdir ${{ env.LLVM_DIR }}
tar xf llvm.tar.gz --strip-components=1 -C ${{ env.LLVM_DIR }}
echo "${{ env.LLVM_DIR }}/bin" >> $GITHUB_PATH
echo "LLVM_SYS_100_PREFIX=${{ env.LLVM_DIR }}" >> $GITHUB_ENV
echo "LLVM_SYS_110_PREFIX=${{ env.LLVM_DIR }}" >> $GITHUB_ENV
env:
LLVM_DIR: ${{ github.workspace }}/llvm-10
LLVM_DIR: ${{ github.workspace }}/llvm-11
LLVM_URL: 'https://github.com/wasmerio/llvm-custom-builds/releases/download/11.x/linux-amd64.tar.gz'
- name: Build & package documentation
run: make package-docs
Expand Down
12 changes: 0 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 22 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,20 @@ else
endif

build-docs:
cargo doc --release $(compiler_features) --document-private-items --no-deps --workspace
cargo doc --release $(compiler_features) --document-private-items --no-deps --workspace --exclude wasmer-c-api

capi-setup:
ifeq ($(IS_WINDOWS), 1)
RUSTFLAGS += -C target-feature=+crt-static
endif

build-docs-capi: capi-setup
# `wasmer-c-api` lib's name is `wasmer`. To avoid a conflict
# when generating the documentation, we rename it to its
# crate's name. Then we restore the lib's name.
sed -i '' -e 's/name = "wasmer" # ##lib.name##/name = "wasmer_c_api" # ##lib.name##/' lib/c-api/Cargo.toml
RUSTFLAGS="${RUSTFLAGS}" cargo doc --manifest-path lib/c-api/Cargo.toml --no-deps --features wat,universal,staticlib,dylib,cranelift,wasi
sed -i '' -e 's/name = "wasmer_c_api" # ##lib.name##/name = "wasmer" # ##lib.name##/' lib/c-api/Cargo.toml

build-capi: capi-setup
RUSTFLAGS="${RUSTFLAGS}" cargo build --manifest-path lib/c-api/Cargo.toml --release \
Expand Down Expand Up @@ -584,33 +589,29 @@ package-capi:
cp lib/c-api/wasm.h* package/include
cp lib/c-api/README.md package/include/README.md

# Windows
if [ -f $(TARGET_DIR)/wasmer_c_api.dll ]; then \
cp $(TARGET_DIR)/wasmer_c_api.dll package/lib/wasmer.dll ;\
if [ -f $(TARGET_DIR)/wasmer.dll ]; then \
cp $(TARGET_DIR)/wasmer.dll package/lib/wasmer.dll ;\
fi
if [ -f $(TARGET_DIR)/wasmer_c_api.lib ]; then \
cp $(TARGET_DIR)/wasmer_c_api.lib package/lib/wasmer.lib ;\
if [ -f $(TARGET_DIR)/wasmer.lib ]; then \
cp $(TARGET_DIR)/wasmer.lib package/lib/wasmer.lib ;\
fi

# For some reason in macOS arm64 there are issues if we copy constantly in the install_name_tool util
rm -f package/lib/libwasmer.dylib
if [ -f $(TARGET_DIR)/libwasmer_c_api.dylib ]; then \
cp $(TARGET_DIR)/libwasmer_c_api.dylib package/lib/libwasmer.dylib ;\
install_name_tool -id "@rpath/libwasmer.dylib" package/lib/libwasmer.dylib ;\
if [ -f $(TARGET_DIR)/libwasmer.dylib ]; then \
cp $(TARGET_DIR)/libwasmer.dylib package/lib/libwasmer.dylib ;\
fi

if [ -f $(TARGET_DIR)/libwasmer_c_api.so ]; then \
cp $(TARGET_DIR)/libwasmer_c_api.so package/lib/libwasmer.so ;\
if [ -f $(TARGET_DIR)/libwasmer.so ]; then \
cp $(TARGET_DIR)/libwasmer.so package/lib/libwasmer.so ;\
fi
if [ -f $(TARGET_DIR)/libwasmer_c_api.a ]; then \
cp $(TARGET_DIR)/libwasmer_c_api.a package/lib/libwasmer.a ;\
if [ -f $(TARGET_DIR)/libwasmer.a ]; then \
cp $(TARGET_DIR)/libwasmer.a package/lib/libwasmer.a ;\
fi

package-docs: build-docs build-docs-capi
mkdir -p "package/docs/c"
cp -R target/doc package/docs/crates
echo '<!-- Build $(SOURCE_VERSION) --><meta http-equiv="refresh" content="0; url=crates/wasmer/index.html">' > package/docs/index.html
echo '<!-- Build $(SOURCE_VERSION) --><meta http-equiv="refresh" content="0; url=wasmer/index.html">' > package/docs/crates/index.html
Comment on lines -612 to -613
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we remove <!-- Build $(SOURCE_VERSION) -->?

This is super useful for debugging the html and see which version was built with.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's not defined :-). This variable doesn't exist.

mkdir -p "package/docs/crates"
cp -R target/doc/ package/docs/crates
echo '<meta http-equiv="refresh" content="0; url=crates/wasmer/index.html">' > package/docs/index.html
echo '<meta http-equiv="refresh" content="0; url=wasmer/index.html">' > package/docs/crates/index.html

package: package-wapm package-wasmer package-minimal-headless-wasmer package-capi

Expand Down Expand Up @@ -645,13 +646,13 @@ install-capi-lib:
pkgver=$$(target/release/wasmer --version | cut -d\ -f2) && \
shortver="$${pkgver%.*}" && \
majorver="$${shortver%.*}" && \
install -Dm755 target/release/libwasmer_c_api.so "$(DESTDIR)/lib/libwasmer.so.$$pkgver" && \
install -Dm755 target/release/libwasmer.so "$(DESTDIR)/lib/libwasmer.so.$$pkgver" && \
ln -sf "libwasmer.so.$$pkgver" "$(DESTDIR)/lib/libwasmer.so.$$shortver" && \
ln -sf "libwasmer.so.$$pkgver" "$(DESTDIR)/lib/libwasmer.so.$$majorver" && \
ln -sf "libwasmer.so.$$pkgver" "$(DESTDIR)/lib/libwasmer.so"

install-capi-staticlib:
install -Dm644 target/release/libwasmer_c_api.a "$(DESTDIR)/lib/libwasmer.a"
install -Dm644 target/release/libwasmer.a "$(DESTDIR)/lib/libwasmer.a"

install-misc:
install -Dm644 LICENSE "$(DESTDIR)"/share/licenses/wasmer/LICENSE
Expand Down
16 changes: 11 additions & 5 deletions lib/c-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ readme = "README.md"
edition = "2018"

[lib]
# The library name is `wasmer` so that we generate dylib like
# `libwasmer.so`, `libwasmer.dylib`, `wasmer.dll` etc. But it creates
# a conflict with the existing `wasmer` crate, see below.
name = "wasmer" # ##lib.name##
# ^ DO NOT REMOVE, it's used the `Makefile`, see `build-docs-capi`.
crate-type = ["cdylib", "rlib", "staticlib"]

[dependencies]
wasmer = { version = "2.0.0", path = "../api", default-features = false }
# We rename `wasmer` to `wasmer-api` to avoid the conflict with this
# library name (see `[lib]`).
wasmer-api = { version = "2.0.0", path = "../api", default-features = false, package = "wasmer" }
wasmer-compiler-cranelift = { version = "2.0.0", path = "../compiler-cranelift", optional = true }
wasmer-compiler-singlepass = { version = "2.0.0", path = "../compiler-singlepass", optional = true }
wasmer-compiler-llvm = { version = "2.0.0", path = "../compiler-llvm", optional = true }
Expand Down Expand Up @@ -48,7 +55,7 @@ default = [
"wasi",
"middlewares",
]
wat = ["wasmer/wat"]
wat = ["wasmer-api/wat"]
wasi = ["wasmer-wasi", "typetag", "serde"]
engine = []
middlewares = ["wasmer-middlewares"]
Expand All @@ -65,7 +72,7 @@ staticlib = [
"engine",
]
compiler = [
"wasmer/compiler",
"wasmer-api/compiler",
"wasmer-engine-universal/compiler",
"wasmer-engine-dylib/compiler",
"wasmer-engine-staticlib/compiler"
Expand All @@ -91,5 +98,4 @@ native = ["dylib"]
#emscripten = ["wasmer-emscripten"]

[build-dependencies]
cbindgen = "0.19"
cdylib-link-lines = "0.1"
cbindgen = "0.19"
150 changes: 89 additions & 61 deletions lib/c-api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use cbindgen::{Builder, Language};
use std::{
env, fs,
env,
ffi::OsStr,
fs,
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -76,7 +78,7 @@ fn main() {

build_wasm_c_api_headers(&crate_dir, &out_dir);
build_inline_c_env_vars();
build_cdylib();
build_cdylib_link_arg();
}

/// Check whether we should build the C API headers or set `inline-c` up.
Expand Down Expand Up @@ -223,36 +225,7 @@ fn new_builder(language: Language, crate_dir: &str, include_guard: &str, header:
}

fn build_inline_c_env_vars() {
use std::ffi::OsStr;

// We start from `OUT_DIR` because `cargo publish` uses a different directory
// so traversing from `CARGO_MANIFEST_DIR` is less reliable.
let mut shared_object_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

assert_eq!(shared_object_dir.file_name(), Some(OsStr::new("out")));
shared_object_dir.pop();

assert!(shared_object_dir
.file_name()
.as_ref()
.unwrap()
.to_string_lossy()
.to_string()
.starts_with("wasmer-c-api"));
shared_object_dir.pop();

assert_eq!(shared_object_dir.file_name(), Some(OsStr::new("build")));
shared_object_dir.pop();
shared_object_dir.pop(); // "debug" or "release"

// We either find `target` or the target triple if cross-compiling.
if shared_object_dir.file_name() != Some(OsStr::new("target")) {
let target = env::var("TARGET").unwrap();
assert_eq!(shared_object_dir.file_name(), Some(OsStr::new(&target)));
}

shared_object_dir.push(env::var("PROFILE").unwrap());

let shared_object_dir = shared_object_dir();
let shared_object_dir = shared_object_dir.as_path().to_string_lossy();
let include_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

Expand All @@ -278,49 +251,104 @@ fn build_inline_c_env_vars() {
}

println!(
"cargo:rustc-env=INLINE_C_RS_LDFLAGS={shared_object_dir}/{lib}",
"cargo:rustc-env=INLINE_C_RS_LDFLAGS=-rpath,{shared_object_dir} {shared_object_dir}/{lib}",
shared_object_dir = shared_object_dir,
lib = if cfg!(target_os = "windows") {
"wasmer_c_api.dll".to_string()
"wasmer.dll".to_string()
} else if cfg!(target_os = "macos") {
"libwasmer_c_api.dylib".to_string()
"libwasmer.dylib".to_string()
} else {
let path = format!(
"{shared_object_dir}/{lib}",
shared_object_dir = shared_object_dir,
lib = "libwasmer_c_api.so"
lib = "libwasmer.so"
);

if Path::new(path.as_str()).exists() {
"libwasmer_c_api.so".to_string()
"libwasmer.so".to_string()
} else {
"libwasmer_c_api.a".to_string()
"libwasmer.a".to_string()
}
}
);
}

fn build_cdylib() {
// Configure the `soname`, `install_name`, `current_version`,
// `out-implib`, `output-def` etc. for as much platforms as
// possible.
cdylib_link_lines::shared_object_link_args(
"wasmer",
&env::var("CARGO_PKG_VERSION_MAJOR").unwrap(),
&env::var("CARGO_PKG_VERSION_MINOR").unwrap(),
&env::var("CARGO_PKG_VERSION_PATCH").unwrap(),
&env::var("CARGO_CFG_TARGET_ARCH").unwrap(),
&env::var("CARGO_CFG_TARGET_OS").unwrap(),
&env::var("CARGO_CFG_TARGET_ENV").unwrap(),
"/usr/local/lib".into(),
env::var_os("CARGO_TARGET_DIR").map_or(
{
let manifest_dir: PathBuf = env::var_os("CARGO_MANIFEST_DIR").unwrap().into();
manifest_dir
.join("target")
.join(env::var("PROFILE").unwrap())
},
Into::into,
),
);
fn build_cdylib_link_arg() {
// Code inspired by the `cdylib-link-lines` crate.
let mut lines = Vec::new();
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
let version_major = env::var("CARGO_PKG_VERSION_MAJOR").unwrap();
let version_minor = env::var("CARGO_PKG_VERSION_MINOR").unwrap();
let version_patch = env::var("CARGO_PKG_VERSION_PATCH").unwrap();
let shared_object_dir = shared_object_dir();

match (os.as_str(), env.as_str()) {
("android", _) => {
lines.push(format!("-Wl,-soname,libwasmer.so"));
}

("linux", _) | ("freebsd", _) | ("dragonfly", _) | ("netbsd", _) if env != "musl" => {
lines.push(format!("-Wl,-soname,libwasmer.so"));
}

("macos", _) | ("ios", _) => {
lines.push(format!(
"-Wl,-install_name,@rpath/libwasmer.dylib,-current_version,{x}.{y}.{z},-compatibility_version,{x}",
x = version_major,
y = version_minor,
z = version_patch,
));
}

("windows", "gnu") => {
// This is only set up to work on GNU toolchain versions of Rust
lines.push(format!(
"-Wl,--out-implib,{}",
shared_object_dir.join(format!("wasmer.dll.a")).display()
));
lines.push(format!(
"-Wl,--output-def,{}",
shared_object_dir.join(format!("wasmer.def")).display()
));
}

_ => {}
}

for line in lines {
println!("cargo:rustc-cdylib-link-arg={}", line);
}
}

fn shared_object_dir() -> PathBuf {
// We start from `OUT_DIR` because `cargo publish` uses a different directory
// so traversing from `CARGO_MANIFEST_DIR` is less reliable.
let mut shared_object_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

assert_eq!(shared_object_dir.file_name(), Some(OsStr::new("out")));
shared_object_dir.pop();

assert!(shared_object_dir
.file_name()
.as_ref()
.unwrap()
.to_string_lossy()
.to_string()
.starts_with("wasmer-c-api"));
shared_object_dir.pop();

assert_eq!(shared_object_dir.file_name(), Some(OsStr::new("build")));
shared_object_dir.pop();
shared_object_dir.pop(); // "debug" or "release"

// We either find `target` or the target triple if cross-compiling.
if shared_object_dir.file_name() != Some(OsStr::new("target")) {
let target = env::var("TARGET").unwrap();
assert_eq!(shared_object_dir.file_name(), Some(OsStr::new(&target)));
}

shared_object_dir.push(env::var("PROFILE").unwrap());

shared_object_dir
}
2 changes: 1 addition & 1 deletion lib/c-api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ thread_local! {
/// # Example
///
/// ```rust,no_run
/// # use wasmer_c_api::error::{update_last_error, CApiError};
/// # use wasmer::error::{update_last_error, CApiError};
///
/// update_last_error(CApiError {
/// msg: "Hello, World!".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion lib/c-api/src/ordered_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! by index and not by module and name.
use std::iter::FromIterator;
use wasmer::{Export, Exportable, Extern, Resolver};
use wasmer_api::{Export, Exportable, Extern, Resolver};

/// An `OrderedResolver` stores all the `externs` provided to an Instance
/// in a Vec, so we can retrieve them later based on index.
Expand Down
Loading