Move the C API to a separate crate (#818)

* Move the C API to a separate crate

This commit moves the C API from `crates/api/src/wasm.rs` to
`crates/capi/src/lib.rs` to be located in a separate crate. There's a
number of reasons for this:

* When a Rust program depends on the `wasmtime` crate, there's no need
  to compile in the C API.
* This should improve compile times of the `wasmtime` crate since it's
  not producing artifacts which aren't always used.
* The development of the C API can be guaranteed to only use the public
  API of the `wasmtime` crate itself.

Some CI pieces are tweaked and this overall shouldn't have much impact
on users, it's intended that it's a cleanup/speedup for developers!

* Disable rustdoc/tests for capi

* Review feedback

* Add back in accidentally deleted comment

* More renamings

* Try to fix dotnet build
This commit is contained in:
Alex Crichton
2020-01-14 13:36:57 -06:00
committed by Dan Gohman
parent 7f997fe7a6
commit 364fa994ed
17 changed files with 289 additions and 29 deletions

View File

@@ -8,10 +8,6 @@ repository = "https://github.com/bytecodealliance/wasmtime"
readme = "README.md"
edition = "2018"
[lib]
name = "wasmtime"
crate-type = ["lib", "staticlib", "cdylib"]
[dependencies]
wasmtime-runtime = { path = "../runtime", version = "0.9.0" }
wasmtime-environ = { path = "../environ", version = "0.9.0" }

View File

@@ -1,3 +1,8 @@
# Implementation of wasm-c-api in Rust
## Wasmtime Embedding API
https://github.com/WebAssembly/wasm-c-api
The `wasmtime` crate is an embedding API of the `wasmtime` WebAssembly runtime.
This is intended to be used in Rust projects and provides a high-level API of
working with WebAssembly modules.
If you're interested in embedding `wasmtime` in other languages, you may wish to
take a look a the [C embedding API](../c-api) instead!

View File

@@ -1,155 +0,0 @@
###############################################################################
# Configuration
# Inherited from wasm-c-api/Makefile to just run C examples
WASM_FLAGS = -DWASM_API_DEBUG # -DWASM_API_DEBUG_LOG
C_FLAGS = ${WASM_FLAGS} -Wall -Werror -ggdb -O -fsanitize=address
CC_FLAGS = -std=c++11 ${C_FLAGS}
LD_FLAGS = -fsanitize-memory-track-origins -fsanitize-memory-use-after-dtor
C_COMP = clang
WASMTIME_API_MODE = debug
# Base directories
WASMTIME_API_DIR = ..
WASM_DIR = wasm-c-api
EXAMPLE_DIR = ${WASM_DIR}/example
OUT_DIR = ${WASM_DIR}/out
# Example config
EXAMPLE_OUT = ${OUT_DIR}/example
EXAMPLES = \
hello \
callback \
trap \
start \
reflect \
global \
table \
memory \
hostref \
finalize \
serialize \
threads \
# multi \
# Wasm config
WASM_INCLUDE = ${WASM_DIR}/include
WASM_SRC = ${WASM_DIR}/src
WASM_OUT = ${OUT_DIR}
WASM_C_LIBS = wasm-bin wasm-rust-api
WASM_CC_LIBS = $(error unsupported C++)
# Compiler config
ifeq (${WASMTIME_API_MODE},release)
CARGO_BUILD_FLAGS = --release
else
CARGO_BUILD_FLAGS =
endif
ifeq (${C_COMP},clang)
CC_COMP = clang++
LD_GROUP_START =
LD_GROUP_END =
else ifeq (${C_COMP},gcc)
CC_COMP = g++
LD_GROUP_START = -Wl,--start-group
LD_GROUP_END = -Wl,--end-group
else
$(error C_COMP set to unknown compiler, must be clang or gcc)
endif
WASMTIME_BIN_DIR = ${WASMTIME_API_DIR}/../../target/${WASMTIME_API_MODE}
WASMTIME_C_LIB_DIR = ${WASMTIME_BIN_DIR}
WASMTIME_C_LIBS = wasmtime
WASMTIME_CC_LIBS = $(error unsupported c++)
WASMTIME_C_BINS = ${WASMTIME_C_LIBS:%=${WASMTIME_C_LIB_DIR}/lib%.a}
###############################################################################
# Examples
#
# To build Wasm APIs and run all examples:
# make all
#
# To run only C examples:
# make c
#
# To run only C++ examples:
# make cc
#
# To run individual C example (e.g. hello):
# make run-hello-c
#
# To run individual C++ example (e.g. hello):
# make run-hello-cc
#
.PHONY: all cc c
all: cc c
cc: ${EXAMPLES:%=run-%-cc}
c: ${EXAMPLES:%=run-%-c}
# Running a C / C++ example
run-%-c: ${EXAMPLE_OUT}/%-c ${EXAMPLE_OUT}/%.wasm
@echo ==== C ${@:run-%-c=%} ====; \
cd ${EXAMPLE_OUT}; ./${@:run-%=%}
@echo ==== Done ====
run-%-cc: ${EXAMPLE_OUT}/%-cc ${EXAMPLE_OUT}/%.wasm
@echo ==== C++ ${@:run-%-cc=%} ====; \
cd ${EXAMPLE_OUT}; ./${@:run-%=%}
@echo ==== Done ====
# Compiling C / C++ example
${EXAMPLE_OUT}/%-c.o: ${EXAMPLE_DIR}/%.c ${WASM_INCLUDE}/wasm.h
mkdir -p ${EXAMPLE_OUT}
${C_COMP} -c ${C_FLAGS} -I. -I${WASM_INCLUDE} $< -o $@
${EXAMPLE_OUT}/%-cc.o: ${EXAMPLE_DIR}/%.cc ${WASM_INCLUDE}/wasm.hh
mkdir -p ${EXAMPLE_OUT}
${CC_COMP} -c ${CC_FLAGS} -I. -I${WASM_INCLUDE} $< -o $@
# Linking C / C++ example
.PRECIOUS: ${EXAMPLES:%=${EXAMPLE_OUT}/%-c}
${EXAMPLE_OUT}/%-c: ${EXAMPLE_OUT}/%-c.o ${WASMTIME_C_BINS}
${CC_COMP} ${CC_FLAGS} ${LD_FLAGS} $< -o $@ \
${LD_GROUP_START} \
${WASMTIME_C_BINS} \
${LD_GROUP_END} \
-ldl -pthread
# Installing Wasm binaries
.PRECIOUS: ${EXAMPLES:%=${EXAMPLE_OUT}/%.wasm}
${EXAMPLE_OUT}/%.wasm: ${EXAMPLE_DIR}/%.wasm
cp $< $@
###############################################################################
# Wasm C / C++ API
#
# To build both C / C++ APIs:
# make wasm
.PHONY: wasm wasm-c wasm-cc
wasm: wasm-c wasm-cc
wasm-c: ${WASMTIME_C_BIN}
wasm-cc: ${WASMTIME_CC_BIN}
${WASMTIME_C_BINS}: CARGO_RUN
cd ${WASMTIME_API_DIR}; cargo build --lib ${CARGO_BUILD_FLAGS}
.PHONY: CARGO_RUN
CARGO_RUN:
###############################################################################
# Clean-up
.PHONY: clean
clean:
rm -rf ${OUT_DIR}

View File

@@ -18,13 +18,11 @@ mod trap;
mod types;
mod values;
pub mod wasm;
pub use crate::callable::Callable;
pub use crate::externals::*;
pub use crate::instance::Instance;
pub use crate::module::Module;
pub use crate::r#ref::AnyRef;
pub use crate::r#ref::{AnyRef, HostInfo, HostRef};
pub use crate::runtime::{Config, Engine, OptLevel, Store, Strategy};
pub use crate::trap::{FrameInfo, Trap};
pub use crate::types::*;

View File

@@ -398,7 +398,13 @@ impl Store {
.cloned()
}
pub(crate) fn ptr_eq(a: &Store, b: &Store) -> bool {
/// Returns whether the stores `a` and `b` refer to the same underlying
/// `Store`.
///
/// Because the `Store` type is reference counted multiple clones may point
/// to the same underlying storage, and this method can be used to determine
/// whether two stores are indeed the same.
pub fn same(a: &Store, b: &Store) -> bool {
Rc::ptr_eq(&a.inner, &b.inner)
}
}

File diff suppressed because it is too large Load Diff