CMake中的message命令用于记录消息,其格式如下:
- message([
] "message text" ...) # General messages - message(
"message text" ...) # Reporting checks
1.General messages:在日志中记录指定的消息文本。如果给出了多个消息字符串(message string),则将它们连接成一条消息,字符串之间没有分隔符。
可选的
(1).FATAL_ERROR:CMake Error,停止处理和生成。
(2).SEND_ERROR:CMake Error,继续处理,但跳过生成。
(3).WARNING:CMake Warning,继续处理。
(4).AUTHOR_WARNING:CMake Warning(dev),继续处理。
(5).DEPRECATION:如果分别启用了CMAKE_ERROR_DEPRECATED或CMAKE_WARN_DEPRECATED,则CMake弃用(Deprecation)Error或Warning,否则没有消息。
(6).(none)或NOTICE:重要的消息打印到stderr以引起用户的注意。
(7).STATUS:project用户可能感兴趣的主要信息。理想情况下,这些信息应该简明扼要,不超过一行,但仍然信息丰富。
(8).VERBOSE:针对project用户的详细信息消息。这些消息应提供在大多数情况下不感兴趣的额外详细信息。
(9).DEBUG:构建项目的其他用户通常不会对这些消息感兴趣。
(10).TRACE:低级的细粒度消息,使用此日志级别的消息通常只是临时的,一般在发布项目、打包文件之前被删除。
--log-level命令行选项可用于控制显示哪些消息。若不指定,默认不会显示verbose, debug, trace消息。也可通过CMAKE_MESSAGE_LOG_LEVEL变量设置。
- message("csdn addr:" "https://blog.csdn.net/fengbingchun") # csdn addr:https://blog.csdn.net/fengbingchun
-
- message(SEND_ERROR "wow, send error") # CMake Error at test_message.cmake:10 (message):
- # wow, send error
-
- message(WARNING "wow, warning") # CMake Warning at test_message.cmake:13 (message):
- # wow, warning
-
- message(AUTHOR_WARNING "wow, author warning") # CMake Warning (dev) at test_message.cmake:16 (message):
- # wow, author warning
-
- message(DEPRECATION "wow, deprecation") # CMake Deprecation Warning at test_message.cmake:19 (message):
- # wow, deprecation
-
- message(NOTICE "github addr:https://github.com/fengbingchun") # github addr:https://github.com/fengbingchun
- message("github addr:https://github.com/fengbingchun") # github addr:https://github.com/fengbingchun
-
- message(STATUS "wow, status") # -- wow, status
-
- # --log-level命令行选项可用于控制显示哪些消息,如:
- # --log-level=trace,会显示verbose,debug,trace
- # --log-level=verbose,仅会显示verbose
- message(VERBOSE "wow, verbose") # -- wow, verbose
- message(DEBUG "wow, debug")
- message(TRACE "wow, trace")
-
- message(FATAL_ERROR "wow, fatal error") # CMake Error at test_message.cmake:34 (message):
- # wow, fatal error
2.Reporting checks:CMake输出中的一个常见模式是一条消息表明某种检查的开始,然后是另一条消息报告检查的结果。这可以使用message命令的CHECK_...关键字形式可以更强大、更方便地表达这一点。其中
(1).CHECK_START:记录关于将要执行的检查的简明信息。
(2).CHECK_PASS:记录检查的成功结果。
(3).CHECK_FAIL:记录检查的不成功结果。
检查消息(check message)始终以STATUS日志级别报告。检查可能是嵌套的,每个CHECK_START都应该有一个匹配的CHECK_PASS或CHECK_FAIL。如果需要,CMAKE_MESSAGE_INDENT变量也可用于向嵌套检查添加缩进
- message(CHECK_START "Finding my things")
- list(APPEND CMAKE_MESSAGE_INDENT " ")
- unset(missingComponents)
-
- message(CHECK_START "Finding partA")
- # ... do check, assume we find A
- message(CHECK_PASS "found")
-
- message(CHECK_START "Finding partB")
- # ... do check, assume we don't find B
- list(APPEND missingComponents B)
- message(CHECK_FAIL "not found")
-
- list(POP_BACK CMAKE_MESSAGE_INDENT)
- if(missingComponents)
- message(CHECK_FAIL "missing components: ${missingComponents}")
- else()
- message(CHECK_PASS "all components found")
- endif()
执行上述测试代码需要3个文件:build.sh, CMakeLists.txt, test_message.cmake
build.sh内容如下:
- #! /bin/bash
-
- # supported input parameters(cmake commands)
- params=(function macro cmake_parse_arguments \
- find_library find_path find_file find_program find_package \
- cmake_policy cmake_minimum_required project include \
- string list set foreach message option if)
-
- usage()
- {
- echo "Error: $0 needs to have an input parameter"
-
- echo "supported input parameters:"
- for param in ${params[@]}; do
- echo " $0 ${param}"
- done
-
- exit -1
- }
-
- if [ $# != 1 ]; then
- usage
- fi
-
- flag=0
- for param in ${params[@]}; do
- if [ $1 == ${param} ]; then
- flag=1
- break
- fi
- done
-
- if [ ${flag} == 0 ]; then
- echo "Error: parameter \"$1\" is not supported"
- usage
- exit -1
- fi
-
- if [[ ! -d "build" ]]; then
- mkdir build
- cd build
- else
- cd build
- fi
-
- echo "==== test $1 ===="
-
- # test_set.cmake: cmake -DTEST_CMAKE_FEATURE=$1 --log-level=verbose ..
- # test_option.cmake: cmake -DTEST_CMAKE_FEATURE=$1 -DBUILD_PYTORCH=ON ..
- cmake -DTEST_CMAKE_FEATURE=$1 ..
- # It can be executed directly on the terminal, no need to execute build.sh, for example: cmake -P test_set.cmake
CMakeLists.txt内容如下:
- cmake_minimum_required(VERSION 3.22)
- project(cmake_feature_usage)
-
- message("#### current cmake version: ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}")
- include(test_${TEST_CMAKE_FEATURE}.cmake)
- message("==== test finish ====")
test_message.cmake:为上面的所有示例代码
可能的执行结果如下图所示: