Skip to content

Commit

Permalink
build: migrate build system from the premaek5 to CMake.
Browse files Browse the repository at this point in the history
Premake5 build is too complex for this project, it showed multiple
compile-time errors and link-time erros.

Replace the build system with CMake to avoid those errors, CMake is
officially supported by Emscripten toolchain, and it worked perfectly.
  • Loading branch information
yoshihitoh committed Jul 25, 2024
1 parent 2668f45 commit b254593
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
1 change: 1 addition & 0 deletions cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build-*/
cmake-build-*/
77 changes: 77 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
cmake_minimum_required(VERSION 3.13)
set(CMAKE_CXX_STANDARD 20)
project(zstd-codec)

OPTION(ZSTD_CODEC_BUILD_TEST "Build Tests" ON)

# Zstandard
set(ZSTD_BUILD_PROGRAMS OFF CACHE BOOL "disable programs")
set(ZSTD_BUILD_CONTRIB OFF CACHE BOOL "disable contrib")
set(ZSTD_BUILD_TESTS OFF CACHE BOOL "disable tests")
set(ZSTD_BUILD_SHARED OFF CACHE BOOL "disable shared library") # use static library only
add_subdirectory(zstd/build/cmake)

# zstd-codec (common)
add_library(zstd-codec STATIC
src/common-types.h
src/raii-resource.h
src/zstd-codec.cc
src/zstd-codec.h
src/zstd-dict.cc
src/zstd-dict.h
src/zstd-stream.cc
src/zstd-stream.h
)
add_dependencies(zstd-codec libzstd_static)
target_compile_definitions(zstd-codec PUBLIC -DZSTD_STATIC_LINKING_ONLY)
target_include_directories(zstd-codec PUBLIC
${CMAKE_SOURCE_DIR}/zstd/lib
src
)
target_link_libraries(zstd-codec PUBLIC libzstd_static)

# zstd-codec-binding/zstd-codec-binding-wasm (for Browser)
if (EMSCRIPTEN)
add_executable(zstd-codec-binding src/binding/emscripten/zstd-binding.cc)
target_link_libraries(zstd-codec-binding PRIVATE zstd-codec)
target_link_options(zstd-codec-binding PRIVATE
"SHELL: --bind"
"SHELL: --memory-init-file 0"
"SHELL: -s DEMANGLE_SUPPORT=1"
"SHELL: -s MODULARIZE=1"
"SHELL: -s NODEJS_CATCH_EXIT=0"
"SHELL: -s NODEJS_CATCH_REJECTION=0"
"SHELL: -s WASM=0"
)

add_executable(zstd-codec-binding-wasm src/binding/emscripten/zstd-binding.cc)
target_link_libraries(zstd-codec-binding-wasm PRIVATE zstd-codec)
target_link_options(zstd-codec-binding-wasm PRIVATE
"SHELL: --bind"
"SHELL: --memory-init-file 0"
"SHELL: -s DEMANGLE_SUPPORT=1"
"SHELL: -s MODULARIZE=1"
"SHELL: -s NODEJS_CATCH_EXIT=0"
"SHELL: -s NODEJS_CATCH_REJECTION=0"
"SHELL: -s WASM=1"
"SHELL: -s SINGLE_FILE=1"
"SHELL: -s BINARYEN_ASYNC_COMPILATION=1"
)
endif (EMSCRIPTEN)

if (ZSTD_CODEC_BUILD_TEST)
add_executable(test-zstd-codec
test/main.cc
test/catch.hpp
)
target_link_libraries(test-zstd-codec PUBLIC zstd-codec)
if (EMSCRIPTEN)
target_compile_definitions(test-zstd-codec PRIVATE
"-DHAS_FS_WRITE=0"
)
target_link_options(test-zstd-codec PRIVATE
"SHELL: --embed-file ${CMAKE_SOURCE_DIR}/test/fixtures@test/fixtures"
"SHELL: --memory-init-file 0"
)
endif (EMSCRIPTEN)
endif (ZSTD_CODEC_BUILD_TEST)
4 changes: 2 additions & 2 deletions cpp/build-emscripten-debug.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ CPP_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

cd ${CPP_DIR} && \
bash update_projects.sh && \
cd build-emscripten && \
emmake make -j$(sysctl -n hw.ncpu) config=debug
cd build-emscripten-debug && \
emmake make -j$(nproc) config=debug


# cd "${PROJ_DIR}"
Expand Down
4 changes: 2 additions & 2 deletions cpp/build-emscripten-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ CPP_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

cd ${CPP_DIR} && \
bash update_projects.sh && \
cd build-emscripten && \
emmake make -j$(sysctl -n hw.ncpu) config=release verbose=1
cd build-emscripten-release && \
NODE_OPTIONS="--max-old-space-size=8192" emmake make -j$(nproc) config=release verbose=1
14 changes: 14 additions & 0 deletions cpp/src/dummy.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <cstdio>
#include <emscripten.h>

#include "zstd.h"

EMSCRIPTEN_KEEPALIVE
extern "C" void say_hello() {
printf("Hello, World!\n");
}

EMSCRIPTEN_KEEPALIVE
extern "C" unsigned int zstd_version() {
return ZSTD_versionNumber();
}
1 change: 1 addition & 0 deletions cpp/src/zstd-codec.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <cstdio>
#include <climits>
#include <functional>

#include "zstd.h"
Expand Down
1 change: 1 addition & 0 deletions cpp/src/zstd-stream.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <algorithm>
#include "zstd-dict.h"
#include "zstd-stream.h"
#include "zstd.h"

//
// ZstdCompressStream
Expand Down
1 change: 1 addition & 0 deletions cpp/src/zstd-stream.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <functional>
#include <memory>

#include "common-types.h"
#include "zstd.h"
Expand Down
29 changes: 22 additions & 7 deletions cpp/test/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@
#include "zstd-stream.h"

#define CATCH_CONFIG_MAIN
#define CATCH_CONFIG_NO_POSIX_SIGNALS
#include "catch.hpp"

#ifndef HAS_FS_WRITE
#define HAS_FS_WRITE (1)
#endif // HAS_FS_WRITE

#define UNUSED(x) (void)x


class FileResource : public Resource<FILE>
{
Expand Down Expand Up @@ -51,6 +58,19 @@ static Vec<u8> loadFixture(const char* name)
}


static void writeTo(const std::string& path, const Vec<u8>& src)
{
#if HAS_FS_WRITE
FileResource dest_file(path, "wb");
fwrite(&src[0], src.size(), 1, dest_file.get());
dest_file.Close();
#else
UNUSED(path);
UNUSED(src);
#endif
}


TEST_CASE("Zstd-Dictionary-Interfaces", "[zstd][compress][decompress][dictionary]")
{
const auto dict_bytes = loadFixture("sample-dict");
Expand Down Expand Up @@ -172,15 +192,12 @@ TEST_CASE("ZstdCompressStream", "[zstd][compress][stream]")
REQUIRE(codec.Decompress(decompressed_bytes, result_bytes) == content_bytes.size());
REQUIRE(decompressed_bytes == content_bytes);

FileResource result_file(tempPath("dance_yorokobi_mai_man.bmp.zst"), "wb");
fwrite(&result_bytes[0], result_bytes.size(), 1, result_file.get());
result_file.Close();
writeTo(tempPath("dance_yorokobi_mai_man.bmp.zst"), result_bytes);
}


TEST_CASE("ZstdDecompressStream", "[zstd][decompress][stream]")
{

const auto block_size = 1024;
size_t read_size;
Vec<u8> read_buff(block_size);
Expand Down Expand Up @@ -222,7 +239,5 @@ TEST_CASE("ZstdDecompressStream", "[zstd][decompress][stream]")
REQUIRE(codec.Decompress(content_bytes, compressed_bytes) == content_size);
REQUIRE(content_bytes == result_bytes);

FileResource result_file(tempPath("dance_yorokobi_mai_woman.bmp"), "wb");
fwrite(&result_bytes[0], result_bytes.size(), 1, result_file.get());
result_file.Close();
writeTo(tempPath("dance_yorokobi_mai_woman.bmp"), result_bytes);
}
7 changes: 4 additions & 3 deletions cpp/update_projects.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if [ -z "${ZSTD_DIR}" ]; then
fi

echo '------------------------------------------------------------'
premake5 gmake2 --with-zstd-dir=${ZSTD_DIR}
echo '------------------------------------------------------------'
premake5 gmake2 --with-zstd-dir=${ZSTD_DIR} --with-emscripten
cmake -Bbuild-gnumake-debug -DCMAKE_BUILD_TYPE=Debug
emcmake cmake -Bbuild-emscripten-debug -DCMAKE_BUILD_TYPE=Debug
cmake -Bbuild-gnumake-release -DCMAKE_BUILD_TYPE=Release
emcmake cmake -Bbuild-emscripten-release -DCMAKE_BUILD_TYPE=Release
7 changes: 4 additions & 3 deletions update-zstd-binding.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CONTAINER_NAME="zstd-emscripten"
IMAGE_NAME="yoshihitoh/zstd-emscripten"

CONTAINER_ID="$(docker container ls -qa -f name=${CONTAINER_NAME})"
BUILD_TARGET="release"

# move to root directory
cd "${ROOT_DIR}"
Expand All @@ -41,16 +42,16 @@ docker container run \
--name "${CONTAINER_NAME}" \
-v "${CPP_DIR}:/emscripten/src" \
"${IMAGE_NAME}" \
/bin/bash --login /emscripten/src/build-emscripten-release.sh
/bin/bash --login "/emscripten/src/build-emscripten-${BUILD_TARGET}.sh"

# copy compiled binindg into js dir
echo "copying compiled binding into js/lib..."
docker container cp \
${CONTAINER_NAME}:/emscripten/src/build-emscripten/bin/Release/zstd-codec-binding.js \
"${CONTAINER_NAME}:/emscripten/src/build-emscripten-${BUILD_TARGET}/zstd-codec-binding.js" \
"${JS_DIR}/lib"

docker container cp \
${CONTAINER_NAME}:/emscripten/src/build-emscripten/bin/Release/zstd-codec-binding-wasm.js \
"${CONTAINER_NAME}:/emscripten/src/build-emscripten-${BUILD_TARGET}/zstd-codec-binding-wasm.js" \
"${JS_DIR}/lib"

echo "done!"

0 comments on commit b254593

Please sign in to comment.