On Thu, 2019-03-14 at 13:30 -0400, frodak17 wrote:
> On Thu, Mar 14, 2019 at 12:53 PM frodak17 <froda...@gmail.com> wrote:
> > On Thu, Mar 14, 2019 at 1:13 AM Paul Smith <p...@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!!
-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake

Reply via email to