[CMake] Setting environment variables in custom command
Michael Wild
themiwi at gmail.com
Mon Dec 12 15:18:59 EST 2011
On 12/12/2011 09:05 PM, Robert Dailey wrote:
> On Mon, Dec 12, 2011 at 1:53 PM, Michael Wild <themiwi at gmail.com
> <mailto:themiwi at gmail.com>> wrote:
>
> > 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)
>
>
> Interesting, I didn't even think about doing this. Good job :) I will do
> this.
>
> I can use add_custom_target instead though, right? Why do you use
> add_custom_command here? I don't know how that translates in Visual Studio.
A custom target is considered to be "always out of date". I.e. it always
runs its COMMAND. A custom command produces one or multiple output files
that CMake (or in your case, also VisualStudio) knows about. The command
is only executed if one of its dependencies is newer than any of the
outputs.
Often it is customary to have pairs of custom commands and targets.
>
>
> >
> > 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 <http://script.cmake.in>
> ${PROJECT_BINARY_DIR}/script.cmake @ONLY)
>
>
> Where script.cmake.in <http://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.
>
>
> If I use a CMake script, I won't need it to be configured, right? As I
> can pass in as many -D arguments as I want it seems.
It will probably still run through the Windows command interpreter, so
the number of arguments is very limited. I'm not experienced enough to
tell for sure, though...
>
> I definitely like CMake scripts better than batch files, so I will go
> with CMake scripts. Thanks for the insight.
You're welcome.
Michael
More information about the CMake
mailing list