Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print all the properties of a target in cmake?

Tags:

cmake

With the following cmake scrpt:

get_cmake_property(_variableNames VARIABLES) foreach (_variableName ${_variableNames})     message(STATUS "${_variableName}=${${_variableName}}") endforeach() 

We can print all the variables in the CMake project. Then my question is: is there a function that can print all the properties of a target?

like image 506
feelfree Avatar asked Aug 24 '15 13:08

feelfree


People also ask

What are target properties in CMake?

Compiler flags, definitions, source files, include folders, link libraries, and linker options are properties of a target. Avoid using variables to express dependencies between targets: use the visibility levels PRIVATE , INTERFACE , PUBLIC and let CMake figure out the details.

How do I print all variables in CMake?

Another way to view all cmake's internal variables, is by executing cmake with the --trace-expand option. This will give you a trace of all . cmake files executed and variables set on each line. Show activity on this post.

How do I use CMake variables?

You can use the command line to set entries in the Cache with the syntax cmake -D var:type=value , just cmake -D var=value or with cmake -C CMakeInitialCache. cmake . You can unset entries in the Cache with unset(... CACHE) .


2 Answers

I have had limited success with the following work-around to the apparent lack of ability to dynamically query for the properties of a target.

I invoke the cmake command to list all properties, and then try each one on the target.

# Get all propreties that cmake supports if(NOT CMAKE_PROPERTY_LIST)     execute_process(COMMAND cmake --help-property-list OUTPUT_VARIABLE CMAKE_PROPERTY_LIST)          # Convert command output into a CMake list     string(REGEX REPLACE ";" "\\\\;" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}")     string(REGEX REPLACE "\n" ";" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}") endif()      function(print_properties)     message("CMAKE_PROPERTY_LIST = ${CMAKE_PROPERTY_LIST}") endfunction()      function(print_target_properties target)     if(NOT TARGET ${target})       message(STATUS "There is no target named '${target}'")       return()     endif()      foreach(property ${CMAKE_PROPERTY_LIST})         string(REPLACE "<CONFIG>" "${CMAKE_BUILD_TYPE}" property ${property})          # Fix https://stackoverflow.com/questions/32197663/how-can-i-remove-the-the-location-property-may-not-be-read-from-target-error-i         if(property STREQUAL "LOCATION" OR property MATCHES "^LOCATION_" OR property MATCHES "_LOCATION$")             continue()         endif()          get_property(was_set TARGET ${target} PROPERTY ${property} SET)         if(was_set)             get_target_property(value ${target} ${property})             message("${target} ${property} = ${value}")         endif()     endforeach() endfunction() 
like image 72
AlwaysTraining Avatar answered Oct 06 '22 23:10

AlwaysTraining


This isn't really intended to be a competing answer, just an expansion on the answer by AlwaysTraining (and edited by Dawid Drozd)!

If the target is an INTERFACE_LIBRARY, this will print out a whole bunch of errors about whitelisted properties. Unfortunately, there doesn't seem to be a good way to dynamically query what the whitelisted properties are - by checking the source for cmTargetPropertyComputer::WhiteListedInterfaceProperty I was able to come up with a regexp, but it could change from version to version (I was looking at the source for cmake-3.11). Anyway, here's my revised version, with support for INTERFACE_LIBRARYtargets - I also removes duplicate, and made the filtering of LOCATION properties happen once, outside of the loop.

(I've made a suggested edit to the original question, but this is here in case it isn't accepted...)

Thanks AlwaysTraining and Dawid Drozd!

# Get all propreties that cmake supports execute_process(COMMAND cmake --help-property-list OUTPUT_VARIABLE CMAKE_PROPERTY_LIST)  # Convert command output into a CMake list STRING(REGEX REPLACE ";" "\\\\;" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}") STRING(REGEX REPLACE "\n" ";" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}") # Fix https://stackoverflow.com/questions/32197663/how-can-i-remove-the-the-location-property-may-not-be-read-from-target-error-i list(FILTER CMAKE_PROPERTY_LIST EXCLUDE REGEX "^LOCATION$|^LOCATION_|_LOCATION$") # For some reason, "TYPE" shows up twice - others might too? list(REMOVE_DUPLICATES CMAKE_PROPERTY_LIST)  # build whitelist by filtering down from CMAKE_PROPERTY_LIST in case cmake is # a different version, and one of our hardcoded whitelisted properties # doesn't exist! unset(CMAKE_WHITELISTED_PROPERTY_LIST) foreach(prop ${CMAKE_PROPERTY_LIST})     if(prop MATCHES "^(INTERFACE|[_a-z]|IMPORTED_LIBNAME_|MAP_IMPORTED_CONFIG_)|^(COMPATIBLE_INTERFACE_(BOOL|NUMBER_MAX|NUMBER_MIN|STRING)|EXPORT_NAME|IMPORTED(_GLOBAL|_CONFIGURATIONS|_LIBNAME)?|NAME|TYPE|NO_SYSTEM_FROM_IMPORTED)$")         list(APPEND CMAKE_WHITELISTED_PROPERTY_LIST ${prop})     endif() endforeach(prop)  function(print_properties)     message ("CMAKE_PROPERTY_LIST = ${CMAKE_PROPERTY_LIST}") endfunction(print_properties)  function(print_whitelisted_properties)     message ("CMAKE_WHITELISTED_PROPERTY_LIST = ${CMAKE_WHITELISTED_PROPERTY_LIST}") endfunction(print_whitelisted_properties)  function(print_target_properties tgt)     if(NOT TARGET ${tgt})       message("There is no target named '${tgt}'")       return()     endif()      get_target_property(target_type ${tgt} TYPE)     if(target_type STREQUAL "INTERFACE_LIBRARY")         set(PROP_LIST ${CMAKE_WHITELISTED_PROPERTY_LIST})     else()         set(PROP_LIST ${CMAKE_PROPERTY_LIST})     endif()      foreach (prop ${PROP_LIST})         string(REPLACE "<CONFIG>" "${CMAKE_BUILD_TYPE}" prop ${prop})         # message ("Checking ${prop}")         get_property(propval TARGET ${tgt} PROPERTY ${prop} SET)         if (propval)             get_target_property(propval ${tgt} ${prop})             message ("${tgt} ${prop} = ${propval}")         endif()     endforeach(prop) endfunction(print_target_properties) 
like image 24
Paul Molodowitch Avatar answered Oct 06 '22 23:10

Paul Molodowitch