Compiling TiKV's Rust Code

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: tikv的rust代码编译

| username: TiDBer_VptqKbTd

When building the TiKV source code with cargo build, the following error occurred. How can I resolve it?

Compiling libtitan_sys v0.0.1 (https://github.com/tikv/rust-rocksdb.git#b2cd4258)
error: failed to run custom build command for `libtitan_sys v0.0.1 (https://github.com/tikv/rust-rocksdb.git#b2cd4258)`

Caused by:
  process didn't exit successfully: `/home/yue/tikv/target/debug/build/libtitan_sys-805bf40a3d57cb5a/build-script-build` (exit status: 101)
  --- stdout
  CMAKE_TOOLCHAIN_FILE_x86_64-unknown-linux-gnu = None
  CMAKE_TOOLCHAIN_FILE_x86_64_unknown_linux_gnu = None
  HOST_CMAKE_TOOLCHAIN_FILE = None
  CMAKE_TOOLCHAIN_FILE = None
  CMAKE_GENERATOR_x86_64-unknown-linux-gnu = None
  CMAKE_GENERATOR_x86_64_unknown_linux_gnu = None
  HOST_CMAKE_GENERATOR = None
  CMAKE_GENERATOR = None
  CMAKE_PREFIX_PATH_x86_64-unknown-linux-gnu = None
  CMAKE_PREFIX_PATH_x86_64_unknown_linux_gnu = None
  HOST_CMAKE_PREFIX_PATH = None
  CMAKE_PREFIX_PATH = Some("/home/yue/tikv/target/debug/build/libz-sys-46bc1b44529cf39a/out/build")
  CMAKE_x86_64-unknown-linux-gnu = None
  CMAKE_x86_64_unknown_linux_gnu = None
  HOST_CMAKE = None
  CMAKE = None
  running: "cmake" "-Wdev" "--debug-output" "/root/.cargo/git/checkouts/rust-rocksdb-a9a28e74c6ead8ef/b2cd425/librocksdb_sys/libtitan_sys/titan" "-DROCKSDB_DIR=/root/.cargo/git/checkouts/rust-rocksdb-a9a28e74c6ead8ef/b2cd425/librocksdb_sys/libtitan_sys/../rocksdb" "-DWITH_TITAN_TESTS=OFF" "-DWITH_TITAN_TOOLS=OFF" "-DWITH_ZLIB=ON" "-DWITH_BZ2=ON" "-DWITH_LZ4=ON" "-DWITH_ZSTD=ON" "-DWITH_SNAPPY=ON" "-DWITH_TITAN_TESTS=OFF" "-DWITH_TITAN_TOOLS=OFF" "-DCMAKE_INSTALL_PREFIX=/home/yue/tikv/target/debug/build/libtitan_sys-5dd3233306f3aad1/out" "-DCMAKE_C_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64" "-DCMAKE_C_COMPILER=/opt/rh/devtoolset-8/root/usr/bin/cc" "-DCMAKE_CXX_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64" "-DCMAKE_CXX_COMPILER=/opt/rh/devtoolset-8/root/usr/bin/c++" "-DCMAKE_ASM_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64" "-DCMAKE_ASM_COMPILER=/opt/rh/devtoolset-8/root/usr/bin/cc" "-DCMAKE_BUILD_TYPE=Release" "-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON"
  Running with debug output on.
  -- Enabling RTTI in Debug builds only (default)
     Called from: [2]	/root/.cargo/git/checkouts/rust-rocksdb-a9a28e74c6ead8ef/b2cd425/librocksdb_sys/libtitan_sys/titan/cmake/rocksdb_flags.cmake
                  [1]	/root/.cargo/git/checkouts/rust-rocksdb-a9a28e74c6ead8ef/b2cd425/librocksdb_sys/libtitan_sys/titan/CMakeLists.txt
  -- Configuring done

CMake Error at CMakeLists.txt:168 (find_package):
    By not providing "Findgflags.cmake" in CMAKE_MODULE_PATH this project has
    asked CMake to find a package configuration file provided by "gflags", but
    CMake did not find one.

    Could not find a package configuration file provided by "gflags" with any
    of the following names:

      gflagsConfig.cmake
      gflags-config.cmake

    Add the installation prefix of "gflags" to CMAKE_PREFIX_PATH or set
    "gflags_DIR" to a directory containing one of the above files.  If "gflags"
    provides a separate development package or SDK, be sure it has been
    installed.

     Called from: [1]	/root/.cargo/git/checkouts/rust-rocksdb-a9a28e74c6ead8ef/b2cd425/librocksdb_sys/libtitan_sys/titan/CMakeLists.txt
  thread 'main' panicked at '
  command did not execute successfully, got: exit status: 1

  build script failed, must exit now', /root/.cargo/git/checkouts/cmake-rs-1b5303aca42ae56b/00e6b22/src/lib.rs:1054:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
| username: ljluestc | Original post link

Based on the error message, it indicates that the package named “gflags” cannot be found. It is recommended to troubleshoot the issue by following these steps:

  1. Check if the gflags package is already installed. If not, please use the system package manager to install gflags.
  2. If the gflags package is already installed, check if it is located in the system’s default path. If not, set gflags_DIR to the path containing “gflagsConfig.cmake” or “gflags-config.cmake”.
  3. If the issue persists, check the relevant code in the CMakeLists.txt file to ensure that the “gflags” dependency is correctly included.
  4. If the problem still cannot be resolved, try cleaning and rebuilding the project, as it might be caused by cache issues during the build process. You can use the following command to clean the cache:
cargo clean

Then rebuild:

cargo build