Set solution folder property in CMake for third party library
My project depends on a large third party library (OpenMVG). It is currently not installed. Official building instructions recommend using it with add_subdirectory
.
Therefore my CMakeLists.txt
contains the following lines
add_subdirectory(${OpenMVG_DIR}/src "${CMAKE_BINARY_DIR}/openMVG")
include_directories(${OpenMVG_INCLUDES})
add_executable(f_matching f_matching.cpp)
target_link_libraries(f_matching ${OpenMVG_LIBS})
My platform is Visual Studio and the generated solution contains 72 projects at the top level. I would like to use the FOLDER
CMake property to organize it in hierarchical levels. For example, I would like to move all OpenMVG projects to a separate VS solutions folder.
However, as the CMake documentation says, this property can only be assigned to targets, not directories. Mine CMakeLists.txt
contains
set_property(DIRECTORY ${OpenMVG_DIR}/src PROPERTY FOLDER OpenMVG)
but this line does nothing, solution folders are not shown. I tried to change a couple of OpenMVG CMakeLists by setting a property FOLDER
on the target, it worked.
Is there a workaround for this situation without changing the CMakeLists from OpenMVG? For the time being, I would like to spend my time on other tasks and postpone this simple edit for the future.
source to share
There is one "hack" to achieve your goal. The original version of 'add_library' is prefixed with an underscore, so you can re-implement the top-level function 'add_library' like this:
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# you need it to properly set variable from function() call
set(target_list "" CACHE INTERNAL "target_list")
function(add_library name)
message("Adding library ${name}")
set(target_list ${target_list} ${name} CACHE INTERNAL "target_list")
_add_library(${name} ${ARGN})
endfunction()
# Your add_subdirectory() goes here
set_target_properties(${target_list} PROPERTIES FOLDER "OpenMVG")
Of course, it is not documented and not official as I know, but it works, at least now with CMake 2.8.12.2.
source to share