1#!/bin/sh
2
3# Where to find the sources (only used to copy zstd.h)
4ZSTD_SRC_ROOT="../../lib"
5
6# Temporary compiled binary
7OUT_FILE="tempbin"
8
9# Optional temporary compiled WebAssembly
10OUT_WASM="temp.wasm"
11
12# Source files to compile using Emscripten.
13IN_FILES="zstd.c examples/roundtrip.c"
14
15# Emscripten build using emcc.
16emscripten_emcc_build() {
17  # Compile the the same example as above
18  CC_FLAGS="-Wall -Wextra -Wshadow -Werror -Os -g0 -flto"
19  emcc $CC_FLAGS -s WASM=1 -I. -o $OUT_WASM $IN_FILES
20  # Did compilation work?
21  if [ $? -ne 0 ]; then
22    echo "Compiling ${IN_FILES}: FAILED"
23    exit 1
24  fi
25  echo "Compiling ${IN_FILES}: PASSED"
26  rm -f $OUT_WASM
27}
28
29# Emscripten build using docker.
30emscripten_docker_build() {
31  docker container run --rm \
32    --volume $PWD:/code \
33    --workdir /code \
34    emscripten/emsdk:latest \
35    emcc $CC_FLAGS -s WASM=1 -I. -o $OUT_WASM $IN_FILES
36  # Did compilation work?
37  if [ $? -ne 0 ]; then
38      echo "Compiling ${IN_FILES} (using docker): FAILED"
39    exit 1
40  fi
41  echo "Compiling ${IN_FILES} (using docker): PASSED"
42  rm -f $OUT_WASM
43}
44
45# Try Emscripten build using emcc or docker.
46try_emscripten_build() {
47  which emcc > /dev/null
48  if [ $? -eq 0 ]; then
49    emscripten_emcc_build
50    return $?
51  fi
52
53  which docker > /dev/null
54  if [ $? -eq 0 ]; then
55    emscripten_docker_build
56    return $?
57  fi
58
59  echo "(Skipping Emscripten test)"
60}
61
62# Amalgamate the sources
63./create_single_file_library.sh
64# Did combining work?
65if [ $? -ne 0 ]; then
66  echo "Single file library creation script: FAILED"
67  exit 1
68fi
69echo "Single file library creation script: PASSED"
70
71# Copy the header to here (for the tests)
72cp "$ZSTD_SRC_ROOT/zstd.h" zstd.h
73
74# Compile the generated output
75cc -Wall -Wextra -Werror -Wshadow -pthread -I. -Os -g0 -o $OUT_FILE zstd.c examples/roundtrip.c
76# Did compilation work?
77if [ $? -ne 0 ]; then
78  echo "Compiling roundtrip.c: FAILED"
79  exit 1
80fi
81echo "Compiling roundtrip.c: PASSED"
82
83# Run then delete the compiled output
84./$OUT_FILE
85retVal=$?
86rm -f $OUT_FILE
87# Did the test work?
88if [ $retVal -ne 0 ]; then
89  echo "Running roundtrip.c: FAILED"
90  exit 1
91fi
92echo "Running roundtrip.c: PASSED"
93
94# Try Emscripten build if emcc or docker command is available.
95try_emscripten_build
96
97exit 0
98