diff --git a/lib/common/portability_macros.h b/lib/common/portability_macros.h index 860734141..bcca634e4 100644 --- a/lib/common/portability_macros.h +++ b/lib/common/portability_macros.h @@ -168,4 +168,23 @@ # define ZSTD_CET_ENDBRANCH #endif +/** + * ZSTD_IS_DETERMINISTIC_BUILD must be set to 0 if any compilation macro is + * active that impacts the compressed output. + * + * NOTE: ZSTD_MULTITHREAD is allowed to be set or unset. + */ +#if defined(ZSTD_CLEVEL_DEFAULT) \ + || defined(ZSTD_EXCLUDE_DFAST_BLOCK_COMPRESSOR) \ + || defined(ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR) \ + || defined(ZSTD_EXCLUDE_LAZY_BLOCK_COMPRESSOR) \ + || defined(ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR) \ + || defined(ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR) \ + || defined(ZSTD_EXCLUDE_BTOPT_BLOCK_COMPRESSOR) \ + || defined(ZSTD_EXCLUDE_BTULTRA_BLOCK_COMPRESSOR) +# define ZSTD_IS_DETERMINISTIC_BUILD 0 +#else +# define ZSTD_IS_DETERMINISTIC_BUILD 1 +#endif + #endif /* ZSTD_PORTABILITY_MACROS_H */ diff --git a/lib/common/zstd_common.c b/lib/common/zstd_common.c index 3f04c22ab..985420618 100644 --- a/lib/common/zstd_common.c +++ b/lib/common/zstd_common.c @@ -46,3 +46,12 @@ ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); } /*! ZSTD_getErrorString() : * provides error code string from enum */ const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); } + +int ZSTD_isDeterministicBuild(void) +{ +#if ZSTD_IS_DETERMINISTIC_BUILD + return 1; +#else + return 0; +#endif +} diff --git a/lib/zstd.h b/lib/zstd.h index b8c0644a7..4ce2f7742 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -3138,6 +3138,18 @@ ZSTDLIB_STATIC_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx); +/*! ZSTD_isDeterministicBuild() : + * Returns 1 if the library is built using standard compilation flags, + * and participates in determinism guarantees with other builds of the + * same version. + * If this function returns 0, it means the library was compiled with + * non-standard compilation flags that change the output of the + * compressor. + * This is mainly used for Zstd's determinism test suite, which is only + * run when this function returns 1. + */ +ZSTDLIB_API int ZSTD_isDeterministicBuild(void); + /* ========================================= */ /** Block level API (DEPRECATED) */ diff --git a/programs/zstdcli.c b/programs/zstdcli.c index e2771f534..66e9d064b 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -695,7 +695,10 @@ static void printVersion(void) #ifdef PLATFORM_POSIX_VERSION DISPLAYOUT("PLATFORM_POSIX_VERSION defined: %ldL\n", (long) PLATFORM_POSIX_VERSION); #endif - } } + + if (!ZSTD_isDeterministicBuild()) { + DISPLAYOUT("non-deterministic build\n"); + } } } } #define ZSTD_NB_STRATEGIES 9