使用FATAL_ERROR

时间:2018-05-21 10:39:12

标签: cmake

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

  

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

但是,它没有提到任何标记格式。除非"非缩进" vs."缩进"就是"简单标记"。

无论如何,我无法使用FATAL_ERROR模式。

此外,我注意到STATUS模式消息打印有前导--(两个破折号和空格)。当FATAL_ERROR消息中的每个换行符变成两行时,(恕我直言)看起来很糟糕。

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

但是,我很惊讶这个主题的描述有多糟糕。而且似乎很长时间以来 - 例如问题[CMake] Extra blank lines with SEND_ERROR and FATAL_ERROR ?!现在已经有近9年没有得到答复......

是否有处理此类讯息的良好做法,建议或提示?或者首先应该避免它们?

1 个答案:

答案 0 :(得分:4)

您是对的。 “简单标记”可以是非缩进(未格式化)或缩进(格式化)的。另外,非缩进文本在段落中用换行符分隔。这就是为什么最后在段落之间使用空白行。

以下是对各种消息的不断解释。警告类型和错误类型在格式化和未格式化文本方面表现相同。当然,区别在于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 -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列开始
  • 在终端窗口中换行,直到换行符
  • 转到stderr
  • 新段落开头没有空格
  • 保留句子和单词间距

状态消息

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

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

  • 从第3列开始
  • 在第77列换行
  • 转到stderr
  • 新段落前面有一个空白行
  • 句子之间用2个空格隔开;单词乘以1个空格

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

  • 从第3列开始,加上字符串具有的所有缩进
  • 在终端窗口中换行,直到换行符
  • 转到stderr
  • 新段落开头没有空格
  • 保留句子和单词间距