diff --git a/CMakeLists.txt b/CMakeLists.txt index 745f969..aec54ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,9 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wno-terminate") set(CMAKE_CXX_STANDARD 11) +find_package(PkgConfig REQUIRED) +find_package(Brotli REQUIRED MODULE) + include_directories(external) include_directories(core/include/attachments) diff --git a/cmake/FindBrotli.cmake b/cmake/FindBrotli.cmake new file mode 100755 index 0000000..7d6cdc2 --- /dev/null +++ b/cmake/FindBrotli.cmake @@ -0,0 +1,225 @@ +# FindBrotli.cmake +# +# Supports COMPONENTS: +# decoder, encoder, common +# +# Exports targets: +# Brotli::decoder +# Brotli::encoder +# Brotli::common +# +# Optional variables: +# BROTLI_ROOT_DIR +# BROTLI_USE_STATIC_LIBS + +# ------------------------------------------------------------ +# Version handling (not supported) +# ------------------------------------------------------------ +if(Brotli_FIND_VERSION) + set(_brotli_version_error_msg "FindBrotli.cmake does not support version checking.") + if(Brotli_FIND_REQUIRED) + message(FATAL_ERROR "${_brotli_version_error_msg}") + elseif(NOT Brotli_FIND_QUIETLY) + message(WARNING "${_brotli_version_error_msg}") + endif() +endif() + +# ------------------------------------------------------------ +# Component dependencies +# ------------------------------------------------------------ +if(Brotli_FIND_REQUIRED_decoder OR Brotli_FIND_REQUIRED_encoder) + set(Brotli_FIND_REQUIRED_common TRUE) +endif() + +# ------------------------------------------------------------ +# Static library preference +# ------------------------------------------------------------ +if(BROTLI_USE_STATIC_LIBS) + set(_brotli_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES}) + if(WIN32) + set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a) + else() + set(CMAKE_FIND_LIBRARY_SUFFIXES .a) + endif() +endif() + +# ------------------------------------------------------------ +# Optional pkg-config +# ------------------------------------------------------------ +find_package(PkgConfig QUIET) + +# ------------------------------------------------------------ +# Includes +# ------------------------------------------------------------ +find_path(Brotli_INCLUDE_DIR + NAMES + brotli/decode.h + brotli/encode.h + HINTS + ${BROTLI_ROOT_DIR} + PATH_SUFFIXES + include + includes +) +mark_as_advanced(Brotli_INCLUDE_DIR) + +# ------------------------------------------------------------ +# Internal state +# ------------------------------------------------------------ +set(_brotli_req_vars "") + +# For figuring out the real (non-ALIAS) targets when using pkg-config +set(_brotli_decoder_real_target "") +set(_brotli_encoder_real_target "") +set(_brotli_common_real_target "") + +if(BROTLI_USE_STATIC_LIBS) + set(_brotli_stat_str "_STATIC") +else() + set(_brotli_stat_str "") +endif() + +# ------------------------------------------------------------ +# Components loop +# ------------------------------------------------------------ +foreach(_listvar "common;common" "decoder;dec" "encoder;enc") + list(GET _listvar 0 _component) + list(GET _listvar 1 _libname) + + # ---- pkg-config path ---- + if(PKG_CONFIG_FOUND) + if(BROTLI_USE_STATIC_LIBS) + pkg_check_modules( + Brotli_${_component}_STATIC + QUIET + GLOBAL + IMPORTED_TARGET + libbrotli${_libname} + ) + else() + pkg_check_modules( + Brotli_${_component} + QUIET + GLOBAL + IMPORTED_TARGET + libbrotli${_libname} + ) + endif() + endif() + + # If pkg-config created an imported target, make our alias to it. + if(TARGET PkgConfig::Brotli_${_component}${_brotli_stat_str}) + add_library( + Brotli::${_component} + ALIAS + PkgConfig::Brotli_${_component}${_brotli_stat_str} + ) + + # Save the underlying real target name for later linkage fixes + set(_brotli_${_component}_real_target "PkgConfig::Brotli_${_component}${_brotli_stat_str}") + + set(Brotli_${_component}_FOUND TRUE) + + if(Brotli_FIND_REQUIRED_${_component}) + # For FindPackageHandleStandardArgs: ensure libraries are actually present + if(BROTLI_USE_STATIC_LIBS) + list(APPEND _brotli_req_vars Brotli_${_component}_STATIC_LIBRARIES) + else() + list(APPEND _brotli_req_vars Brotli_${_component}_LINK_LIBRARIES) + endif() + endif() + + continue() + endif() + + # ---- find_library path ---- + if(Brotli_FIND_REQUIRED_${_component}) + list(APPEND _brotli_req_vars Brotli_${_component}) + endif() + + if(BROTLI_USE_STATIC_LIBS) + set(_brotli_names + brotli${_libname}-static + libbrotli${_libname}-static + ) + else() + set(_brotli_names + brotli${_libname} + libbrotli${_libname} + ) + endif() + + find_library(Brotli_${_component} + NAMES ${_brotli_names} + HINTS ${BROTLI_ROOT_DIR} + PATH_SUFFIXES + lib + lib64 + libs + libs64 + lib/x86_64-linux-gnu + ) + mark_as_advanced(Brotli_${_component}) + + if(Brotli_${_component}) + set(Brotli_${_component}_FOUND TRUE) + + add_library(Brotli::${_component} UNKNOWN IMPORTED) + set_target_properties(Brotli::${_component} PROPERTIES + IMPORTED_LOCATION "${Brotli_${_component}}" + INTERFACE_INCLUDE_DIRECTORIES "${Brotli_INCLUDE_DIR}" + ) + + # In this branch, our target is real (not ALIAS), so it can be linked later. + set(_brotli_${_component}_real_target "Brotli::${_component}") + else() + set(Brotli_${_component}_FOUND FALSE) + endif() +endforeach() + +# ------------------------------------------------------------ +# Link decoder/encoder → common (but never on ALIAS targets or IMPORTED targets) +# ------------------------------------------------------------ +if(_brotli_common_real_target) + foreach(_comp decoder encoder) + if(_brotli_${_comp}_real_target) + # Only link if the target is NOT an ALIAS and NOT an IMPORTED target + get_target_property(_aliased ${_brotli_${_comp}_real_target} ALIASED_TARGET) + get_target_property(_imported ${_brotli_${_comp}_real_target} IMPORTED) + if(NOT _aliased AND NOT _imported) + target_link_libraries(${_brotli_${_comp}_real_target} INTERFACE ${_brotli_common_real_target}) + endif() + endif() + endforeach() +endif() + +# ------------------------------------------------------------ +# Aggregate convenience variables +# ------------------------------------------------------------ +set(Brotli_LIBRARIES "") +foreach(_comp decoder encoder common) + if(TARGET Brotli::${_comp}) + list(APPEND Brotli_LIBRARIES Brotli::${_comp}) + endif() +endforeach() + +# ------------------------------------------------------------ +# Final package check (FIXED: use _brotli_req_vars) +# ------------------------------------------------------------ +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Brotli + FOUND_VAR + Brotli_FOUND + REQUIRED_VARS + Brotli_INCLUDE_DIR + ${_brotli_req_vars} + HANDLE_COMPONENTS +) + +# ------------------------------------------------------------ +# Restore suffixes +# ------------------------------------------------------------ +if(BROTLI_USE_STATIC_LIBS) + set(CMAKE_FIND_LIBRARY_SUFFIXES ${_brotli_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES}) +endif() + diff --git a/core/compression/CMakeLists.txt b/core/compression/CMakeLists.txt index 2331156..89ef3bd 100644 --- a/core/compression/CMakeLists.txt +++ b/core/compression/CMakeLists.txt @@ -2,5 +2,9 @@ add_definitions(-DZLIB_CONST) add_library(osrc_compression_utils SHARED compression_utils.cc) +target_link_libraries(compression_utils + ${Brotli_LIBRARIES} +) + install(TARGETS osrc_compression_utils DESTINATION lib) install(TARGETS osrc_compression_utils DESTINATION nginx_attachment/lib) diff --git a/docker/Dockerfile b/docker/Dockerfile index 6144724..de3db6b 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -5,6 +5,7 @@ USER root RUN apk update RUN apk add --no-cache -u busybox RUN apk add --no-cache -u zlib +RUN apk add --no-cache -u brotli brotli-dev RUN apk add --no-cache libstdc++ RUN mkdir -p /usr/lib/nginx/modules/ RUN mkdir -p /usr/lib64/nginx/modules/ diff --git a/nodes/nginx_attachment/CMakeLists.txt b/nodes/nginx_attachment/CMakeLists.txt index 73b0045..18687ab 100644 --- a/nodes/nginx_attachment/CMakeLists.txt +++ b/nodes/nginx_attachment/CMakeLists.txt @@ -1,5 +1,11 @@ install(FILES install-nginx-attachment.sh DESTINATION nginx_attachment/ PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ) +execute_process ( + COMMAND sh -c "find /usr/lib* -name \"libbrotli*.so*\" | awk '{printf \$0\";\"}'" + OUTPUT_VARIABLE brotli +) +install(FILES ${brotli} DESTINATION nginx_attachment/lib) + gen_package( install-cp-nano-nginx-attachment.sh nginx_attachment diff --git a/nodes/nginx_attachment/install-nginx-attachment.sh b/nodes/nginx_attachment/install-nginx-attachment.sh index 617657f..669fb54 100755 --- a/nodes/nginx_attachment/install-nginx-attachment.sh +++ b/nodes/nginx_attachment/install-nginx-attachment.sh @@ -40,6 +40,7 @@ run_installation() { cp_print "Starting installation of Check Point ${NANO_SERVICE_NAME} Nano service [$INSTALLATION_TIME]\n" $FORCE_STDOUT + cp_exec "cp lib/libbrotli*.so* /usr/lib/" cp_exec "cp lib/libosrc_compression_utils.so /usr/lib/libosrc_compression_utils.so" cp_exec "cp lib/libosrc_compression_utils.so /usr/lib64/libosrc_compression_utils.so" cp_exec "cp lib/libosrc_nginx_attachment_util.so /usr/lib/libosrc_nginx_attachment_util.so"