我想在
CMakeLists.txt
中有消息功能,输出彩色文本。也许是一个转义序列。
例如:
message("\x1b[31m;This text must be in red")
这不起作用。我得到:
Syntax error in cmake code at /home/taurus/cmakecolor/CMakeLists.txt:1 when parsing string \x1b[31m;This text must be in red Invalid escape sequence \x
为了扩展@grim的正确答案,您可以通过设置变量来处理各种颜色来使事情变得更方便:
if(NOT WIN32)
string(ASCII 27 Esc)
set(ColourReset "${Esc}[m")
set(ColourBold "${Esc}[1m")
set(Red "${Esc}[31m")
set(Green "${Esc}[32m")
set(Yellow "${Esc}[33m")
set(Blue "${Esc}[34m")
set(Magenta "${Esc}[35m")
set(Cyan "${Esc}[36m")
set(White "${Esc}[37m")
set(BoldRed "${Esc}[1;31m")
set(BoldGreen "${Esc}[1;32m")
set(BoldYellow "${Esc}[1;33m")
set(BoldBlue "${Esc}[1;34m")
set(BoldMagenta "${Esc}[1;35m")
set(BoldCyan "${Esc}[1;36m")
set(BoldWhite "${Esc}[1;37m")
endif()
message("This is normal")
message("${Red}This is Red${ColourReset}")
message("${Green}This is Green${ColourReset}")
message("${Yellow}This is Yellow${ColourReset}")
message("${Blue}This is Blue${ColourReset}")
message("${Magenta}This is Magenta${ColourReset}")
message("${Cyan}This is Cyan${ColourReset}")
message("${White}This is White${ColourReset}")
message("${BoldRed}This is BoldRed${ColourReset}")
message("${BoldGreen}This is BoldGreen${ColourReset}")
message("${BoldYellow}This is BoldYellow${ColourReset}")
message("${BoldBlue}This is BoldBlue${ColourReset}")
message("${BoldMagenta}This is BoldMagenta${ColourReset}")
message("${BoldCyan}This is BoldCyan${ColourReset}")
message("${BoldWhite}This is BoldWhite\n\n${ColourReset}")
如果您真的喜欢把船推出去,您可以用自己的函数替换内置的
message
函数,该函数根据消息类型对输出进行着色:
function(message)
list(GET ARGV 0 MessageType)
if(MessageType STREQUAL FATAL_ERROR OR MessageType STREQUAL SEND_ERROR)
list(REMOVE_AT ARGV 0)
_message(${MessageType} "${BoldRed}${ARGV}${ColourReset}")
elseif(MessageType STREQUAL WARNING)
list(REMOVE_AT ARGV 0)
_message(${MessageType} "${BoldYellow}${ARGV}${ColourReset}")
elseif(MessageType STREQUAL AUTHOR_WARNING)
list(REMOVE_AT ARGV 0)
_message(${MessageType} "${BoldCyan}${ARGV}${ColourReset}")
elseif(MessageType STREQUAL STATUS)
list(REMOVE_AT ARGV 0)
_message(${MessageType} "${Green}${ARGV}${ColourReset}")
else()
_message("${ARGV}")
endif()
endfunction()
message("No colour at all.")
message(STATUS "\"Colour\" is spelled correctly.")
message(AUTHOR_WARNING "\"Color\" is misspelled.")
message(WARNING "Final warning: spell \"color\" correctly.")
message(SEND_ERROR "Game over. It's \"colour\", not \"color\".")
message(FATAL_ERROR "And there's no \"z\" in \"colourise\" either.")
我不能说我建议以这种方式覆盖内置的
message
函数,但话虽如此,我也没有发现这样做有任何重大问题。
一个更简单的解决方案可能只是使用 CMake 的内置功能来发出彩色输出,即这些命令
cmake -E cmake_echo_color --normal hello
cmake -E cmake_echo_color --black hello
cmake -E cmake_echo_color --red hello
cmake -E cmake_echo_color --green hello
cmake -E cmake_echo_color --yellow hello
cmake -E cmake_echo_color --blue hello
cmake -E cmake_echo_color --magenta hello
cmake -E cmake_echo_color --cyan hello
cmake -E cmake_echo_color --white hello
cmake -E cmake_echo_color --red --bold hello
cmake -E cmake_echo_color --red --no-newline hello
在 CMake 中,您可以使用
execute_process()
命令来调用 ${CMAKE_COMMAND}
。您可以编写一个方便的函数来执行此操作。
cmake_echo_color
与 execute_process()
正如@sjm324跑步所指出的
execute_process(COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --red --bold hello)
我的猜测是标准输出没有附加到终端,因此当 CMake 内部调用
isatty()
时,这会失败。
我有两个技巧可以解决这个问题,但如果你真的关心这个问题,你应该联系 CMake 开发人员以获得一个不太脆弱的解决方案
CLICOLOR_FORCE=1
环境变量。execute_process(COMMAND
${CMAKE_COMMAND} -E env CLICOLOR_FORCE=1
${CMAKE_COMMAND} -E cmake_echo_color --red --bold hello
)
这不是一个好主意。如果您将构建的输出记录到文件中 它将包含转义序列,因为你强迫它们成为 总是发出。
不要这样做。 HACK 1 可能更便携。
execute_process(COMMAND
/usr/bin/tty
OUTPUT_VARIABLE TTY_NAME
OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND
${CMAKE_COMMAND} -E cmake_echo_color --red --bold hello
OUTPUT_FILE ${TTY_NAME})
虽然很乏味,但您可以定义一个包含转义字符的变量并在消息字符串中使用它
string(ASCII 27 ESCAPE)
message("${ESCAPE}[34mblue${ESCAPE}[0m")
运行execute_process并将输出分配给VARIBALE,然后使用消息进行打印。
macro ( print_color NAME )
print ( COLOR ${NAME} " ${NAME}" )
endmacro ()
function ( text )
cmake_parse_arguments ( PARSE_ARGV 0 "_TEXT" "BOLD" "COLOR" "" )
set ( _TEXT_OPTIONS -E cmake_echo_color --no-newline )
if ( _TEXT_COLOR )
string ( TOLOWER "${_TEXT_COLOR}" _TEXT_COLOR_LOWER )
if ( NOT ${_TEXT_COLOR_LOWER} MATCHES "^normal|black|red|green|yellow|blue|magenta|cyan|white" )
print ( "Only these colours are supported:" )
print_color ( NORMAL )
print_color ( BLACK )
print_color ( RED )
print_color ( GREEN )
print_color ( YELLOW )
print_color ( BLUE )
print_color ( MAGENTA )
print_color ( CYAN )
print_color ( WHITE )
TEXT ( WARING "Color ${_TEXT_COLOR} is not support." )
else ()
list ( APPEND _TEXT_OPTIONS --${_TEXT_COLOR_LOWER} )
endif ()
endif ()
if ( _TEXT_BOLD )
list ( APPEND _TEXT_OPTIONS --bold )
endif ()
execute_process ( COMMAND ${CMAKE_COMMAND} -E env CLICOLOR_FORCE=1 ${CMAKE_COMMAND} ${_TEXT_OPTIONS} ${_TEXT_UNPARSED_ARGUMENTS}
OUTPUT_VARIABLE _TEXT_RESULT
ECHO_ERROR_VARIABLE
)
set ( TEXT_RESULT ${_TEXT_RESULT} PARENT_SCOPE )
endfunction ()
unset ( print_color )
function ( print )
text ( ${ARGN} )
message ( ${TEXT_RESULT} )
endfunction ()
print ( COLOR NORMAL TEST_NORMAL )
print ( BOLD COLOR NORMAL TEST_NORMAL_BOLD )
print ( COLOR BLACK TEST_BLACK )
print ( BOLD COLOR BLACK TEST_BLACK_BOLD )
print ( COLOR RED TEST_RED )
print ( BOLD COLOR RED TEST_RED_BOLD )
print ( COLOR GREEN TEST_GREEN )
print ( BOLD COLOR GREEN TEST_GREEN_BOLD )
print ( COLOR YELLOW TEST_YELLOW )
print ( BOLD COLOR YELLOW TEST_YELLOW_BOLD )
print ( COLOR BLUE TEST_BLUE )
print ( BOLD COLOR BLUE TEST_BLUE_BOLD )
print ( COLOR MAGENTA TEST_MAGENTA )
print ( BOLD COLOR MAGENTA TEST_MAGENTA_BOLD )
print ( COLOR CYAN TEST_CYAN )
print ( BOLD COLOR CYAN TEST_CYAN_BOLD )
print ( COLOR WHITE TEST_WHITE )
print ( BOLD COLOR WHITE TEST_WHITE_BOLD )
<script src="https://gist.github.com/Invincibl-e/3a2dd433c338284cfe95f0d5a6153c06.js"></script>
对于 CMake 函数模板,并根据此处的答案,我制作了一个模块/函数来打印彩色消息。
更多信息在这里: https://github.com/7R35C0/cmake-function-template.git