graywolf
graywolf

Reputation: 7470

How to treat warnings as errors for custom builds?

As part of our build, we have one project (.vcxproj) which contains just one python script with Custom Build Tool building, command line: $(python_bin)/python.exe %(FullPath). Msbuild is able to parse warnings from this python script and display them after build:

"F:\Generators.vcxproj" (default target) (1) ->
(CustomBuild target) -> 
  F:\Tool.py(274): warning : OPSEC [F:\Generators.vcxproj]
  F:\Tool.py(274): warning : OPSEC [F:\Generators.vcxproj]

    2 Warning(s)
    0 Error(s)

now, how can I force it to treat those warnings as errors? Only option I found is Treat Linker Warning As Errors, which obviously didn't help since linking on this project is not done (since it's just one .py file with custom build) and anyway those warning are from build step.

I even tried adding <TreatWarningAsError>true</TreatWarningAsError> into ClCompile section. Didn't help.

How can I force msbuild to treat those warnings as errors?

Upvotes: 0

Views: 1141

Answers (1)

Christian.K
Christian.K

Reputation: 49220

Let my start off by one of your recent comments:

... msbuild recognized output of python script as warnings. but it could as well be bash, perl, php script or something like that. msbuild recognizes output as warning, and I want to treat those warnings as error. ...

There is no generic "turn warnings into errors" for any tool imaginable in MSBuild. How could it? Every tool invoked by MSBuild has its one way of doing that, if at all. The C++ compiler and C# compiler for example, have specific command line options that are included in their command line by the msbuild files for these languages - nothing that the core msbuild engine could do.

So what you need to do is instruct the tool (python in the context of the question) to do that when you want it to. You can do this by using an existing/well known property (TreatWarningsAsErrors) as a trigger. If it has a certain value (true), then you need to do whatever you need to do the make your tool emit warnings as errors and act accordingly.

Example:

<PropertyGroup>
    <PythonCmd>$(python_bin)\python.exe</PythonCmd>
    <PythonCmd Condition="'$(TreatWarningsAsErrors)' == 'true'">$(PythonCmd) -W error</PythonCmd>
    <PythonCmd>$(PythonCmd) %(FullPath)</PythonCmd>
</PropertyGroup>

<Exec Command="$(PythonCmd)" .../>

Now, if you don't know how to exactly make python do what you want (turn warnings into errors), there is no msbuild magic that can help you do that. You suddenly do have a python specific question. Because the actual problem you have now is not related to python being executed from within msbuild or from, say, the command line.

A second option: if you can't or don't want to use Python's built in capability of turning warnings into errors, you can also use the Exec-Tasks CustomErrorRegularExpression-property to specify a regular expression that should be treatet as "error output" by the tool (python.exe here).

Something like

 <PropertyGroup>
     <ErrorExp>.*: error :.*</ErrorExp>
     <ErrorExp Condition="'$(TreatWarningsAsErrors)' == 'true'">.*: (warning|error) :.*</ErrorExp>
 </PropertyGroup>

 <Exec ... CustomErrorRegularExpression="$(ErrorExp)"/>

(Untested!) should get you started.

However, since Python already provides a way to set warnings as errors (via -W error it seems) that option seems to be unnecessarily convoluted.

Again, regarding your comment about potential other tools in use, mind you that the above approach also requires specific knowledge about those other tools and how they format their warnings (if they do at all), because you need a custom regex for them.

So even that approach requires specific knowledge about the tool you invoke and not so much msbuild.

I might be totally off here and you really have a different issue, question or need. Checking the comments to your question, it looks like I'm not the only one then and thus you might need to change your question or make it more precise (possible with an example of what you have already tried) to get further/better help.

Upvotes: 1

Related Questions