[CMake] Custom command concurrency

Attila Krasznahorkay attila.krasznahorkay at gmail.com
Mon Nov 30 07:21:40 EST 2015


Dear All,

I'm running into a new issue with my custom commands. :-(

I have a project with many subdirectories. Where most subdirectories generate some files during the build. Some of which need to be merged into a single file at the end of the build.

In my current implementation the subdirectories declare both a custom command and a custom target for generating these files. So that the merge command in the main directory could depend on the custom targets of the subdirectories. (As the merge command can't depend on the generated files themselves directly.)

Now, the merge command is not the only use using these generated files. They are also used inside the subdirectories in order to build some libraries. And now the problem comes. Apparently CMake / GNU Make doesn't make sure that the generation command would only run once. Attached is a simple example. When running make in single-process mode, things happen as I would expect them to.

Scanning dependencies of target SubdirTarget
[ 33%] Generating partialFile.txt
[ 33%] Built target SubdirTarget
Scanning dependencies of target MergeTarget
[ 66%] Generating merged.txt
[ 66%] Built target MergeTarget
Scanning dependencies of target CompileSubdirTarget
[100%] Built target CompileSubdirTarget

But when I use multiple build processes, I get:

Scanning dependencies of target SubdirTarget
Scanning dependencies of target CompileSubdirTarget
[ 66%] Generating partialFile.txt
[ 66%] Generating partialFile.txt
[ 66%] Built target CompileSubdirTarget
[ 66%] Built target SubdirTarget
Scanning dependencies of target MergeTarget
[100%] Generating merged.txt
[100%] Built target MergeTarget

So, the file generation gets triggered twice at the same time. Of course in this simple example this wouldn't matter. But the generator that I use in real life can't handle this. As it creates some temporary files as it runs, which interfere with each other. So I come out with a corrupt output file.

Does anyone have any suggestions? I would really need to be able to make sure that this custom command would not be executed multiple times. And most of all, not multiple times at once.

Cheers,
            Attila

P.S. It seems that the Ninja generator is a bit better in this respect. But of course it's not an option to not have my project work correctly with the Makefile generator.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: custom_command_concurrency.tar.bz2
Type: application/x-bzip2
Size: 535 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/cmake/attachments/20151130/301b6299/attachment.bin>


More information about the CMake mailing list