diff --git a/.github/workflows/ubuntu22.yml b/.github/workflows/ubuntu22.yml index 4d5fa33..fd7f24b 100644 --- a/.github/workflows/ubuntu22.yml +++ b/.github/workflows/ubuntu22.yml @@ -49,3 +49,11 @@ jobs: cmake .. -DSTREAMVBYTE_SANITIZE_UNDEFINED=ON -DCMAKE_BUILD_TYPE=buildundefsani -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON && cmake --build . && ctest --output-on-failure + + - name: Install sreamvbyte + run: | + cd buildrelease + sudo cmake --install . + + - name: Check cmake target installation + run: cmake --find-package -DNAME=streamvbyte -DCOMPILER_ID=GNU -DLANGUAGE=C -DMODE=EXIST diff --git a/CMakeLists.txt b/CMakeLists.txt index c5a3b4b..067f859 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,16 +1,36 @@ -cmake_minimum_required(VERSION 3.3) +cmake_minimum_required(VERSION 3.12) set(CMAKE_MACOSX_RPATH OFF) -project(STREAMVBYTE VERSION "1.0.0") +project(streamvbyte VERSION "0.5.3" LANGUAGES C) -set(STREAMVBYTE_LIB_VERSION "1.0.0" CACHE STRING "streamvbyte library version") +set(STREAMVBYTE_LIB_VERSION "0.5.3" CACHE STRING "streamvbyte library version") set(STREAMVBYTE_LIB_SOVERSION "1" CACHE STRING "streamvbyte library soversion") -set(CMAKE_C_STANDARD 99) -set(CMAKE_C_STANDARD_REQUIRED ON) +# Create target +add_library( + streamvbyte + src/streamvbyte_encode.c + src/streamvbyte_decode.c + src/streamvbyte_zigzag.c + src/streamvbytedelta_encode.c + src/streamvbytedelta_decode.c + src/streamvbyte_0124_encode.c + src/streamvbyte_0124_decode.c +) + +# Add alias +add_library(streamvbyte::streamvbyte ALIAS streamvbyte) + +# Set C standard +target_compile_features(streamvbyte PUBLIC c_std_99) +set_target_properties(streamvbyte PROPERTIES C_STANDARD_REQUIRED ON) + +# Set fPIC +set_target_properties(streamvbyte PROPERTIES POSITION_INDEPENDENT_CODE ON) option(STREAMVBYTE_SANITIZE "Sanitize addresses" OFF) +# Set CMAKE_BUILD_TYPE if(NOT CMAKE_BUILD_TYPE) message(STATUS "No build type selected") if(STREAMVBYTE_SANITIZE) @@ -35,33 +55,25 @@ if(STREAMVBYTE_SANITIZE) -fno-omit-frame-pointer -fno-sanitize-recover=all ) - add_compile_definitions(ASAN_OPTIONS=detect_leaks=1) + add_compile_definitions("ASAN_OPTIONS=detect_leaks=1") +endif() + +option(STREAMVBYTE_SANITIZE_UNDEFINED "Sanitize undefined behavior" OFF) +if(STREAMVBYTE_SANITIZE_UNDEFINED) + add_compile_options(-fsanitize=undefined -fno-sanitize-recover=all) + add_link_options(-fsanitize=undefined -fno-sanitize-recover=all) endif() if(MSVC) - add_definitions("-D__restrict__=__restrict") + target_compile_definitions(streamvbyte PRIVATE "__restrict__=__restrict") endif() -# test for arm +# Add Neon support flag if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)") - set(BASE_FLAGS ${BASE_FLAGS} "-D__ARM_NEON__") + add_compile_definitions("__ARM_NEON__") endif() -set(STREAMVBYTE_SRCS - ${PROJECT_SOURCE_DIR}/src/streamvbyte_encode.c - ${PROJECT_SOURCE_DIR}/src/streamvbyte_decode.c - ${PROJECT_SOURCE_DIR}/src/streamvbyte_zigzag.c - ${PROJECT_SOURCE_DIR}/src/streamvbytedelta_encode.c - ${PROJECT_SOURCE_DIR}/src/streamvbytedelta_decode.c - ${PROJECT_SOURCE_DIR}/src/streamvbyte_0124_encode.c - ${PROJECT_SOURCE_DIR}/src/streamvbyte_0124_decode.c -) - -set(CMAKE_POSITION_INDEPENDENT_CODE ON) - -add_library(streamvbyte "${STREAMVBYTE_SRCS}") -target_link_libraries(streamvbyte ${BASE_FLAGS}) - +# Set version set_target_properties( streamvbyte PROPERTIES @@ -70,6 +82,7 @@ set_target_properties( WINDOWS_EXPORT_ALL_SYMBOLS YES ) +# Add root include directory target_include_directories( streamvbyte PUBLIC @@ -77,21 +90,7 @@ target_include_directories( $ ) -install( - FILES - ${PROJECT_SOURCE_DIR}/include/streamvbyte.h - ${PROJECT_SOURCE_DIR}/include/streamvbytedelta.h - ${PROJECT_SOURCE_DIR}/include/streamvbyte_zigzag.h - DESTINATION include -) -install(TARGETS streamvbyte DESTINATION lib) - -option(STREAMVBYTE_SANITIZE_UNDEFINED "Sanitize undefined behavior" OFF) -if(STREAMVBYTE_SANITIZE_UNDEFINED) - add_compile_options(-fsanitize=undefined -fno-sanitize-recover=all) - add_link_options(-fsanitize=undefined -fno-sanitize-recover=all) -endif() - +# Print info message(STATUS "CMAKE_SYSTEM_PROCESSOR: " ${CMAKE_SYSTEM_PROCESSOR}) message(STATUS "CMAKE_BUILD_TYPE: " ${CMAKE_BUILD_TYPE}) # this tends to be "sticky" so you can remain unknowingly in debug mode message(STATUS "CMAKE_C_COMPILER: " ${CMAKE_C_COMPILER}) # important to know which compiler is used @@ -99,35 +98,76 @@ message(STATUS "CMAKE_C_FLAGS: " ${CMAKE_C_FLAGS}) # important to know the flags message(STATUS "CMAKE_C_FLAGS_DEBUG: " ${CMAKE_C_FLAGS_DEBUG}) message(STATUS "CMAKE_C_FLAGS_RELEASE: " ${CMAKE_C_FLAGS_RELEASE}) -# build programs - -# example +# Example option(STREAMVBYTE_ENABLE_EXAMPLES "Enable examples for streamvbyte" OFF) if(STREAMVBYTE_ENABLE_EXAMPLES) - add_executable(example ${PROJECT_SOURCE_DIR}/examples/example.c) + add_executable(example examples/example.c) target_link_libraries(example streamvbyte) endif() +# Tests option(STREAMVBYTE_ENABLE_TESTS "Enable unit tests for streamvbyte" OFF) if(STREAMVBYTE_ENABLE_TESTS) if(NOT MSVC) - # perf - add_executable(perf ${PROJECT_SOURCE_DIR}/tests/perf.c) + # Perf + add_executable(perf tests/perf.c) target_link_libraries(perf streamvbyte) target_link_libraries(perf m) endif() - # writeseq - add_executable(writeseq ${PROJECT_SOURCE_DIR}/tests/writeseq.c) + # Writeseq + add_executable(writeseq tests/writeseq.c) target_link_libraries(writeseq streamvbyte) - # unit - add_executable(unit ${PROJECT_SOURCE_DIR}/tests/unit.c) + # Unit + add_executable(unit tests/unit.c) target_link_libraries(unit streamvbyte) enable_testing() - # add unit tests + # Add unit tests add_test(NAME unit COMMAND unit) add_custom_target(check COMMAND ctest --output-on-failure DEPENDS unit) endif() + +# Install +include(GNUInstallDirs) + +install(TARGETS streamvbyte EXPORT streamvbyte-target) +install( + EXPORT streamvbyte-target + FILE streamvbyte-targets.cmake + NAMESPACE streamvbyte:: + DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake +) + +include(CMakePackageConfigHelpers) + +# Create package config file +configure_package_config_file( + "${PROJECT_SOURCE_DIR}/cmake/streamvbyte-config.cmake.in" + "streamvbyte-config.cmake" + INSTALL_DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake +) + +# Create package version file +write_basic_package_version_file( + streamvbyte-config-version.cmake + COMPATIBILITY SameMajorVersion +) + +# Install cmake files +install( + FILES + "${PROJECT_BINARY_DIR}/streamvbyte-config.cmake" + "${PROJECT_BINARY_DIR}/streamvbyte-config-version.cmake" + DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake +) + +install( + FILES + ${PROJECT_SOURCE_DIR}/include/streamvbyte.h + ${PROJECT_SOURCE_DIR}/include/streamvbytedelta.h + ${PROJECT_SOURCE_DIR}/include/streamvbyte_zigzag.h + DESTINATION include +) diff --git a/README.md b/README.md index a22dcad..fc7270b 100644 --- a/README.md +++ b/README.md @@ -37,36 +37,80 @@ This library is used by * [Facebook Thrift](https://github.com/facebook/fbthrift), * [Trinity Information Retrieval framework](https://github.com/phaistos-networks/Trinity). -# Usage +# Installation + +## CMake + +``` +cd streamvbyte +mkdir build && cd build +cmake .. -DSTREAMVBYTE_ENABLE_EXAMPLES=ON \ # default: OFF + -DSTREAMVBYTE_ENABLE_TESTS=ON # default: OFF +cmake --build . + +ctest -V # run tests if builded + +sudo cmake --install . +``` + +## Makefile + +You can install the library (as a dynamic library) on your machine if you have root access: -Usage with Makefile: + sudo make install + +To uninstall, simply type: + + sudo make uninstall + +It is recommended that you try ``make dyntest`` before proceeding. + + +Unit test make ./unit -Usage with CMake: -The cmake build system also offers a `libstreamvbyte_static` static library -(`libstreamvbyte_static` under linux) in addition to -`libstreamvbyte` shared library (`libstreamvbyte.so` under linux). +# CMake integration + +## FindPackage -`-DCMAKE_INSTALL_PREFIX:PATH=/path/to/install` is optional. -Defaults to /usr/local{include,lib} +```cmake +find_package(streamvbyte REQUIRED) +add_executable(program program.cpp) +target_link_libraries(program streamvbyte::streamvbyte) ``` -mkdir build -cd build -cmake .. -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_INSTALL_PREFIX:PATH=/path/to/install \ - -DSTREAMVBYTE_ENABLE_EXAMPLES=ON \ - -DSTREAMVBYTE_ENABLE_TESTS=ON -make install -# run the tests like: -ctest -V +## FetchContent +```cmake +Include(FetchContent) + +FetchContent_Declare( + streamvbyte + GIT_REPOSITORY https://github.com/lemire/streamvbyte.git + GIT_TAG v0.5.4 # or a later release +) + +FetchContent_MakeAvailable(streamvbyte) + +add_executable(program program.cpp) +target_link_libraries(program streamvbyte::streamvbyte) +``` + +## Subdirectory + +```cmake +add_subdirectory(streamvbyte) + +add_executable(program program.cpp) +target_link_libraries(program streamvbyte::streamvbyte) ``` +# Usage + See `examples/example.c` for an example. Short code sample: @@ -104,18 +148,6 @@ zigzag_encode(mysignedints, myunsignedints, number); // mysignedints => myunsign zigzag_decode(myunsignedints, mysignedints, number); // myunsignedints => mysignedints ``` -Installation ----------------- - -You can install the library (as a dynamic library) on your machine if you have root access: - - sudo make install - -To uninstall, simply type: - - sudo make uninstall - -It is recommended that you try ``make dyntest`` before proceeding. Benchmarking ----------------- diff --git a/cmake/streamvbyte-config.cmake.in b/cmake/streamvbyte-config.cmake.in new file mode 100644 index 0000000..a18cb7d --- /dev/null +++ b/cmake/streamvbyte-config.cmake.in @@ -0,0 +1 @@ +@PACKAGE_INIT@