CMake Target Properties

A few months back, I wrote what I called a “tech-nish blog post” about obtaining the values of all the presently defined CMake variables. This is a companion post that does the same thing for target properties.

 

Here’s a CMake language function to print out all the target properties of a given CMake target. Try it out: come on, you know you want to…

 

 

Some things to notice about this code:

It lists targets from different projects in the “targets” variable. That’s ok because echo_target uses “if(NOT TARGET” to avoid trying to access a target that is not defined. You can save this code out as a CMake script, and then just include the script in a CMakeLists file, and use the echo_targets function with your own project-specific list of targets.

The echo_target_property function uses get_property in 3 different ways to get the value, the defined-ness and the set-ness of the property. Sometimes you just want to know if a target has a given property, and sometimes you need to know its actual value. The defined-ness of a property lets you know whether CMake has a definition (and usually a documentation string) for it.

echo_target_property only shows the values for properties that have been set.

echo_target defines a props variable that lists all the common target properties that CMake knows about. You may have additional properties you are interested in: if so, just add them to the list.

Any questions? 😉

One Response to CMake Target Properties

  1. anon says:

    There is a bug that prevents it from working. To fix it, replace
    echo_target_property(“${t}” “${p}”)
    with
    echo_target_property(“${tgt}” “${p}”)

Questions or comments are always welcome!