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