CMakeLists.txt不在根目录中,无法自动构建

时间:2018-11-12 11:14:56

标签: cmake lz4

我遇到一个问题,即存储库的根目录中没有其CMakeList,即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 (but is included in source))上检查了类似的问题,但是它们仅提供替代方案,而不能提供适合我情况的解决方案。

这里是我想出的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进行此操作,但是由于某种原因(即使路径正确),仍然出现找不到文件的错误。 该模块也存在其他一些问题,但是我只对非根CMakeList感兴趣。

谢谢!

2 个答案:

答案 0 :(得分: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}
    ...
)

答案 1 :(得分: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}
)