diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 0aca5b3..d6278b7 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -113,6 +113,9 @@ jobs: working-directory: build run: ctest --output-on-failure + - name: Installed package consumer + run: scripts/consumer_smoke_test.sh build + - name: Dump ctest log on failure if: failure() working-directory: build @@ -175,4 +178,4 @@ jobs: - uses: actions/checkout@v4 - name: Check shell syntax - run: bash -n scripts/perf_gate.sh + run: bash -n scripts/perf_gate.sh scripts/consumer_smoke_test.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 672a581..235ad2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,9 @@ project(ctar VERSION ${CTAR_VERSION} LANGUAGES C CXX) message(STATUS "${PROJECT_NAME} ${CTAR_VERSION} (file format ${CTAR_FORMAT_VERSION})") +# Must come before any target that records an INSTALL_INTERFACE: the install +# include directory has to be defined when the interface is evaluated. +include(GNUInstallDirs) set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -44,14 +47,18 @@ else () set(lz4_SOURCE_DIR "${LZ4_LOCAL_DIR}") endif () -add_library(lz4 STATIC +# Object library, not a separate static archive: its objects are merged into +# libctar.a so the installed library is self-contained and consumers do not need +# lz4 installed. lz4frame.c is not built because the project never uses the +# frame API (no LZ4F_* references); leaving it out keeps the bundled symbol set +# smaller. +add_library(lz4 OBJECT ${lz4_SOURCE_DIR}/lib/lz4.c ${lz4_SOURCE_DIR}/lib/lz4hc.c - ${lz4_SOURCE_DIR}/lib/lz4frame.c ${lz4_SOURCE_DIR}/lib/xxhash.c ) target_include_directories(lz4 PUBLIC ${lz4_SOURCE_DIR}/lib) -add_library(lz4::lz4 ALIAS lz4) +set_target_properties(lz4 PROPERTIES POSITION_INDEPENDENT_CODE ON) # Warning flags as variable, no INTERFACE target if (MSVC) @@ -78,24 +85,31 @@ set(CORE_SOURCES src/core/block_store.cpp src/core/file_format.cpp src/core/file_handle.cpp + src/core/version.cpp src/utils/coding.cpp src/thread_pool/thread_pool.cpp ) -add_library(${PROJECT_NAME}_lib STATIC ${CORE_SOURCES}) +add_library(${PROJECT_NAME}_lib STATIC ${CORE_SOURCES} $) target_compile_options(${PROJECT_NAME}_lib PRIVATE ${PROJECT_WARNING_FLAGS}) +target_compile_features(${PROJECT_NAME}_lib PUBLIC cxx_std_23) target_include_directories(${PROJECT_NAME}_lib PUBLIC $ $ + $ $ $ ) -target_link_libraries(${PROJECT_NAME}_lib PRIVATE $ Threads::Threads) +target_link_libraries(${PROJECT_NAME}_lib PRIVATE Threads::Threads) # Threads::Threads is intentionally NOT wrapped in $: because # ctar_lib is a static library, consumers have to link pthread as well. -set_target_properties(${PROJECT_NAME}_lib PROPERTIES POSITION_INDEPENDENT_CODE ON) +# LZ4 objects are part of this target, so there is no lz4 link dependency. +set_target_properties(${PROJECT_NAME}_lib PROPERTIES + POSITION_INDEPENDENT_CODE ON + OUTPUT_NAME ${PROJECT_NAME} + EXPORT_NAME ${PROJECT_NAME}) # ========= CLI ========= if (BUILD_CLI) @@ -150,6 +164,10 @@ if (BUILD_TEST) target_compile_options(test_warning_fix INTERFACE "-Wno-maybe-uninitialized") endif () + # Googletest adds install rules (headers, libraries, .pc files) by default. + # Keep them out of the ctar install prefix; the tests only need the targets. + set(INSTALL_GTEST OFF CACHE BOOL "" FORCE) + set(GTEST_SOURCE_DIR "${PROJECT_SOURCE_DIR}/third_party/googletest") if (EXISTS "${GTEST_SOURCE_DIR}/CMakeLists.txt") message(STATUS "[TEST] Use local googletest at third_party/googletest") @@ -256,9 +274,9 @@ if (BUILD_BENCHMARKS) endif () # ========= Install & CMake package export ========= -include(GNUInstallDirs) -# Install library targets +# Install library targets. ctar_cli_lib is an internal implementation detail of +# the CLI executable, so it is installed but not exported. install(TARGETS ${PROJECT_NAME}_lib EXPORT ${PROJECT_NAME}Targets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} @@ -267,31 +285,17 @@ install(TARGETS ${PROJECT_NAME}_lib ) if (BUILD_CLI) - install(TARGETS ${PROJECT_NAME}_cli_lib - EXPORT ${PROJECT_NAME}Targets - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - ) install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) endif() -# Install api public headers +# Public headers only. Internal headers under src/ and api/ are implementation +# details and are deliberately not installed. install(FILES - ${PROJECT_SOURCE_DIR}/api/ctar.h - ${PROJECT_SOURCE_DIR}/api/export.h - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} -) - -# Install dependent src headers, preserve src/core / src/utils folder structure -install(DIRECTORY - ${PROJECT_SOURCE_DIR}/src/core - ${PROJECT_SOURCE_DIR}/src/utils - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/src - FILES_MATCHING - PATTERN "block_store.h" - PATTERN "status.h" + ${PROJECT_SOURCE_DIR}/include/ctar/ctar.h + ${PROJECT_SOURCE_DIR}/include/ctar/version.h + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/ctar ) install(EXPORT ${PROJECT_NAME}Targets @@ -327,7 +331,12 @@ install(FILES ) # ========= pkg-config .pc file ========= -set(prefix "${CMAKE_INSTALL_PREFIX}") +# Relocatable prefix: derive it from the .pc file location so +# `cmake --install --prefix ` still points at the real prefix. +file(RELATIVE_PATH CTAR_PC_PREFIX_REL + "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" + "${CMAKE_INSTALL_PREFIX}") +set(prefix "\${pcfiledir}/${CTAR_PC_PREFIX_REL}") set(exec_prefix "\${prefix}") set(libdir "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}") set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}") diff --git a/README.md b/README.md index d630c84..e09caef 100644 --- a/README.md +++ b/README.md @@ -342,6 +342,22 @@ cmake --build . cmake --install . # --prefix ./user_defined_install_path ``` +### Using the installed library + +```cmake +find_package(ctar CONFIG REQUIRED) +target_link_libraries(app PRIVATE ctar::ctar) +``` + +```cpp +#include +``` + +The installed `libctar.a` bundles LZ4 and xxhash, so no separate compression library is needed. Only the +headers under `include/ctar/` are public; `src/` and `api/` headers are internal and are not installed. The +current public API exposes version information; the archive read API is tracked in #101. The bundled +`LZ4_*`/`XXH*` symbols are part of `libctar.a` and may collide with a consumer's own copies of those libraries. + **Not supported**: Windows, Intel Macs, arm64 Linux, systems with glibc older than 2.28 (such as CentOS 7), and musl-based distributions such as Alpine. diff --git a/README_CN.md b/README_CN.md index 2a06a49..fab3c3f 100644 --- a/README_CN.md +++ b/README_CN.md @@ -327,6 +327,21 @@ cmake --build . cmake --install . # --prefix ./user_defined_install_path ``` +### 在 CMake 项目中使用安装的库 + +```cmake +find_package(ctar CONFIG REQUIRED) +target_link_libraries(app PRIVATE ctar::ctar) +``` + +```cpp +#include +``` + +安装的 `libctar.a` 已内置 LZ4 和 xxhash,不需要额外安装压缩库。只有 `include/ctar/` 下的头文件是公共的; +`src/` 和 `api/` 下的头文件属于内部实现,不会安装。当前公共 API 只提供版本信息,归档读取 API 见 #101。 +内置的 `LZ4_*`/`XXH*` 符号属于 `libctar.a`,可能与使用方自己的同名库冲突。 + **暂不支持**:Windows、Intel Mac、arm64 Linux、glibc 低于 2.28 的系统(如 CentOS 7),以及使用 musl 的系统(如 Alpine)。 ### Sanitizer 构建 diff --git a/cmake/ctar.pc.in b/cmake/ctar.pc.in index 21ab8ba..79a2cce 100644 --- a/cmake/ctar.pc.in +++ b/cmake/ctar.pc.in @@ -6,5 +6,6 @@ includedir=@includedir@ Name: ctar Description: Cloud‑era Tar — modern archive library optimized for cloud blob storage Version: @PROJECT_VERSION@ -Libs: -L${libdir} -lctar_lib -Cflags: -I${includedir} \ No newline at end of file +Libs: -L${libdir} -lctar +Libs.private: -pthread +Cflags: -I${includedir} -std=c++23 diff --git a/cmake/utils.cmake b/cmake/utils.cmake index 884439f..73a404e 100644 --- a/cmake/utils.cmake +++ b/cmake/utils.cmake @@ -1,5 +1,5 @@ -# src/core/version.h is the single source of truth for both the tool version +# include/ctar/version.h is the single source of truth for both the tool version # and the on-disk archive format version. The numbers are read as plain text, # so nothing else has to be kept in sync by hand. function(ctar_extract_version) @@ -8,7 +8,7 @@ function(ctar_extract_version) set(_tool_prefix "kToolVersion") set(_format_prefix "kFileFormatVersion") - set(_header "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../src/core/version.h") + set(_header "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../include/ctar/version.h") if(NOT EXISTS "${_header}") message(FATAL_ERROR "ctar_extract_version: ${_header} not found") endif() diff --git a/include/ctar/ctar.h b/include/ctar/ctar.h new file mode 100644 index 0000000..95f4524 --- /dev/null +++ b/include/ctar/ctar.h @@ -0,0 +1,15 @@ +#ifndef CTAR_CTAR_PUBLIC_H +#define CTAR_CTAR_PUBLIC_H + +// Umbrella header for the installed ctar library. +// +// Only public, self-contained headers belong here. Internal headers under +// src/ and api/ are not installed and must never be included from this file. +// +// The public surface currently exposes version information only; the archive +// read API (ctar::ArchiveReader) is tracked in issue #101 and will be added +// here without changing the install layout. + +#include + +#endif // CTAR_CTAR_PUBLIC_H diff --git a/include/ctar/version.h b/include/ctar/version.h new file mode 100644 index 0000000..44c0d19 --- /dev/null +++ b/include/ctar/version.h @@ -0,0 +1,72 @@ +#ifndef CTAR_VERSION_H +#define CTAR_VERSION_H + +#include +#include +#include + +namespace ctar +{ + + struct Version + { + uint8_t major{}; + uint8_t minor{}; + uint8_t patch{}; + uint8_t reserved{}; + + constexpr Version() noexcept = default; + + constexpr Version(uint8_t maj, uint8_t min, uint8_t pat) noexcept : major(maj), minor(min), patch(pat) {} + + [[nodiscard]] constexpr uint32_t Encode() const noexcept + { + return static_cast(major) | (static_cast(minor) << 8U) | + (static_cast(patch) << 16U) | (static_cast(reserved) << 24U); + } + + [[nodiscard]] static constexpr Version Decode(const uint32_t ver) noexcept + { + Version v; + v.major = static_cast(ver & 0xFFU); + v.minor = static_cast((ver >> 8U) & 0xFFU); + v.patch = static_cast((ver >> 16U) & 0xFFU); + v.reserved = static_cast((ver >> 24U) & 0xFFU); + return v; + } + + [[nodiscard]] std::string to_string() const { return std::format("{}.{}.{}", major, minor, patch); } + }; + + + // Tool version -- the single source of truth for the release number. + // cmake/utils.cmake extracts these three scalars textually and feeds them + // into project(VERSION ...), so the CLI, ctar.pc and ctarConfigVersion.cmake + // all agree. + constexpr uint8_t kToolVersionMajor = 1; + constexpr uint8_t kToolVersionMinor = 1; + constexpr uint8_t kToolVersionPatch = 1; + + // On-disk archive format version, stamped into every archive header. + // Bump this ONLY when the byte layout changes -- never to match the tool + // version above. + constexpr uint8_t kFileFormatVersionMajor = 1; + constexpr uint8_t kFileFormatVersionMinor = 1; + constexpr uint8_t kFileFormatVersionPatch = 0; + + + // Compile-time conveniences derived from the scalars above. The scalars stay + // the authority because they are what CMake parses. + inline constexpr Version kToolVersion{kToolVersionMajor, kToolVersionMinor, kToolVersionPatch}; + + inline constexpr Version kFileFormatVersion{kFileFormatVersionMajor, kFileFormatVersionMinor, + kFileFormatVersionPatch}; + + /// @brief Human-readable tool version, e.g. "1.1.1". + /// This is defined in the library (not inline) so a consumer that calls it + /// links against libctar and cannot succeed with headers alone. + [[nodiscard]] std::string VersionString(); + +} // namespace ctar + +#endif // CTAR_VERSION_H diff --git a/scripts/consumer_smoke_test.sh b/scripts/consumer_smoke_test.sh new file mode 100755 index 0000000..0fa5406 --- /dev/null +++ b/scripts/consumer_smoke_test.sh @@ -0,0 +1,91 @@ +#!/usr/bin/env bash +# Verify that `cmake --install` produces a package a downstream project can use. +# +# Usage: scripts/consumer_smoke_test.sh +# +# The script installs the build into a private temporary prefix, configures and +# builds test/consumer against it, runs the binary, and checks that lz4 is +# bundled into libctar.a (the installed library must not require a separate lz4). +set -euo pipefail + +if [[ $# -ne 1 ]]; then + echo "usage: $0 " >&2 + exit 2 +fi + +build_dir=$(cd "$1" && pwd -P) +repo_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P) + +tmp_root="${TMPDIR:-/tmp}/ctar-consumer-$(id -u)" +umask 077 +rm -rf "$tmp_root" +mkdir -p "$tmp_root" +cleanup() { rm -rf "$tmp_root"; } +trap cleanup EXIT + +prefix="$tmp_root/prefix" +consumer_build="$tmp_root/consumer-build" + +echo "== installing to $prefix ==" +cmake --install "$build_dir" --prefix "$prefix" >/dev/null + +echo "== configuring and building the consumer ==" +cmake -S "$repo_root/test/consumer" -B "$consumer_build" \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_PREFIX_PATH="$prefix" >/dev/null +cmake --build "$consumer_build" >/dev/null + +echo "== running the consumer ==" +"$consumer_build/ctar_consumer" + +echo "== checking that no test framework was installed ==" +pollution=$(find "$prefix" \( -name '*gtest*' -o -name '*gmock*' \) -print) +if [[ -n "$pollution" ]]; then + echo "error: test framework files were installed:" >&2 + echo "$pollution" >&2 + exit 1 +fi + +echo "== checking pkg-config ==" +if command -v pkg-config >/dev/null 2>&1; then + pc_file=$(find "$prefix" -name 'ctar.pc' -type f -print -quit) + if [[ -z "$pc_file" ]]; then + echo "error: ctar.pc was not installed" >&2 + exit 1 + fi + pc_dir=$(dirname "$pc_file") + pkg_cflags=$(PKG_CONFIG_PATH="$pc_dir" pkg-config --cflags ctar) + pkg_libs=$(PKG_CONFIG_PATH="$pc_dir" pkg-config --static --libs ctar) + # The prefix is expressed relative to the .pc file, so normalize before + # comparing it with the install prefix. + pc_prefix=$(PKG_CONFIG_PATH="$pc_dir" pkg-config --variable=prefix ctar) + prefix_real=$(cd "$prefix" && pwd -P) + pc_prefix_real=$(cd "$pc_prefix" 2>/dev/null && pwd -P) || pc_prefix_real="" + if [[ "$pc_prefix_real" != "$prefix_real" ]]; then + echo "error: pkg-config prefix '$pc_prefix' does not resolve to '$prefix'" >&2 + exit 1 + fi + read -r -a pc_cflags <<< "$pkg_cflags" + read -r -a pc_libs <<< "$pkg_libs" + "${CXX:-c++}" "${pc_cflags[@]}" "$repo_root/test/consumer/main.cpp" "${pc_libs[@]}" \ + -o "$tmp_root/pkgconfig-consumer" + "$tmp_root/pkgconfig-consumer" +else + echo "pkg-config not found; skipping the pkg-config check" +fi + +echo "== checking that lz4 is bundled ==" +archive=$(find "$prefix" -name 'libctar.a' -type f -print -quit) +if [[ -z "$archive" ]]; then + echo "error: libctar.a not found under $prefix" >&2 + exit 1 +fi +# Capture first: `nm | grep -q` would make nm die on SIGPIPE, and `pipefail` +# would turn that into a false failure even when the symbol is present. +symbols=$(nm -g "$archive") +if [[ "$symbols" != *LZ4_compress* ]]; then + echo "error: lz4 symbols are not bundled into $archive" >&2 + exit 1 +fi + +echo "consumer smoke test: ok" diff --git a/src/core/version.cpp b/src/core/version.cpp new file mode 100644 index 0000000..ae4b642 --- /dev/null +++ b/src/core/version.cpp @@ -0,0 +1,6 @@ +#include + +namespace ctar +{ + std::string VersionString() { return kToolVersion.to_string(); } +} // namespace ctar diff --git a/src/core/version.h b/src/core/version.h index bac6e5b..9a30459 100644 --- a/src/core/version.h +++ b/src/core/version.h @@ -1,67 +1,9 @@ -#ifndef CTAR_VERSION_H -#define CTAR_VERSION_H +#ifndef CTAR_VERSION_INTERNAL_H +#define CTAR_VERSION_INTERNAL_H -#include -#include -#include +// The version constants live in the public header, which is the single source +// of truth parsed by cmake/utils.cmake. This forwarding header keeps the +// existing internal includes working without a second definition. +#include -namespace ctar -{ - - struct Version - { - uint8_t major{}; - uint8_t minor{}; - uint8_t patch{}; - uint8_t reserved{}; - - constexpr Version() noexcept = default; - - constexpr Version(uint8_t maj, uint8_t min, uint8_t pat) noexcept : major(maj), minor(min), patch(pat) {} - - [[nodiscard]] constexpr uint32_t Encode() const noexcept - { - return static_cast(major) | (static_cast(minor) << 8U) | - (static_cast(patch) << 16U) | (static_cast(reserved) << 24U); - } - - [[nodiscard]] static constexpr Version Decode(const uint32_t ver) noexcept - { - Version v; - v.major = static_cast(ver & 0xFFU); - v.minor = static_cast((ver >> 8U) & 0xFFU); - v.patch = static_cast((ver >> 16U) & 0xFFU); - v.reserved = static_cast((ver >> 24U) & 0xFFU); - return v; - } - - [[nodiscard]] std::string to_string() const { return std::format("{}.{}.{}", major, minor, patch); } - }; - - - // Tool version -- the single source of truth for the release number. - // cmake/version.cmake extracts these three scalars textually and feeds them - // into project(VERSION ...), so the CLI, ctar.pc and ctarConfigVersion.cmake - // all agree. - constexpr uint8_t kToolVersionMajor = 1; - constexpr uint8_t kToolVersionMinor = 1; - constexpr uint8_t kToolVersionPatch = 1; - - // On-disk archive format version, stamped into every archive header. - // Bump this ONLY when the byte layout changes -- never to match the tool - // version above. - constexpr uint8_t kFileFormatVersionMajor = 1; - constexpr uint8_t kFileFormatVersionMinor = 1; - constexpr uint8_t kFileFormatVersionPatch = 0; - - - // Compile-time conveniences derived from the scalars above. The scalars stay - // the authority because they are what CMake parses. - inline constexpr Version kToolVersion{kToolVersionMajor, kToolVersionMinor, kToolVersionPatch}; - - inline constexpr Version kFileFormatVersion{kFileFormatVersionMajor, kFileFormatVersionMinor, - kFileFormatVersionPatch}; - -} // namespace ctar - -#endif // CTAR_VERSION_H +#endif // CTAR_VERSION_INTERNAL_H diff --git a/test/consumer/CMakeLists.txt b/test/consumer/CMakeLists.txt new file mode 100644 index 0000000..bde1dad --- /dev/null +++ b/test/consumer/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.20) +project(ctar_consumer LANGUAGES CXX) + +# Exercises the installed package exactly as a downstream user would. +find_package(ctar CONFIG REQUIRED) + +add_executable(ctar_consumer main.cpp) +target_link_libraries(ctar_consumer PRIVATE ctar::ctar) diff --git a/test/consumer/main.cpp b/test/consumer/main.cpp new file mode 100644 index 0000000..259a363 --- /dev/null +++ b/test/consumer/main.cpp @@ -0,0 +1,19 @@ +#include + +#include +#include + +int main() +{ + // VersionString() is defined in libctar, so this only links when the + // installed archive is actually usable. + const std::string version = ctar::VersionString(); + if (version != ctar::kToolVersion.to_string()) + { + std::cerr << "version mismatch: library " << version << ", headers " << ctar::kToolVersion.to_string() + << '\n'; + return 1; + } + std::cout << "ctar " << version << " (format " << ctar::kFileFormatVersion.to_string() << ")\n"; + return 0; +}