Skip to content

cmake: Fix option parsing robustness issues

Craig Scott requested to merge craig.scott/cmake:cmake-option-parsing into master

The cmake command-line parsing logic did not always set valid source and binary directories. In particular, with the formal addition/documentation of the -S and -B options in CMake 3.13, specifying just -S would result in an empty string for the binary directory. This results in the root directory being used as the build directory and in the case of Windows where the current user will often have write access to there, CMake pollutes the root directory with build output. No error message was being logged. It is ambiguous whether specifying -S without -B is valid - the cmake command docs seem to show them as always specified together, but the 3.13 release notes state that they can be used independently. Either way though, it is already clear that people often try to use just -S without -B (I've seen multiple people do this), so best we make that behavior predictable and safe.

While clearing up the -S/-B handling and updating the tests, it also became clear that although an error message was logged if no script file name was given with the -P option, processing would continue with the -P omitted from the arguments. If the remaining arguments formed a valid set, cmake would continue processing and could have fairly arbitrary unintended side effects. This becomes more important with the -S/-B handling fixed since the likelihood of a valid set of options increases (source/binary directory defaults are applied more consistently now).

Merge request reports