Skip to content

Instantly share code, notes, and snippets.

@brccabral
Last active April 24, 2025 21:14
Show Gist options
  • Save brccabral/d85e7e737926d714b4711a1f36b0e700 to your computer and use it in GitHub Desktop.
Save brccabral/d85e7e737926d714b4711a1f36b0e700 to your computer and use it in GitHub Desktop.
CMake helper functions

CMake print all variables matching pattern

Helper function that prints all variables matching pattern

function(print_cmake_variables_matching pattern)
    get_cmake_property(_variableNames VARIABLES)
    string(TOLOWER "${pattern}" pattern_lower)
    foreach (_variableName ${_variableNames})
        string(TOLOWER "${_variableName}" variable_lower)
        if (variable_lower MATCHES "${pattern_lower}")
            message(STATUS "${_variableName}=${${_variableName}}")
        endif()
    endforeach()
endfunction()

print_cmake_variables_matching("^OpenCV")

print given properties

function(print_target_properties target)
    get_all_properties(_props)
    foreach(_prop IN LISTS _props)
        get_target_property(_value ${target} ${_prop})
        if(NOT _value STREQUAL "NOTFOUND")
            message(STATUS "${target} - ${_prop} = ${_value}")
        endif()
    endforeach()
endfunction()

# Helper function to get all known target properties
function(get_all_properties out_var)
    set(_all_props
        # General
        NAME
        TYPE
        IMPORTED
        LINK_LIBRARIES
        LINK_INTERFACE_LIBRARIES
        INTERFACE_LINK_LIBRARIES
        INTERFACE_LINK_DEPENDS
        INTERFACE_LINK_OPTIONS
        INTERFACE_INCLUDE_DIRECTORIES
        INTERFACE_COMPILE_DEFINITIONS
        INTERFACE_COMPILE_FEATURES
        INTERFACE_COMPILE_OPTIONS
        INTERFACE_SOURCES
        SOURCES
        INCLUDE_DIRECTORIES
        COMPILE_DEFINITIONS
        COMPILE_OPTIONS
        COMPILE_FEATURES
        POSITION_INDEPENDENT_CODE
        C_STANDARD
        CXX_STANDARD
        CUDA_STANDARD
        LINK_OPTIONS
        OUTPUT_NAME
        VERSION
        ARCHIVE_OUTPUT_DIRECTORY
        LIBRARY_OUTPUT_DIRECTORY
        RUNTIME_OUTPUT_DIRECTORY
    )
    set(${out_var} "${_all_props}" PARENT_SCOPE)
endfunction()

print_target_properties(MyTarget)

dynamically gets all properties of a target using CMake 3.19+

function(print_all_target_properties target)
    get_target_property(_dummy ${target} NAME)
    if(_dummy STREQUAL "NOTFOUND")
        message(FATAL_ERROR "Target '${target}' does not exist.")
    endif()

    get_property(_props GLOBAL PROPERTY TARGET_PROPERTIES)
    list(REMOVE_DUPLICATES _props)

    foreach(_prop IN LISTS _props)
        get_target_property(_value ${target} ${_prop})
        if(NOT _value STREQUAL "NOTFOUND")
            message(STATUS "${target} - ${_prop} = ${_value}")
        endif()
    endforeach()
endfunction()

print_all_target_properties(MyTarget)

Compile Windows C Runtime statically

set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDebug$<$<CONFIG:Debug>Debug>")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Release>Release>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment