blob: f781ac57e3d610a88f91a5b9a853ae14fdfe9a14 [file] [log] [blame] [view]
inikep6416b0d2016-08-29 13:04:26 +02001Zstandard library files
Yann Colletd56a4192016-02-17 17:47:29 +01002================================
3
Yann Collet36374cc2017-09-06 16:15:18 -07004The __lib__ directory is split into several sub-directories,
Yann Collet2eff2172017-12-31 15:50:00 +01005in order to make it easier to select or exclude features.
Yann Collet36374cc2017-09-06 16:15:18 -07006
7
8#### Building
9
Yann Collet0fb4b212018-12-25 03:10:07 -080010`Makefile` script is provided, supporting [Makefile conventions](https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html#Makefile-Conventions),
Yann Collet2eff2172017-12-31 15:50:00 +010011including commands variables, staged install, directory variables and standard targets.
Yann Collet36374cc2017-09-06 16:15:18 -070012- `make` : generates both static and dynamic libraries
Yann Collet0fb4b212018-12-25 03:10:07 -080013- `make install` : install libraries and headers in target system directories
inikepa8138fd2016-04-25 11:36:44 +020014
Yann Collet0fb4b212018-12-25 03:10:07 -080015`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
sen91465e22021-05-07 11:13:30 -040022When building with `make`, by default the dynamic library is multithreaded and static library is single-threaded (for compatibility reasons).
23
Yann Collet0fb4b212018-12-25 03:10:07 -080024Enabling multithreading requires 2 conditions :
25- set build macro `ZSTD_MULTITHREAD` (`-DZSTD_MULTITHREAD` for `gcc`)
26- for POSIX systems : compile with pthread (`-pthread` compilation flag for `gcc`)
27
sen91465e22021-05-07 11:13:30 -040028For convenience, we provide a build target to generate multi and single threaded libraries:
29- Force enable multithreading on both dynamic and static libraries by appending `-mt` to the target, e.g. `make lib-mt`.
30- Force disable multithreading on both dynamic and static libraries by appending `-nomt` to the target, e.g. `make lib-nomt`.
31- By default, as mentioned before, dynamic library is multithreaded, and static library is single-threaded, e.g. `make lib`.
Yann Collet0fb4b212018-12-25 03:10:07 -080032
33When linking a POSIX program with a multithreaded version of `libzstd`,
Yann Colletf966cd02019-10-22 17:43:09 -070034note that it's necessary to invoke the `-pthread` flag during link stage.
Yann Collet0fb4b212018-12-25 03:10:07 -080035
36Multithreading capabilities are exposed
Yann Colletf966cd02019-10-22 17:43:09 -070037via the [advanced API defined in `lib/zstd.h`](https://github.com/facebook/zstd/blob/v1.4.3/lib/zstd.h#L351).
Yann Collet2eff2172017-12-31 15:50:00 +010038
Yann Colletd56a4192016-02-17 17:47:29 +010039
Yann Collet85f39192016-07-17 20:42:21 +020040#### API
inikep49794312016-04-25 11:31:28 +020041
Yann Collet36374cc2017-09-06 16:15:18 -070042Zstandard's stable API is exposed within [lib/zstd.h](zstd.h).
inikep49794312016-04-25 11:31:28 +020043
Yann Colletdcc000f2016-03-10 01:09:41 +010044
Yann Collet85f39192016-07-17 20:42:21 +020045#### Advanced API
Yann Colletd56a4192016-02-17 17:47:29 +010046
Yann Collet36374cc2017-09-06 16:15:18 -070047Optional advanced features are exposed via :
48
Nick Terrell09149be2021-04-30 15:02:12 -070049- `lib/zstd_errors.h` : translates `size_t` function results
50 into a `ZSTD_ErrorCode`, for accurate error handling.
Yann Collet0fb4b212018-12-25 03:10:07 -080051
Yann Collet36374cc2017-09-06 16:15:18 -070052- `ZSTD_STATIC_LINKING_ONLY` : if this macro is defined _before_ including `zstd.h`,
Yann Collet0fb4b212018-12-25 03:10:07 -080053 it unlocks access to the experimental API,
54 exposed in the second part of `zstd.h`.
55 All definitions in the experimental APIs are unstable,
56 they may still change in the future, or even be removed.
57 As a consequence, experimental definitions shall ___never be used with dynamic library___ !
Yann Collet85f39192016-07-17 20:42:21 +020058 Only static linking is allowed.
Yann Colletd56a4192016-02-17 17:47:29 +010059
Yann Collet26f68142016-07-08 10:42:59 +020060
Yann Collet85f39192016-07-17 20:42:21 +020061#### Modular build
Yann Colletd56a4192016-02-17 17:47:29 +010062
Yann Collet0fb4b212018-12-25 03:10:07 -080063It's possible to compile only a limited set of features within `libzstd`.
64The file structure is designed to make this selection manually achievable for any build system :
Yann Collet2eff2172017-12-31 15:50:00 +010065
Yann Collet36374cc2017-09-06 16:15:18 -070066- Directory `lib/common` is always required, for all variants.
Yann Collet0fb4b212018-12-25 03:10:07 -080067
Yann Collet36374cc2017-09-06 16:15:18 -070068- Compression source code lies in `lib/compress`
Yann Collet0fb4b212018-12-25 03:10:07 -080069
Yann Collet36374cc2017-09-06 16:15:18 -070070- Decompression source code lies in `lib/decompress`
Yann Collet0fb4b212018-12-25 03:10:07 -080071
Yann Collet36374cc2017-09-06 16:15:18 -070072- It's possible to include only `compress` or only `decompress`, they don't depend on each other.
Yann Collet0fb4b212018-12-25 03:10:07 -080073
Yann Collet36374cc2017-09-06 16:15:18 -070074- `lib/dictBuilder` : makes it possible to generate dictionaries from a set of samples.
Yann Collet2eff2172017-12-31 15:50:00 +010075 The API is exposed in `lib/dictBuilder/zdict.h`.
76 This module depends on both `lib/common` and `lib/compress` .
Yann Collet0fb4b212018-12-25 03:10:07 -080077
78- `lib/legacy` : makes it possible to decompress legacy zstd formats, starting from `v0.1.0`.
Yann Collet2eff2172017-12-31 15:50:00 +010079 This module depends on `lib/common` and `lib/decompress`.
Yann Colletb2939162018-09-20 14:24:23 -070080 To enable this feature, define `ZSTD_LEGACY_SUPPORT` during compilation.
81 Specifying a number limits versions supported to that version onward.
Yann Collet2eff2172017-12-31 15:50:00 +010082 For example, `ZSTD_LEGACY_SUPPORT=2` means : "support legacy formats >= v0.2.0".
Yann Collet0fb4b212018-12-25 03:10:07 -080083 Conversely, `ZSTD_LEGACY_SUPPORT=0` means "do __not__ support legacy formats".
84 By default, this build macro is set as `ZSTD_LEGACY_SUPPORT=5`.
85 Decoding supported legacy format is a transparent capability triggered within decompression functions.
86 It's also allowed to invoke legacy API directly, exposed in `lib/legacy/zstd_legacy.h`.
87 Each version does also provide its own set of advanced API.
Yann Collet2eff2172017-12-31 15:50:00 +010088 For example, advanced API for version `v0.4` is exposed in `lib/legacy/zstd_v04.h` .
Yann Collet0fb4b212018-12-25 03:10:07 -080089
90- While invoking `make libzstd`, it's possible to define build macros
91 `ZSTD_LIB_COMPRESSION, ZSTD_LIB_DECOMPRESSION`, `ZSTD_LIB_DICTBUILDER`,
W. Felix Handtefa5e01c2020-01-27 17:14:08 -050092 and `ZSTD_LIB_DEPRECATED` as `0` to forgo compilation of the
93 corresponding features. This will also disable compilation of all
94 dependencies (eg. `ZSTD_LIB_COMPRESSION=0` will also disable
95 dictBuilder).
Yann Collet0fb4b212018-12-25 03:10:07 -080096
W. Felix Handtefa5e01c2020-01-27 17:14:08 -050097- There are a number of options that can help minimize the binary size of
98 `libzstd`.
W. Felix Handteece2c182018-12-06 10:32:36 -080099
W. Felix Handtefa5e01c2020-01-27 17:14:08 -0500100 The first step is to select the components needed (using the above-described
101 `ZSTD_LIB_COMPRESSION` etc.).
102
103 The next step is to set `ZSTD_LIB_MINIFY` to `1` when invoking `make`. This
104 disables various optional components and changes the compilation flags to
105 prioritize space-saving.
106
107 Detailed options: Zstandard's code and build environment is set up by default
108 to optimize above all else for performance. In pursuit of this goal, Zstandard
109 makes significant trade-offs in code size. For example, Zstandard often has
110 more than one implementation of a particular component, with each
111 implementation optimized for different scenarios. For example, the Huffman
112 decoder has complementary implementations that decode the stream one symbol at
113 a time or two symbols at a time. Zstd normally includes both (and dispatches
114 between them at runtime), but by defining `HUF_FORCE_DECOMPRESS_X1` or
115 `HUF_FORCE_DECOMPRESS_X2`, you can force the use of one or the other, avoiding
W. Felix Handteece2c182018-12-06 10:32:36 -0800116 compilation of the other. Similarly, `ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT`
117 and `ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG` force the compilation and use of
118 only one or the other of two decompression implementations. The smallest
119 binary is achieved by using `HUF_FORCE_DECOMPRESS_X1` and
W. Felix Handtefa5e01c2020-01-27 17:14:08 -0500120 `ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT` (implied by `ZSTD_LIB_MINIFY`).
W. Felix Handteece2c182018-12-06 10:32:36 -0800121
122 For squeezing the last ounce of size out, you can also define
123 `ZSTD_NO_INLINE`, which disables inlining, and `ZSTD_STRIP_ERROR_STRINGS`,
124 which removes the error messages that are otherwise returned by
W. Felix Handtefa5e01c2020-01-27 17:14:08 -0500125 `ZSTD_getErrorName` (implied by `ZSTD_LIB_MINIFY`).
126
127 Finally, when integrating into your application, make sure you're doing link-
128 time optimation and unused symbol garbage collection (via some combination of,
129 e.g., `-flto`, `-ffat-lto-objects`, `-fuse-linker-plugin`,
130 `-ffunction-sections`, `-fdata-sections`, `-fmerge-all-constants`,
131 `-Wl,--gc-sections`, `-Wl,-z,norelro`, and an archiver that understands
132 the compiler's intermediate representation, e.g., `AR=gcc-ar`). Consult your
133 compiler's documentation.
Yann Colletd56a4192016-02-17 17:47:29 +0100134
Nick Terrell641e5942019-04-07 18:47:52 -0700135- While invoking `make libzstd`, the build macro `ZSTD_LEGACY_MULTITHREADED_API=1`
Nick Terrell947548c2019-04-08 16:50:18 -0700136 will expose the deprecated `ZSTDMT` API exposed by `zstdmt_compress.h` in
Nick Terrell641e5942019-04-07 18:47:52 -0700137 the shared library, which is now hidden by default.
138
Yann Colletf966cd02019-10-22 17:43:09 -0700139- The build macro `DYNAMIC_BMI2` can be set to 1 or 0 in order to generate binaries
140 which can detect at runtime the presence of BMI2 instructions, and use them only if present.
141 These instructions contribute to better performance, notably on the decoder side.
142 By default, this feature is automatically enabled on detecting
143 the right instruction set (x64) and compiler (clang or gcc >= 5).
144 It's obviously disabled for different cpus,
145 or when BMI2 instruction set is _required_ by the compiler command line
146 (in this case, only the BMI2 code path is generated).
147 Setting this macro will either force to generate the BMI2 dispatcher (1)
148 or prevent it (0). It overrides automatic detection.
149
Nick Terrellb92569a2020-09-09 17:13:16 -0700150- The build macro `ZSTD_NO_UNUSED_FUNCTIONS` can be defined to hide the definitions of functions
151 that zstd does not use. Not all unused functions are hidden, but they can be if needed.
152 Currently, this macro will hide function definitions in FSE and HUF that use an excessive
153 amount of stack space.
154
155- The build macro `ZSTD_NO_INTRINSICS` can be defined to disable all explicit intrinsics.
156 Compiler builtins are still used.
157
Yann Collet26f68142016-07-08 10:42:59 +0200158
Yann Collet36374cc2017-09-06 16:15:18 -0700159#### Windows : using MinGW+MSYS to create DLL
Przemyslaw Skibinski62d19a62016-11-21 14:22:08 +0100160
161DLL can be created using MinGW+MSYS with the `make libzstd` command.
162This command creates `dll\libzstd.dll` and the import library `dll\libzstd.lib`.
163The import library is only required with Visual C++.
164The header file `zstd.h` and the dynamic library `dll\libzstd.dll` are required to
165compile a project using gcc/MinGW.
166The dynamic library has to be added to linking options.
167It means that if a project that uses ZSTD consists of a single `test-dll.c`
168file it should be linked with `dll\libzstd.dll`. For example:
169```
170 gcc $(CFLAGS) -Iinclude/ test-dll.c -o test-dll dll\libzstd.dll
171```
Yann Collet825dffb2016-12-05 19:28:19 -0800172The compiled executable will require ZSTD DLL which is available at `dll\libzstd.dll`.
Przemyslaw Skibinski62d19a62016-11-21 14:22:08 +0100173
174
Yann Colletce6cd072020-10-22 12:31:23 -0700175#### Advanced Build options
176
177The build system requires a hash function in order to
178separate object files created with different compilation flags.
179By default, it tries to use `md5sum` or equivalent.
180The hash function can be manually switched by setting the `HASH` variable.
181For example : `make HASH=xxhsum`
182The hash function needs to generate at least 64-bit using hexadecimal format.
183When no hash function is found,
184the Makefile just generates all object files into the same default directory,
185irrespective of compilation flags.
186This functionality only matters if `libzstd` is compiled multiple times
187with different build flags.
188
189The build directory, where object files are stored
190can also be manually controlled using variable `BUILD_DIR`,
191for example `make BUILD_DIR=objectDir/v1`.
192In which case, the hash function doesn't matter.
193
194
Yann Collet36374cc2017-09-06 16:15:18 -0700195#### Deprecated API
Yann Collet26f68142016-07-08 10:42:59 +0200196
Yann Collet36374cc2017-09-06 16:15:18 -0700197Obsolete API on their way out are stored in directory `lib/deprecated`.
198At this stage, it contains older streaming prototypes, in `lib/deprecated/zbuff.h`.
Yann Collet36374cc2017-09-06 16:15:18 -0700199These prototypes will be removed in some future version.
200Consider migrating code towards supported streaming API exposed in `zstd.h`.
Yann Collet26f68142016-07-08 10:42:59 +0200201
Yann Colletd56a4192016-02-17 17:47:29 +0100202
203#### Miscellaneous
204
205The other files are not source code. There are :
206
Yann Collet1c7b9142017-09-06 16:23:39 -0700207 - `BUCK` : support for `buck` build system (https://buckbuild.com/)
Yann Collet0fb4b212018-12-25 03:10:07 -0800208 - `Makefile` : `make` script to build and install zstd library (static and dynamic)
Yann Collet36374cc2017-09-06 16:15:18 -0700209 - `README.md` : this file
Yann Collet0fb4b212018-12-25 03:10:07 -0800210 - `dll/` : resources directory for Windows compilation
211 - `libzstd.pc.in` : script for `pkg-config` (used in `make install`)