cmake:FindLIBCONFIG.cmake查詢libconfig的指令碼find_package指令碼
阿新 • • 發佈:2018-12-15
libconfig 是一個用於讀取,操作和編寫結構化配置檔案的庫。cmake並沒有為libconfig提供find_package指令碼。所以自己擼一個,實現跨平臺的查詢libconfig庫
FindLIBCONFIG.cmake
# - Find libconfig
# - This module determines the libconfig library of the system
# The following variables are set if the library found:
# LIBCONFIG_FOUND - If false do nnt try to use libconfig.
# LIBCONFIG_INCLUDE_DIRS - where to find the headfile of library.
# LIBCONFIG_LIBRARY_DIRS - where to find the libconfig library.
# LIBCONFIG_LIBRARIES, the library file name needed to use libconfig.
# LIBCONFIG_LIBRARY - the library needed to use libconfig.
# imported target
# libconfig::libconfig
if(LIBCONFIG_FOUND)
return()
endif()
if (WIN32)
# windows下使用CONFIG模式呼叫find_package查詢
find_package(LIBCONFIG CONFIG)
else ()
# linux下呼叫pkg_check_modules 查詢
include(FindPkgConfig)
unset(_verexp)
if(LIBCONFIG_FIND_VERSION)
if(LIBCONFIG_FIND_VERSION_EXACT)
set(_verexp "=${LIBCONFIG_FIND_VERSION} ")
else()
set(_verexp ">=${LIBCONFIG_FIND_VERSION}")
endif()
endif()
pkg_check_modules (LIBCONFIG libconfig${_verexp})
endif()
# handle the QUIETLY and REQUIRED arguments and set LIBCONFIG_FOUND to TRUE if
# all listed variables are TRUE
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LIBCONFIG DEFAULT_MSG LIBCONFIG_LIBRARIES LIBCONFIG_INCLUDE_DIRS)
if(LIBCONFIG_FOUND)
if(NOT LIBCONFIG_FIND_QUIETLY)
message(STATUS " -I: ${LIBCONFIG_INCLUDE_DIRS}")
message(STATUS " -L: ${LIBCONFIG_LIBRARY_DIRS}")
message(STATUS " -l: ${LIBCONFIG_LIBRARIES}")
endif()
find_library (LIBCONFIG_LIBRARY NAMES ${LIBCONFIG_LIBRARIES} PATHS ${LIBCONFIG_LIBRARY_DIRS})
# 建立 imported target
if (NOT TARGET libconfig::libconfig)
add_library(libconfig::libconfig INTERFACE IMPORTED)
unset(_flags)
if(LIBCONFIG_CFLAGS)
set(_flags ${LIBCONFIG_CFLAGS})
endif()
if(LIBCONFIG_LDFLAGS)
set(_flags LIBCONFIG -Wl,${LIBCONFIG_LDFLAGS})
endif()
if(_flags)
set_target_properties(libconfig::libconfig PROPERTIES
INTERFACE_COMPILE_OPTIONS "${_flags}")
endif()
set_target_properties(libconfig::libconfig PROPERTIES
INTERFACE_COMPILE_OPTIONS "${LIBCONFIG_CFLAGS}"
INTERFACE_INCLUDE_DIRECTORIES "${LIBCONFIG_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "-L${LIBCONFIG_LIBRARY_DIRS} -l${LIBCONFIG_LIBRARIES }"
)
if(NOT LIBCONFIG_FIND_QUIETLY)
message(STATUS "IMPORTED TARGET: libconfig::libconfig,link libraies ${_link_libs}")
endif()
endif ()
endif(LIBCONFIG_FOUND)
呼叫示例
# 新增FindLIBCONFIG.cmake所在的路徑到CMAKE_MODULE_PATH
list (APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/Modules")
find_package (LIBCONFIG 1.7.2 MODULE QUIET REQUIRED )