William
William

Reputation: 4935

Is there a good way to display "clean" text on stdout in CMake

Cmake version 2.8.10.2, OS centos 6.3

We're trying to get a "clean" display of text on stdout from within our cmake files. That is, text just as we intend, without a prefix. So far I've tried these variations

This goes to stderr (surprised me):

MESSAGE("my text")

This goes to stdout but prefixes each line with '-- ':

MESSAGE(STATUS "my text")

This sort of works, but the side effects are weird and make it undesirable for us:

FILE(WRITE /dev/stdout "my text")

The above goes to stdout, but breaks if the output from cmake itself is redirected to a file (cmake > file), although it is OK if you you pipe stdout first (cmake | cat > file) but that's hacky and means we have to tell everyone about the workaround which just isn't going to happen.

Upvotes: 5

Views: 3404

Answers (2)

lujomu
lujomu

Reputation: 371

If you prepend your message with a carriage return, you can simply overwrite the dashes.

string( ASCII 13 CR ) # carriage return character
message( STATUS "${CR}Hello!" )

To make it also work for e.g. empty or single-letter messages, you can explicitly overwrite the dashes with spaces.

function( CleanMessage Message )
    string( ASCII 13 CR ) # carriage return character
    set( ClearLine "${CR}  ${CR}" )
    message( STATUS "${ClearLine}${Message}" )
endfunction()

Upvotes: 0

Fraser
Fraser

Reputation: 78290

You could provide the following function:

function(CleanMessage)
  execute_process(COMMAND ${CMAKE_COMMAND} -E echo "${ARGN}")
endfunction()

and use it like this:

CleanMessage("Clean text")


If you want to push the boat out, you could even extend the built-in message options to include a CLEAN one:

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

and use it like this:

message(STATUS "Incidental information")
message(CLEAN "Clean Incidental information")
message(WARNING "CMake Warning, continue processing")

This is safe to define in your top-level CMakeLists.txt once. However, if it's defined in e.g. a utilities file which could be included more than once, it will lead to infinite recursion. To avoid this, at the start of the utility file in which the function is defined, add:

if(OverloadMessageIncluded)
  return()
endif()
set(OverloadMessageIncluded TRUE)

This is effectively a CMake version of a header guard.

Upvotes: 9

Related Questions