From 83e3853f0187435614de0586d26f1d2897cac9e6 Mon Sep 17 00:00:00 2001 From: Victor Kataev Date: Sat, 15 Apr 2023 21:03:44 +0400 Subject: [PATCH 1/9] Refactor cmake, add target installation --- CMakeLists.txt | 104 +++++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c5a3b4b..d8cd2ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,16 +1,39 @@ cmake_minimum_required(VERSION 3.3) set(CMAKE_MACOSX_RPATH OFF) -project(STREAMVBYTE VERSION "1.0.0") +project(streamvbyte VERSION "1.0.0") set(STREAMVBYTE_LIB_VERSION "1.0.0" 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(${WB_TARGET_NAME} 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) @@ -38,6 +61,12 @@ if(STREAMVBYTE_SANITIZE) 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") endif() @@ -47,21 +76,9 @@ if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)") set(BASE_FLAGS ${BASE_FLAGS} "-D__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 +87,7 @@ set_target_properties( WINDOWS_EXPORT_ALL_SYMBOLS YES ) +# Add root include directory target_include_directories( streamvbyte PUBLIC @@ -77,21 +95,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 +103,51 @@ 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 +install(TARGETS streamvbyte EXPORT streamvbyte-target) +install( + EXPORT streamvbyte-target + FILE streamvbyte-targets.cmake + NAMESPACE streamvbyte:: + 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 +) + From 59772d4a84d772db00b66e5d0af22bb751036ae0 Mon Sep 17 00:00:00 2001 From: Victor Kataev Date: Mon, 17 Apr 2023 15:55:39 +0400 Subject: [PATCH 2/9] Add cmake intallation section --- CMakeLists.txt | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d8cd2ba..0735934 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,13 +23,10 @@ add_library(streamvbyte::streamvbyte ALIAS streamvbyte) # Set C standard target_compile_features(streamvbyte PUBLIC c_std_99) -set_target_properties(${WB_TARGET_NAME} PROPERTIES C_STANDARD_REQUIRED ON) +set_target_properties(streamvbyte PROPERTIES C_STANDARD_REQUIRED ON) # Set fPIC -set_target_properties( - streamvbyte - PROPERTIES POSITION_INDEPENDENT_CODE ON -) +set_target_properties(streamvbyte PROPERTIES POSITION_INDEPENDENT_CODE ON) option(STREAMVBYTE_SANITIZE "Sanitize addresses" OFF) @@ -136,6 +133,8 @@ if(STREAMVBYTE_ENABLE_TESTS) endif() # Install +include(GNUInstallDirs) + install(TARGETS streamvbyte EXPORT streamvbyte-target) install( EXPORT streamvbyte-target @@ -143,6 +142,30 @@ install( 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 @@ -150,4 +173,3 @@ install( ${PROJECT_SOURCE_DIR}/include/streamvbyte_zigzag.h DESTINATION include ) - From 4fa97e58862421096168234cb0951cb78d11317e Mon Sep 17 00:00:00 2001 From: Victor Kataev Date: Mon, 17 Apr 2023 19:06:37 +0400 Subject: [PATCH 3/9] Add find_package check to Ubuntu CI --- .github/workflows/ubuntu22.yml | 8 ++++++++ cmake/streamvbyte-config.cmake.in | 1 + 2 files changed, 9 insertions(+) create mode 100644 cmake/streamvbyte-config.cmake.in 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/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@ From 0998caf24808f3923f92b5b3967b6d30aaacd51e Mon Sep 17 00:00:00 2001 From: Victor Kataev Date: Fri, 21 Apr 2023 01:18:35 +0400 Subject: [PATCH 4/9] Fix version set, replace add_definitions() --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0735934..daf1456 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,9 @@ cmake_minimum_required(VERSION 3.3) 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") # Create target @@ -65,7 +65,7 @@ if(STREAMVBYTE_SANITIZE_UNDEFINED) endif() if(MSVC) - add_definitions("-D__restrict__=__restrict") + target_compiler_definitions(streamvbyte PRIVATE "__restrict__=__restrict") endif() # test for arm From 873d0457ea14fe1c003e21e1e38e1be167851d73 Mon Sep 17 00:00:00 2001 From: Victor Kataev Date: Tue, 25 Apr 2023 12:39:44 +0400 Subject: [PATCH 5/9] Fix --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index daf1456..d93be9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,7 +65,7 @@ if(STREAMVBYTE_SANITIZE_UNDEFINED) endif() if(MSVC) - target_compiler_definitions(streamvbyte PRIVATE "__restrict__=__restrict") + target_compile_definitions(streamvbyte PRIVATE "__restrict__=__restrict") endif() # test for arm From 6ee849250899f2181a9410230cda0ad38d3db184 Mon Sep 17 00:00:00 2001 From: Victor Kataev Date: Tue, 25 Apr 2023 12:54:43 +0400 Subject: [PATCH 6/9] Remove old code --- CMakeLists.txt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d93be9d..19bc128 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,7 +55,7 @@ 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) @@ -68,13 +68,11 @@ if(MSVC) 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() -target_link_libraries(streamvbyte ${BASE_FLAGS}) - # Set version set_target_properties( streamvbyte From 25a6049548ca109c7d002c626b920f9b600f6958 Mon Sep 17 00:00:00 2001 From: Victor Kataev Date: Tue, 25 Apr 2023 13:08:28 +0400 Subject: [PATCH 7/9] Set minimum cmake version to 3.12 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 19bc128..067f859 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.3) +cmake_minimum_required(VERSION 3.12) set(CMAKE_MACOSX_RPATH OFF) project(streamvbyte VERSION "0.5.3" LANGUAGES C) From e6168068627e2a4183731b283aeb2ec7c5ed8922 Mon Sep 17 00:00:00 2001 From: Victor Kataev Date: Tue, 25 Apr 2023 14:21:45 +0400 Subject: [PATCH 8/9] Update readme with cmake installtion section --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index a22dcad..ba18963 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,15 @@ zigzag_encode(mysignedints, myunsignedints, number); // mysignedints => myunsign zigzag_decode(myunsignedints, mysignedints, number); // myunsignedints => mysignedints ``` +Installation (CMake) +---------------- + +``` +cmake -DCMAKE_BUILD_TYPE=Release .. +cmake --build . +sudo cmake --install . +``` + Installation ---------------- From 4a182a0e3544fc95a6df61b37a2f2f29340e82d7 Mon Sep 17 00:00:00 2001 From: Victor Kataev Date: Thu, 27 Apr 2023 12:01:39 +0400 Subject: [PATCH 9/9] Add CMake integration section to readme --- README.md | 99 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 61 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index ba18963..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 -`-DCMAKE_INSTALL_PREFIX:PATH=/path/to/install` is optional. -Defaults to /usr/local{include,lib} +## FindPackage +```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,27 +148,6 @@ zigzag_encode(mysignedints, myunsignedints, number); // mysignedints => myunsign zigzag_decode(myunsignedints, mysignedints, number); // myunsignedints => mysignedints ``` -Installation (CMake) ----------------- - -``` -cmake -DCMAKE_BUILD_TYPE=Release .. -cmake --build . -sudo cmake --install . -``` - -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 -----------------