有没有一种在CMake中在stdout上显示"干净"文本的好方法

Wil*_*iam 2 cmake

Cmake版本2.8.10.2,OS centos 6.3

我们正试图从我们的cmake文件中的stdout上"清理"显示文本.也就是说,文本就像我们想要的那样,没有前缀.到目前为止,我已尝试过这些变化

这是stderr(让我感到惊讶):

MESSAGE("my text")
Run Code Online (Sandbox Code Playgroud)

这是stdout,但每行前缀为' - ':

MESSAGE(STATUS "my text")
Run Code Online (Sandbox Code Playgroud)

这种作品,但副作用很奇怪,使我们不受欢迎:

FILE(WRITE /dev/stdout "my text")
Run Code Online (Sandbox Code Playgroud)

以上是stdout,但如果cmake本身的输出被重定向到文件(cmake>文件),则会中断,但如果你先管道stdout(cmake | cat>文件),那就好了,但是这很hacky并且意味着我们必须告诉大家有关不会发生的变通办法.

Fra*_*ser 6

您可以提供以下功能:

function(CleanMessage)
  execute_process(COMMAND ${CMAKE_COMMAND} -E echo "${ARGN}")
endfunction()
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

CleanMessage("Clean text")
Run Code Online (Sandbox Code Playgroud)


如果你想把船推出去,你甚至可以扩展内置message选项,包括CLEAN一个:

function(message MessageType)
  if(${ARGV0} STREQUAL "CLEAN")
    execute_process(COMMAND ${CMAKE_COMMAND} -E echo "${ARGN}")
  else()
    _message(${MessageType} "${ARGN}")
  endif()
endfunction()
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

message(STATUS "Incidental information")
message(CLEAN "Clean Incidental information")
message(WARNING "CMake Warning, continue processing")
Run Code Online (Sandbox Code Playgroud)

在您的顶级CMakeLists.txt中定义一次是安全的.但是,如果它在例如可以包含多次的实用程序文件中定义,则会导致无限递归.为避免这种情况,在定义函数的实用程序文件的开头,添加:

if(OverloadMessageIncluded)
  return()
endif()
set(OverloadMessageIncluded TRUE)
Run Code Online (Sandbox Code Playgroud)

这实际上是头部防护的CMake版本.