Saving Command Line Configuration


I’d like to save the command line arguments users pass at configure time into a source file that will be included with my project so that users can access that information at run time when benchmarking.

Is there an easy way to do this? I haven’t been able to find a way.


Ideally I could run something like:
cmake -B build -DOPTION1=VAL1 -DOPTION2=VAL2

And I would store -B build -DOPTION1=VAL1 -DOPTION2=VAL2 in source.

As far as I know, getting the full command-line isn’t supported. But you can check the values of any variables, which should get you pretty close to what you want. I would probably have a template header with content such as:

static const char* const OPTION_1 = "@OPTION_1@";

and then use configure_file to substitute in all the variable values in the template header.

You cannot know what command line arguments were passed, and in generally you shouldn’t try. CMake can be re-run for various scenarios, and in those cases, the command line arguments will be different to previous runs. CMake relies heavily on caching things between runs. Any information you need to access should come from the cache variables, not from trying to work out command line arguments.

I believe that this feature request is of interest here.