2

I want to display contents of Foo::Bar with CMake. How to do it?

message(STATUS "Foo::Bar -> ${Foo::Bar}")

# example case : Boost
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost REQUIRED)

# I want to display contents of Boost::headers.
# I think it is contain header path. How to display it on CMake output?
# message(STATUS "Boost::headers -> ${Boost::headers}")
# I expect the following output.
# Boost::headers -> (path_to_boost_root)/include/boost-1_70
genpfault
  • 51,148
  • 11
  • 85
  • 139
ken
  • 65
  • 8
  • What is `Foo::Bar`? Is it a variable? A target? What is it? `I think it is contain header path.` no, Boost::headers is the name of a target, it will not "print" anything, there is no `Boost::headers` variable. – KamilCuk Jul 02 '19 at 20:05

1 Answers1

4

I know of two ways how to print properties of a target.

  1. For specific properties, you can use cmake_print_properties from CMakePrintHelpers.cmake.

cmake_print_properties(TARGETS Boost::headers PROPERTIES <property1> <property2> ...)

  1. To print all defined properties of a target, you can use this trick.

print_target_properties

# Get all properties 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_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)

And then you can call the function like this:

print_target_properties(Boost::headers)

Adam Badura
  • 5,069
  • 1
  • 35
  • 70
serkan.tuerker
  • 1,681
  • 10
  • 20