我有一个存储库在根目录中没有其 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 CLion,cmake - 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 感兴趣。
提前致谢!