CMake ExternalProject_Add() - 使用自定义的CMakeLists.txt构建

时间:2011-09-25 16:09:24

标签: cmake external-project

我正在构建lua作为外部项目,我想使用我自己的CMakeLists.txt而不是捆绑的Makefile。这就是我在我的主要CMakeLists.txt中所拥有的:

include(ExternalProject)
set(lua_RELEASE 5.1.4)
ExternalProject_Add(
    lua-${lua_RELEASE}
    URL http://www.lua.org/ftp/lua-${lua_RELEASE}.tar.gz
    DOWNLOAD_DIR ${CMAKE_CURRENT_BINARY_DIR}/download/lua-${lua_RELEASE}
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/lua/lua-${lua_RELEASE}
    BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/build/lua-${lua_RELEASE}
    UPDATE_COMMAND ""
    PATCH_COMMAND ""
    INSTALL_COMMAND ""
)

要使BUILD步骤起作用,SOURCE_DIR中必须有一个CMakeLists.txt。我现在在SOURCE_DIR中有这个CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

project(lua)

set(lua_library
  lapi.c lcode.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c
  lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c
  ltable.c ltm.c lundump.c lvm.c lzio.c
  lauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c loslib.c
  ltablib.c lstrlib.c loadlib.c linit.c
)

foreach(s ${lua_library})
    set(lua_LIBRARY ${lua_LIBRARY} src/${s})
endforeach()

add_definitions(-DLUA_ANSI=1)
add_library(lua STATIC ${lua_LIBRARY})

这有效,但我不满意让lua源文件混乱我的版本控制的CMakeLists.txt。

有没有办法为SOURCE_DIR中的构建步骤指定自定义CMakeLists.txt?

1 个答案:

答案 0 :(得分:16)

我自己想通了。我现在将其用作PATCH_COMMAND

    PATCH_COMMAND ${CMAKE_COMMAND} -E copy
      "${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/lua/CMakeLists.txt" <SOURCE_DIR>/CMakeLists.txt

这允许我在thirdparty/lua中拥有自定义的CMakeLists.txt,并将上游包下载到thirdparty/lua/lua-${lua_RELEASE}。完美!