Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ contrib: lib
$(MAKE) -C contrib/pzstd all
$(MAKE) -C contrib/seekable_format/examples all
$(MAKE) -C contrib/largeNbDicts all
cd contrib/single_file_decoder/ ; ./build_test.sh
cd contrib/single_file_libs/ ; ./build_decoder_test.sh
cd contrib/single_file_libs/ ; ./build_library_test.sh

.PHONY: cleanTabs
cleanTabs:
Expand Down
2 changes: 0 additions & 2 deletions contrib/single_file_decoder/.gitignore

This file was deleted.

18 changes: 0 additions & 18 deletions contrib/single_file_decoder/README.md

This file was deleted.

124 changes: 0 additions & 124 deletions contrib/single_file_decoder/combine.sh

This file was deleted.

600 changes: 0 additions & 600 deletions contrib/single_file_decoder/examples/emscripten.c

This file was deleted.

3,065 changes: 0 additions & 3,065 deletions contrib/single_file_decoder/examples/simple.c

This file was deleted.

72 changes: 0 additions & 72 deletions contrib/single_file_decoder/zstddeclib-in.c

This file was deleted.

4 changes: 4 additions & 0 deletions contrib/single_file_libs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
zstddeclib.c
zstdenclib.c
zstd.c
zstd.h
33 changes: 33 additions & 0 deletions contrib/single_file_libs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Single File Zstandard Libraries

The script `combine.sh` creates an _amalgamated_ source file that can be used with or without `zstd.h`. This isn't a _header-only_ file but it does offer a similar level of simplicity when integrating into a project.

All it now takes to support Zstd in your own projects is the addition of a single file, two if using the header, with no configuration or further build steps.

Decompressor
------------

This is the most common use case. The decompression library is small, adding, for example, 26kB to an Emscripten compiled WebAssembly project. Native implementations add a little more, 40-70kB depending on the compiler and platform.

Create `zstddeclib.c` from the Zstd source using:
```
cd zstd/contrib/single_file_libs
./combine.sh -r ../../lib -r ../../lib/common -r ../../lib/decompress -o zstddeclib.c zstddeclib-in.c
```
Then add the resulting file to your project (see the [example files](examples)).

`create_single_file_decoder.sh` will run the above script, creating the file `zstddeclib.c` (`build_decoder_test.sh` will also create `zstddeclib.c`, then compile and test the result).

Full Library
------------

The same tool can amalgamate the entire Zstd library for ease of adding both compression and decompression to a project. The [roundtrip example](examples/roundtrip.c) uses the original `zstd.h` with the remaining source files combined into `zstd.c` (currently just over 1MB) created from `zstd-in.c`. As with the standalone decoder the most useful compile flags have already been rolled-in and the resulting file can be added to a project as-is.

Create `zstd.c` from the Zstd source using:
```
cd zstd/contrib/single_file_libs
combine.sh -r ../../lib -r ../../lib/common -r ../../lib/compress -r ../../lib/decompress -k zstd.h -o zstd.c zstd-in.c
```
It's possible to create a compressor-only library but since the decompressor is so small in comparison this doesn't bring much of a gain (but for the curious, simply remove the files in the _decompress_ section at the end of `zstd-in.c`).

`create_single_file_library.sh` will run the script to create `zstd.c` (`build_library_test.sh` will also create `zstd.c`, then compile and test the result).
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#!/bin/sh

# Where to find the sources
ZSTD_SRC_ROOT="../../lib"

# Temporary compiled binary
OUT_FILE="tempbin"

Expand Down
61 changes: 61 additions & 0 deletions contrib/single_file_libs/build_library_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/sh

# Where to find the sources (only used to copy zstd.h)
ZSTD_SRC_ROOT="../../lib"

# Temporary compiled binary
OUT_FILE="tempbin"

# Optional temporary compiled WebAssembly
OUT_WASM="temp.wasm"

# Amalgamate the sources
./create_single_file_library.sh
# Did combining work?
if [ $? -ne 0 ]; then
echo "Single file library creation script: FAILED"
exit 1
fi
echo "Single file library creation script: PASSED"

# Copy the header to here (for the tests)
cp "$ZSTD_SRC_ROOT/zstd.h" zstd.h

# Compile the generated output
cc -Wall -Wextra -Werror -pthread -I. -Os -g0 -o $OUT_FILE zstd.c examples/roundtrip.c
# Did compilation work?
if [ $? -ne 0 ]; then
echo "Compiling roundtrip.c: FAILED"
exit 1
fi
echo "Compiling roundtrip.c: PASSED"

# Run then delete the compiled output
./$OUT_FILE
retVal=$?
rm -f $OUT_FILE
# Did the test work?
if [ $retVal -ne 0 ]; then
echo "Running roundtrip.c: FAILED"
exit 1
fi
echo "Running roundtrip.c: PASSED"

# Is Emscripten available?
which emcc > /dev/null
if [ $? -ne 0 ]; then
echo "(Skipping Emscripten test)"
else
# Compile the the same example as above
CC_FLAGS="-Wall -Wextra -Werror -Os -g0 -flto"
emcc $CC_FLAGS -s WASM=1 -I. -o $OUT_WASM zstd.c examples/roundtrip.c
# Did compilation work?
if [ $? -ne 0 ]; then
echo "Compiling emscripten.c: FAILED"
exit 1
fi
echo "Compiling emscripten.c: PASSED"
rm -f $OUT_WASM
fi

exit 0
Loading