旧项目Makefile 迁移CMake的一种方法:include Makefile

有些c++旧项目用Makefile,要迁移CMake的时候非常痛苦,有些像static pattern的语法和make自带命令 cmake要重写一套非常的麻烦。

因此这里用trick的方法实现了一种include makefile的方法。

 

# trick to include raw makefile
FILE(WRITE ${CMAKE_BINARY_DIR}/.cmake_dummy.c "int main(void){return 0;}\n")

function(INCLUDE_MAKEFILE tg mkfile)
  STRING(CONCAT OTPT ${tg} .out)
  ADD_CUSTOM_COMMAND(
      OUTPUT ${OTPT}
      COMMAND ${CMAKE_COMMAND} -E touch ${OTPT}
      COMMAND ${CMAKE_COMMAND} -E echo
      'include ${mkfile}'
      >> ${CMAKE_SOURCE_DIR}/Makefile
  )
  ADD_EXECUTABLE(${tg} ${CMAKE_BINARY_DIR}/.cmake_dummy.c ${OTPT})
endfunction()

# remember to execute `make ${target}` first to active makefile including
# for example : make rocksdbjni
INCLUDE_MAKEFILE(rocksdbjni ${CMAKE_SOURCE_DIR}/rocksdbjni.mk)

 

大致就是创建一个target用于执行将 include makefile 的语句追加到Makefile 。比较麻烦的是目前 include要手动激活,比如上述示例,cmake . 完后需要 make rocksdbjni 来激活这个target对应的include。当然实在不行也可以考虑加一个execute_process到最后触发一个后台延迟去代码激活的方法来省去还要make target这一步。。

 

posted @ 2018-03-29 17:15  Lhfcws  阅读(2223)  评论(0编辑  收藏  举报