[CMake] Setting environment variables in custom command

Michael Wild themiwi at gmail.com
Mon Dec 12 14:53:51 EST 2011


On 12/12/2011 08:46 PM, Robert Dailey wrote:
> On Mon, Dec 12, 2011 at 1:31 PM, Michael Wild <themiwi at gmail.com
> <mailto:themiwi at gmail.com>> wrote:
> 
>     On 12/12/2011 08:28 PM, Robert Dailey wrote:
>     > Hi,
>     >
>     > I'm creating a custom target in CMake 2.8.7 RC1 and running it in
>     Visual
>     > Studio 2008. I set an environment variable called BUILDBIN in the
>     custom
>     > command but for some reason my scripts that I invoke are not
>     picking up
>     > that environment variable. Am I doing something wrong? These custom
>     > commands are starting to get pretty complex and I'm wondering if I
>     > should pull out the logic into a batch file. What do you guys think?
>     >
>     [snip long code]
> 
>     Definitely batch file or, preferably CMake script. In CMake scripts,
>     just assign to the ENV{VAR} variable and use execute_process().
> 
> 
> CMake script would imply that it runs as part of the configuration
> process. I can't build an installer until all binaries are compiled and
> linked, so it needs to be a target in visual studio.

Wrong.

add_custom_command(
  COMMAND ${CMAKE_EXECUTABLE} -DVAR=VAL
    -P ${PROJECT_BINARY_DIR}/script.cmake
  DEPENDS ${PROJECT_BINARY_DIR}/script.cmake
  WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  COMMENT "Running script.cmake"
  VERBATIM)


> 
> Problem with pushing this out to a batch file is that I only have 9
> parameters maximum that I can use to pass CMake variables. I need more
> than 9, as you can see from the number of variables I use in the script
> I sent you. I can't think of a good design for this. Not to mention I
> hate batch files.

Configured CMake file, it is.

set(VAR1 VAL1)
set(VAR2 VAL2)
set(VAR3 VAL3)
configure_file(script.cmake.in ${PROJECT_BINARY_DIR}/script.cmake @ONLY)


Where script.cmake.in could look like this:

set(ENV{VAR1} "@VAR1@")
set(ENV{VAR2} "@VAR2@")
set(ENV{VAR3} "@VAR3@")
message(STATUS
  "DEBUG: VAR1=$ENV{VAR1}, VAR2=$ENV{VAR2}, VAR3=$ENV{VAR3}")


> 
> But moving it out to a batch file will guarantee that I can setup the
> environment properly (set environment variables) and also when users
> change the way the installer works, CMake won't need to be reconfigured,
> which is a huge plus.
> 
> Any advice here on how I could setup my batch file to accept more than 9
> parameters from CMake?

Make it a configured file. Either batch or CMake script, as shown above.

Michael



More information about the CMake mailing list