Skip to content

Commit 4463339

Browse files
committed
[erts] Make it possible to use the systems libzstd
1 parent d0a8614 commit 4463339

6 files changed

Lines changed: 165 additions & 3 deletions

File tree

erts/config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@
8484
/* Define if builtin zlib should be used */
8585
#undef ERTS_USE_BUILTIN_ZLIB
8686

87+
/* Define if builtin zstd should be used */
88+
#undef ERTS_USE_BUILTIN_ZSTD
89+
8790
/* Define to 1 if alternatives to embedded 3pps should be used, 0 if not, and
8891
-1 to use the default */
8992
#undef ERTS_USE_EMBEDDED_3PP_ALTS

erts/configure

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ LIBSCTP
670670
SYSTEMD_DAEMON_LIBS
671671
SOCKET_LIBS
672672
Z_LIB
673+
LIBZSTD
673674
ERTS_USE_BUILTIN_ERRNO_ID
674675
TERMCAP_LIB
675676
THR_DEFS
@@ -855,6 +856,7 @@ enable_ppc_lwsync_instruction
855856
with_threadnames
856857
enable_use_embedded_3pp_alternatives
857858
enable_builtin_errno_id
859+
enable_builtin_zstd
858860
enable_builtin_zlib
859861
enable_esock
860862
enable_esock_use_rcvsndtimeo
@@ -1610,6 +1612,8 @@ Optional Features:
16101612
--disable-builtin-errno-id
16111613
try to find an alternative on the system to use for
16121614
errno-id
1615+
--enable-builtin-zstd force use of our own built-in zstd
1616+
--disable-builtin-zstd try to find a static libzstd on the system to use
16131617
--enable-builtin-zlib force use of our own built-in zlib
16141618
--disable-builtin-zlib try to find a zlib on the system to use
16151619
--enable-esock enable builtin socket (as a nif) support (default)
@@ -16667,12 +16671,14 @@ then :
1666716671
enableval=$enable_use_embedded_3pp_alternatives; case "$enableval" in
1666816672
no)
1666916673
enable_builtin_errno_id=yes
16674+
enable_builtin_zstd=yes
1667016675
enable_builtin_zlib=yes
1667116676
use_embedded_3pp_alts=no
1667216677
use_embedded_3pp_alts_val=0
1667316678
;;
1667416679
*)
1667516680
enable_builtin_errno_id=no
16681+
enable_builtin_zstd=no
1667616682
enable_builtin_zlib=no
1667716683
use_embedded_3pp_alts=yes
1667816684
use_embedded_3pp_alts_val=1
@@ -16682,6 +16688,7 @@ then :
1668216688
else case e in #(
1668316689
e)
1668416690
enable_builtin_errno_id=yes
16691+
enable_builtin_zstd=yes
1668516692
enable_builtin_zlib=no
1668616693
use_embedded_3pp_alts=default
1668716694
use_embedded_3pp_alts_val=-1
@@ -16731,6 +16738,82 @@ ERTS_USE_BUILTIN_ERRNO_ID=$enable_builtin_errno_id
1673116738

1673216739

1673316740

16741+
# Check whether --enable-builtin-zstd was given.
16742+
if test ${enable_builtin_zstd+y}
16743+
then :
16744+
enableval=$enable_builtin_zstd; case "$enableval" in
16745+
no) enable_builtin_zstd=no ;;
16746+
*) enable_builtin_zstd=yes ;;
16747+
esac
16748+
fi
16749+
16750+
16751+
LIBZSTD=
16752+
16753+
if test "$enable_builtin_zstd" = "no"
16754+
then :
16755+
16756+
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for static libzstd of version 1.5 or higher" >&5
16757+
printf %s "checking for static libzstd of version 1.5 or higher... " >&6; }
16758+
zstd_save_LIBS=$LIBS
16759+
LIBS="-l:libzstd.a $LIBS"
16760+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
16761+
/* end confdefs.h. */
16762+
16763+
#define ZSTD_STATIC_LINKING_ONLY
16764+
#include <zstd.h>
16765+
#include <zdict.h>
16766+
16767+
int
16768+
main (void)
16769+
{
16770+
16771+
#if ZSTD_VERSION_NUMBER >= 10500
16772+
(void) ZSTD_createCCtx_advanced(ZSTD_defaultCMem);
16773+
#else
16774+
#error "No libzstd of version 1.5 or higher found"
16775+
error
16776+
#endif
16777+
16778+
;
16779+
return 0;
16780+
}
16781+
16782+
_ACEOF
16783+
if ac_fn_c_try_link "$LINENO"
16784+
then :
16785+
16786+
LIBZSTD="-l:libzstd.a"
16787+
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
16788+
printf "%s\n" "yes" >&6; }
16789+
16790+
else case e in #(
16791+
e)
16792+
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no, using builtin zstd instead" >&5
16793+
printf "%s\n" "no, using builtin zstd instead" >&6; }
16794+
;;
16795+
esac
16796+
fi
16797+
rm -f core conftest.err conftest.$ac_objext conftest.beam \
16798+
conftest$ac_exeext conftest.$ac_ext
16799+
16800+
LIBS="$zstd_save_LIBS"
16801+
16802+
16803+
fi
16804+
16805+
16806+
16807+
if test "$LIBZSTD" = ""
16808+
then :
16809+
16810+
16811+
printf "%s\n" "#define ERTS_USE_BUILTIN_ZSTD 1" >>confdefs.h
16812+
16813+
16814+
fi
16815+
16816+
1673416817
# Check whether --enable-builtin-zlib was given.
1673516818
if test ${enable_builtin_zlib+y}
1673616819
then :

erts/configure.ac

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,12 +1259,14 @@ AS_HELP_STRING([--disable-use-embedded-3pp-alternatives],
12591259
[ case "$enableval" in
12601260
no)
12611261
enable_builtin_errno_id=yes
1262+
enable_builtin_zstd=yes
12621263
enable_builtin_zlib=yes
12631264
use_embedded_3pp_alts=no
12641265
use_embedded_3pp_alts_val=0
12651266
;;
12661267
*)
12671268
enable_builtin_errno_id=no
1269+
enable_builtin_zstd=no
12681270
enable_builtin_zlib=no
12691271
use_embedded_3pp_alts=yes
12701272
use_embedded_3pp_alts_val=1
@@ -1273,6 +1275,7 @@ AS_HELP_STRING([--disable-use-embedded-3pp-alternatives],
12731275
],
12741276
[
12751277
enable_builtin_errno_id=yes
1278+
enable_builtin_zstd=yes
12761279
enable_builtin_zlib=no
12771280
use_embedded_3pp_alts=default
12781281
use_embedded_3pp_alts_val=-1
@@ -1308,6 +1311,59 @@ AS_IF([test $enable_builtin_errno_id = yes],
13081311
ERTS_USE_BUILTIN_ERRNO_ID=$enable_builtin_errno_id
13091312
AC_SUBST(ERTS_USE_BUILTIN_ERRNO_ID)
13101313

1314+
dnl -------------
1315+
dnl zstd
1316+
dnl -------------
1317+
1318+
AC_ARG_ENABLE(builtin-zstd,
1319+
AS_HELP_STRING([--enable-builtin-zstd],
1320+
[force use of our own built-in zstd])
1321+
AS_HELP_STRING([--disable-builtin-zstd],
1322+
[try to find a static libzstd on the system to use]),
1323+
[ case "$enableval" in
1324+
no) enable_builtin_zstd=no ;;
1325+
*) enable_builtin_zstd=yes ;;
1326+
esac ])
1327+
1328+
LIBZSTD=
1329+
1330+
AS_IF([test "$enable_builtin_zstd" = "no"],
1331+
[
1332+
AC_MSG_CHECKING(for static libzstd of version 1.5 or higher)
1333+
zstd_save_LIBS=$LIBS
1334+
LIBS="-l:libzstd.a $LIBS"
1335+
AC_LINK_IFELSE(
1336+
[AC_LANG_PROGRAM([[
1337+
#define ZSTD_STATIC_LINKING_ONLY
1338+
#include <zstd.h>
1339+
#include <zdict.h>
1340+
]],[[
1341+
#if ZSTD_VERSION_NUMBER >= 10500
1342+
(void) ZSTD_createCCtx_advanced(ZSTD_defaultCMem);
1343+
#else
1344+
#error "No libzstd of version 1.5 or higher found"
1345+
error
1346+
#endif
1347+
]])
1348+
],
1349+
[
1350+
LIBZSTD="-l:libzstd.a"
1351+
AC_MSG_RESULT(yes)
1352+
],[
1353+
AC_MSG_RESULT([no, using builtin zstd instead])
1354+
])
1355+
1356+
LIBS="$zstd_save_LIBS"
1357+
1358+
])
1359+
1360+
AC_SUBST(LIBZSTD)
1361+
1362+
AS_IF([test "$LIBZSTD" = ""],
1363+
[
1364+
AC_DEFINE(ERTS_USE_BUILTIN_ZSTD, [1], [Define if builtin zstd should be used])
1365+
])
1366+
13111367
dnl -------------
13121368
dnl zlib
13131369
dnl -------------

erts/emulator/Makefile.in

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ DTRACE_ENABLED=@DTRACE_ENABLED@
3232
DTRACE_ENABLED_2STEP=@DTRACE_ENABLED_2STEP@
3333
USE_VM_PROBES=@USE_VM_PROBES@
3434
LIBS = @LIBS@
35+
LIBZSTD=@LIBZSTD@
3536
Z_LIB=@Z_LIB@
3637
CROSS_COMPILING = @CROSS_COMPILING@
3738
NO_INLINE_FUNCTIONS=false
@@ -398,6 +399,10 @@ else
398399
LIBS += $(OPENSSL_LIB)
399400
endif
400401

402+
ifdef LIBZSTD
403+
# Use system libzstd
404+
LIBS += $(LIBZSTD)
405+
else
401406
ZSTD_DIR = $(ERL_TOP)/erts/emulator/zstd/obj/$(TARGET)/$(TYPE)
402407
ZSTD_LIB = $(ZSTD_DIR)/$(LIB_PREFIX)zstd$(LIB_SUFFIX)
403408
DEPLIBS += $(ZSTD_LIB)
@@ -408,6 +413,7 @@ else
408413
# Build on darwin fails if -l$(ZSTD_LIB) is used
409414
LIBS += $(ZSTD_LIB)
410415
endif
416+
endif
411417

412418
LIBSCTP = @LIBSCTP@
413419

@@ -842,7 +848,10 @@ COMMON_INCLUDES = -Ibeam -Isys/$(ERLANG_OSTYPE) -Isys/common -I$(TARGET)
842848
ifndef Z_LIB
843849
COMMON_INCLUDES += -Izlib
844850
endif
845-
COMMON_INCLUDES += -Ipcre -Iryu -Iopenssl/include -Izstd
851+
ifndef LIBZSTD
852+
COMMON_INCLUDES += -Izstd
853+
endif
854+
COMMON_INCLUDES += -Ipcre -Iryu -Iopenssl/include
846855
COMMON_INCLUDES += -I../include -I../include/$(TARGET)
847856
COMMON_INCLUDES += -I../include/internal -I../include/internal/$(TARGET)
848857

erts/emulator/beam/erl_bif_info.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3589,7 +3589,11 @@ BIF_RETTYPE system_info_1(BIF_ALIST_1)
35893589
xtra -= 2;
35903590

35913591
hp = erts_produce_heap(&hfact, 2, xtra);
3592+
#ifdef ERTS_USE_BUILTIN_ZSTD
35923593
included = CONS(hp, AM_zstd, included);
3594+
#else
3595+
excluded = CONS(hp, AM_zstd, excluded);
3596+
#endif
35933597
xtra -= 2;
35943598

35953599
hp = erts_produce_heap(&hfact, 2, xtra);

erts/emulator/nifs/common/zstd_nif.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@
2828
#define STATIC_ERLANG_NIF 1
2929

3030
#include "erl_nif.h"
31+
#include "config.h"
3132
#define ZSTD_STATIC_LINKING_ONLY
32-
#include "erl_zstd.h"
33-
#include "erl_zdict.h"
33+
34+
#ifdef ERTS_USE_BUILTIN_ZSTD
35+
# include "erl_zstd.h"
36+
# include "erl_zdict.h"
37+
#else
38+
# include <zstd.h>
39+
# include <zdict.h>
40+
#endif
3441

3542
static ErlNifResourceType *compress_type;
3643
static ErlNifResourceType *decompress_type;

0 commit comments

Comments
 (0)