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…

1

function(echo_target_property tgt prop)

1

# v for value, d for defined, s for set

1

get_property(vTARGET${tgt}PROPERTY${prop})

1

get_property(dTARGET${tgt}PROPERTY${prop}DEFINED)

1

get_property(sTARGET${tgt}PROPERTY${prop}SET)

1

&nbsp;

1

# only produce output for values that are set

1

if(s)

1

message("tgt='${tgt}' prop='${prop}'")

1

message(" value='${v}'")

1

message(" defined='${d}'")

1

message(" set='${s}'")

1

message("")

1

endif()

1

endfunction()

1

&nbsp;

1

function(echo_target tgt)

1

if(NOTTARGET${tgt})

1

message("There is no target named '${tgt}'")

1

return()

1

endif()

1

&nbsp;

1

set(props

1

DEBUG_OUTPUT_NAME

1

DEBUG_POSTFIX

1

RELEASE_OUTPUT_NAME

1

RELEASE_POSTFIX

1

ARCHIVE_OUTPUT_DIRECTORY

1

ARCHIVE_OUTPUT_DIRECTORY_DEBUG

1

ARCHIVE_OUTPUT_DIRECTORY_RELEASE

1

ARCHIVE_OUTPUT_NAME

1

ARCHIVE_OUTPUT_NAME_DEBUG

1

ARCHIVE_OUTPUT_NAME_RELEASE

1

AUTOMOC

1

AUTOMOC_MOC_OPTIONS

1

BUILD_WITH_INSTALL_RPATH

1

BUNDLE

1

BUNDLE_EXTENSION

1

COMPILE_DEFINITIONS

1

COMPILE_DEFINITIONS_DEBUG

1

COMPILE_DEFINITIONS_RELEASE

1

COMPILE_FLAGS

1

DEBUG_POSTFIX

1

RELEASE_POSTFIX

1

DEFINE_SYMBOL

1

ENABLE_EXPORTS

1

EXCLUDE_FROM_ALL

1

EchoString

1

FOLDER

1

FRAMEWORK

1

Fortran_FORMAT

1

Fortran_MODULE_DIRECTORY

1

GENERATOR_FILE_NAME

1

GNUtoMS

1

HAS_CXX

1

IMPLICIT_DEPENDS_INCLUDE_TRANSFORM

1

IMPORTED

1

IMPORTED_CONFIGURATIONS

1

IMPORTED_IMPLIB

1

IMPORTED_IMPLIB_DEBUG

1

IMPORTED_IMPLIB_RELEASE

1

IMPORTED_LINK_DEPENDENT_LIBRARIES

1

IMPORTED_LINK_DEPENDENT_LIBRARIES_DEBUG

1

IMPORTED_LINK_DEPENDENT_LIBRARIES_RELEASE

1

IMPORTED_LINK_INTERFACE_LANGUAGES

1

IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG

1

IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE

1

IMPORTED_LINK_INTERFACE_LIBRARIES

1

IMPORTED_LINK_INTERFACE_LIBRARIES_DEBUG

1

IMPORTED_LINK_INTERFACE_LIBRARIES_RELEASE

1

IMPORTED_LINK_INTERFACE_MULTIPLICITY

1

IMPORTED_LINK_INTERFACE_MULTIPLICITY_DEBUG

1

IMPORTED_LINK_INTERFACE_MULTIPLICITY_RELEASE

1

IMPORTED_LOCATION

1

IMPORTED_LOCATION_DEBUG

1

IMPORTED_LOCATION_RELEASE

1

IMPORTED_NO_SONAME

1

IMPORTED_NO_SONAME_DEBUG

1

IMPORTED_NO_SONAME_RELEASE

1

IMPORTED_SONAME

1

IMPORTED_SONAME_DEBUG

1

IMPORTED_SONAME_RELEASE

1

IMPORT_PREFIX

1

IMPORT_SUFFIX

1

INCLUDE_DIRECTORIES

1

INSTALL_NAME_DIR

1

INSTALL_RPATH

1

INSTALL_RPATH_USE_LINK_PATH

1

INTERPROCEDURAL_OPTIMIZATION

1

INTERPROCEDURAL_OPTIMIZATION_DEBUG

1

INTERPROCEDURAL_OPTIMIZATION_RELEASE

1

LABELS

1

LIBRARY_OUTPUT_DIRECTORY

1

LIBRARY_OUTPUT_DIRECTORY_DEBUG

1

LIBRARY_OUTPUT_DIRECTORY_RELEASE

1

LIBRARY_OUTPUT_NAME

1

LIBRARY_OUTPUT_NAME_DEBUG

1

LIBRARY_OUTPUT_NAME_RELEASE

1

LINKER_LANGUAGE

1

LINK_DEPENDS

1

LINK_FLAGS

1

LINK_FLAGS_DEBUG

1

LINK_FLAGS_RELEASE

1

LINK_INTERFACE_LIBRARIES

1

LINK_INTERFACE_LIBRARIES_DEBUG

1

LINK_INTERFACE_LIBRARIES_RELEASE

1

LINK_INTERFACE_MULTIPLICITY

1

LINK_INTERFACE_MULTIPLICITY_DEBUG

1

LINK_INTERFACE_MULTIPLICITY_RELEASE

1

LINK_SEARCH_END_STATIC

1

LINK_SEARCH_START_STATIC

1

LOCATION

1

LOCATION_DEBUG

1

LOCATION_RELEASE

1

MACOSX_BUNDLE

1

MACOSX_BUNDLE_INFO_PLIST

1

MACOSX_FRAMEWORK_INFO_PLIST

1

MAP_IMPORTED_CONFIG_DEBUG

1

MAP_IMPORTED_CONFIG_RELEASE

1

OSX_ARCHITECTURES

1

OSX_ARCHITECTURES_DEBUG

1

OSX_ARCHITECTURES_RELEASE

1

OUTPUT_NAME

1

OUTPUT_NAME_DEBUG

1

OUTPUT_NAME_RELEASE

1

POST_INSTALL_SCRIPT

1

PREFIX

1

PRE_INSTALL_SCRIPT

1

PRIVATE_HEADER

1

PROJECT_LABEL

1

PUBLIC_HEADER

1

RESOURCE

1

RULE_LAUNCH_COMPILE

1

RULE_LAUNCH_CUSTOM

1

RULE_LAUNCH_LINK

1

RUNTIME_OUTPUT_DIRECTORY

1

RUNTIME_OUTPUT_DIRECTORY_DEBUG

1

RUNTIME_OUTPUT_DIRECTORY_RELEASE

1

RUNTIME_OUTPUT_NAME

1

RUNTIME_OUTPUT_NAME_DEBUG

1

RUNTIME_OUTPUT_NAME_RELEASE

1

SKIP_BUILD_RPATH

1

SOURCES

1

SOVERSION

1

STATIC_LIBRARY_FLAGS

1

STATIC_LIBRARY_FLAGS_DEBUG

1

STATIC_LIBRARY_FLAGS_RELEASE

1

SUFFIX

1

TYPE

1

VERSION

1

VS_DOTNET_REFERENCES

1

VS_GLOBAL_WHATEVER

1

VS_GLOBAL_KEYWORD

1

VS_GLOBAL_PROJECT_TYPES

1

VS_KEYWORD

1

VS_SCC_AUXPATH

1

VS_SCC_LOCALPATH

1

VS_SCC_PROJECTNAME

1

VS_SCC_PROVIDER

1

VS_WINRT_EXTENSIONS

1

VS_WINRT_REFERENCES

1

WIN32_EXECUTABLE

1

XCODE_ATTRIBUTE_WHATEVER

1

)

1

&nbsp;

1

message("======================== ${tgt} ========================")

1

foreach(p${props})

1

echo_target_property("${t}""${p}")

1

endforeach()

1

message("")

1

endfunction()

1

&nbsp;

1

&nbsp;

1

function(echo_targets)

1

set(tgts${ARGV})

1

foreach(t${tgts})

1

echo_target("${t}")

1

endforeach()

1

endfunction()

1

&nbsp;

1

&nbsp;

1

set(targets

1

CMakeLib

1

cmake-gui

1

MathFunctions

1

Tutorial

1

vtkCommonCore

1

)

1

&nbsp;

1

echo_targets(${targets})

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.