1*3117ece4SchristosZstandard library files 2*3117ece4Schristos================================ 3*3117ece4Schristos 4*3117ece4SchristosThe __lib__ directory is split into several sub-directories, 5*3117ece4Schristosin order to make it easier to select or exclude features. 6*3117ece4Schristos 7*3117ece4Schristos 8*3117ece4Schristos#### Building 9*3117ece4Schristos 10*3117ece4Schristos`Makefile` script is provided, supporting [Makefile conventions](https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html#Makefile-Conventions), 11*3117ece4Schristosincluding commands variables, staged install, directory variables and standard targets. 12*3117ece4Schristos- `make` : generates both static and dynamic libraries 13*3117ece4Schristos- `make install` : install libraries and headers in target system directories 14*3117ece4Schristos 15*3117ece4Schristos`libzstd` default scope is pretty large, including compression, decompression, dictionary builder, 16*3117ece4Schristosand support for decoding legacy formats >= v0.5.0. 17*3117ece4SchristosThe scope can be reduced on demand (see paragraph _modular build_). 18*3117ece4Schristos 19*3117ece4Schristos 20*3117ece4Schristos#### Multithreading support 21*3117ece4Schristos 22*3117ece4SchristosWhen building with `make`, by default the dynamic library is multithreaded and static library is single-threaded (for compatibility reasons). 23*3117ece4Schristos 24*3117ece4SchristosEnabling multithreading requires 2 conditions : 25*3117ece4Schristos- set build macro `ZSTD_MULTITHREAD` (`-DZSTD_MULTITHREAD` for `gcc`) 26*3117ece4Schristos- for POSIX systems : compile with pthread (`-pthread` compilation flag for `gcc`) 27*3117ece4Schristos 28*3117ece4SchristosFor convenience, we provide a build target to generate multi and single threaded libraries: 29*3117ece4Schristos- Force enable multithreading on both dynamic and static libraries by appending `-mt` to the target, e.g. `make lib-mt`. 30*3117ece4Schristos- Force disable multithreading on both dynamic and static libraries by appending `-nomt` to the target, e.g. `make lib-nomt`. 31*3117ece4Schristos- By default, as mentioned before, dynamic library is multithreaded, and static library is single-threaded, e.g. `make lib`. 32*3117ece4Schristos 33*3117ece4SchristosWhen linking a POSIX program with a multithreaded version of `libzstd`, 34*3117ece4Schristosnote that it's necessary to invoke the `-pthread` flag during link stage. 35*3117ece4Schristos 36*3117ece4SchristosMultithreading capabilities are exposed 37*3117ece4Schristosvia the [advanced API defined in `lib/zstd.h`](https://github.com/facebook/zstd/blob/v1.4.3/lib/zstd.h#L351). 38*3117ece4Schristos 39*3117ece4Schristos 40*3117ece4Schristos#### API 41*3117ece4Schristos 42*3117ece4SchristosZstandard's stable API is exposed within [lib/zstd.h](zstd.h). 43*3117ece4Schristos 44*3117ece4Schristos 45*3117ece4Schristos#### Advanced API 46*3117ece4Schristos 47*3117ece4SchristosOptional advanced features are exposed via : 48*3117ece4Schristos 49*3117ece4Schristos- `lib/zstd_errors.h` : translates `size_t` function results 50*3117ece4Schristos into a `ZSTD_ErrorCode`, for accurate error handling. 51*3117ece4Schristos 52*3117ece4Schristos- `ZSTD_STATIC_LINKING_ONLY` : if this macro is defined _before_ including `zstd.h`, 53*3117ece4Schristos it unlocks access to the experimental API, 54*3117ece4Schristos exposed in the second part of `zstd.h`. 55*3117ece4Schristos All definitions in the experimental APIs are unstable, 56*3117ece4Schristos they may still change in the future, or even be removed. 57*3117ece4Schristos As a consequence, experimental definitions shall ___never be used with dynamic library___ ! 58*3117ece4Schristos Only static linking is allowed. 59*3117ece4Schristos 60*3117ece4Schristos 61*3117ece4Schristos#### Modular build 62*3117ece4Schristos 63*3117ece4SchristosIt's possible to compile only a limited set of features within `libzstd`. 64*3117ece4SchristosThe file structure is designed to make this selection manually achievable for any build system : 65*3117ece4Schristos 66*3117ece4Schristos- Directory `lib/common` is always required, for all variants. 67*3117ece4Schristos 68*3117ece4Schristos- Compression source code lies in `lib/compress` 69*3117ece4Schristos 70*3117ece4Schristos- Decompression source code lies in `lib/decompress` 71*3117ece4Schristos 72*3117ece4Schristos- It's possible to include only `compress` or only `decompress`, they don't depend on each other. 73*3117ece4Schristos 74*3117ece4Schristos- `lib/dictBuilder` : makes it possible to generate dictionaries from a set of samples. 75*3117ece4Schristos The API is exposed in `lib/dictBuilder/zdict.h`. 76*3117ece4Schristos This module depends on both `lib/common` and `lib/compress` . 77*3117ece4Schristos 78*3117ece4Schristos- `lib/legacy` : makes it possible to decompress legacy zstd formats, starting from `v0.1.0`. 79*3117ece4Schristos This module depends on `lib/common` and `lib/decompress`. 80*3117ece4Schristos To enable this feature, define `ZSTD_LEGACY_SUPPORT` during compilation. 81*3117ece4Schristos Specifying a number limits versions supported to that version onward. 82*3117ece4Schristos For example, `ZSTD_LEGACY_SUPPORT=2` means : "support legacy formats >= v0.2.0". 83*3117ece4Schristos Conversely, `ZSTD_LEGACY_SUPPORT=0` means "do __not__ support legacy formats". 84*3117ece4Schristos By default, this build macro is set as `ZSTD_LEGACY_SUPPORT=5`. 85*3117ece4Schristos Decoding supported legacy format is a transparent capability triggered within decompression functions. 86*3117ece4Schristos It's also allowed to invoke legacy API directly, exposed in `lib/legacy/zstd_legacy.h`. 87*3117ece4Schristos Each version does also provide its own set of advanced API. 88*3117ece4Schristos For example, advanced API for version `v0.4` is exposed in `lib/legacy/zstd_v04.h` . 89*3117ece4Schristos 90*3117ece4Schristos- While invoking `make libzstd`, it's possible to define build macros 91*3117ece4Schristos `ZSTD_LIB_COMPRESSION`, `ZSTD_LIB_DECOMPRESSION`, `ZSTD_LIB_DICTBUILDER`, 92*3117ece4Schristos and `ZSTD_LIB_DEPRECATED` as `0` to forgo compilation of the 93*3117ece4Schristos corresponding features. This will also disable compilation of all 94*3117ece4Schristos dependencies (e.g. `ZSTD_LIB_COMPRESSION=0` will also disable 95*3117ece4Schristos dictBuilder). 96*3117ece4Schristos 97*3117ece4Schristos- There are a number of options that can help minimize the binary size of 98*3117ece4Schristos `libzstd`. 99*3117ece4Schristos 100*3117ece4Schristos The first step is to select the components needed (using the above-described 101*3117ece4Schristos `ZSTD_LIB_COMPRESSION` etc.). 102*3117ece4Schristos 103*3117ece4Schristos The next step is to set `ZSTD_LIB_MINIFY` to `1` when invoking `make`. This 104*3117ece4Schristos disables various optional components and changes the compilation flags to 105*3117ece4Schristos prioritize space-saving. 106*3117ece4Schristos 107*3117ece4Schristos Detailed options: Zstandard's code and build environment is set up by default 108*3117ece4Schristos to optimize above all else for performance. In pursuit of this goal, Zstandard 109*3117ece4Schristos makes significant trade-offs in code size. For example, Zstandard often has 110*3117ece4Schristos more than one implementation of a particular component, with each 111*3117ece4Schristos implementation optimized for different scenarios. For example, the Huffman 112*3117ece4Schristos decoder has complementary implementations that decode the stream one symbol at 113*3117ece4Schristos a time or two symbols at a time. Zstd normally includes both (and dispatches 114*3117ece4Schristos between them at runtime), but by defining `HUF_FORCE_DECOMPRESS_X1` or 115*3117ece4Schristos `HUF_FORCE_DECOMPRESS_X2`, you can force the use of one or the other, avoiding 116*3117ece4Schristos compilation of the other. Similarly, `ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT` 117*3117ece4Schristos and `ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG` force the compilation and use of 118*3117ece4Schristos only one or the other of two decompression implementations. The smallest 119*3117ece4Schristos binary is achieved by using `HUF_FORCE_DECOMPRESS_X1` and 120*3117ece4Schristos `ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT` (implied by `ZSTD_LIB_MINIFY`). 121*3117ece4Schristos 122*3117ece4Schristos On the compressor side, Zstd's compression levels map to several internal 123*3117ece4Schristos strategies. In environments where the higher compression levels aren't used, 124*3117ece4Schristos it is possible to exclude all but the fastest strategy with 125*3117ece4Schristos `ZSTD_LIB_EXCLUDE_COMPRESSORS_DFAST_AND_UP=1`. (Note that this will change 126*3117ece4Schristos the behavior of the default compression level.) Or if you want to retain the 127*3117ece4Schristos default compressor as well, you can set 128*3117ece4Schristos `ZSTD_LIB_EXCLUDE_COMPRESSORS_GREEDY_AND_UP=1`, at the cost of an additional 129*3117ece4Schristos ~20KB or so. 130*3117ece4Schristos 131*3117ece4Schristos For squeezing the last ounce of size out, you can also define 132*3117ece4Schristos `ZSTD_NO_INLINE`, which disables inlining, and `ZSTD_STRIP_ERROR_STRINGS`, 133*3117ece4Schristos which removes the error messages that are otherwise returned by 134*3117ece4Schristos `ZSTD_getErrorName` (implied by `ZSTD_LIB_MINIFY`). 135*3117ece4Schristos 136*3117ece4Schristos Finally, when integrating into your application, make sure you're doing link- 137*3117ece4Schristos time optimization and unused symbol garbage collection (via some combination of, 138*3117ece4Schristos e.g., `-flto`, `-ffat-lto-objects`, `-fuse-linker-plugin`, 139*3117ece4Schristos `-ffunction-sections`, `-fdata-sections`, `-fmerge-all-constants`, 140*3117ece4Schristos `-Wl,--gc-sections`, `-Wl,-z,norelro`, and an archiver that understands 141*3117ece4Schristos the compiler's intermediate representation, e.g., `AR=gcc-ar`). Consult your 142*3117ece4Schristos compiler's documentation. 143*3117ece4Schristos 144*3117ece4Schristos- While invoking `make libzstd`, the build macro `ZSTD_LEGACY_MULTITHREADED_API=1` 145*3117ece4Schristos will expose the deprecated `ZSTDMT` API exposed by `zstdmt_compress.h` in 146*3117ece4Schristos the shared library, which is now hidden by default. 147*3117ece4Schristos 148*3117ece4Schristos- The build macro `DYNAMIC_BMI2` can be set to 1 or 0 in order to generate binaries 149*3117ece4Schristos which can detect at runtime the presence of BMI2 instructions, and use them only if present. 150*3117ece4Schristos These instructions contribute to better performance, notably on the decoder side. 151*3117ece4Schristos By default, this feature is automatically enabled on detecting 152*3117ece4Schristos the right instruction set (x64) and compiler (clang or gcc >= 5). 153*3117ece4Schristos It's obviously disabled for different cpus, 154*3117ece4Schristos or when BMI2 instruction set is _required_ by the compiler command line 155*3117ece4Schristos (in this case, only the BMI2 code path is generated). 156*3117ece4Schristos Setting this macro will either force to generate the BMI2 dispatcher (1) 157*3117ece4Schristos or prevent it (0). It overrides automatic detection. 158*3117ece4Schristos 159*3117ece4Schristos- The build macro `ZSTD_NO_UNUSED_FUNCTIONS` can be defined to hide the definitions of functions 160*3117ece4Schristos that zstd does not use. Not all unused functions are hidden, but they can be if needed. 161*3117ece4Schristos Currently, this macro will hide function definitions in FSE and HUF that use an excessive 162*3117ece4Schristos amount of stack space. 163*3117ece4Schristos 164*3117ece4Schristos- The build macro `ZSTD_NO_INTRINSICS` can be defined to disable all explicit intrinsics. 165*3117ece4Schristos Compiler builtins are still used. 166*3117ece4Schristos 167*3117ece4Schristos- The build macro `ZSTD_DECODER_INTERNAL_BUFFER` can be set to control 168*3117ece4Schristos the amount of extra memory used during decompression to store literals. 169*3117ece4Schristos This defaults to 64kB. Reducing this value reduces the memory footprint of 170*3117ece4Schristos `ZSTD_DCtx` decompression contexts, 171*3117ece4Schristos but might also result in a small decompression speed cost. 172*3117ece4Schristos 173*3117ece4Schristos- The C compiler macros `ZSTDLIB_VISIBLE`, `ZSTDERRORLIB_VISIBLE` and `ZDICTLIB_VISIBLE` 174*3117ece4Schristos can be overridden to control the visibility of zstd's API. Additionally, 175*3117ece4Schristos `ZSTDLIB_STATIC_API` and `ZDICTLIB_STATIC_API` can be overridden to control the visibility 176*3117ece4Schristos of zstd's static API. Specifically, it can be set to `ZSTDLIB_HIDDEN` to hide the symbols 177*3117ece4Schristos from the shared library. These macros default to `ZSTDLIB_VISIBILITY`, 178*3117ece4Schristos `ZSTDERRORLIB_VSIBILITY`, and `ZDICTLIB_VISIBILITY` if unset, for backwards compatibility 179*3117ece4Schristos with the old macro names. 180*3117ece4Schristos 181*3117ece4Schristos- The C compiler macro `HUF_DISABLE_FAST_DECODE` disables the newer Huffman fast C 182*3117ece4Schristos and assembly decoding loops. You may want to use this macro if these loops are 183*3117ece4Schristos slower on your platform. 184*3117ece4Schristos 185*3117ece4Schristos#### Windows : using MinGW+MSYS to create DLL 186*3117ece4Schristos 187*3117ece4SchristosDLL can be created using MinGW+MSYS with the `make libzstd` command. 188*3117ece4SchristosThis command creates `dll\libzstd.dll` and the import library `dll\libzstd.lib`. 189*3117ece4SchristosThe import library is only required with Visual C++. 190*3117ece4SchristosThe header file `zstd.h` and the dynamic library `dll\libzstd.dll` are required to 191*3117ece4Schristoscompile a project using gcc/MinGW. 192*3117ece4SchristosThe dynamic library has to be added to linking options. 193*3117ece4SchristosIt means that if a project that uses ZSTD consists of a single `test-dll.c` 194*3117ece4Schristosfile it should be linked with `dll\libzstd.dll`. For example: 195*3117ece4Schristos``` 196*3117ece4Schristos gcc $(CFLAGS) -Iinclude/ test-dll.c -o test-dll dll\libzstd.dll 197*3117ece4Schristos``` 198*3117ece4SchristosThe compiled executable will require ZSTD DLL which is available at `dll\libzstd.dll`. 199*3117ece4Schristos 200*3117ece4Schristos 201*3117ece4Schristos#### Advanced Build options 202*3117ece4Schristos 203*3117ece4SchristosThe build system requires a hash function in order to 204*3117ece4Schristosseparate object files created with different compilation flags. 205*3117ece4SchristosBy default, it tries to use `md5sum` or equivalent. 206*3117ece4SchristosThe hash function can be manually switched by setting the `HASH` variable. 207*3117ece4SchristosFor example : `make HASH=xxhsum` 208*3117ece4SchristosThe hash function needs to generate at least 64-bit using hexadecimal format. 209*3117ece4SchristosWhen no hash function is found, 210*3117ece4Schristosthe Makefile just generates all object files into the same default directory, 211*3117ece4Schristosirrespective of compilation flags. 212*3117ece4SchristosThis functionality only matters if `libzstd` is compiled multiple times 213*3117ece4Schristoswith different build flags. 214*3117ece4Schristos 215*3117ece4SchristosThe build directory, where object files are stored 216*3117ece4Schristoscan also be manually controlled using variable `BUILD_DIR`, 217*3117ece4Schristosfor example `make BUILD_DIR=objectDir/v1`. 218*3117ece4SchristosIn which case, the hash function doesn't matter. 219*3117ece4Schristos 220*3117ece4Schristos 221*3117ece4Schristos#### Deprecated API 222*3117ece4Schristos 223*3117ece4SchristosObsolete API on their way out are stored in directory `lib/deprecated`. 224*3117ece4SchristosAt this stage, it contains older streaming prototypes, in `lib/deprecated/zbuff.h`. 225*3117ece4SchristosThese prototypes will be removed in some future version. 226*3117ece4SchristosConsider migrating code towards supported streaming API exposed in `zstd.h`. 227*3117ece4Schristos 228*3117ece4Schristos 229*3117ece4Schristos#### Miscellaneous 230*3117ece4Schristos 231*3117ece4SchristosThe other files are not source code. There are : 232*3117ece4Schristos 233*3117ece4Schristos - `BUCK` : support for `buck` build system (https://buckbuild.com/) 234*3117ece4Schristos - `Makefile` : `make` script to build and install zstd library (static and dynamic) 235*3117ece4Schristos - `README.md` : this file 236*3117ece4Schristos - `dll/` : resources directory for Windows compilation 237*3117ece4Schristos - `libzstd.pc.in` : script for `pkg-config` (used in `make install`) 238