10c16b537SWarner LoshZstandard library files 20c16b537SWarner Losh================================ 30c16b537SWarner Losh 40c16b537SWarner LoshThe __lib__ directory is split into several sub-directories, 519fcbaf1SConrad Meyerin order to make it easier to select or exclude features. 60c16b537SWarner Losh 70c16b537SWarner Losh 80c16b537SWarner Losh#### Building 90c16b537SWarner Losh 10a0483764SConrad Meyer`Makefile` script is provided, supporting [Makefile conventions](https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html#Makefile-Conventions), 1119fcbaf1SConrad Meyerincluding commands variables, staged install, directory variables and standard targets. 120c16b537SWarner Losh- `make` : generates both static and dynamic libraries 13a0483764SConrad Meyer- `make install` : install libraries and headers in target system directories 140c16b537SWarner Losh 15a0483764SConrad Meyer`libzstd` default scope is pretty large, including compression, decompression, dictionary builder, 16a0483764SConrad Meyerand support for decoding legacy formats >= v0.5.0. 17a0483764SConrad MeyerThe scope can be reduced on demand (see paragraph _modular build_). 18a0483764SConrad Meyer 19a0483764SConrad Meyer 20a0483764SConrad Meyer#### Multithreading support 21a0483764SConrad Meyer 22*5ff13fbcSAllan JudeWhen building with `make`, by default the dynamic library is multithreaded and static library is single-threaded (for compatibility reasons). 23*5ff13fbcSAllan Jude 24a0483764SConrad MeyerEnabling multithreading requires 2 conditions : 25a0483764SConrad Meyer- set build macro `ZSTD_MULTITHREAD` (`-DZSTD_MULTITHREAD` for `gcc`) 26a0483764SConrad Meyer- for POSIX systems : compile with pthread (`-pthread` compilation flag for `gcc`) 27a0483764SConrad Meyer 28*5ff13fbcSAllan JudeFor convenience, we provide a build target to generate multi and single threaded libraries: 29*5ff13fbcSAllan Jude- Force enable multithreading on both dynamic and static libraries by appending `-mt` to the target, e.g. `make lib-mt`. 30*5ff13fbcSAllan Jude- Force disable multithreading on both dynamic and static libraries by appending `-nomt` to the target, e.g. `make lib-nomt`. 31*5ff13fbcSAllan Jude- By default, as mentioned before, dynamic library is multithreaded, and static library is single-threaded, e.g. `make lib`. 32a0483764SConrad Meyer 33a0483764SConrad MeyerWhen linking a POSIX program with a multithreaded version of `libzstd`, 349cbefe25SConrad Meyernote that it's necessary to invoke the `-pthread` flag during link stage. 35a0483764SConrad Meyer 36a0483764SConrad MeyerMultithreading capabilities are exposed 379cbefe25SConrad Meyervia the [advanced API defined in `lib/zstd.h`](https://github.com/facebook/zstd/blob/v1.4.3/lib/zstd.h#L351). 3819fcbaf1SConrad Meyer 390c16b537SWarner Losh 400c16b537SWarner Losh#### API 410c16b537SWarner Losh 420c16b537SWarner LoshZstandard's stable API is exposed within [lib/zstd.h](zstd.h). 430c16b537SWarner Losh 440c16b537SWarner Losh 450c16b537SWarner Losh#### Advanced API 460c16b537SWarner Losh 470c16b537SWarner LoshOptional advanced features are exposed via : 480c16b537SWarner Losh 49*5ff13fbcSAllan Jude- `lib/zstd_errors.h` : translates `size_t` function results 50a0483764SConrad Meyer into a `ZSTD_ErrorCode`, for accurate error handling. 51a0483764SConrad Meyer 520c16b537SWarner Losh- `ZSTD_STATIC_LINKING_ONLY` : if this macro is defined _before_ including `zstd.h`, 53a0483764SConrad Meyer it unlocks access to the experimental API, 54a0483764SConrad Meyer exposed in the second part of `zstd.h`. 55a0483764SConrad Meyer All definitions in the experimental APIs are unstable, 56a0483764SConrad Meyer they may still change in the future, or even be removed. 57a0483764SConrad Meyer As a consequence, experimental definitions shall ___never be used with dynamic library___ ! 580c16b537SWarner Losh Only static linking is allowed. 590c16b537SWarner Losh 600c16b537SWarner Losh 610c16b537SWarner Losh#### Modular build 620c16b537SWarner Losh 63a0483764SConrad MeyerIt's possible to compile only a limited set of features within `libzstd`. 64a0483764SConrad MeyerThe file structure is designed to make this selection manually achievable for any build system : 6519fcbaf1SConrad Meyer 660c16b537SWarner Losh- Directory `lib/common` is always required, for all variants. 67a0483764SConrad Meyer 680c16b537SWarner Losh- Compression source code lies in `lib/compress` 69a0483764SConrad Meyer 700c16b537SWarner Losh- Decompression source code lies in `lib/decompress` 71a0483764SConrad Meyer 720c16b537SWarner Losh- It's possible to include only `compress` or only `decompress`, they don't depend on each other. 73a0483764SConrad Meyer 740c16b537SWarner Losh- `lib/dictBuilder` : makes it possible to generate dictionaries from a set of samples. 750c16b537SWarner Losh The API is exposed in `lib/dictBuilder/zdict.h`. 760c16b537SWarner Losh This module depends on both `lib/common` and `lib/compress` . 77a0483764SConrad Meyer 78a0483764SConrad Meyer- `lib/legacy` : makes it possible to decompress legacy zstd formats, starting from `v0.1.0`. 790c16b537SWarner Losh This module depends on `lib/common` and `lib/decompress`. 800f743729SConrad Meyer To enable this feature, define `ZSTD_LEGACY_SUPPORT` during compilation. 810f743729SConrad Meyer Specifying a number limits versions supported to that version onward. 8219fcbaf1SConrad Meyer For example, `ZSTD_LEGACY_SUPPORT=2` means : "support legacy formats >= v0.2.0". 83a0483764SConrad Meyer Conversely, `ZSTD_LEGACY_SUPPORT=0` means "do __not__ support legacy formats". 84a0483764SConrad Meyer By default, this build macro is set as `ZSTD_LEGACY_SUPPORT=5`. 85a0483764SConrad Meyer Decoding supported legacy format is a transparent capability triggered within decompression functions. 86a0483764SConrad Meyer It's also allowed to invoke legacy API directly, exposed in `lib/legacy/zstd_legacy.h`. 87a0483764SConrad Meyer Each version does also provide its own set of advanced API. 880c16b537SWarner Losh For example, advanced API for version `v0.4` is exposed in `lib/legacy/zstd_v04.h` . 890c16b537SWarner Losh 90a0483764SConrad Meyer- While invoking `make libzstd`, it's possible to define build macros 91a0483764SConrad Meyer `ZSTD_LIB_COMPRESSION, ZSTD_LIB_DECOMPRESSION`, `ZSTD_LIB_DICTBUILDER`, 9237f1f268SConrad Meyer and `ZSTD_LIB_DEPRECATED` as `0` to forgo compilation of the 9337f1f268SConrad Meyer corresponding features. This will also disable compilation of all 9437f1f268SConrad Meyer dependencies (eg. `ZSTD_LIB_COMPRESSION=0` will also disable 9537f1f268SConrad Meyer dictBuilder). 960c16b537SWarner Losh 9737f1f268SConrad Meyer- There are a number of options that can help minimize the binary size of 9837f1f268SConrad Meyer `libzstd`. 990c16b537SWarner Losh 10037f1f268SConrad Meyer The first step is to select the components needed (using the above-described 10137f1f268SConrad Meyer `ZSTD_LIB_COMPRESSION` etc.). 10237f1f268SConrad Meyer 10337f1f268SConrad Meyer The next step is to set `ZSTD_LIB_MINIFY` to `1` when invoking `make`. This 10437f1f268SConrad Meyer disables various optional components and changes the compilation flags to 10537f1f268SConrad Meyer prioritize space-saving. 10637f1f268SConrad Meyer 10737f1f268SConrad Meyer Detailed options: Zstandard's code and build environment is set up by default 10837f1f268SConrad Meyer to optimize above all else for performance. In pursuit of this goal, Zstandard 10937f1f268SConrad Meyer makes significant trade-offs in code size. For example, Zstandard often has 11037f1f268SConrad Meyer more than one implementation of a particular component, with each 11137f1f268SConrad Meyer implementation optimized for different scenarios. For example, the Huffman 11237f1f268SConrad Meyer decoder has complementary implementations that decode the stream one symbol at 11337f1f268SConrad Meyer a time or two symbols at a time. Zstd normally includes both (and dispatches 11437f1f268SConrad Meyer between them at runtime), but by defining `HUF_FORCE_DECOMPRESS_X1` or 11537f1f268SConrad Meyer `HUF_FORCE_DECOMPRESS_X2`, you can force the use of one or the other, avoiding 116a0483764SConrad Meyer compilation of the other. Similarly, `ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT` 117a0483764SConrad Meyer and `ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG` force the compilation and use of 118a0483764SConrad Meyer only one or the other of two decompression implementations. The smallest 119a0483764SConrad Meyer binary is achieved by using `HUF_FORCE_DECOMPRESS_X1` and 12037f1f268SConrad Meyer `ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT` (implied by `ZSTD_LIB_MINIFY`). 1210c16b537SWarner Losh 122a0483764SConrad Meyer For squeezing the last ounce of size out, you can also define 123a0483764SConrad Meyer `ZSTD_NO_INLINE`, which disables inlining, and `ZSTD_STRIP_ERROR_STRINGS`, 124a0483764SConrad Meyer which removes the error messages that are otherwise returned by 12537f1f268SConrad Meyer `ZSTD_getErrorName` (implied by `ZSTD_LIB_MINIFY`). 12637f1f268SConrad Meyer 12737f1f268SConrad Meyer Finally, when integrating into your application, make sure you're doing link- 128*5ff13fbcSAllan Jude time optimization and unused symbol garbage collection (via some combination of, 12937f1f268SConrad Meyer e.g., `-flto`, `-ffat-lto-objects`, `-fuse-linker-plugin`, 13037f1f268SConrad Meyer `-ffunction-sections`, `-fdata-sections`, `-fmerge-all-constants`, 13137f1f268SConrad Meyer `-Wl,--gc-sections`, `-Wl,-z,norelro`, and an archiver that understands 13237f1f268SConrad Meyer the compiler's intermediate representation, e.g., `AR=gcc-ar`). Consult your 13337f1f268SConrad Meyer compiler's documentation. 1340c16b537SWarner Losh 1352b9c00cbSConrad Meyer- While invoking `make libzstd`, the build macro `ZSTD_LEGACY_MULTITHREADED_API=1` 1362b9c00cbSConrad Meyer will expose the deprecated `ZSTDMT` API exposed by `zstdmt_compress.h` in 1372b9c00cbSConrad Meyer the shared library, which is now hidden by default. 1382b9c00cbSConrad Meyer 1399cbefe25SConrad Meyer- The build macro `DYNAMIC_BMI2` can be set to 1 or 0 in order to generate binaries 1409cbefe25SConrad Meyer which can detect at runtime the presence of BMI2 instructions, and use them only if present. 1419cbefe25SConrad Meyer These instructions contribute to better performance, notably on the decoder side. 1429cbefe25SConrad Meyer By default, this feature is automatically enabled on detecting 1439cbefe25SConrad Meyer the right instruction set (x64) and compiler (clang or gcc >= 5). 1449cbefe25SConrad Meyer It's obviously disabled for different cpus, 1459cbefe25SConrad Meyer or when BMI2 instruction set is _required_ by the compiler command line 1469cbefe25SConrad Meyer (in this case, only the BMI2 code path is generated). 1479cbefe25SConrad Meyer Setting this macro will either force to generate the BMI2 dispatcher (1) 1489cbefe25SConrad Meyer or prevent it (0). It overrides automatic detection. 1499cbefe25SConrad Meyer 150f7cd7fe5SConrad Meyer- The build macro `ZSTD_NO_UNUSED_FUNCTIONS` can be defined to hide the definitions of functions 151f7cd7fe5SConrad Meyer that zstd does not use. Not all unused functions are hidden, but they can be if needed. 152f7cd7fe5SConrad Meyer Currently, this macro will hide function definitions in FSE and HUF that use an excessive 153f7cd7fe5SConrad Meyer amount of stack space. 154f7cd7fe5SConrad Meyer 155f7cd7fe5SConrad Meyer- The build macro `ZSTD_NO_INTRINSICS` can be defined to disable all explicit intrinsics. 156f7cd7fe5SConrad Meyer Compiler builtins are still used. 157f7cd7fe5SConrad Meyer 158*5ff13fbcSAllan Jude- The build macro `ZSTD_DECODER_INTERNAL_BUFFER` can be set to control 159*5ff13fbcSAllan Jude the amount of extra memory used during decompression to store literals. 160*5ff13fbcSAllan Jude This defaults to 64kB. Reducing this value reduces the memory footprint of 161*5ff13fbcSAllan Jude `ZSTD_DCtx` decompression contexts, 162*5ff13fbcSAllan Jude but might also result in a small decompression speed cost. 163*5ff13fbcSAllan Jude 1640c16b537SWarner Losh 1650c16b537SWarner Losh#### Windows : using MinGW+MSYS to create DLL 1660c16b537SWarner Losh 1670c16b537SWarner LoshDLL can be created using MinGW+MSYS with the `make libzstd` command. 1680c16b537SWarner LoshThis command creates `dll\libzstd.dll` and the import library `dll\libzstd.lib`. 1690c16b537SWarner LoshThe import library is only required with Visual C++. 1700c16b537SWarner LoshThe header file `zstd.h` and the dynamic library `dll\libzstd.dll` are required to 1710c16b537SWarner Loshcompile a project using gcc/MinGW. 1720c16b537SWarner LoshThe dynamic library has to be added to linking options. 1730c16b537SWarner LoshIt means that if a project that uses ZSTD consists of a single `test-dll.c` 1740c16b537SWarner Loshfile it should be linked with `dll\libzstd.dll`. For example: 1750c16b537SWarner Losh``` 1760c16b537SWarner Losh gcc $(CFLAGS) -Iinclude/ test-dll.c -o test-dll dll\libzstd.dll 1770c16b537SWarner Losh``` 1780c16b537SWarner LoshThe compiled executable will require ZSTD DLL which is available at `dll\libzstd.dll`. 1790c16b537SWarner Losh 1800c16b537SWarner Losh 181f7cd7fe5SConrad Meyer#### Advanced Build options 182f7cd7fe5SConrad Meyer 183f7cd7fe5SConrad MeyerThe build system requires a hash function in order to 184f7cd7fe5SConrad Meyerseparate object files created with different compilation flags. 185f7cd7fe5SConrad MeyerBy default, it tries to use `md5sum` or equivalent. 186f7cd7fe5SConrad MeyerThe hash function can be manually switched by setting the `HASH` variable. 187f7cd7fe5SConrad MeyerFor example : `make HASH=xxhsum` 188f7cd7fe5SConrad MeyerThe hash function needs to generate at least 64-bit using hexadecimal format. 189f7cd7fe5SConrad MeyerWhen no hash function is found, 190f7cd7fe5SConrad Meyerthe Makefile just generates all object files into the same default directory, 191f7cd7fe5SConrad Meyerirrespective of compilation flags. 192f7cd7fe5SConrad MeyerThis functionality only matters if `libzstd` is compiled multiple times 193f7cd7fe5SConrad Meyerwith different build flags. 194f7cd7fe5SConrad Meyer 195f7cd7fe5SConrad MeyerThe build directory, where object files are stored 196f7cd7fe5SConrad Meyercan also be manually controlled using variable `BUILD_DIR`, 197f7cd7fe5SConrad Meyerfor example `make BUILD_DIR=objectDir/v1`. 198f7cd7fe5SConrad MeyerIn which case, the hash function doesn't matter. 199f7cd7fe5SConrad Meyer 200f7cd7fe5SConrad Meyer 2010c16b537SWarner Losh#### Deprecated API 2020c16b537SWarner Losh 2030c16b537SWarner LoshObsolete API on their way out are stored in directory `lib/deprecated`. 2040c16b537SWarner LoshAt this stage, it contains older streaming prototypes, in `lib/deprecated/zbuff.h`. 2050c16b537SWarner LoshThese prototypes will be removed in some future version. 2060c16b537SWarner LoshConsider migrating code towards supported streaming API exposed in `zstd.h`. 2070c16b537SWarner Losh 2080c16b537SWarner Losh 2090c16b537SWarner Losh#### Miscellaneous 2100c16b537SWarner Losh 2110c16b537SWarner LoshThe other files are not source code. There are : 2120c16b537SWarner Losh 2130c16b537SWarner Losh - `BUCK` : support for `buck` build system (https://buckbuild.com/) 214a0483764SConrad Meyer - `Makefile` : `make` script to build and install zstd library (static and dynamic) 2150c16b537SWarner Losh - `README.md` : this file 216a0483764SConrad Meyer - `dll/` : resources directory for Windows compilation 217a0483764SConrad Meyer - `libzstd.pc.in` : script for `pkg-config` (used in `make install`) 218