首页 > 解决方案 > 带有 FATAL_ERROR 的 CMake 多行消息

问题描述

CMake 文档(例如当前版本 3.11.2)状态

CMake 警告和错误消息文本使用简单的标记语言显示。非缩进文本以换行符分隔的换行段落格式化。缩进的文本被认为是预先格式化的。

但是,它没有提到任何标记格式。除非“非缩进”与“缩进”是关于“简单标记”的全部内容。

无论如何,我未能使其与FATAL_ERROR模式一起使用。

此外,我注意到 with STATUSmode 消息打印前导--(两个破折号和空格)。虽然FATAL_ERROR消息中的每个换行符都会变成两行,这(恕我直言)看起来很糟糕。

现在我有一条多行消息,其中列出了错误CMAKE_BUILD_TYPE以及接受了哪些值。由于上述问题,我最终将消息打印为STATUS并用三个空格缩进后续行(因此它们与 . 对齐--)。然后我做了一个简单的FATAL_ERROR重复“标题行”(说明这CMAKE_BUILD_TYPE是错误的)。这在控制台输出和cmake-gui. (虽然 3 个空格的缩进在cmake-gui... 上是不必要的)

但是,我很惊讶这个主题的描述如此糟糕。很久以来似乎就是这样 - 例如,请参阅问题[CMake] Extra blank lines with SEND_ERROR and FATAL_ERROR ?!现在已经将近 9 年没有答案了……

是否有任何处理此类消息的良好做法、建议或技巧?还是应该首先避免它们?

标签: cmake

解决方案


你是对的。“简单标记”是非缩进(未格式化)或缩进(格式化)。此外,非缩进文本在由换行符分隔的段落中。这就是为什么您最终会在段落之间出现空行。

这是各种消息的运行解释。就格式化文本和未格式化文本而言,警告类型和错误类型的行为相同。当然,不同之处在于 CMake 的处理和生成阶段会发生什么。为了便于阅读,您可以将字符串拆分为多个将被连接起来的双引号部分。

# STATUS
message(STATUS
    "This is a status message. It is prefixed with \"-- \". It goes to stdout. The "
    "lines will wrap according to the width of your terminal.\n"
    "New lines will begin a new line at column 1, but without the \"-- \" prefix, "
    "unless you provide it; they will not create a blank line (i.e., new "
    "paragraph).   Spacing between sentences is unaltered by CMake.\n"
    "-- Here's a new paragraph with an explicit \"-- \" prefix added.")

# no mode given (informational)
message(
    "This is an informational message. It goes to stderr. Each line begins at column "
    "1. The lines will wrap according to the width of your terminal.\n"
    "New lines will begin a new line at column 1; they will not create a blank line "
    "(i.e., new paragraph).   Spacing between sentences is unaltered by CMake (3 spaces "
    "preceded this sentence.).")

# WARNING--unformatted
message(WARNING
    "This is an unformatted warning message.   It goes to stderr. Each line begins "
    "at column 3. The lines will wrap at a particular column (it appears to be "
    "column 77, set within CMake) and wrap back to column 3.\n"
    "New lines will begin a new paragraph, so they will create a blank line. A final "
    "thing about unformatted messages: They will separate sentences with 2 spaces, "
    "even if your string had something  different.")

# WARNING--formatted and unformatted
message(WARNING
    " This is a formatted warning message. It goes to stderr. Formatted lines will"
    " be indented an additional 2 spaces beyond what was provided in the output"
    " string. The lines will wrap according to the width of your terminal.\n"
    " Indented new lines will begin a new line. They will not create a blank line."
    " If you separate sentences with 1 space, that's what you'll get.  If you"
    " separate them with 2 spaces, that's also what you'll get.\n"
    " If you want to control the width of the formatted paragraphs\n"
    " (a good practice), just keep track of the width of each line and place\n"
    " a \"\\n\" at the end of each line.\n \n"
    " And, if you want a blank line between paragraphs, just place \"\\n \\n\"\n"
    " (i.e., 2 newlines separated by a space) at the end of the first paragraph.\n"
    "Non-indented new lines, however, will be treated like unformatted warning "
    "messages, described above. They will begin at and wrap to column 3. They begin "
    "a new paragraph, so they will create a blank line.    There will be 2 spaces "
    "between sentences, regardless of how many you placed after the period (In the "
    "script, there were 4 spaces before this sentence).\n"
    "And, as you'd expect, a second unindented paragraph will be preceded by a "
    "blank line. But why would you mix formatted and unformatted text?")

我将它保存到 Message.cmake 并使用 .cmake 调用它cmake -P Message.cmake 2> output.txt。它产生以下标准输出:

-- This is a status message. It is prefixed with "-- ". It goes to stdout. The lines will wrap according to the width of your terminal.
New lines will begin a new line at column 1, but without the "-- " prefix, unless you provide it; they will not create a blank line (i.e., new paragraph).   Spacing between sentences is unaltered by CMake.
-- Here's a new paragraph with an explicit "-- " prefix added.

文件 output.txt 包含:

This is an informational message. It goes to stderr. Each line begins at column 1. The lines will wrap according to the width of your terminal.
New lines will begin a new line at column 1; they will not create a blank line (i.e., new paragraph).   Spacing between sentences is unaltered by CMake (3 spaces preceded this sentence.).
CMake Warning at MessageScript.cmake:19 (message):
  This is an unformatted warning message.  It goes to stderr.  Each line
  begins at column 3.  The lines will wrap at a particular column (it appears
  to be column 77, set within CMake) and wrap back to column 3.

  New lines will begin a new paragraph, so they will create a blank line.  A
  final thing about unformatted messages: They will separate sentences with 2
  spaces, even if your string had something different.


CMake Warning at MessageScript.cmake:28 (message):
   This is a formatted warning message. It goes to stderr. Formatted lines will be indented an additional 2 spaces beyond what was provided in the output string. The lines will wrap according to the width of your terminal.
   Indented new lines will begin a new line. They will not create a blank line. If you separate sentences with 1 space, that's what you'll get.  If you separate them with 2 spaces, that's also what you'll get.
   If you want to control the width of the formatted paragraphs
   (a good practice), just keep track of the width of each line and place
   a "\n" at the end of each line.

   And, if you want a blank line between paragraphs, just place "\n \n"
   (i.e., 2 newlines separated by a space) at the end of the first paragraph.

  Non-indented new lines, however, will be treated like unformatted warning
  messages, described above.  They will begin at and wrap to column 3.  They
  begin a new paragraph, so they will create a blank line.  There will be 2
  spaces between sentences, regardless of how many you placed after the
  period (In the script, there were 4 spaces before this sentence).

  And, as you'd expect, a second unindented paragraph will be preceded by a
  blank line.  But why would you mix formatted and unformatted text?

概括

信息消息(未给出模式)

  • 从第 1 列开始
  • 在终端窗口中换行直到换行
  • 去标准错误
  • 新段落开头没有前面的空行
  • 保留句子和单词间距

状态信息

  • 从第 1 列开始,第一段带有“--”前缀
  • 在终端窗口中换行直到换行
  • 转到标准输出
  • 新段落开头没有前面的空行
  • 保留句子和单词间距

未格式化的警告和错误消息(未缩进的字符串)

  • 从第 3 列开始
  • 在第 77 列换行
  • 去标准错误
  • 新段落前面有一个空行
  • 用 2 个空格分隔的句子;单词 1 个空格

格式化警告和错误消息(缩进字符串)

  • 从第 3 列开始,加上字符串的任何缩进
  • 在终端窗口中换行直到换行
  • 去标准错误
  • 新段落开头没有前面的空行
  • 保留句子和单词间距

推荐阅读