1#!/bin/bash 2 3# A script to build the release artifacts of Wasmtime into the `target` 4# directory. For now this is the CLI and the C API. Note that this script only 5# produces the artifacts through Cargo and doesn't package things up. That's 6# intended for the `build-tarballs.sh` script. 7# 8# This script takes a Rust target as its first input and optionally a parameter 9# afterwards which can be "-min" to indicate that a minimal build should be 10# produced with as many features as possible stripped out. 11 12set -ex 13 14build=$1 15target=$2 16wrapper="" 17 18# If `$DOCKER_IMAGE` is set then run the build inside of that docker container 19# instead of on the host machine. In CI this uses `./ci/docker/*/Dockerfile` to 20# have precise glibc requirements for Linux platforms for example. 21if [ "$DOCKER_IMAGE" != "" ]; then 22 if [ -f "$DOCKER_IMAGE" ]; then 23 docker build --tag build-image --file $DOCKER_IMAGE ci/docker 24 DOCKER_IMAGE=build-image 25 fi 26 27 # Inherit the environment's rustc and env vars related to cargo/rust, and then 28 # otherwise re-execute ourselves and we'll be missing `$DOCKER_IMAGE` in the 29 # container so we'll continue below. 30 exec docker run --interactive \ 31 --volume `pwd`:`pwd` \ 32 --volume `rustc --print sysroot`:/rust:ro \ 33 --workdir `pwd` \ 34 --interactive \ 35 --env-file <(env | grep 'CARGO\|RUST') \ 36 $DOCKER_IMAGE \ 37 bash -c "PATH=\$PATH:/rust/bin RUSTFLAGS=\"\$RUSTFLAGS \$EXTRA_RUSTFLAGS\" `pwd`/$0 $*" 38fi 39 40# Default build flags for release artifacts. Leave debugging for 41# builds-from-source which have richer information anyway, and additionally the 42# CLI won't benefit from catching unwinds and neither will the C API so use 43# panic=abort in both situations. 44export CARGO_PROFILE_RELEASE_STRIP=debuginfo 45export CARGO_PROFILE_RELEASE_PANIC=abort 46 47if [[ "$build" = *-min ]]; then 48 # Configure a whole bunch of compile-time options which help reduce the size 49 # of the binary artifact produced. 50 export CARGO_PROFILE_RELEASE_OPT_LEVEL=s 51 export RUSTFLAGS="-Zlocation-detail=none $RUSTFLAGS" 52 export CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1 53 export CARGO_PROFILE_RELEASE_LTO=true 54 build_std=-Zbuild-std=std,panic_abort 55 flags="$build_std --no-default-features --features disable-logging" 56 cmake_flags="-DWASMTIME_DISABLE_ALL_FEATURES=ON" 57 cmake_flags="$cmake_flags -DWASMTIME_FEATURE_DISABLE_LOGGING=ON" 58 cmake_flags="$cmake_flags -DWASMTIME_USER_CARGO_BUILD_OPTIONS:LIST=$build_std" 59else 60 # For release builds the CLI is built a bit more feature-ful than the Cargo 61 # defaults to provide artifacts that can do as much as possible. 62 bin_flags="--features all-arch,component-model,gdbstub" 63fi 64 65if [[ "$target" = "x86_64-pc-windows-msvc" ]]; then 66 # Avoid emitting `/DEFAULTLIB:MSVCRT` into the static library by using clang. 67 export CC=clang 68 export CXX=clang++ 69fi 70 71cargo build --release $flags --target $target -p wasmtime-cli $bin_flags --features run 72 73# For the C API force unwind tables to be emitted to make the generated objects 74# more flexible. Embedders can always build without this but this enables 75# libunwind to produce better backtraces by default when Wasmtime is linked into 76# a different project that wants to unwind. 77export RUSTFLAGS="$RUSTFLAGS -C force-unwind-tables" 78 79# Shrink the size of `*.a` artifacts without spending too much extra time in CI. 80# See #11476 for some more context. Here Windows builds achieve this with 1 CGU 81# which results in modest size gains, and other platforms use LTO to achieve 82# much more significant size gains. The reason the platforms are different is 83# that CI is extremely slow on Windows, almost 2x slower, so this is an attempt 84# to keep CI cycle time under control. 85case $build in 86 *-mingw* | *-windows*) 87 export CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1 88 ;; 89 *) 90 export CARGO_PROFILE_RELEASE_LTO=true 91 ;; 92esac 93 94mkdir -p target/c-api-build 95cd target/c-api-build 96cmake \ 97 -G Ninja \ 98 ../../crates/c-api \ 99 $cmake_flags \ 100 -DCMAKE_BUILD_TYPE=Release \ 101 -DWASMTIME_TARGET=$target \ 102 -DCMAKE_INSTALL_PREFIX=../c-api-install \ 103 -DCMAKE_INSTALL_LIBDIR=../c-api-install/lib 104cmake --build . --target install 105