简体   繁体   English

将警告视为错误,但忽略googletest中的警告

[英]Treat warnings as errors but ignore warnings in googletest

I have a C++ project where I want to use the -Wsign-conversion compile option. 我有一个C ++项目,我想使用-Wsign-conversion编译选项。 As build tool Bazel 0.28.1 is used. 使用构建工具Bazel 0.28.1。 My self-written code does not generate warning of that type. 我自编的代码不会生成该类型的警告。 The project uses Googletest. 该项目使用Googletest。 Unfortunately, Googletest generates this kind of warning, which breaks my build. 不幸的是,Googletest会产生这种警告,这会打破我的构建。 Here are my files: 这是我的文件:

.bazelrc .bazelrc

build --cxxopt=-Werror           # Every warning is treated as an error.
build --cxxopt=-std=c++14
build --cxxopt=-Wsign-conversion # Warn for implicit conversions that may change the sign of an integer value

gtest.BUILD gtest.BUILD

cc_library(
    name = "main",
    srcs = glob(
        ["src/*.cc"],
        exclude = ["src/gtest-all.cc"]
    ),
    hdrs = glob([
        "include/**/*.h",
        "src/*.h"
    ]),
    copts = ["-Iexternal/gtest/include"],
    linkopts = ["-pthread"],
    visibility = ["//visibility:public"],
)

WORKSPACE 工作区

workspace(name = "GTestDemo")

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
    name = "googletest",
    remote = "https://github.com/google/googletest",
    #tag = "release-1.8.1",
    commit = "2fe3bd994b3189899d93f1d5a881e725e046fdc2", 
    shallow_since = "1535728917 -0400",
)

BUILD 建立

cc_test(
    name = "tests",
    srcs = ["test.cpp"],
    copts = ["-Iexternal/gtest/include"],
    deps = [
            "@googletest//:gtest_main",
        ],
)

test.cpp TEST.CPP

#include <iostream>

#include "gtest/gtest.h"

TEST(sample_test_case, sample_test)
{
    EXPECT_EQ(1, 1);
}

When I try to build the code the following error is shown: 当我尝试构建代码时,显示以下错误:

Starting local Bazel server and connecting to it...
INFO: Analyzed target //:tests (21 packages loaded, 540 targets configured).
INFO: Found 1 target...
INFO: Deleting stale sandbox base /mnt/ramdisk/bazel-sandbox.be60b2910864108c1e29c6fce8ad6ea4
ERROR: /home/admin/.cache/bazel/_bazel_admin/cc9b56275ffa85d1a0fca263d1d708e4/external/googletest/BUILD.bazel:55:1: C++ compilation of rule '@googletest//:gtest' failed (Exit 1) gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF ... (remaining 36 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
external/googletest/googletest/src/gtest-filepath.cc: In member function 'testing::internal::FilePath testing::internal::FilePath::RemoveFileName() const':
external/googletest/googletest/src/gtest-filepath.cc:168:45: error: conversion to 'std::__cxx11::basic_string<char>::size_type {aka long unsigned int}' from 'long int' may change the sign of the result [-Werror=sign-conversion]
     dir = std::string(c_str(), last_sep + 1 - c_str());
                                ~~~~~~~~~~~~~^~~~~~~~~
cc1plus: all warnings being treated as errors
Target //:tests failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 7.225s, Critical Path: 0.66s
INFO: 0 processes.
FAILED: Build did NOT complete successfully

Is there a possibility in Bazel to ignore warnings in 3rd-party-library such as googletest? Bazel是否有可能忽略第三方库中的警告,例如googletest?

This is more of a GCC question. 这更像是海湾合作委员会的一个问题。

"System headers" are immune from this kind of thing : “系统标题” 不受此类事件的影响

-Wsystem-headers : Print warning messages for constructs found in system header files. -Wsystem-headers :打印系统头文件中的构造的警告消息。 Warnings from system headers are normally suppressed , on the assumption that they usually do not indicate real problems and would only make the compiler output harder to read. 系统头的警告通常被抑制 ,假设它们通常不表示实际问题并且只会使编译器输出更难读。

So, you can just pretend that the 3rd-party lib is a system header, using GCC's -isystem flag (instead of -I ): 因此,您可以假设第三方lib是系统头,使用GCC的-isystem标志 (而不是-I ):

copts = ["-isystem external/gtest/include"],

Bazel's --per_file_copt allows setting flags for all files except ones matching some regular expression. Bazel的--per_file_copt允许为除了匹配某些正则表达式的文件之外的所有文件设置标志。 Something more like this in your .bazelrc should do what you're looking for: 在.bazelrc中更像这样的东西应该做你想要的:

# Warn for implicit conversions that may change the sign of an integer value,
# for C++ files not in googletest
build --per_file_copt=.*\.(cc|cpp),-googletest/.*@-Wsign-conversion

You'll need to update that to match any extensions you use for C++ files other than .cc and .cpp. 您需要更新它以匹配用于除.cc和.cpp之外的C ++文件的任何扩展。 The documentation for cc_library.srcs lists all the extensions Bazel uses, for reference. cc_library.srcs的文档列出了Bazel使用的所有扩展,以供参考。

I couldn't figure out how to match @googletest// in the flag, because I don't see a way to escape an @ there... However, I'm pretty sure it's matching against @googletest and not external/googletest or something, because /googletest doesn't match anything. 我无法弄清楚如何匹配@googletest//在旗帜中,因为我没有办法逃避@那里...但是,我很确定它与@googletest匹配而不是external/googletest或某事,因为/googletest与任何东西都不匹配。 Probably won't matter, but it is something to keep in mind if you have any other filenames with googletest in them. 可能无关紧要,但如果您有任何其他文件名包含googletest ,请googletest这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM