Skip to content

Commit

Permalink
Replace XZ with ZSTD
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Jan 22, 2025
1 parent a47d436 commit ea30d4c
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 98 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
path = external/qmqtt
url = https://github.com/emqx/qmqtt.git
ignore = dirty
[submodule "external/xz"]
path = external/xz
url = https://github.com/tukaani-project/xz.git
[submodule "external/mdns"]
path = external/mdns
url = https://github.com/mjansson/mdns.git
Expand All @@ -34,3 +31,6 @@
[submodule "external/linalg"]
path = external/linalg
url = https://github.com/sgorsten/linalg.git
[submodule "external/zstd"]
path = external/zstd
url = https://github.com/facebook/zstd.git
28 changes: 14 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ SET ( DEFAULT_BONJOUR ON )
SET ( DEFAULT_MQTT ON )
SET ( DEFAULT_STATIC_QT_PLUGINS OFF )
SET ( DEFAULT_PRECOMPILED_HEADERS ON )
SET ( DEFAULT_XZ ON )
SET ( DEFAULT_ZSTD ON )
SET ( DEFAULT_POWER_MANAGEMENT ON )
SET ( DEFAULT_SYSTRAY ON )
SET ( DEFAULT_SHARED_LIBS ON )
Expand Down Expand Up @@ -504,8 +504,8 @@ colorMe("ENABLE_PROTOBUF = " ${ENABLE_PROTOBUF})
option(ENABLE_SYSTRAY "Enable SYSTRAY" ${DEFAULT_SYSTRAY})
colorMe("ENABLE_SYSTRAY = " ${ENABLE_SYSTRAY})

option(ENABLE_XZ "Enable XZ support" ${DEFAULT_XZ})
colorMe("ENABLE_XZ = " ${ENABLE_XZ})
option(ENABLE_ZSTD "Enable ZSTD support" ${DEFAULT_ZSTD})
colorMe("ENABLE_ZSTD = " ${ENABLE_ZSTD})

message( STATUS "\n${CyanColor}BUILD FEATURES${ColorReset}")

Expand Down Expand Up @@ -547,8 +547,8 @@ SET ( FLATBUFFERS_INSTALL_LIB_DIR ${CMAKE_BINARY_DIR}/flatbuf )

find_package(GitVersion)

if(ENABLE_XZ)
find_package(LibLZMA)
if(ENABLE_ZSTD)
find_package(zstd)
endif()

# configure a header file to pass some of the CMake settings
Expand Down Expand Up @@ -694,13 +694,6 @@ if (WIN32)
endif()
endif()

# Add the source/lib directories
add_subdirectory(external)
add_subdirectory(sources)

# Add resources directory
add_subdirectory(resources)

# remove generated files on make cleaan too
LIST( APPEND GENERATED_QRC
${CMAKE_BINARY_DIR}/WebConfig.qrc
Expand All @@ -712,12 +705,19 @@ set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${GENERATED_QRC
configure_file( "${OUR_CMAKE_MODULES}/cmake_uninstall.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" IMMEDIATE @ONLY)
add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)

# Add the source/lib directories
add_subdirectory(external)
add_subdirectory(sources)

# Add resources directory
add_subdirectory(resources)

# enable make package - no code after this line !
include (${CMAKE_CURRENT_SOURCE_DIR}/cmake/packages.cmake)

# external targets
if (WIN32 AND TARGET stb AND TARGET flatbuffers AND TARGET protobuf-nanopb AND TARGET lunasvg AND TARGET flatc AND TARGET qmqtt AND TARGET liblzma AND TARGET sqlite3 AND TARGET precompiled_hyperhdr_headers)
set_target_properties(stb qmqtt flatbuffers protobuf-nanopb lunasvg flatc resources uninstall liblzma sqlite3 precompiled_hyperhdr_headers PROPERTIES FOLDER ExternalLibsTargets)
if (WIN32 AND TARGET stb AND TARGET flatbuffers AND TARGET protobuf-nanopb AND TARGET lunasvg AND TARGET flatc AND TARGET qmqtt AND TARGET libzstd_static AND TARGET libzstd_shared AND TARGET zstd AND TARGET clean-all AND TARGET sqlite3 AND TARGET precompiled_hyperhdr_headers)
set_target_properties(stb qmqtt flatbuffers protobuf-nanopb lunasvg flatc resources uninstall libzstd_static libzstd_shared zstd clean-all sqlite3 precompiled_hyperhdr_headers PROPERTIES FOLDER ExternalLibsTargets)
else()
set_target_properties(resources uninstall PROPERTIES FOLDER ExternalLibsTargets)
endif()
Expand Down
4 changes: 2 additions & 2 deletions HyperhdrConfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
// Define to enable system qmqtt
#cmakedefine USE_SYSTEM_MQTT_LIBS

// Define to enable protobuf
#cmakedefine ENABLE_XZ
// Define to enable zstd
#cmakedefine ENABLE_ZSTD

// Define to enable protobuf
#cmakedefine ENABLE_PROTOBUF
Expand Down
29 changes: 29 additions & 0 deletions cmake/cmake_modules/Findzstd.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# - Find zstd
# Find the zstd compression library and includes
#
# ZSTD_INCLUDE_DIRS - where to find zstd.h, etc.
# ZSTD_LIBRARIES - List of libraries when using zstd.
# ZSTD_FOUND - True if zstd found.

find_path(ZSTD_INCLUDE_DIRS
NAMES zstd.h
HINTS ${zstd_ROOT_DIR}/include)

find_library(ZSTD_LIBRARIES
NAMES zstd
HINTS ${zstd_ROOT_DIR}/lib)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(zstd DEFAULT_MSG ZSTD_LIBRARIES ZSTD_INCLUDE_DIRS)

mark_as_advanced(
ZSTD_LIBRARIES
ZSTD_INCLUDE_DIRS)

if(ZSTD_FOUND AND NOT (TARGET zstd::zstd))
add_library (zstd::zstd UNKNOWN IMPORTED)
set_target_properties(zstd::zstd
PROPERTIES
IMPORTED_LOCATION ${ZSTD_LIBRARIES}
INTERFACE_INCLUDE_DIRECTORIES ${ZSTD_INCLUDE_DIRS})
endif()
20 changes: 10 additions & 10 deletions cmake/installers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ macro(DeployApple TARGET)
install(CODE [[ file(INSTALL FILES $<TARGET_FILE:sqlite3> DESTINATION "${CMAKE_INSTALL_PREFIX}/hyperhdr.app/Contents/lib" TYPE SHARED_LIBRARY) ]] COMPONENT "HyperHDR")
endif()

# Copy utils-xz
if (USE_SHARED_LIBS AND TARGET utils-xz)
install(CODE [[ file(INSTALL FILES $<TARGET_FILE:utils-xz> DESTINATION "${CMAKE_INSTALL_PREFIX}/hyperhdr.app/Contents/lib" TYPE SHARED_LIBRARY) ]] COMPONENT "HyperHDR")
# Copy utils-zstd
if (USE_SHARED_LIBS AND TARGET utils-zstd)
install(CODE [[ file(INSTALL FILES $<TARGET_FILE:utils-zstd> DESTINATION "${CMAKE_INSTALL_PREFIX}/hyperhdr.app/Contents/lib" TYPE SHARED_LIBRARY) ]] COMPONENT "HyperHDR")
endif()

# Copy utils-image
Expand Down Expand Up @@ -243,9 +243,9 @@ macro(DeployUnix TARGET)
install(CODE [[ file(INSTALL FILES $<TARGET_FILE:utils-image> DESTINATION "${CMAKE_INSTALL_PREFIX}/share/hyperhdr/lib" TYPE SHARED_LIBRARY) ]] COMPONENT "HyperHDR")
endif()

# Copy UTILS-XZ lib
if (TARGET utils-image)
install(CODE [[ file(INSTALL FILES $<TARGET_FILE:utils-xz> DESTINATION "${CMAKE_INSTALL_PREFIX}/share/hyperhdr/lib" TYPE SHARED_LIBRARY) ]] COMPONENT "HyperHDR")
# Copy UTILS-ZSTD lib
if (TARGET utils-zstd)
install(CODE [[ file(INSTALL FILES $<TARGET_FILE:utils-zstd> DESTINATION "${CMAKE_INSTALL_PREFIX}/share/hyperhdr/lib" TYPE SHARED_LIBRARY) ]] COMPONENT "HyperHDR")
endif()


Expand Down Expand Up @@ -354,7 +354,7 @@ macro(DeployUnix TARGET)
"librt"
"libstdc++"
"libudev"
"libz"
"libz.so"
"libxrender1"
"libxi6"
"libxext6"
Expand Down Expand Up @@ -593,9 +593,9 @@ macro(DeployWindows TARGET)
install(CODE [[ file(INSTALL FILES $<TARGET_FILE:sqlite3> DESTINATION "${CMAKE_INSTALL_PREFIX}/bin" TYPE SHARED_LIBRARY) ]] COMPONENT "HyperHDR")
endif()

# Copy utils-xz
if (USE_SHARED_LIBS AND TARGET utils-xz)
install(CODE [[ file(INSTALL FILES $<TARGET_FILE:utils-xz> DESTINATION "${CMAKE_INSTALL_PREFIX}/bin" TYPE SHARED_LIBRARY) ]] COMPONENT "HyperHDR")
# Copy utils-zstd
if (USE_SHARED_LIBS AND TARGET utils-zstd)
install(CODE [[ file(INSTALL FILES $<TARGET_FILE:utils-zstd> DESTINATION "${CMAKE_INSTALL_PREFIX}/bin" TYPE SHARED_LIBRARY) ]] COMPONENT "HyperHDR")
endif()

# Copy utils-image
Expand Down
22 changes: 13 additions & 9 deletions external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,21 @@ if ( ENABLE_MQTT )
endif()

#=============================================================================
# XZ
# ZSTD
#=============================================================================

if ( ENABLE_XZ )
if (NOT LIBLZMA_FOUND)
message( STATUS "System library xz could not be found. Using embedded xz library.")
option(BUILD_TESTING "" OFF)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/xz)
set_target_properties(liblzma PROPERTIES INTERFACE_INCLUDE_DIRECTORIES $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/external/xz/src/liblzma/api>)
set_property(TARGET liblzma PROPERTY POSITION_INDEPENDENT_CODE ON)
add_library(LibLZMA::LibLZMA ALIAS liblzma)
if ( ENABLE_ZSTD )
if (NOT zstd_FOUND)
message( STATUS "System library zstd could not be found. Using embedded zstd library.")
if(WIN32)
set(CMAKE_RC_FLAGS "${CMAKE_RC_FLAGS} -I${CMAKE_SOURCE_DIR}/external/zstd/lib")
endif()
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/zstd/build/cmake)
option(ZSTD_BUILD_TESTS "" OFF)
option(ZSTD_LEGACY_SUPPORT "" OFF)
set_target_properties(libzstd PROPERTIES INTERFACE_INCLUDE_DIRECTORIES $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/external/zstd/lib>)
set_property(TARGET libzstd PROPERTY POSITION_INDEPENDENT_CODE ON)
add_library(zstd::zstd ALIAS libzstd)
endif()
ENDIF()

Expand Down
1 change: 0 additions & 1 deletion external/xz
Submodule xz deleted from 238b4e
1 change: 1 addition & 0 deletions external/zstd
Submodule zstd added at 794ea1
10 changes: 0 additions & 10 deletions include/utils-xz/utils-xz.h

This file was deleted.

10 changes: 10 additions & 0 deletions include/utils-zstd/utils-zstd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#ifndef _ZSTD_SHARED_API
#define _ZSTD_SHARED_API
#endif

#include <image/MemoryBuffer.h>

_ZSTD_SHARED_API const char* DecompressZSTD(size_t downloadedDataSize, const uint8_t* downloadedData, const char* fileName);
_ZSTD_SHARED_API const char* DecompressZSTD(size_t downloadedDataSize, const uint8_t* downloadedData, uint8_t* dest, int destSeek, int destSize);
4 changes: 2 additions & 2 deletions sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ add_subdirectory(utils)
add_subdirectory(utils-image)
add_subdirectory(webserver)

if(ENABLE_XZ)
add_subdirectory(utils-xz)
if(ENABLE_ZSTD)
add_subdirectory(utils-zstd)
endif()


Expand Down
8 changes: 2 additions & 6 deletions sources/hyperhdr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ else()
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endif()

if(ENABLE_XZ)
if (LIBLZMA_FOUND)
target_link_libraries(hyperhdr LibLZMA::LibLZMA utils-xz)
else()
target_link_libraries(hyperhdr liblzma utils-xz)
endif()
if(ENABLE_ZSTD)
target_link_libraries(hyperhdr zstd::zstd utils-zstd)
endif()

if (USE_STATIC_QT_PLUGINS)
Expand Down
34 changes: 0 additions & 34 deletions sources/utils-xz/CMakeLists.txt

This file was deleted.

33 changes: 33 additions & 0 deletions sources/utils-zstd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Define the current source locations

SET(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/utils-zstd)
SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/sources/utils-zstd)

FILE ( GLOB_RECURSE utils_zstd_SOURCES "${CURRENT_HEADER_DIR}/*.h" "${CURRENT_SOURCE_DIR}/*.cpp" )

if (USE_SHARED_LIBS)
add_library(utils-zstd SHARED ${utils_zstd_SOURCES})
if(WIN32)
target_compile_definitions(utils-zstd
INTERFACE
"_ZSTD_SHARED_API=__declspec(dllimport)"
PRIVATE
"_ZSTD_SHARED_API=__declspec(dllexport)"
)
else()
target_compile_definitions(utils-zstd
INTERFACE
"_ZSTD_SHARED_API=__attribute__((visibility(\"default\")))"
)
endif()
install(
TARGETS utils-zstd
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)
else()
add_library(utils-zstd STATIC ${utils_zstd_SOURCES})
endif()

target_link_libraries(utils-zstd PRIVATE zstd::zstd)
target_include_directories(utils-zstd PRIVATE zstd::zstd)
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <lzma.h>
#include <cstdint>
#include <zstd.h>
#include <cstring>
#include <utils-xz/utils-xz.h>
#include <utils-zstd/utils-zstd.h>

_XZ_SHARED_API const char* DecompressXZ(size_t downloadedDataSize, const uint8_t* downloadedData, const char* fileName)
_ZSTD_SHARED_API const char* DecompressZSTD(size_t downloadedDataSize, const uint8_t* downloadedData, const char* fileName)
{
size_t outSize = 67174456;
std::vector<uint8_t> outBuffer;
const char* error = nullptr;

/*
std::ofstream file;
file.open(fileName, std::ios::out | std::ios::trunc | std::ios::binary);
Expand Down Expand Up @@ -81,18 +82,19 @@ _XZ_SHARED_API const char* DecompressXZ(size_t downloadedDataSize, const uint8_t
if (error != nullptr)
std::remove(fileName);

*/
return error;
}

_XZ_SHARED_API const char* DecompressXZ(size_t downloadedDataSize, const uint8_t* downloadedData, uint8_t* dest, int destSeek, int destSize)
_ZSTD_SHARED_API const char* DecompresZSTD(size_t downloadedDataSize, const uint8_t* downloadedData, uint8_t* dest, int destSeek, int destSize)
{
size_t outSize = 16842808/2;
std::vector<uint8_t> outBuffer;
const char* error = nullptr;
int currentPos = 0;
int writePos = 0;

/*
try
{
outBuffer.resize(outSize);
Expand Down Expand Up @@ -177,6 +179,6 @@ _XZ_SHARED_API const char* DecompressXZ(size_t downloadedDataSize, const uint8_t
{
error = "Incorrect final LUT size";
}

*/
return error;
}

0 comments on commit ea30d4c

Please sign in to comment.