[CMake] Generate a file with a custom command as part of build

Michael Ellery mellery451 at gmail.com
Tue Jan 24 15:52:15 EST 2017


The syntax of your add_custom_target is not quite right…I would try something more like:

add_custom_target( Webpack ALL 
    $WEBPACK 
    DEPENDS  webpack.config.js
    BYPRODUCTS  public/bundle.js
)

…that’s untested, of course. I you do that, then I think you don’t need the add_custom_command at all….but you will still want to make some of your other targets depend on this target if they need bundle.js before they run.

> On Jan 24, 2017, at 11:34 AM, doug livesey <biot023 at gmail.com> wrote:
> 
> Hi -- sorry about the really late reply, I've been away. Up to Hadrian's Wall, and then for a Dark Skies observatory evening.
> It was ace. :)
> I've added the following to my CMakeLists.txt file:
> 
> > set(WEBPACK ${CMAKE_SOURCE_DIR}/node_modules/.bin/webpack)
> > configure_file(webpack.config.js webpack.config.js)
> > add_custom_target(public/bundle.js)
> > add_custom_command(OUTPUT public/bundle.js COMMAND ${WEBPACK})
> 
> However, when I try to run `cmake ..` (I'm using a build subdirectory), I get the following error:
> 
> > CMake Error at CMakeLists.txt:30 (add_custom_target):
> >   add_custom_target called with invalid target name "public/bundle.js".
> >   Target names may not contain a slash.  Use ADD_CUSTOM_COMMAND to generate
> >   files.
> 
> I'm guessing that this means that I should be running some of those commands in a CMakeLists.txt file in a subdirectory (maybe public/) and calling `add_subdirectory(public)` from my top-level file?
> I've tried this a couple of ways, but so far haven't got it working.
> Am I on the right sort of approach, or have I wandered far from the beaten track?
> Thanks again for your help,
>    Doug.
> 
> On 22 January 2017 at 00:16, Michael Ellery <mellery451 at gmail.com> wrote:
> add_custom_command is the probably the right thing…but it needs to be triggered by a dependency relationship. You will specify public/bundle.js as the OUTPUT parameter of the add_custom_command and then some other target or install command needs to have that file listed in its sources, and then CMAKE will understand that it needs to run the custom command to generate that OUTPUT file before it runs the dependent target. I think add_custom_command can also be explicitly linked to a specific target (there are two forms of the command) - and in that case I think it always runs (?) when that target rebuilds…I’m not completely sure about that, so you might need to test.
> 
> Alternatively, if you want to run the generation at the time of makefile generation (when CMAKE is run…), then have a look at execute_process. The downside of that is that it will generally always run when CMAKE is run, but never during the make process so it doesn’t really handle updates to the source (input) files very well. I’m not sure which technique applies best to your situation here.
> 
> HTH,
> Mike
> 
> > On Jan 21, 2017, at 4:01 PM, doug livesey <biot023 at gmail.com> wrote:
> >
> > Hi, I want to call the following command as part of a build:
> >
> > $ ./node_modules/.bin/webpack
> >
> > This should generate a file public/bundle.js
> >
> > I'm really struggling with this. I guess there's something fundamental that I'm not understanding. `add_custom_command` doesn't seem to be doing anything.
> > I would anticipate that it would put the command I want to call in the makefile, but it hasn't.
> > If anyone could advise me on what I'm not getting here, that would be brilliant.
> > Cheers,
> >    Doug.
> > --
> >
> > 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:
> > http://public.kitware.com/mailman/listinfo/cmake
> 
> 



More information about the CMake mailing list