2

我有一个存储库在根目录中没有其 CMakeLists 的问题,即https://github.com/lz4/lz4 CMakeLists.txt 位于子文件夹 contrib/cmake_unofficial 中。

我已经检查了关于 SO 的类似问题(Is it possible to have cmake build file (CMakeLists.txt) not in root in CLioncmake - CMakeLists.txt is not in root folder(但包含在源代码中)),但它们只提供替代方案,而不是适用于我的情况的解决方案。

这是我想出的cmake模块:

if(ENABLE_LZ4)
    message(STATUS "Using LZ4.")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DENABLE_LZ4")

    # Enable ExternalProject CMake module
    include(ExternalProject)

    set(LZ4_SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/lz4)
    set(LZ4_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
    # Download and install lz4
    ExternalProject_Add(
        lz4
        GIT_REPOSITORY https://github.com/lz4/lz4.git
        GIT_TAG dev
        SOURCE_DIR ${LZ4_SOURCE_DIR}
        BINARY_DIR ${LZ4_BINARY_DIR}
        INSTALL_COMMAND ""
        CMAKE_ARGS
            ${LZ4_SOURCE_DIR}/contrib/cmake_unofficial
            -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
    )

    # Get lz4 source and binary directories from CMake project
    ExternalProject_Get_Property(lz4 source_dir binary_dir)

    # Create a liblz4 target to be used as a dependency by the program
    add_library(liblz4 IMPORTED SHARED GLOBAL)
    add_dependencies(liblz4 lz4)

    include_directories(
        ${LZ4_SOURCE_DIR}/lib
    )
    set(LZ4_LIB ${LZ4_BINARY_DIR}/liblz4.so)
else()
    message(STATUS "Not using LZ4.")
    set(LZ4_LIB "")
endif()

这里是完整的错误输出:

[  0%] Performing update step for 'lz4'
Current branch dev is up to date.
[  1%] Performing configure step for 'lz4'
CMake Error: The source directory "/****/build/lz4" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
CMakeFiles/lz4.dir/build.make:105: recipe for target 'lz4-prefix/src/lz4-stamp/lz4-configure' failed
make[2]: *** [lz4-prefix/src/lz4-stamp/lz4-configure] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/lz4.dir/all' failed
make[1]: *** [CMakeFiles/lz4.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2

我尝试将路径“contrib/cmake_unofficial”添加到 CMAKE_ARGS 变量(如上面的模块中所示),但它不起作用(似乎被忽略了?)。

我还尝试在构建开始之前使用 PATCH_COMMAND 将 CMakeLists.txt 复制到根目录,但是文件的相对路径变得混乱。换句话说,我需要调用 cmake 命令来构建库:cmake contrib/cmake_unofficial。

我也尝试为此使用 CONFIGURE_COMMAND ,但由于某种原因(即使路径正确)不断收到文件未找到错误。该模块还有其他一些问题,但我只对非根 CMakeLists 感兴趣。

提前致谢!

4

2 回答 2

2

ExternalProject分离下载目录:

  • DOWNLOAD_DIR-执行下载步骤的目录
  • SOURCE_DIR-执行配置步骤时用作源的目录

git用于提取项目时,请注意从下载目录git clone调用它,它会在其中创建带有项目源的新目录。

set(LZ4_DOWNLOAD_DIR ${CMAKE_CURRENT_BINARY_DIR}/lz4)
# Set a source dir based on the download one.
set(LZ4_SOURCE_DIR ${LZ4_DOWNLOAD_DIR}/lz4/contrib/cmake_unofficial)
set(LZ4_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
# Download and install lz4
ExternalProject_Add(
    lz4
    DOWNLOAD_DIR ${LZ4_DOWNLOAD_DIR} # Set download directory explicitely
    GIT_REPOSITORY https://github.com/lz4/lz4.git
    GIT_TAG dev
    SOURCE_DIR ${LZ4_SOURCE_DIR}
    BINARY_DIR ${LZ4_BINARY_DIR}
    ...
)
于 2018-11-12T14:55:24.767 回答
1

可能不是正确的方法,但这似乎对我有用。我用CONFIGURE_COMMAND在正确的目录上调用 cmake 。然后使用BUILD_COMMAND调用 make 所以本质上,它分解为:

ExternalProject_Add(
    lz4
    GIT_REPOSITORY https://github.com/lz4/lz4.git
    GIT_TAG dev
    SOURCE_DIR ${LZ4_SOURCE_DIR}
    BINARY_DIR ${LZ4_BINARY_DIR}
    CONFIGURE_COMMAND cmake ${LZ4_SOURCE_DIR}/contrib/cmake_unofficial
    BUILD_COMMAND make
    INSTALL_COMMAND ""
    CMAKE_ARGS
        -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
)
于 2018-11-12T12:52:19.023 回答