CMake User String Input

4.3k views Asked by At

I am attempting to use CMake to convert a generic wrapper into a specific one, via token replacement. I hope that all the user would have to do is input a specific set of strings, have CMake do configure_file, and the wrapper would read in the values and work as intended.

I am aware of the possibility to use set to set the token that needs to be replaced:

set(FAV_FOOD "Sushi" CACHE STRING "What is your favorite food?")

As well as the option to have the user select from a set of answers like so:

set(MY_SELECTION "Option A" CACHE STRING "Help message for this variable")
set_property(
    CACHE MY_SELECTION
    PROPERTY STRINGS
    "Option A" "Option B" "Option C"
)

The problem with this is that I can't enumerate all valid answers. Is there any way for CMake to have a GUI pop up and allow the user to answer with any string? I could just have the user fill out these values in a file before calling make, but I'd like to avoid the users doing anything by hand in the files, and I want to take advance of the CMake cache and avoid assuming that the user has already filled out the variables in a file.

Any advice would be most helpful. Thanks.

1

There are 1 answers

1
taranaki On BEST ANSWER

Your first example is the standard way to provide user-specified options to CMake, typically including some suitable default value just as you have demonstrated.

You could omit the default value (pass the empty string) and then check to see if the value has been specified so an error will be generated when the user tries to configure.

In order to provide these values automatically the user can specific them on the command line using -D= syntax:

cmake -DSOME_VAR=some_val <path_to_CMakeLists.txt>

I typically use cmake/ccmake, and I assume you are using windows cmake-gui, which is very similar to ccmake. I know of no method to pop up some additional windows using cmake-gui. However, it does appear possible to invoke cmake-gui with the aforementioned options, as described here. Using this method you could provide a .bat file the user could edit in order to enter the settings that you wish them to specify.

In my opinion, as long as you're already having them run the cmake-gui, then simply utilizing your first proposed solution and letting them change the values in the gui "the old fashioned way" is really the most appropriate option.