[CMake] How to set environment variable for custom command

Andreas Pakulat apaku at gmx.de
Fri Jul 19 11:21:46 EDT 2013


Hi,


On Fri, Jul 19, 2013 at 4:37 PM, Nicolas Desprès
<nicolas.despres at gmail.com>wrote:

> Hi,
>
> In Unix shell we can do that:
> $ VAR=foo cmd in out
>
> This way the environment variable is only set in the environment of the
> process of the command and not the in current shell like when using the
> "export" built-in.
>
> I would like to be able to do the same for a custom command in CMake.
> Ideally I was looking for something like that:
>
> add_custom_command(
>   OUTPUT out
>   COMMAND cmd in out
>   DEPENDS in
>   ENVIRONMENT VAR foo
>   )
>

I can see two options to achieve this:

a) set the environment variable when calling make in the same way as above.
Custom commands are actually executed by make and get the envvars from the
make environment

b) let cmake generate a shell script (using configure_file) and set the
envvar in there, it would also contain the command executable and pass on
any arguments using $@. Then use the script as custom command instead of
the real command.

Andreas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20130719/8ca836e8/attachment.htm>


More information about the CMake mailing list