Skip to content

PrintHelpers: Fix target SOURCES property

Frank Dana requested to merge ferdnyc/cmake:print-sources into master

Bugfix to CMakePrintHelpers.cmake

It's been a long-standing bug (#14883 (closed)) in CMakePrintHelpers that the cmake_print_properties() function cannot print the SOURCES property of a requested TARGET, confusing it with a request to print properties of SOURCES.

This MR works around this by parsing the arguments in two stages, so that a SOURCES that comes after the PROPERTIES keyword is handled differently from a SOURCES that comes before it.

This adds the new restriction that the "mode"/scope keyword (TARGETS SOURCES DIRECTORIES etc...) and its arguments must precede the PROPERTIES keyword and its arguments. In other words:

  1. Both of these are now valid and will be interpreted correctly, whereas previously only the first was, and the second caused a FATAL_ERROR:

    cmake_print_properties(SOURCES foo.c PROPERTIES LANGUAGE)
    cmake_print_properties(TARGETS foo PROPERTIES SOURCES)
  2. This, OTOH, which used to be valid, no longer is, and will now trigger a FATAL_ERROR:

    cmake_print_properties(PROPERTIES LANGUAGE SOURCES foo.c)

The new restriction is somewhat unfortunate (the unfortunate part being that it wasn't previously imposed, but should have been), however the only realistic alternative would be to change the scope keyword for SOURCE type requests from SOURCES to SOURCE_FILES, which would be at least as big of a breaking change.

Tests added

In addition, after what felt like a half-dozen false starts flailing around inside the Tests/ directory, a second commit also adds basic functionality tests for the CMakePrintHelpers module, in the directory Tests/RunCMake/PrintHelpers/. Tests added are:

  • Variables: Test the results of a cmake_print_variables() call on two variables set within the test script.

  • Properties: Test cmake_print_properties() calls on a pair of SOURCES and a pair of TARGETS, printing some basic properties.

  • PropertiesSources: Specifically test the fix implemented in the previous commit, by calling cmake_print_properties(TARGET ___ PROPERTIES SOURCES) which, in any CMake prior to this fix, produced a FATAL_ERROR.

Hopefully I haven't made too much of a disaster of the test construction.

Additional housekeeping

In addition to fixing the bug, the commits in this MR:

  1. Update the module documentation to display a more accurate grammar for the cmake_print_properties() command (using the more modern notation found in the documentation for commands like set_property()), and to make the argument order restrictions explicit.

  2. Remove PROPERTIES from the list of scope keywords presented in the error messages when cmake_print_properties() fails. Instead, the ${multiValueArgs} argument to the second-stage cmake_parse_arguments() call is used to construct an error message that will always accurately reflect the currently-implemented syntax.

Fixes: #14883 (closed)

Edited by Frank Dana

Merge request reports