Current section
16 Versions
Jump to
Current section
16 Versions
Compare versions
6
files changed
+88
additions
-62
deletions
| @@ -1,79 +1,87 @@ | |
| 1 1 | #!/usr/bin/env bash |
| 2 | + set -euo pipefail |
| 2 3 | |
| 3 | - ROOT=$(pwd) |
| 4 | - DEPS_LOCATION=_build/deps |
| 5 | - OS=$(uname -s) |
| 4 | + ROOT="$(pwd)" |
| 5 | + DEPS_DIR="_build/deps" |
| 6 | + OS="$(uname -s)" |
| 6 7 | KERNEL=$(echo $(lsb_release -ds 2>/dev/null || cat /etc/*release 2>/dev/null | head -n1 | awk '{print $1;}') | awk '{print $1;}') |
| 7 | - CPUS=`getconf _NPROCESSORS_ONLN 2>/dev/null || sysctl -n hw.ncpu` |
| 8 | + CPUS="$(getconf _NPROCESSORS_ONLN 2>/dev/null || sysctl -n hw.ncpu)" |
| 8 9 | |
| 10 | + # Zstandard configuration |
| 9 11 | # https://github.com/facebook/zstd.git |
| 10 12 | |
| 11 | - ZSTD_DESTINATION=zstd |
| 12 | - ZSTD_REPO=https://github.com/facebook/zstd.git |
| 13 | - ZSTD_BRANCH=release |
| 14 | - ZSTD_TAG=v1.5.7 |
| 15 | - ZSTD_SUCCESS=lib/libzstd.a |
| 13 | + ZSTD_REPO="https://github.com/facebook/zstd.git" |
| 14 | + ZSTD_BRANCH="release" |
| 15 | + ZSTD_TAG="v1.5.7" |
| 16 | + ZSTD_DIR="zstd" |
| 17 | + ZSTD_SUCCESS_FILE="lib/libzstd.a" |
| 16 18 | |
| 17 | - fail_check() |
| 18 | - { |
| 19 | + fail_check() { |
| 19 20 | "$@" |
| 20 21 | local status=$? |
| 21 22 | if [ $status -ne 0 ]; then |
| 22 | - echo "error with $1" >&2 |
| 23 | - exit 1 |
| 23 | + echo "❌ Error running command: $*" >&2 |
| 24 | + exit $status |
| 24 25 | fi |
| 25 26 | } |
| 26 27 | |
| 27 | - CheckoutLib() |
| 28 | - { |
| 29 | - if [ -f "$DEPS_LOCATION/$4/$5" ]; then |
| 30 | - echo "$4 fork already exist. delete $DEPS_LOCATION/$4 for a fresh checkout ..." |
| 31 | - else |
| 32 | - #repo rev branch destination |
| 28 | + checkout_lib() { |
| 29 | + local repo_url="$1" |
| 30 | + local tag="$2" |
| 31 | + local branch="$3" |
| 32 | + local dir_name="$4" |
| 33 | + local success_file="$5" |
| 33 34 | |
| 34 | - echo "repo=$1 tag=$2 branch=$3" |
| 35 | - |
| 36 | - mkdir -p $DEPS_LOCATION |
| 37 | - pushd $DEPS_LOCATION |
| 38 | - |
| 39 | - if [ ! -d "$4" ]; then |
| 40 | - fail_check git clone -b $3 $1 $4 |
| 35 | + local full_path="$DEPS_DIR/$dir_name/$success_file" |
| 36 | + if [ -f "$full_path" ]; then |
| 37 | + echo "✅ $dir_name already exists at $full_path" |
| 38 | + echo " To rebuild, delete: $DEPS_DIR/$dir_name" |
| 39 | + return |
| 41 40 | fi |
| 42 41 | |
| 43 | - pushd $4 |
| 44 | - fail_check git checkout $2 |
| 45 | - BuildLibrary $4 |
| 46 | - popd |
| 47 | - popd |
| 42 | + echo "📦 Cloning $repo_url (branch: $branch, tag: $tag)" |
| 48 43 | |
| 44 | + mkdir -p "$DEPS_DIR" |
| 45 | + pushd "$DEPS_DIR" > /dev/null |
| 49 46 | |
| 47 | + if [ ! -d "$dir_name" ]; then |
| 48 | + fail_check git clone --branch "$branch" "$repo_url" "$dir_name" |
| 50 49 | fi |
| 50 | + |
| 51 | + pushd "$dir_name" > /dev/null |
| 52 | + fail_check git checkout "$tag" |
| 53 | + build_library "$dir_name" |
| 54 | + popd > /dev/null |
| 55 | + popd > /dev/null |
| 51 56 | } |
| 52 57 | |
| 53 | - BuildLibrary() |
| 54 | - { |
| 55 | - if [ -z "$NO_CMAKE" ] && command -v cmake >/dev/null 2>&1; then |
| 56 | - echo "Using cmake build system..." |
| 57 | - cmake -S build/cmake |
| 58 | - else |
| 59 | - echo "cmake not found, using make directly..." |
| 60 | - # Only define -O2 if CFLAGS is not already defined |
| 61 | - export CFLAGS=${CFLAGS:-"-O2"} |
| 58 | + build_library() { |
| 59 | + local dir="$1" |
| 60 | + echo "🔧 Building $dir" |
| 62 61 | |
| 63 | - case $OS in |
| 64 | - Linux) |
| 62 | + if [[ -z "${NO_CMAKE:-}" ]] && command -v cmake >/dev/null 2>&1; then |
| 63 | + echo " ➤ Using CMake..." |
| 64 | + cmake -S build/cmake -DZSTD_BUILD_PROGRAMS=OFF -DZSTD_LEGACY_SUPPORT=OFF |
| 65 | + fail_check make libzstd_static -j "$CPUS" |
| 66 | + else |
| 67 | + echo " ➤ Using Make directly..." |
| 68 | + export CFLAGS="${CFLAGS:--O2}" |
| 69 | + |
| 70 | + if [[ "$OS" == "Linux" ]]; then |
| 65 71 | export CFLAGS="$CFLAGS -fPIC" |
| 66 | - export CXXFLAGS="$CXXFLAGS -fPIC" |
| 67 | - ;; |
| 68 | - *) |
| 69 | - ;; |
| 70 | - esac |
| 72 | + export CXXFLAGS="${CXXFLAGS:-} -fPIC" |
| 71 73 | fi |
| 72 74 | |
| 73 | - fail_check make -j $CPUS |
| 74 | - rm -rf lib/*.so |
| 75 | - rm -rf lib/*.so.* |
| 76 | - rm -rf lib/*.dylib |
| 75 | + fail_check make lib-release -j "$CPUS" |
| 76 | + |
| 77 | + # Remove shared libs to ensure a static-only build |
| 78 | + rm -f lib/*.so lib/*.so.* lib/*.dylib |
| 79 | + fi |
| 77 80 | } |
| 78 81 | |
| 79 | - CheckoutLib $ZSTD_REPO $ZSTD_TAG $ZSTD_BRANCH $ZSTD_DESTINATION $ZSTD_SUCCESS |
| 82 | + echo "🖥️ Detected system configuration:" |
| 83 | + echo " ➤ OS Type : $OS" |
| 84 | + echo " ➤ OS Name : $KERNEL" |
| 85 | + echo " ➤ CPU Cores : $CPUS" |
| 86 | + |
| 87 | + checkout_lib "$ZSTD_REPO" "$ZSTD_TAG" "$ZSTD_BRANCH" "$ZSTD_DIR" "$ZSTD_SUCCESS_FILE" |
| @@ -21,7 +21,8 @@ ifeq ($(UNAME_SYS), darwin) | |
| 21 21 | -Wl,-U,_enif_get_int \ |
| 22 22 | -Wl,-U,_enif_get_ulong \ |
| 23 23 | -Wl,-U,_enif_is_identical \ |
| 24 | - -Wl,-U,_enif_keep_resource |
| 24 | + -Wl,-U,_enif_keep_resource \ |
| 25 | + -Wl,-U,_enif_make_sub_binary |
| 25 26 | endif |
| 26 27 | |
| 27 28 | CXXFLAGS += -DNDEBUG \ |
| @@ -647,17 +647,28 @@ static ERL_NIF_TERM zstd_nif_decompress(ErlNifEnv* env, int argc, const ERL_NIF_ | |
| 647 647 | |
| 648 648 | uint64_t uncompressed_size = ZSTD_getFrameContentSize(bin.data, bin.size); |
| 649 649 | |
| 650 | - if (uncompressed_size == ZSTD_CONTENTSIZE_UNKNOWN) |
| 651 | - return make_error(env, "failed to decompress: ZSTD_CONTENTSIZE_UNKNOWN"); |
| 652 | - |
| 653 650 | if (uncompressed_size == ZSTD_CONTENTSIZE_ERROR) |
| 654 | - return make_error(env, "failed to decompress: ZSTD_CONTENTSIZE_ERROR"); |
| 651 | + return make_error(env, "invalid ZSTD frame"); |
| 652 | + else if (uncompressed_size == ZSTD_CONTENTSIZE_UNKNOWN) |
| 653 | + uncompressed_size = ZSTD_decompressBound(bin.data, bin.size); |
| 654 | + |
| 655 | + if (uncompressed_size == 0 || uncompressed_size > MAX_BUFFER_SIZE) |
| 656 | + return make_error(env, "frame too large or invalid"); |
| 655 657 | |
| 656 658 | ERL_NIF_TERM out_term; |
| 657 | - uint8_t *destination_buffer = enif_make_new_binary(env, uncompressed_size, &out_term); |
| 658 659 | |
| 659 | - if (ZSTD_decompress(destination_buffer, uncompressed_size, bin.data, bin.size) != uncompressed_size) |
| 660 | - return make_error(env, "failed to decompress"); |
| 660 | + uint8_t* destination_buffer = enif_make_new_binary(env, uncompressed_size, &out_term); |
| 661 | + if (!destination_buffer) |
| 662 | + return make_error(env, "memory allocation failed"); |
| 663 | + |
| 664 | + size_t dsize = ZSTD_decompress(destination_buffer, uncompressed_size, bin.data, bin.size); |
| 665 | + |
| 666 | + if (ZSTD_isError(dsize)) |
| 667 | + return make_error(env, ZSTD_getErrorName(dsize)); |
| 668 | + |
| 669 | + // shrink the binary if actual size < allocated |
| 670 | + if (dsize < uncompressed_size) |
| 671 | + out_term = enif_make_sub_binary(env, out_term, 0, dsize); |
| 661 672 | |
| 662 673 | return out_term; |
| 663 674 | } |
| @@ -13,4 +13,4 @@ | |
| 13 13 | {<<"links">>,[{<<"GitHub">>,<<"https://github.com/silviucpp/ezstd">>}]}. |
| 14 14 | {<<"name">>,<<"ezstd">>}. |
| 15 15 | {<<"requirements">>,[]}. |
| 16 | - {<<"version">>,<<"1.2.2">>}. |
| 16 | + {<<"version">>,<<"1.2.3">>}. |
| @@ -2,7 +2,7 @@ | |
| 2 2 | [{description,"zstd binding for erlang"}, |
| 3 3 | {licenses,["MIT"]}, |
| 4 4 | {links,[{"GitHub","https://github.com/silviucpp/ezstd"}]}, |
| 5 | - {vsn,"1.2.2"}, |
| 5 | + {vsn,"1.2.3"}, |
| 6 6 | {registered,[]}, |
| 7 7 | {applications,[kernel,stdlib]}, |
| 8 8 | {modules,[]}, |
Loading more files…