保存并重新打印警告对后续构建成功编译的文件? [英] Save and reprint warnings for successfully-compiled files on subsequent builds?

查看:329
本文介绍了保存并重新打印警告对后续构建成功编译的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当重复建设的项目,当有警告,但在翻译单元中没有错误,主要的源文件通常不会重新编译。

When repeatedly building a project, when there are warnings but no errors in a translation unit, the main source file is typically not recompiled.

这可以使它难以合作,通过错误的的警告,试图获得项目建设,没有警告。通常,一个人必须保持反复建设,直到所有的错误的悉心照料的,然后做了全面清理,并建立以确保没有任何的警告的(以及以确保previously完成的版本是不是侥幸所造成剩构建构件)。

This can make it difficult to work through errors and warnings to attempt to get the project to build with no warnings. Typically one must keep iteratively building until all errors are taken care of, then do a full clean and build to ensure that there are no warnings (as well as to ensure that the previously-completed build wasn't a "fluke" caused by leftover build artifacts).

有什么办法用CMake的(或其他工具,如Bash脚本)来解析建立预警输出,将它们保存在某个文本文件,然后在后续构建重新显示它们吗?

Is there any way with CMake (or some other utility such as a Bash script) to parse build output for warnings, save them in a text file somewhere, and then re-display them on subsequent builds?

有关加分,因为我着色我的编译器输出,可警告保存与色彩控制字符和相同的着色重新显示?

For bonus points, since I'm colorizing my compiler output, can warnings be saved with the color control-characters and re-displayed with the same colorization?

(如果它的事项,此刻我只编译C ++,而且我通常使用GCC这样做。我建立的选择发生器是忍者,我有我写的总结都是些的bash脚本我的电话向CMake和忍者)。

(If it matters, at the moment I'm only compiling C++, and I'm typically using GCC to do so. My build generator of choice is Ninja, and I have some Bash scripts I've written that wrap all my calls to CMake and Ninja.)

推荐答案

我不是一个庆典专家 - 所以下面code肯定能提高 - 但这里是 CMake的 / 庆典 / 海湾合作委员会的工作示例 / 忍者应该了解基本的概念我有:

I'm not a bash expert - so the following code can certainly be improved - but here is a working example with CMake/bash/gcc/ninja that should give the basic idea I had:


  • 检测如果编译器穿上警告/错误标准错误

  • 储存于<目标文件名称> .warnings

  • 删除未来建设开始之前有一个警告的对象文件

  • 编译器本身将再次输出警报(如果尚未固定)

的CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(CaptureWarnings CXX)

add_compile_options(-fdiagnostics-color=always -Wconversion)
configure_file(capture_warnings.sh.in ${CMAKE_BINARY_DIR}/capture_warnings.sh @ONLY)

file(WRITE foo.cc "int main() {\nreturn 0.5;\n}")
file(WRITE bar.cc "")

set_directory_properties(PROPERTIES 
    RULE_LAUNCH_COMPILE "bash ${CMAKE_BINARY_DIR}/capture_warnings.sh")

add_executable(MyExe foo.cc bar.cc)

capture_warnings.sh.in

#!/bin/bash

# shell script invoked with the following arguments
# $(CXX) $(CXX_DEFINES) $(CXX_FLAGS) -MMD -MT OBJ_FILE -MF DEP_FILE -o OBJ_FILE -c SRC_FILE

# extract parameters
OBJECT_FILE="${@: -3:1}"

# invoke compiler
set -o pipefail
$@ 2> ${OBJECT_FILE}.warnings
ERROR=${PIPESTATUS}

OUT=$(<${OBJECT_FILE}.warnings)

if ! [[ -z "$OUT" ]]; then
    # reprint the warning/error
    >&2 echo "${OUT}"
    echo "rm -f ${PWD}/${OBJECT_FILE}" >> @CMAKE_BINARY_DIR@/remove_obj_with_warnings.sh
else
    rm -f ${OBJECT_FILE}.warnings
fi

exit ${ERROR}

build.sh

#!/bin/bash

if ! [ -d build ]; then
    mkdir build
    cmake -H. -Bbuild -G "Ninja"
fi

if [ -f build/remove_obj_with_warnings.sh ]; then 
    sh ./build/remove_obj_with_warnings.sh
    rm build/remove_obj_with_warnings.sh
fi

cmake --build build

我想收集在 remove_obj_with_warnings.sh 要删除的文件会比grepping更快的 .warnings 文件。其缺点是,它可能包含已删除或还未存在的文件(通过给覆盖 RM -f )。

I thought collecting the files to be deleted in remove_obj_with_warnings.sh would be faster than grepping for the .warnings files. The disadvantage would be that it could contain files that are already deleted or not-yet-existing (covered by giving rm -f).

如果您特别真实的 remove_obj_with_warnings.sh 调用可选的。

Especially true if you make the remove_obj_with_warnings.sh call optional.

参考文献:


  1. 使CMake的使用gccfilter

  2. Make GCC把相对文件名在调试信息

  3. bash:重定向(和app​​end)输出和错误到文件终端和得到适当的退出状态

  4. Handling海湾合作委员会的警告和输出bash脚本

  1. Make CMake use gccfilter
  2. Make gcc put relative filenames in debug information
  3. bash: redirect (and append) stdout and stderr to file and terminal and get proper exit status
  4. Handling gcc warnings and output in a Bash Script

这篇关于保存并重新打印警告对后续构建成功编译的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆