xref: /freebsd/sys/contrib/zstd/lib/README.md (revision 0957b409)
1Zstandard library files
2================================
3
4The __lib__ directory is split into several sub-directories,
5in order to make it easier to select or exclude features.
6
7
8#### Building
9
10`Makefile` script is provided, supporting [Makefile conventions](https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html#Makefile-Conventions),
11including commands variables, staged install, directory variables and standard targets.
12- `make` : generates both static and dynamic libraries
13- `make install` : install libraries and headers in target system directories
14
15`libzstd` default scope is pretty large, including compression, decompression, dictionary builder,
16and support for decoding legacy formats >= v0.5.0.
17The scope can be reduced on demand (see paragraph _modular build_).
18
19
20#### Multithreading support
21
22Multithreading is disabled by default when building with `make`.
23Enabling multithreading requires 2 conditions :
24- set build macro `ZSTD_MULTITHREAD` (`-DZSTD_MULTITHREAD` for `gcc`)
25- for POSIX systems : compile with pthread (`-pthread` compilation flag for `gcc`)
26
27Both conditions are automatically applied when invoking `make lib-mt` target.
28
29When linking a POSIX program with a multithreaded version of `libzstd`,
30note that it's necessary to request the `-pthread` flag during link stage.
31
32Multithreading capabilities are exposed
33via the [advanced API defined in `lib/zstd.h`](https://github.com/facebook/zstd/blob/v1.3.8/lib/zstd.h#L592).
34This API is still labelled experimental,
35but is expected to become "stable" in the near future.
36
37
38#### API
39
40Zstandard's stable API is exposed within [lib/zstd.h](zstd.h).
41
42
43#### Advanced API
44
45Optional advanced features are exposed via :
46
47- `lib/common/zstd_errors.h` : translates `size_t` function results
48                               into a `ZSTD_ErrorCode`, for accurate error handling.
49
50- `ZSTD_STATIC_LINKING_ONLY` : if this macro is defined _before_ including `zstd.h`,
51                          it unlocks access to the experimental API,
52                          exposed in the second part of `zstd.h`.
53                          All definitions in the experimental APIs are unstable,
54                          they may still change in the future, or even be removed.
55                          As a consequence, experimental definitions shall ___never be used with dynamic library___ !
56                          Only static linking is allowed.
57
58
59#### Modular build
60
61It's possible to compile only a limited set of features within `libzstd`.
62The file structure is designed to make this selection manually achievable for any build system :
63
64- Directory `lib/common` is always required, for all variants.
65
66- Compression source code lies in `lib/compress`
67
68- Decompression source code lies in `lib/decompress`
69
70- It's possible to include only `compress` or only `decompress`, they don't depend on each other.
71
72- `lib/dictBuilder` : makes it possible to generate dictionaries from a set of samples.
73        The API is exposed in `lib/dictBuilder/zdict.h`.
74        This module depends on both `lib/common` and `lib/compress` .
75
76- `lib/legacy` : makes it possible to decompress legacy zstd formats, starting from `v0.1.0`.
77        This module depends on `lib/common` and `lib/decompress`.
78        To enable this feature, define `ZSTD_LEGACY_SUPPORT` during compilation.
79        Specifying a number limits versions supported to that version onward.
80        For example, `ZSTD_LEGACY_SUPPORT=2` means : "support legacy formats >= v0.2.0".
81        Conversely, `ZSTD_LEGACY_SUPPORT=0` means "do __not__ support legacy formats".
82        By default, this build macro is set as `ZSTD_LEGACY_SUPPORT=5`.
83        Decoding supported legacy format is a transparent capability triggered within decompression functions.
84        It's also allowed to invoke legacy API directly, exposed in `lib/legacy/zstd_legacy.h`.
85        Each version does also provide its own set of advanced API.
86        For example, advanced API for version `v0.4` is exposed in `lib/legacy/zstd_v04.h` .
87
88- While invoking `make libzstd`, it's possible to define build macros
89        `ZSTD_LIB_COMPRESSION, ZSTD_LIB_DECOMPRESSION`, `ZSTD_LIB_DICTBUILDER`,
90        and `ZSTD_LIB_DEPRECATED` as `0` to forgo compilation of the corresponding features.
91        This will also disable compilation of all dependencies
92        (eg. `ZSTD_LIB_COMPRESSION=0` will also disable dictBuilder).
93
94- There are some additional build macros that can be used to minify the decoder.
95
96  Zstandard often has more than one implementation of a piece of functionality,
97  where each implementation optimizes for different scenarios. For example, the
98  Huffman decoder has complementary implementations that decode the stream one
99  symbol at a time or two symbols at a time. Zstd normally includes both (and
100  dispatches between them at runtime), but by defining `HUF_FORCE_DECOMPRESS_X1`
101  or `HUF_FORCE_DECOMPRESS_X2`, you can force the use of one or the other, avoiding
102  compilation of the other. Similarly, `ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT`
103  and `ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG` force the compilation and use of
104  only one or the other of two decompression implementations. The smallest
105  binary is achieved by using `HUF_FORCE_DECOMPRESS_X1` and
106  `ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT`.
107
108  For squeezing the last ounce of size out, you can also define
109  `ZSTD_NO_INLINE`, which disables inlining, and `ZSTD_STRIP_ERROR_STRINGS`,
110  which removes the error messages that are otherwise returned by
111  `ZSTD_getErrorName`.
112
113
114#### Windows : using MinGW+MSYS to create DLL
115
116DLL can be created using MinGW+MSYS with the `make libzstd` command.
117This command creates `dll\libzstd.dll` and the import library `dll\libzstd.lib`.
118The import library is only required with Visual C++.
119The header file `zstd.h` and the dynamic library `dll\libzstd.dll` are required to
120compile a project using gcc/MinGW.
121The dynamic library has to be added to linking options.
122It means that if a project that uses ZSTD consists of a single `test-dll.c`
123file it should be linked with `dll\libzstd.dll`. For example:
124```
125    gcc $(CFLAGS) -Iinclude/ test-dll.c -o test-dll dll\libzstd.dll
126```
127The compiled executable will require ZSTD DLL which is available at `dll\libzstd.dll`.
128
129
130#### Deprecated API
131
132Obsolete API on their way out are stored in directory `lib/deprecated`.
133At this stage, it contains older streaming prototypes, in `lib/deprecated/zbuff.h`.
134These prototypes will be removed in some future version.
135Consider migrating code towards supported streaming API exposed in `zstd.h`.
136
137
138#### Miscellaneous
139
140The other files are not source code. There are :
141
142 - `BUCK` : support for `buck` build system (https://buckbuild.com/)
143 - `Makefile` : `make` script to build and install zstd library (static and dynamic)
144 - `README.md` : this file
145 - `dll/` : resources directory for Windows compilation
146 - `libzstd.pc.in` : script for `pkg-config` (used in `make install`)
147