How to set environment variable for custom command

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to set environment variable for custom command

Nicolas Desprès
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
  )

Any idea?

Cheers,

--
Nicolas Desprès

--

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://www.cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: How to set environment variable for custom command

Andreas Pakulat-2
Hi,


On Fri, Jul 19, 2013 at 4:37 PM, Nicolas Desprès <[hidden email]> 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

--

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://www.cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: How to set environment variable for custom command

Dan Kegel
On Fri, Jul 19, 2013 at 8:21 AM, Andreas Pakulat <[hidden email]> wrote:
>> In Unix shell we can do that:
>> $ VAR=foo cmd in out
>> I would like to be able to do the same for a custom command in CMake.

See http://public.kitware.com/Bug/view.php?id=5145
for previous discussion.

>> add_custom_command(
>>   OUTPUT out
>>   COMMAND cmd in out
>>   DEPENDS in
>>   ENVIRONMENT VAR foo
>>   )
>
> I can see two options to achieve this: ...
> 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.

c) On Mac and Linux, you can do it inline something like this:

 add_custom_command(
   OUTPUT out
   COMMAND "FOO=${bar}" my_cmd.sh"
   DEPENDS in
   )

On Windows, you might be able to do it inline like this:

 add_custom_command(
   OUTPUT out
   COMMAND "set FOO=${bar}" & my_cmd.bat"
   DEPENDS in
   )

(although I haven't tried it, so in both cases you might need to
coax cmake into using a shell by prefixing the command with sh -c
or cmd /c or something.)
--

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://www.cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: How to set environment variable for custom command

Nicolas Desprès
Thank you all for your suggestion. I will go with the script shell wrapper trick even if I am not a big fan of it. I don't like the burden of generating a wrapper just to set environment variable. Luckily CMake will provides such a feature in the future.

Cheers,
Nico

On Fri, Jul 19, 2013 at 5:39 PM, Dan Kegel <[hidden email]> wrote:
On Fri, Jul 19, 2013 at 8:21 AM, Andreas Pakulat <[hidden email]> wrote:
>> In Unix shell we can do that:
>> $ VAR=foo cmd in out
>> I would like to be able to do the same for a custom command in CMake.

See http://public.kitware.com/Bug/view.php?id=5145
for previous discussion.

>> add_custom_command(
>>   OUTPUT out
>>   COMMAND cmd in out
>>   DEPENDS in
>>   ENVIRONMENT VAR foo
>>   )
>
> I can see two options to achieve this: ...
> 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.

c) On Mac and Linux, you can do it inline something like this:

 add_custom_command(
   OUTPUT out
   COMMAND "FOO=${bar}" my_cmd.sh"
   DEPENDS in
   )

On Windows, you might be able to do it inline like this:

 add_custom_command(
   OUTPUT out
   COMMAND "set FOO=${bar}" & my_cmd.bat"
   DEPENDS in
   )

(although I haven't tried it, so in both cases you might need to
coax cmake into using a shell by prefixing the command with sh -c
or cmd /c or something.)



--
Nicolas Desprès

--

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://www.cmake.org/mailman/listinfo/cmake