[CMake] Add lots of commands

Brandon Van Every bvanevery at gmail.com
Mon Mar 3 12:36:28 EST 2008


On Mon, Mar 3, 2008 at 11:24 AM, Robert Bielik <robert.bielik at xponaut.se> wrote:
> In ADD_CUSTOM_COMMAND(TARGET xxxx POST_BUILD
>      COMMAND cmd1 args1
>      [COMMAND cmd2 args2]
>  )
>
>  I'd need to be able to add as many commands as are in a list, sort of like:
>
>  In ADD_CUSTOM_COMMAND(TARGET xxxx POST_BUILD
>  FOREACH(entry)
>      COMMAND ${entry}
>  ENDFOREACH(entry)
>  )
>
>  Is it possible to achieve this is some way?

Not in any nice way.  You've hit the dis-unity of add_custom_command
with other things.  You could write all your commands out to a file,
and then execute that file as a CMake script.  But you can only use
FOREACH at configuration time, so if for some odd reason you're trying
to construct commands at build time after CMake is gone, that won't
work.  Assuming you are getting all your commands at configuration
time and that their configuration is static, you could do:

# make a chain of dependent targets, each executing 1 command
add_custom_command(TARGET mytargetname)
set(chain_num 0)
set(prevchain_name mytargetname)
foreach(entry ${some_command_list})
  set(thischain_name chain${chain_num})
  add_custom_command(TARGET ${thischain_name}
    COMMAND ${entry})
  add_dependencies(${prevchain_name} ${thischain_name})
  math(EXPR chain_num "${chain_num} + 1")
  set(prevchain_name ${thischain_name})
endforeach(entry)

I have used this kind of dependency chaining approach to automagically
disambiguate conditionals in Autoconf Makefile.in's.  Autoconf allows
conditionals inside of makefile targets and CMake does not.  If you
happen to be worrying about that kind of problem, and you have lots of
Makefile.in's to worry about, I'd strongly suggest you look at the
code I did for Mozilla.  There's a chance it might just parse your
conditionals into sanity, breaking them into appropriate dependent
chains.  Of course I wrote that code for Mozilla, not the rest of the
world, so no guarantees.  I'm willing to advise on what must be done
for anyone's specific purposes though.  The code is available under
Mozilla's usual tri-license.
https://bugzilla.mozilla.org/show_bug.cgi?id=416982


Cheers,
Brandon Van Every


More information about the CMake mailing list