[CMake] Trouble with parallel builds for targets created by add_executable, target_link_libraries, and add_library

Marcel Loose loose at astron.nl
Wed Sep 30 17:20:30 EDT 2009


On Wednesday 30 September 2009 21:16:10 Alexander Neundorf wrote:
> On Wednesday 30 September 2009, Marcel Loose wrote:
> > On Tue, 2009-09-29 at 19:46 -0400, Bill Hoffman wrote:
> > > Alan W. Irwin wrote:
> > > > On 2009-09-29 16:36-0400 Bill Hoffman wrote:
> > > >> make -j N is only supported with the all target.
> > > >
> > > > How difficult would it be to implement parallel build support for
> > > > more than just the "all" target? Note, if there is some limitation
> > > > that makes it impractical or inefficient to implement this current
> > > > "all" target capability
> > > > for every target, you could instead implement this capability just
> > > > for the targets where this capability is wanted (as designated, say,
> > > > by a PARALLEL_BUILD=ON target property).
> > >
> > > It is very hard to implement on top of make.  Let me restate what I
> > > mean, it is not the all target that is special.  You can do make -j N
> > > target.  You just can't do make -jN target1 target2 if target1 and
> > > target2 have a common target that they depend on.  You can only specify
> > > one target at a time for make -j N.  The /fast targets could be used to
> > > avoid the issue you are having maybe...
> >
> > Hi Alan,
> >
> > I don't think this is a limitation of CMake. It's rather a limitation of
> > make. An (in)famous example of doing a parallel build with 'make' that
> > will likely cause a broken build is:
> >
> > $ make -j4 all install
> >
> > In this example, it's possible that 'make' will try to install, e.g., a
> > library that has yet to be built. The only safe way to run a parallel
> > 'make' with multiple targets is to serialize them (sounds contradictory,
> > doesn't it?):
> >
> > $ make -j4 all && make -j4 install
> >
> > Or better:
> >
> > $ export MAKEFLAGS=-j4
> > $ make all && make install
>
> I never had a problem with "make -j4 install" (which builds "all" first)
>
> Alex
> _______________________________________________

Hmm, bad example. 

What I wanted to point out is that doing a parallel 'make' may result in 
unexpected or incorrect behavior when specifying more than one target. In your 
case target 'install' implicitly depends on 'all', so you may not run into 
this issue.

In general, you should do
	$ make <target1> && make <target2>,
instead of
	$ make <target1> <target2>
when doing a parallel 'make'.

On second thought, I realize that this 'unexpected' or 'incorrect' behavior is 
usually caused by wrong or missing dependencies, which don't show up when 
doing a serial build. 

So maybe the original problem is a limitation of CMake after all.

Best regards,
Marcel Loose.



More information about the CMake mailing list