cmake学习笔记(七)编写自己的xxxConfig.cmake
1. onnxruntimeConfig.cmake
因为项目是统一用cmake管理的,因此对于没有xxxConfig.cmake文件的库需要自己编写。
比如:ONNXRUNTIMEConfig.cmake
find_path(ONNXRUNTIME_INCLUDE_DIR NAMES onnxruntime_cxx_api.h PATHS "D:/ProgramFiles/onnxruntime-win-x64-1.8.1/include")
find_library(ONNXRUNTIME_LIBRARY NAMES onnxruntime.lib PATHS "D:/ProgramFiles/onnxruntime-win-x64-1.8.1/lib")
set(ONNXRUNTIME_FOUND TRUE)
set(ONNXRUNTIME_INCLUDE_DIRS ${ONNXRUNTIME_INCLUDE_DIR})
set(ONNXRUNTIME_LIBRARY ${ONNXRUNTIME_LIBRARY})
写成这样就可以使用了。当然路径那里应该使用相对路径才 具有通用性,后面会改正。
参考:编写自定义cmake配置文件FindXXX.cmake或者xxx-config.cmake | cmake with user defined entry
2. 进阶版onnxruntimeConfig.cmake
onnxruntimeConfig.cmake 该文件放置到下载好的onnxruntime库的根目录下,然后指定onnxruntime_DIR到该根目录。
# Custom cmake config file by jcarius to enable find_package(onnxruntime) without modifying LIBRARY_PATH and LD_LIBRARY_PATH
#
# This will define the following variables:
# onnxruntime_FOUND -- True if the system has the onnxruntime library
# onnxruntime_INCLUDE_DIRS -- The include directories for onnxruntime
# onnxruntime_LIBRARIES -- Libraries to link against
# onnxruntime_CXX_FLAGS -- Additional (required) compiler flags
include(FindPackageHandleStandardArgs)
set(ONNXRUNTIME_FOUND TRUE) # auto
message("ONNXRUNTIME_FOUND " ${ONNXRUNTIME_FOUND})
# Assume we are in <install-prefix>/share/cmake/onnxruntime/onnxruntimeConfig.cmake
get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
message("CMAKE_CURRENT_LIST_DIR " ${CMAKE_CURRENT_LIST_DIR})
get_filename_component(onnxruntime_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_DIR}/" ABSOLUTE)
set(onnxruntime_INCLUDE_DIRS ${onnxruntime_INSTALL_PREFIX}/include)
set(onnxruntime_LIBRARIES onnxruntime)
set(onnxruntime_CXX_FLAGS "") # no flags needed
find_library(onnxruntime_LIBRARY onnxruntime
PATHS "${onnxruntime_INSTALL_PREFIX}/lib"
)
message("onnxruntime_INCLUDE_DIRS " ${onnxruntime_INCLUDE_DIRS})
message("onnxruntime_LIBRARY " ${onnxruntime_LIBRARY})
add_library(onnxruntime SHARED IMPORTED)
set_property(TARGET onnxruntime PROPERTY IMPORTED_LOCATION "${onnxruntime_LIBRARY}")
set_property(TARGET onnxruntime PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_INCLUDE_DIRS}")
set_property(TARGET onnxruntime PROPERTY INTERFACE_COMPILE_OPTIONS "${onnxruntime_CXX_FLAGS}")
find_package_handle_standard_args(onnxruntime DEFAULT_MSG onnxruntime_LIBRARY onnxruntime_INCLUDE_DIRS)
CMakeLists.txt中的调用:
FIND_PACKAGE(onnxruntime REQUIRED)
message("onnxruntime_INCLUDE_DIRS " ${onnxruntime_INCLUDE_DIRS})
message("onnxruntime_LIBRARY " ${onnxruntime_LIBRARY})