cmake.js配合conan的使用

原创
2020/12/18 13:58
阅读数 1.1K

最近公司有一个项目需要使用electron + cpp。cpp部分打算使用cmake + conan的管理方式。但有一个问题,全网都没有cmake.js + conan使用的例子。突然灵光一闪,conan能不能生成的到指定目录, 尝试一下居然还真可以。基于此,我修改了cmake文件。

cmake_minimum_required(VERSION 3.18)
project (test)
include_directories(${CMAKE_JS_INC})
file(GLOB_RECURSE SOURCE_FILES  "src/*.cpp" "src/*.cc" "src/*.c"  )

#==================================================================
include(${CMAKE_BINARY_DIR}/../conan/conanbuildinfo.cmake)
conan_basic_setup()
#==================================================================

add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")

# Include N-API wrappers
#==================================================================
execute_process(COMMAND node -p "require('node-addon-api').include"
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        OUTPUT_VARIABLE NODE_ADDON_API_DIR
        )
string(REPLACE "\n" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
string(REPLACE "\"" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE ${NODE_ADDON_API_DIR})
#==================================================================

target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB}  ${CONAN_LIBS})

为了方便,我修改package.json。当执行npm install 的时候自动conan install

{
  "name": "testnapi",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "conan": "conan install -if conan .",
    "install": "npm run conan && cmake-js compile",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cmake-js": "^6.1.0",
    "node-addon-api": "^3.1.0"
  }
}

另外,还有一种方法。直接将conan命令加入到cmake里面。

execute_process(COMMAND conan install -if conan . WORKING_DIRECTORY  ${CMAKE_SOURCE_DIR}) 
include(${CMAKE_SOURCE_DIR}/conan/conanbuildinfo.cmake)
conan_basic_setup()
展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部