Skip to content

Commit

Permalink
Added preliminary support for building and deploying JasPer for a
Browse files Browse the repository at this point in the history
WebAssembly target that supports WASI.
  • Loading branch information
mdadams committed Nov 4, 2023
1 parent 354722a commit 0a013da
Show file tree
Hide file tree
Showing 9 changed files with 477 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ on:
- version-[0-9]+.[0-9]+.[0-9]+-rc[0-9]+
- version-[0-9]+.[0-9]+.[0-9]+-alpha[0-9]+
- version-[0-9]+.[0-9]+.[0-9]+-beta[0-9]+
- experimental-version-*
- mdadams-experimental-version-*

jobs:
build:
Expand Down
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ option(JAS_ENABLE_DANGEROUS_INTERNAL_TESTING_MODE
option(JAS_ENABLE_CXX "Enable C++ code (for testing)." OFF)
option(JAS_ENABLE_CONFORMANCE_TESTS "Enable conformance tests." OFF)

option(JAS_WASM "Enable WebAssembly mode" OFF)
if(JAS_WASM)
set(JAS_CROSSCOMPILING 1)
set(JAS_STDC_VERSION "201112L")
set(JAS_ENABLE_MULTITHREADING_SUPPORT 0)
set(JAS_ENABLE_SHARED 0)
add_compile_definitions(JAS_WASI_LIBC)
endif()

################################################################################
#
################################################################################
Expand Down Expand Up @@ -412,6 +421,7 @@ if((DEFINED JAS_CROSSCOMPILING AND JAS_CROSSCOMPILING) OR
# cross-compiling. If cross-compiling, the value of JAS_STDC_VERSION
# will need to be set manually from the command line
# (e.g., using -DJAS_STDC_VERSION=YYYYMML) or by changing the line below.
if(NOT JAS_WASM)
set(JAS_STDC_VERSION "0L" CACHE INTERNAL "The value of __STDC_VERSION__.")
if (JAS_STDC_VERSION STREQUAL "0L")
message(FATAL_ERROR
Expand All @@ -421,6 +431,7 @@ if((DEFINED JAS_CROSSCOMPILING AND JAS_CROSSCOMPILING) OR
"option -DJAS_STDC_VERSION=...) or modify the CMakeLists.txt "
"appropriately.")
endif()
endif()
else()
jas_get_stdc_version(status JAS_STDC_VERSION)
if(NOT status)
Expand Down
161 changes: 161 additions & 0 deletions build/build_wasi_jasper
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#! /usr/bin/env bash

panic()
{
echo "ERROR: $*" 1>& 2
exit 1
}

perform_cleanup()
{
if [ -n "$tmp_dir" -a -d "$tmp_dir" ]; then
rm -rf "$tmp_dir" || \
echo "warning: cannot remove temporary directory $tmp_dir"
fi
}

usage()
{
cat <<- EOF
usage:
$0 [options] -p \$out_dir
Examples
========
$0 -C -t /tmp/mdadams/wasi_jasper-XXXXXX -o /tmp/mdadams/wasi_jasper
EOF
if [ $# -ne 0 ]; then
echo "BAD USAGE: $*"
fi
exit 2
}

self_dir="$(dirname "$0")" || panic
install_wasi_sdk="$self_dir/install_wasi_sdk"
install_wasmtime="$self_dir/install_wasmtime"
wasm_cc="$self_dir/wasm_cc"
wasm_cxx="$self_dir/wasm_cxx"

wasi_sdk_version=20.0
wasmtime_version=12.0.1

tmp_dir_template="${JAS_TMP_DIR:-/tmp}/wasi_jasper-XXXXXX"
cleanup=1
source_dir="$self_dir/.."
out_dir=
version=unknown-version

while getopts ":hCt:s:o:V:" option; do
case "$option" in
C)
cleanup=0;;
t)
tmp_dir_template="$OPTARG";;
s)
source_dir="$OPTARG";;
o)
out_dir="$OPTARG";;
V)
version="$OPTARG";;
h)
usage;;
*)
usage "invalid option $option";;
esac
done
shift $((OPTIND - 1))

if [ -z "$tmp_dir_template" ]; then
usage "no temporary directory pathname template specified"
fi

if [ -z "$source_dir" ]; then
usage "no source directory specified"
fi

if [ -z "$out_dir" ]; then
usage "no output directory specified"
fi

tmp_dir="$(mktemp -d "$tmp_dir_template")" || \
panic "cannot create temporary directory"
if [ "$cleanup" -ne 0 ]; then
trap perform_cleanup EXIT
fi

sde_dir="$tmp_dir/wasm"
wasi_sdk_dir="$sde_dir/wasi_sdk"
wasmtime_dir="$sde_dir/wasmtime"

"$install_wasi_sdk" -d "$wasi_sdk_dir" -v "$wasi_sdk_version" || \
panic "cannot install WASI SDK"
"$install_wasmtime" -d "$wasmtime_dir" -v "$wasmtime_version" || \
panic "cannot install wasmtime"
wasmtime="$wasmtime_dir/bin/wasmtime"

export WASI_SDK_ROOT_DIR="$wasi_sdk_dir"
export CC="$wasm_cc"
export CXX="$wasm_cxx"

#source_dir="$tmp_dir/git"
build_dir="$tmp_dir/build"
install_dir="$tmp_dir/install"
readme_file="$install_dir/README.txt"

configure_options=(
-DCMAKE_INSTALL_PREFIX="$install_dir"
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_VERBOSE_MAKEFILE=1
-DJAS_CROSSCOMPILING=1
-DJAS_STDC_VERSION=201112L
-DJAS_ENABLE_MULTITHREADING_SUPPORT=0
-DJAS_ENABLE_SHARED=0
-DJAS_ENABLE_CXX=0
-DJAS_USE_JAS_INIT=1
-DJAS_STRICT=0
-DJAS_WASM=1
-DJAS_ENABLE_DOC=0
)

cmake -H"$source_dir" -B"$build_dir" "${configure_options[@]}" || \
panic "configure failed"

cmake --build "$build_dir" || \
panic "build failed"

cmake --build "$build_dir" --target install || \
panic "install failed"

"$wasmtime" "$install_dir/bin/imginfo" < "$source_dir/data/images/goldenears.jp2" || panic "jasper failed"

cat > "$readme_file" <<- EOF
JasPer WebAssembly Edition ($version)
This directory contains several JasPer programs built for a WebAssembly
target that uses WASI for interfacing to the host environment.
The command-line interface (CLI) for these programs is identical to
their non-WebAssembly counterparts. For detailed information on the
CLI for these programs, refer to the online manual at:
https://jasper-software.github.io/jasper-manual/latest/html/index.html
In order to run these WebAssembly programs, you will need a WebAssembly
runtime with WASI support, such as:
Wasmtime (https://wasmtime.dev)
Wasmer (https://wasmer.io)
WAMR (https://bytecodealliance.github.io/wamr.dev)
WasmEdge (https://wasmedge.org)
EOF
[ $? -eq 0 ] || panic "cannot make readme file"

if [ ! -d "$out_dir" ]; then
mkdir -p "$out_dir" || panic
fi

for file in jasper imginfo imgcmp; do
cp -a "$install_dir/bin/$file" "$out_dir" || panic
done
cp -a "$readme_file" "$out_dir" || panic
53 changes: 53 additions & 0 deletions build/install_wasi_sdk
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#! /usr/bin/env bash

panic()
{
echo "ERROR: $*"
exit 1
}

wasi_sdk_version=
install_dir=

while getopts :d:v: option; do
case "$option" in
d)
install_dir="$OPTARG";;
v)
wasi_sdk_version="$OPTARG";;
esac
done

if [ -z "$install_dir" ]; then
panic "no install directory specified"
fi
if [ -z "$wasi_sdk_version" ]; then
panic "no version specified"
fi

if [ ! -d "$install_dir" ]; then
mkdir -p "$install_dir" || \
panic "cannot make directory $install_dir"
fi

#source_dir="$install_dir/.source"
#build_dir="$install_dir/.build"
#build_dir_2="$install_dir/.build2"

wasi_sdk_archive_url="https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk-$wasi_sdk_version-linux.tar.gz"

wasi_sdk_archive="$install_dir/.wasi-sdk.tar.xz"

if [ ! -d "$install_dir" ]; then
mkdir -p "$install_dir" || \
panic "cannot make directory $install_dir"
fi

wget -O "$wasi_sdk_archive" "$wasi_sdk_archive_url" || \
panic "wget failed"
tar -x -z -f "$wasi_sdk_archive" --strip-components=1 -C "$install_dir" || \
panic "tar failed"

#rm -rf "$source_dir" || panic
#rm -rf "$build_dir" || panic
#rm -rf "$build_dir_2" || panic
74 changes: 74 additions & 0 deletions build/install_wasmtime
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#! /usr/bin/env bash

# Wasmtime release artifacts for download:
# - library and headers for C API:
# wasmtime-$version-x86_64-linux-c-api.tar.xz
# - program to run WASM applications:
# wasmtime-$version-x86_64-linux.tar.xz

panic()
{
echo "ERROR: $*"
exit 1
}

usage()
{
cat <<- EOF
usage: $0 [options]
-d \$install_dir
-v \$version
EOF
exit 2
}

wasmtime_version=
install_dir=

while getopts :d:v: option; do
case "$option" in
d)
install_dir="$OPTARG";;
v)
wasmtime_version="$OPTARG";;
*)
usage "invalid option $OPTARG";;
esac
done

if [ -z "$install_dir" ]; then
panic "no install directory specified"
fi
if [ -z "$wasmtime_version" ]; then
panic "no version specified"
fi

if [ ! -d "$install_dir" ]; then
mkdir -p "$install_dir" || \
panic "cannot make directory $install_dir"
fi

#source_dir="$install_dir/.source"
#build_dir="$install_dir/.build"
#build_dir_2="$install_dir/.build2"

wasmtime_archive="$install_dir/.wasmtime.tar.xz"
wasmtime_capi_archive="$install_dir/.wasmtime-capi.tar.xz"
wasmtime_archive_url="https://github.com/bytecodealliance/wasmtime/releases/download/v$wasmtime_version/wasmtime-v$wasmtime_version-x86_64-linux.tar.xz"
wasmtime_capi_archive_url="https://github.com/bytecodealliance/wasmtime/releases/download/v$wasmtime_version/wasmtime-v$wasmtime_version-x86_64-linux-c-api.tar.xz"

wget -O "$wasmtime_archive" "$wasmtime_archive_url" || \
panic "wget failed"
wget -O "$wasmtime_capi_archive" "$wasmtime_capi_archive_url" || \
panic "wget failed"
if [ ! -d "$install_dir/bin" ]; then
mkdir -p "$install_dir/bin"
fi
tar -x -J -f "$wasmtime_archive" --strip-components=1 -C "$install_dir/bin" || \
panic "tar failed"
tar -x -J -f "$wasmtime_capi_archive" --strip-components=1 -C "$install_dir" || \
panic "tar failed"

#rm -rf "$source_dir" || panic
#rm -rf "$build_dir" || panic
#rm -rf "$build_dir_2" || panic
Loading

0 comments on commit 0a013da

Please sign in to comment.