Skip to content

Commit f22f415

Browse files
authored
Document cross-compiling Wasmtime (bytecodealliance#1721)
Closes bytecodealliance#1720
1 parent e40fd9d commit f22f415

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

docs/contributing-building.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ The built executable will be located at `target/release/wasmtime`.
5252
You can also build and run a local `wasmtime` CLI by replacing `cargo build`
5353
with `cargo run`.
5454

55+
## Building the Wasmtime C API
56+
57+
To build the C API of Wasmtime you can run:
58+
59+
```shell
60+
cargo build --release --manifest-path crates/c-api/Cargo.toml
61+
```
62+
63+
This will place the shared library inside of `target/release`. On Linux it will
64+
be called `libwasmtime.{a,so}`, on macOS it will be called
65+
`libwasmtime.{a,dylib}`, and on Windows it will be called
66+
`wasmtime.{lib,dll,dll.lib}`.
67+
5568
## Building Other Wasmtime Crates
5669

5770
You can build any of the Wasmtime crates by appending `-p wasmtime-whatever` to
@@ -69,3 +82,63 @@ there, without needing to supply the `-p` flag:
6982
cd crates/jit/
7083
cargo build
7184
```
85+
86+
## Cross Compiling Wasmtime
87+
88+
By default `cargo build` will build Wasmtime for the platform you're running the
89+
build on. You might, however, want to build Wasmtime for a different platform!
90+
Let's say for example that you want to build Wasmtime for
91+
`aarch64-unknown-linux-gnu`. First you'll want to acquire the Rust standard
92+
library for this target:
93+
94+
```shell
95+
rustup target add aarch64-unknown-linux-gnu
96+
```
97+
98+
Next you need to install a native C toolchain which has a C compiler, runtime
99+
libraries, and linker for the desired target. This is unfortunately not very
100+
easy to acquire on most platforms:
101+
102+
* On Windows you can install build tools for AArch64 Windows, but targeting
103+
platforms like Linux or macOS is not easy. While toolchains exist for
104+
targeting non-Windows platforms you'll have to hunt yourself to find the right
105+
one.
106+
107+
* On macOS you can install, through Xcode, toolchains for iOS but the main
108+
`x86_64-apple-darwin` is really the only easy target to install. You'll need
109+
to hunt for toolchains if you want to compile for Linux or Windows.
110+
111+
* On Linux you can relatively easily compile for other Linux architectures most
112+
of the time. For example on Debian-based distributions you can install the
113+
`gcc-aarch64-linux-gnu` package which should come with the C compiler, runtime
114+
libraries, and linker all in one (assuming you don't explicitly request
115+
disabling recommended packages). Other Linux distributions may have
116+
differently named toolchains. Compiling for macOS from Linux will require
117+
finding your own toolchain. Compiling for Windows MSVC will require finding
118+
your own toolchain, but compiling for MinGW can work easily enough if you
119+
install the MinGW toolchain via your package manager.
120+
121+
For now we'll assume you're on Linux compiling for a different Linux
122+
architecture. Once you've got the native toolchain, you'll want to find the C
123+
compiler that came with it. On Debian, for example, this is called
124+
`aarch64-linux-gnu-gcc`. Next up you'll need to configure two environment
125+
variables to configure the Rust build:
126+
127+
```shell
128+
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
129+
export CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc
130+
```
131+
132+
The first environment variable tells Cargo to tell rustc what the correct linker
133+
for your target is. The second configures the [`cc` Rust
134+
crate](https://crates.io/crates/cc) for C code compiled as part of the build.
135+
136+
Finally you can execute.
137+
138+
```shell
139+
cargo build --target aarch64-unknown-linux-gnu --release
140+
```
141+
142+
The built executable will be located at
143+
`target/aarch64-unknown-linux-gnu/release/wasmtime`. Note that you can
144+
cross-compile the C API in the same manner as the CLI too.

0 commit comments

Comments
 (0)