[CMake] Visual Studio generator running custom_command twice

Paul Smith paul at mad-scientist.net
Fri Mar 15 13:23:04 EDT 2019


On Thu, 2019-03-14 at 13:30 -0400, frodak17 wrote:
> On Thu, Mar 14, 2019 at 12:53 PM frodak17 <frodak17 at gmail.com> wrote:
> > On Thu, Mar 14, 2019 at 1:13 AM Paul Smith <paul at mad-scientist.net>
> > wrote:
> > > I have a situation where I've created a custom command to
> > > generate .cpp
> > > 
> > > files to be compiled (in my case running bison/flex).
> > > 
> > > 
> > > 
> > > I'm using CMake 3.13.4
> > > 
> > > 
> > > 
> > >   set(MyParserOutput
> > > 
> > >       ${OUT_DIR}/MyParser.tab.cpp
> > > 
> > >       ${OUT_DIR}/MyParser.tab.hpp)
> > > 
> > > 
> > > 
> > >   add_custom_target(MyGenParser DEPENDS ${MyParserOutput})
> > > 
> > > 
> > > 
> > > Then I have two different libraries, both depending on this:
> > > 
> > > 
> > > 
> > >   add_library(OneLib STATIC ${MyParserOutput} ...)
> > > 
> > > 
> > >   add_dependencies(OneLib MyGenparser)
> > > 
> > > 
> > > 
> > > 
> > > 
> > >   add_library(TwoLib STATIC ${MyParserOutput} ...)
> > > 
> > > 
> > >   add_dependencies(TwoLib MyGenparser)
> > > 
> > > 
> > From add_custom_command() 
> > Do not list the output in more than one independent target that
> > may build in parallel or the two instances of the rule may conflict
> > (instead use the add_custom_target() command to drive the
> > command and make the other targets depend on that one)

Yeah, I did see that in the manual and I do that, as above.  It wan't
at all clear to me that in addition to those requirements, I ALSO could
NOT list the output of the add_custom_command() in any of my other
targets.

That's unfortunate :(.

> In that case you need to keep ${MyParserOutput} and set the GENERATED
> properties for the files.
> 
> Also the building the custom target needs to be done in a separate
> directory as the add_custom_commands() need to be in a different
> CMakeLists.txt file from the libraries.  Otherwise the rules get
> pulled into the libraries and cause the commands to be run multiple
> times.

Ouch.  That's painful as it could mean moving my code around.

However in this case it ended up not being too horrible because my
parser input files happened to be in a subdirectory already, so I
created a new CMakeLists.txt file there that only ran the generators,
then used set(... PARENT_SCOPE) to publish the variables to the parent
directory.

I did then have to add a GENERATED property to those files in the
parent directory, as you noted, so overall it's a bit leaky in terms of
abstractions, but it seems to work... now when I run on Windows I see
that the generators are run only one time.

Thanks for the help!!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190315/fede2723/attachment-0001.html>


More information about the CMake mailing list