Search code examples
visual-studiocmakevcpkg

Set a target VS_GLOBAL property that applies to all targets using CMake


I would like to set a target property that applies to all targets in a CMake project. Specifically, I want to disable vcpkg integration in Visual Studio. I can do it on target by target basis with the following:

set_target_properties(${mytarget} PROPERTIES VS_GLOBAL_VcpkgEnabled FALSE)

But I don't know how to set globally for all targets.


Solution

  • I stumbled upon a similar problem. The solution I discovered involves setting the properties to all targets.

    function(get_all_targets var)
        set(targets)
        get_all_targets_recursive(targets ${CMAKE_CURRENT_SOURCE_DIR})
        set(${var} ${targets} PARENT_SCOPE)
    endfunction()
    macro(get_all_targets_recursive targets dir)
        get_property(subdirectories DIRECTORY ${dir} PROPERTY SUBDIRECTORIES)
        foreach(subdir ${subdirectories})
            get_all_targets_recursive(${targets} ${subdir})
        endforeach()
    
        get_property(current_targets DIRECTORY ${dir} PROPERTY BUILDSYSTEM_TARGETS)
        list(APPEND ${targets} ${current_targets})
    endmacro()
    
    set_target_properties(${all_targets} PROPERTIES VS_GLOBAL_VcpkgEnabled false)
    

    Based on this resource: https://newbedev.com/how-do-i-iterate-over-all-cmake-targets-programmatically