11-cmake语法-函数和宏的定义
cmake语法不仅仅可以适用于 CMakeLists.txt,也适用于 xxx.cmake 文档。
在 OpenCV 的 CMakeLists.txt 中,很多 ocv_XXX() 的函数,都是定义在相应的 xxx.cmake 中。
比如:L116
include(cmake/OpenCVUtils.cmake) # must go before the project command # @ 解说: # @ ocv_update 函数定义在 cmake/OpenCVUtils.cmake 这个组件中,可以用Notepad++打开看看。 ocv_update(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Configs" FORCE) if(DEFINED CMAKE_BUILD_TYPE) set_property( CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${CMAKE_CONFIGURATION_TYPES} ) endif()
函数定义:
function(函数名 参数名) ... endfunction()
例子:cmake/OpenCVMinDepVersions.cmake中
function(ocv_cmake_eval var_name) if(DEFINED ${var_name}) file(WRITE "${CMAKE_BINARY_DIR}/CMakeCommand-${var_name}.cmake" ${${var_name}}) include("${CMAKE_BINARY_DIR}/CMakeCommand-${var_name}.cmake") endif() if(";${ARGN};" MATCHES ";ONCE;") unset(${var_name} CACHE) endif() endfunction()
宏的定义:
macro(宏名 参数) ... endmacro()
例子:cmake/OpenCVMinDepVersions.cmake中
macro(ocv_cmake_configure file_name var_name) configure_file(${file_name} "${CMAKE_BINARY_DIR}/CMakeConfig-${var_name}.cmake" ${ARGN}) file(READ "${CMAKE_BINARY_DIR}/CMakeConfig-${var_name}.cmake" ${var_name}) endmacro()
函数和宏不支持return,需要传参出去,可以通过形参输入传出。
macro(ocv_xxx return_hello_world) set(return_hello_world "Hello_World") endmacro()
参考:
https://www.jianshu.com/p/44ba033c262a