Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
65 changes: 37 additions & 28 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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_OBJECTS:lz4>)
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
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/api>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${lz4_SOURCE_DIR}/lib>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)


target_link_libraries(${PROJECT_NAME}_lib PRIVATE $<BUILD_INTERFACE:lz4::lz4> Threads::Threads)
target_link_libraries(${PROJECT_NAME}_lib PRIVATE Threads::Threads)
# Threads::Threads is intentionally NOT wrapped in $<BUILD_INTERFACE:...>: 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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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}
Expand All @@ -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
Expand Down Expand Up @@ -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 <build> --prefix <dir>` 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}")
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ctar/ctar.h>
```

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.

Expand Down
15 changes: 15 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ctar/ctar.h>
```

安装的 `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 构建
Expand Down
5 changes: 3 additions & 2 deletions cmake/ctar.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Libs: -L${libdir} -lctar
Libs.private: -pthread
Cflags: -I${includedir} -std=c++23
4 changes: 2 additions & 2 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions include/ctar/ctar.h
Original file line number Diff line number Diff line change
@@ -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 <ctar/version.h>

#endif // CTAR_CTAR_PUBLIC_H
72 changes: 72 additions & 0 deletions include/ctar/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#ifndef CTAR_VERSION_H
#define CTAR_VERSION_H

#include <cstdint>
#include <format>
#include <string>

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<uint32_t>(major) | (static_cast<uint32_t>(minor) << 8U) |
(static_cast<uint32_t>(patch) << 16U) | (static_cast<uint32_t>(reserved) << 24U);
}

[[nodiscard]] static constexpr Version Decode(const uint32_t ver) noexcept
{
Version v;
v.major = static_cast<uint8_t>(ver & 0xFFU);
v.minor = static_cast<uint8_t>((ver >> 8U) & 0xFFU);
v.patch = static_cast<uint8_t>((ver >> 16U) & 0xFFU);
v.reserved = static_cast<uint8_t>((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
Loading
Loading