updated cmake build documentation

This commit is contained in:
Yann Collet
2025-05-29 02:55:51 +00:00
parent 1ec193eacf
commit 5748b75745

View File

@@ -1,34 +1,54 @@
## Usage
# xxHash CMake Integration
### Way 1: import targets
Build xxHash targets:
This document explains how to integrate xxHash into your CMake project. Choose the method that best fits your needs.
cd </path/to/xxHash/>
cmake -S build/cmake -B cmake_build
cmake --build cmake_build --parallel
cmake --install cmake_build
## Method 1: Install and Import (Recommended)
Where possible options are:
- `-DXXHASH_BUILD_XXHSUM=<ON|OFF>`: build the command line binary. ON by default
- `-DBUILD_SHARED_LIBS=<ON|OFF>`: build dynamic library. ON by default.
- `-DCMAKE_INSTALL_PREFIX=<path>`: use custom install prefix path.
- `-DDISPATCH=<ON|OFF>`: enable dispatch mode. Default is ON for x64 cpus, OFF otherwise.
**Best for:** Projects that want to use xxHash as a system-wide library.
Add lines into downstream CMakeLists.txt:
### Step 1: Build and Install xxHash
find_package(xxHash 0.8 CONFIG REQUIRED)
...
target_link_libraries(MyTarget PRIVATE xxHash::xxhash)
```bash
cd /path/to/xxHash
cmake -S build/cmake -B cmake_build
cmake --build cmake_build --parallel
cmake --install cmake_build
```
### Way 2: Add subdirectory
Add lines into downstream CMakeLists.txt:
### Step 2: Use in Your Project
option(BUILD_SHARED_LIBS "Build shared libs" OFF) #optional
...
set(XXHASH_BUILD_ENABLE_INLINE_API OFF) #optional
set(XXHASH_BUILD_XXHSUM OFF) #optional
add_subdirectory(</path/to/xxHash/build/cmake/> </path/to/xxHash/build/> EXCLUDE_FROM_ALL)
...
target_link_libraries(MyTarget PRIVATE xxHash::xxhash)
Add to your `CMakeLists.txt`:
```cmake
find_package(xxHash 0.8 CONFIG REQUIRED)
target_link_libraries(YourTarget PRIVATE xxHash::xxhash)
```
### Build Options
Configure the build with these options:
- `-DXXHASH_BUILD_XXHSUM=OFF` - Skip building the command line tool (default: ON)
- `-DBUILD_SHARED_LIBS=OFF` - Build static library instead of shared (default: ON)
- `-DCMAKE_INSTALL_PREFIX=/custom/path` - Install to custom location
- `-DDISPATCH=OFF` - Disable CPU dispatch optimization (default: ON for x64)
## Method 2: Add as Subdirectory
**Best for:** Projects that want to bundle xxHash directly without system installation.
Add to your `CMakeLists.txt`:
```cmake
# Optional: Configure xxHash before adding
set(XXHASH_BUILD_XXHSUM OFF) # Don't build command line tool
option(BUILD_SHARED_LIBS OFF) # Build static library
# Add xxHash to your project
add_subdirectory(path/to/xxHash/build/cmake xxhash_build EXCLUDE_FROM_ALL)
# Link to your target
target_link_libraries(YourTarget PRIVATE xxHash::xxhash)
```