[cmake-developers] How to set environment variable for custom command.

Matthew Woehlke matthew.woehlke at kitware.com
Fri Jul 19 12:35:43 EDT 2013


On 2013-07-19 10:36, Nicolas Desprès wrote:
> 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
>    )
>
> Any idea?

Well, if you only care about UNIX you can always use /bin/env :-).

I'm not sure if build tools on Windows are able to support setting 
environment variables. If you need to do it portably (and can't use the 
code Brad mentioned), you may need to use cmake's -P mode with a script 
that wraps your command (using execute_process) after first setting up 
the desired environment.

-- 
Matthew




More information about the cmake-developers mailing list