CMake教程——如何编写FindXXX.cmake
2020-05-23 本文已影响0人
生活简单些
先解答几个关于FindXXX.cmake的常见问题
问:当第三方库并没有采用CMake组织代码并且仅仅提供了编译好当birary库时候为了让find_package(xxx)找到它怎么办?
答:得自己写FindXXX.cmake
问:FindXXX.cmake文件放哪里才能让find_package()识别到呢?
答:将写好的FindXXX.cmake放在cmake软件的Modules目录下即可。
问:cmake软件的Modules目录在哪里?
答:如果是linux下通过apt安装的cmake,则Modules目录一般为/usr/share/cmake-3.xx/Modules
,如果你的cmake是基于源码编译安装的,那么Modules目录在/usr/local/cmake
里。
如果希望所有的cmake配置跟随项目源码走,也可以将FindXXX.cmake放在指定的目录,比如当前项目的cmake
目录下,随后需要在项目的CMakeList.txt里告知FindXXX.cmake所在目录,即:list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
下面进入正题——如何编写FindXXX.cmake
1. 以单个头文件和单个库文件为例
# FindLogger
# --------
#
# Find the Logger libraries
#
# Result Variables
# ^^^^^^^^^^^^^^^^
#
# The following variables will be defined:
#
# ``Logger_FOUND`` True if Logger found on the local system
#
# ``Logger_INCLUDE_DIRS`` Location of Logger header files
#
# ``Logger_LIBRARY_DIRS`` Location of Logger libraries
#
# ``Logger_LIBRARIES`` List of the Logger libraries found
#
include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
include(${CMAKE_ROOT}/Modules/SelectLibraryConfigurations.cmake)
include(${CMAKE_ROOT}/Modules/CMakeFindDependencyMacro.cmake)
#include(FindPackageHandleStandardArgs)
find_package(PkgConfig)
set(Baumer_FOUND TRUR)
set(Logger_VERSION "1.0.0")
if (DEFINED ENV{USR_LOCAL})
set(USR_LOCAL "$ENV{USR_LOCAL}" CACHE STRING "")
else()
set(USR_LOCAL /usr/local CACHE STRING "")
endif()
set(Logger_INCLUDE_DIRS ${USR_LOCAL}/include/logger.h CACHE STRING "")
set(Logger_LIBRARY_DIRS ${USR_LOCAL}/lib CACHE STRING "")
set(Logger_LIBRARIES ${USR_LOCAL}/lib/logger.so CACHE STRING "")
find_package_handle_standard_args(Logger REQUIRED_VARS Logger_LIBRARIES Logger_LIBRARY_DIRS Logger_INCLUDE_DIRS)
mark_as_advanced(Logger_INCLUDE_DIR Logger_LIBRARY)
2. 以多个头文件和多个库文件为例
# FindBaumer
# --------
#
# Find the Baumer libraries
#
# Result Variables
# ^^^^^^^^^^^^^^^^
#
# The following variables will be defined:
#
# ``Baumer_FOUND`` True if Baumer found on the local system
#
# ``Baumer_INCLUDE_DIRS`` Location of Baumer header files
#
# ``Baumer_LIBRARY_DIRS`` Location of Baumer libraries
#
# ``Baumer_LIBRARIES`` List of the Baumer libraries found
#
include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
include(${CMAKE_ROOT}/Modules/SelectLibraryConfigurations.cmake)
include(${CMAKE_ROOT}/Modules/CMakeFindDependencyMacro.cmake)
find_package(PkgConfig)
set(Baumer_FOUND TRUR)
set(Baumer_VERSION "1.0.0")
if (DEFINED ENV{VCPKG_ROOT} AND DEFINED ENV{VCPKG_DEFAULT_TRIPLET})
set(Baumer_ROOT_DIR "$ENV{VCPKG_ROOT}/installed/$ENV{VCPKG_DEFAULT_TRIPLET}" CACHE STRING "")
endif()
if (DEFINED ENV{CMAKE_PREFIX_PATH})
set(Baumer_ROOT_DIR $ENV{CMAKE_PREFIX_PATH})
endif()
# ---------------- find header files ----------------
# define macro func to find headers
macro(Baumer_FIND_INCLUDE varname foldername headername)
if(NOT Baumer_${varname}_INCLUDE_DIR)
find_path(
Baumer_${varname}_INCLUDE_DIR
NAMES ${foldername}/${headername}
PATHS ${Baumer_ROOT_DIR}/include /usr/local/include)
list(APPEND Baumer_INCLUDE_DIRS ${Baumer_${varname}_INCLUDE_DIR})
endif()
endmacro(Baumer_FIND_INCLUDE)
# call macro func to find headers
Baumer_FIND_INCLUDE(bgapi2_ext bgapi2_ext bgapi2_ext.h)
Baumer_FIND_INCLUDE(bgapi2_ext_addons bgapi2_ext bgapi2_ext_addons.h)
Baumer_FIND_INCLUDE(bgapi2_ext_sc bgapi2_ext_sc bgapi2_ext_sc.h)
Baumer_FIND_INCLUDE(bgapi2_def bgapi2_genicam bgapi2_def.h)
Baumer_FIND_INCLUDE(bgapi2_featurenames bgapi2_genicam bgapi2_featurenames.h)
Baumer_FIND_INCLUDE(bgapi2_genicam bgapi2_genicam bgapi2_genicam.hpp)
if (UNIX)
Baumer_FIND_INCLUDE(bgapi2_genicam bgapi2_genicam bgapi2_genicam.h)
Baumer_FIND_INCLUDE(bgapi2_types bgapi2_genicam bgapi2_types.h)
endif()
set(Baumer_INCLUDE_DIRS ${Baumer_INCLUDE_DIRS} CACHE STRING "")
message("Baumer_INCLUDE_DIRS: " ${Baumer_INCLUDE_DIRS})
# ---------------- find library files----------------
# define macro func to find libs
macro(Baumer_FIND_LIBRARY libname)
if(NOT Baumer_${varname}_LIBRARY)
find_library(
Baumer_${libname}_LIBRARY
NAMES ${libname}
PATHS ${Baumer_ROOT_DIR}/lib /usr/local/lib)
get_filename_component(Baumer_${libname}_LIBRARY_DIR ${Baumer_${libname}_LIBRARY} DIRECTORY)
list(APPEND Baumer_LIBRARY_DIRS ${Baumer_${libname}_LIBRARY_DIR})
list(APPEND Baumer_LIBRARIES ${Baumer_${libname}_LIBRARY})
endif()
endmacro(Baumer_FIND_LIBRARY)
# call macro func to find libs
if(WIN32)
Baumer_FIND_LIBRARY(bgapi2_ext.lib)
Baumer_FIND_LIBRARY(bgapi2_ext_sc.lib)
Baumer_FIND_LIBRARY(bgapi2_genicam.lib)
elseif(UNIX)
Baumer_FIND_LIBRARY(libbgapi2_ext.so)
Baumer_FIND_LIBRARY(libbgapi2_ext_sc.so)
Baumer_FIND_LIBRARY(libbgapi2_genicam.so)
Baumer_FIND_LIBRARY(libbgapi2_gige.so)
Baumer_FIND_LIBRARY(libbgapi2_img.so)
Baumer_FIND_LIBRARY(libbgapi2_usb.so)
Baumer_FIND_LIBRARY(libGCBase_gcc54_v3_2.so)
Baumer_FIND_LIBRARY(libGenApi_gcc54_v3_2.so)
Baumer_FIND_LIBRARY(libLog_gcc54_v3_2.so)
Baumer_FIND_LIBRARY(liblog4cpp_gcc54_v3_2.so)
Baumer_FIND_LIBRARY(libMathParser_gcc54_v3_2.so)
Baumer_FIND_LIBRARY(libNodeMapData_gcc54_v3_2.so)
Baumer_FIND_LIBRARY(libXmlParser_gcc54_v3_2.so)
endif(WIN32)
message("Baumer_LIBRARY_DIRS: " ${Baumer_LIBRARY_DIRS})
message("Baumer_LIBRARIES: " ${Baumer_LIBRARIES})
set(Baumer_LIBRARY_DIRS ${Baumer_LIBRARY_DIRS} CACHE STRING "")
set(Baumer_LIBRARIES ${Baumer_LIBRARIES} CACHE STRING "")
find_package_handle_standard_args(Baumer REQUIRED_VARS Baumer_LIBRARIES Baumer_LIBRARY_DIRS Baumer_INCLUDE_DIRS)
mark_as_advanced(Baumer_INCLUDE_DIRS Baumer_LIBRARIES)