如何在每次cmake调用中仅打印一次消息? [英] How to print a message exactly once per cmake invocation?

查看:145
本文介绍了如何在每次cmake调用中仅打印一次消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要在使用时使软件包foobar打印到

Wanting to cause a package foobar to print where it was found, when using

find_package(foobar CONFIG)

我正在使用

find_package_message(foobar
  "Found foobar: ${info} (version ${foobar_VERSION})"
  "[${info}][${foobar_VERSION}]"
)

使用 find_package_message 的想法只有
仅打印一次此消息。

The idea of using find_package_message is to only print this message once.

但是,我想每次从头开始执行cmake时都打印一次。
我只想避免在同一轮cmake中重复。

However, I want to print it every time cmake is run from the start. I only want to avoid duplicates during the same run of cmake.

find_package_message 将变量存储在缓存( FIND_PACKAGE_MESSAGE_DETAILS_foobar
,其中包含上述第三个参数的值( [$ {info}] [$ {foobar_VERSION}] )和
在该变量不存在或未更改时再次打印消息。

find_package_message stores a variable in the cache (FIND_PACKAGE_MESSAGE_DETAILS_foobar) containing the value of the above third argument ("[${info}][${foobar_VERSION}]") and prints the message again when that variable doesn't exist or changed.

因此,运行cmake的结果第二次是什么都不打印: FIND_PACKAGE_MESSAGE_DETAILS_foobar 在缓存中已经存在并且没有更改。

So, the result of running cmake a second time is that nothing is printed: FIND_PACKAGE_MESSAGE_DETAILS_foobar already exists in the cache and didn't change.

如何我可以解决这个问题,以便在个新的cmake调用后打印一条消息吗?

How can I fix this to print a message once every new invocation of cmake?

推荐答案

功能 find_package_message 用于将消息打印一次,直到更改详细信息为止。要实现不同的语义-每次 cmake 调用一次打印一条消息-使用此功能有点道理,但实现自己的

Function find_package_message is intended for print the message once until "details" are changed. For achieve different semantic - print message once per cmake invocation - there are a little sense to use this function but implement your own one.

为了区分第一个函数调用和其他函数,可以检查是否定义了GLOBAL 属性

For differentiate the first function invocation from further ones one may check whether GLOBAL property is defined:

function(print_message_once name message)
  # Name of the custom GLOBAL property to check
  set(pname PRINT_MESSAGE_ONCE_DUMMY_${name})
  get_property(prop_defined GLOBAL PROPERTY ${pname} DEFINED)
  if (NOT prop_defined)
    message(STATUS "${message}")
    # Define a property so next time it will exist
    define_property(GLOBAL PROPERTY ${pname} BRIEF_DOCS "${name}" FULL_DOCS "${name}")
  endif()
endfunction()

注意,此函数不再需要 details 参数。在单个 cmake 调用期间,不太可能会在第一次找到包后清除缓存并使用不同的参数执行第二次搜索。

Note, this function is no longer requires details argument. It is very unlikely that during a single cmake invocation one will clear the cache after the first finding the package and performs second search with different parameters.

或者,也可以检查功能的存在,而不是属性检查:

Alternatively, instead of property check, one may check existence of the function:

function(print_message_once name message)
  # Name of the custom function to check
  set(fname _check_first_dummy_${name})
  if (NOT COMMAND ${fname})
    message(STATUS "${message}")
    # Define a function so next time it will exist
    function(${fname})
    endfunction()
  endif()
endfunction()

函数用法 print_message_once (上面使用2种方式中的任何一种定义)与 find_package_message 兼容是

Usage of function print_message_once (defined above using any of 2 ways) "compatible" with find_package_message is

if(NOT foobar_FIND_QUIETLY)
  print_message_once(foobar
    "Found foobar: ${info} (version ${foobar_VERSION})"
  )
endif()

如果需要,请检查 XXX_FIND_QUIETLY 变量(反映了 find_package()调用的QUIET选项)可以合并到 print_message_once 函数本身。

If desired, checking for XXX_FIND_QUIETLY variable (which reflects QUIET option of find_package() call) could be incorporated into print_message_once function itself.

这篇关于如何在每次cmake调用中仅打印一次消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆