Skip to content

Commit

Permalink
Put all external project targets under the 'External' folder in VS (#…
Browse files Browse the repository at this point in the history
…21765)

### Description
<!-- Describe your changes. -->
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
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
Improve developer experience.
  • Loading branch information
skottmckay authored Aug 16, 2024
1 parent ef2ccc4 commit c97cc5c
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions cmake/external/helper_functions.cmake
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c97cc5c

Please sign in to comment.