From c97cc5c1b0dacefc125af4aa9a37c4020c9c2ee2 Mon Sep 17 00:00:00 2001 From: Scott McKay Date: Fri, 16 Aug 2024 15:51:50 +1000 Subject: [PATCH] Put all external project targets under the 'External' folder in VS (#21765) ### Description Handle targets in subdirectories for external projects. All targets will now go in a per-project folder under 'External' e.g. gmock and gtest now get handled correctly and are under External/googletest vs. existing setup where they ended up as top-level projects. ![image](https://github.com/user-attachments/assets/99ec259c-47cd-44f3-954d-58569c941cc2) ### Motivation and Context Improve developer experience. --- cmake/external/helper_functions.cmake | 46 +++++++++++++++++++-------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/cmake/external/helper_functions.cmake b/cmake/external/helper_functions.cmake index eefb3ba2e800a..e3f2211f96158 100644 --- a/cmake/external/helper_functions.cmake +++ b/cmake/external/helper_functions.cmake @@ -1,6 +1,19 @@ # Distributed under the OSI-approved BSD 3-Clause License. See accompanying # file Copyright.txt or https://cmake.org/licensing for details. +# Recursively set the folder for all targets in the subdirectories of the given source directory. +function(set_folder_for_subdir_targets srcDir folderName) + get_property(subdirs DIRECTORY "${srcDir}" PROPERTY SUBDIRECTORIES) + foreach(subdir ${subdirs}) + get_property(subdir_import_targets DIRECTORY "${subdir}" PROPERTY BUILDSYSTEM_TARGETS) + foreach(subdir_target ${subdir_import_targets}) + set_target_properties(${subdir_target} PROPERTIES FOLDER ${folderName}) + endforeach() + + set_folder_for_subdir_targets(${subdir} ${folderName}) + endforeach() +endfunction() + # This file was copied from cmake source with modifications: # 1. Add the EXCLUDE_FROM_ALL keyword when this function calls add_subdirectory. It will also resolve the # 'make install' issue. @@ -165,23 +178,28 @@ macro(onnxruntime_fetchcontent_makeavailable) else() add_subdirectory(${__cmake_srcdir} ${${__cmake_contentNameLower}_BINARY_DIR} EXCLUDE_FROM_ALL) endif() - get_property(subdir_import_targets DIRECTORY "${__cmake_srcdir}" PROPERTY BUILDSYSTEM_TARGETS) - foreach(subdir_target ${subdir_import_targets}) - if(TARGET ${subdir_target}) - get_target_property(subdir_target_type ${subdir_target} TYPE) - if(subdir_target_type STREQUAL "EXECUTABLE") - get_target_property(subdir_target_osx_arch ${subdir_target} OSX_ARCHITECTURES) - if (subdir_target_osx_arch) - if (NOT ${CMAKE_HOST_SYSTEM_PROCESSOR} IN_LIST subdir_target_osx_arch) - message("Added an executable target ${subdir_target} but it can not run natively on ${CMAKE_HOST_SYSTEM_PROCESSOR}, we will try to modify it") - endif() + + get_property(subdir_import_targets DIRECTORY "${__cmake_srcdir}" PROPERTY BUILDSYSTEM_TARGETS) + + foreach(subdir_target ${subdir_import_targets}) + if(TARGET ${subdir_target}) + get_target_property(subdir_target_type ${subdir_target} TYPE) + if(subdir_target_type STREQUAL "EXECUTABLE") + get_target_property(subdir_target_osx_arch ${subdir_target} OSX_ARCHITECTURES) + if (subdir_target_osx_arch) + if (NOT ${CMAKE_HOST_SYSTEM_PROCESSOR} IN_LIST subdir_target_osx_arch) + message("Added an executable target ${subdir_target} but it can not run natively on ${CMAKE_HOST_SYSTEM_PROCESSOR}, we will try to modify it") endif() endif() - set_target_properties(${subdir_target} PROPERTIES FOLDER "External") - set_target_properties(${subdir_target} PROPERTIES COMPILE_WARNING_AS_ERROR OFF) endif() - endforeach() - set(CMAKE_SKIP_INSTALL_RULES FALSE) + set_target_properties(${subdir_target} PROPERTIES FOLDER "External/${__cmake_contentName}") + set_target_properties(${subdir_target} PROPERTIES COMPILE_WARNING_AS_ERROR OFF) + endif() + endforeach() + set(CMAKE_SKIP_INSTALL_RULES FALSE) + + # set the FOLDER property for all targets contained in source directory and subfolders + set_folder_for_subdir_targets(${__cmake_srcdir} "External/${__cmake_contentName}") endif() unset(__cmake_srcdir)