parallel cppcheck invocation corrupts common output file

I’m using set(CMAKE_C_CPPCHECK "....;--output-file=cppcheck_output.txt") to capture the result of the run in one signe file. In many cases, this works well, but occasionally the output file is corrupted. It seem the root problem here is, that since CMake uses multiple threads to compile the sources, cppcheck is also invoked multiple times in parallel. Check finding to the output file is not appended atomically, thus the output gets intermixed then and that results in a non-parsable file then

What is the recommended way avoid this? Serializing the build is not really an option. Is there an easy way to use a target-specific cppcheck output file and then merge all these files? Or force the cppcheck to run serialized?

Thanks, Axel

See this thread for a similar question and possible solution.

1 Like

Thanks for the pointer.

I wonder, is there a way to use the actual target file name in --output-file=... there? Using ${target} does not work, this variable is empty then.

No, you’ll need to parse the command line in the launcher script/tool you use for the output file then make the flag based on its argument.

The issue here is, that this information is not available. All I get is the input file with absolute path. And a bunch of compile parameters, that are important for cppcheck to get the full context of the compiler invocation to have the proper configuration for the code the check is supported to run on. Actually, I cannot even blame the tool for not making the output file name available to cppcheck, because this is a piece of information that cppcheck does not really need to run. It’s just that in the context of the build system, where this information is needed again when I specify the output file.

Actually, I consider raising a CMake feature request now to add support for this, as this would really improve usability. Can’t this be made available via supporting generator expressions there somehow?

Yep. This issue is probably what you’d be looking for.

1 Like

I have created as a feature request.