Skip to content

fix build for MacOs M1 laptops and other fixes #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
22 changes: 14 additions & 8 deletions c_src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ BASEDIR := $(abspath $(CURDIR)/..)

PROJECT ?= zstd_nif

ERTS_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s/erts-~s/include/\", [code:root_dir(), erlang:system_info(version)]).")
ERL_INTERFACE_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, include)]).")
ERL_INTERFACE_LIB_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, lib)]).")
ERTS_INCLUDE_DIR ?= $(shell erl -noshell -eval "io:format(\"~s/erts-~s/include/\", [code:root_dir(), erlang:system_info(version)])." -s erlang halt)
ERL_INTERFACE_INCLUDE_DIR ?= $(shell erl -noshell -eval "io:format(\"~s\", [code:lib_dir(erl_interface, include)])." -s erlang halt)
ERL_INTERFACE_LIB_DIR ?= $(shell erl -noshell -eval "io:format(\"~s\", [code:lib_dir(erl_interface, lib)])." -s erlang halt)

C_SRC_DIR = $(CURDIR)
C_SRC_OUTPUT ?= $(CURDIR)/../priv/$(PROJECT).so
Expand All @@ -16,10 +16,16 @@ C_SRC_OUTPUT ?= $(CURDIR)/../priv/$(PROJECT).so

UNAME_SYS := $(shell uname -s)
ifeq ($(UNAME_SYS), Darwin)
CC ?= cc
CFLAGS ?= -O3 -std=c99 -arch x86_64 -Wall -Wmissing-prototypes
CXXFLAGS ?= -O3 -arch x86_64 -finline-functions -Wall
LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress
HOMEBREW_PREFIX ?= $(shell brew --prefix)

CC ?= cc
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
CFLAGS += -I $(HOMEBREW_PREFIX)/include -I $(HOMEBREW_PREFIX)/opt/openssl/include
LDFLAGS := $(LDFLAGS) -L $(HOMEBREW_PREFIX)/lib -L $(HOMEBREW_PREFIX)/opt/openssl/lib \
-mmacosx-version-min=$(shell sw_vers -productVersion) \
-Wl,-syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk \
-flat_namespace -undefined suppress
LDLIBS += -L $(HOMEBREW_PREFIX)/opt/openssl/lib
else ifeq ($(UNAME_SYS), FreeBSD)
CC ?= cc
CFLAGS ?= -O3 -std=c99 -Wall -Wmissing-prototypes
Expand All @@ -33,7 +39,7 @@ endif
CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) -I ../priv/zstd/lib
CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)

LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei
LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lei
LDFLAGS += -shared $(CURDIR)/../priv/zstd/lib/libzstd.a

# Verbosity.
Expand Down
15 changes: 10 additions & 5 deletions c_src/zstd_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,24 @@ static ERL_NIF_TERM zstd_nif_decompress(ErlNifEnv* env, int argc, const ERL_NIF_
if(!enif_inspect_binary(env, argv[0], &bin))
return enif_make_badarg(env);

uncompressed_size = ZSTD_getDecompressedSize(bin.data, bin.size);
uncompressed_size = ZSTD_getFrameContentSize(bin.data, bin.size);

if (uncompressed_size==ZSTD_CONTENTSIZE_ERROR) {
return enif_make_badarg(env);
} else if (uncompressed_size==ZSTD_CONTENTSIZE_UNKNOWN) {
uncompressed_size = 10000000;
}

outp = enif_make_new_binary(env, uncompressed_size, &out);

if(ZSTD_decompress(outp, uncompressed_size, bin.data, bin.size) != uncompressed_size)
return enif_make_atom(env, "error");
ZSTD_decompress(outp, uncompressed_size, bin.data, bin.size);

return out;
}

static ErlNifFunc nif_funcs[] = {
{"compress", 2, zstd_nif_compress},
{"decompress", 1, zstd_nif_decompress}
{"compress", 2, zstd_nif_compress, ERL_NIF_DIRTY_JOB_CPU_BOUND},
{"decompress", 1, zstd_nif_decompress, ERL_NIF_DIRTY_JOB_CPU_BOUND}
};

ERL_NIF_INIT(zstd, nif_funcs, NULL, NULL, NULL, NULL);