From edf50561d4155a7dd8df21c653ca576d0aa727dc Mon Sep 17 00:00:00 2001 From: Scott McKay Date: Mon, 16 Sep 2024 11:21:43 +1000 Subject: [PATCH] Try again --- cmake/external/helper_functions.cmake | 59 +++++++++++ .../external/onnxruntime_external_deps.cmake | 97 ++++--------------- 2 files changed, 80 insertions(+), 76 deletions(-) diff --git a/cmake/external/helper_functions.cmake b/cmake/external/helper_functions.cmake index e3f2211f96158..f16b7baeeb240 100644 --- a/cmake/external/helper_functions.cmake +++ b/cmake/external/helper_functions.cmake @@ -14,6 +14,65 @@ function(set_folder_for_subdir_targets srcDir folderName) endforeach() endfunction() +# Add a new library and it's dependencies to an existing list. +# The new library and any dependencies it has that are not already in the list will be prepended to the existing list. +# output_var will be set to the combined list. +# +# e.g. libA is new, and depends on libB and libC. libB is already in extended_list. +# add_dependencies_to_external_lib(libA output_var "${existing_dependencies}") # need to quote existing list values +# before: existing_dependencies = [libB] +# after: output_var = [libA, libC, libB] +function(add_dependencies_to_external_libs new_lib output_var) + set(existing_deps ${ARGN}) + set(new_deps) + + function(get_dependencies input_target) + get_target_property(alias ${input_target} ALIASED_TARGET) + if(TARGET ${alias}) + set(input_target ${alias}) + endif() + + # if this already exists we don't need to recurse any more + if(${input_target} IN_LIST existing_deps OR ${input_target} IN_LIST new_deps) + return() + endif() + + list(APPEND new_deps ${input_target}) + + get_target_property(link_libraries ${input_target} LINK_LIBRARIES) + foreach(dependency IN LISTS link_libraries) + if(TARGET ${dependency}) + get_dependencies(${dependency}) + endif() + endforeach() + + # Add if needed. As this is to primarily update the items to link against, interface libraries shouldn't be relevant + # get_target_property(link_libraries ${input_target} INTERFACE_LINK_LIBRARIES) + # foreach(dependency IN LISTS link_libraries) + # if(TARGET ${dependency}) + # get_dependencies(${dependency}) + # endif() + # endforeach() + + set(new_deps ${new_deps} PARENT_SCOPE) + endfunction() + + message(STATUS "### Getting dependencies for ${new_lib}") + get_dependencies(${new_lib}) + + set(combined_deps) + foreach(dependency IN LISTS new_deps) + get_target_property(type ${dependency} TYPE) + if(${type} STREQUAL "STATIC_LIBRARY" OR ${type} STREQUAL "OBJECT_LIBRARY") + list(APPEND combined_deps ${dependency}) + endif() + endforeach() + + list(APPEND combined_deps ${existing_deps}) + message(STATUS "Combined: ${combined_deps}") + set(${output_var} ${combined_deps} PARENT_SCOPE) +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. diff --git a/cmake/external/onnxruntime_external_deps.cmake b/cmake/external/onnxruntime_external_deps.cmake index daec9ad75e061..5930b292eaf75 100644 --- a/cmake/external/onnxruntime_external_deps.cmake +++ b/cmake/external/onnxruntime_external_deps.cmake @@ -575,6 +575,11 @@ if (onnxruntime_USE_MIMALLOC) onnxruntime_fetchcontent_makeavailable(mimalloc) endif() +set(onnxruntime_EXTERNAL_LIBRARIES ${onnxruntime_EXTERNAL_LIBRARIES_XNNPACK} ${WIL_TARGET} nlohmann_json::nlohmann_json + onnx onnx_proto ${PROTOBUF_LIB} re2::re2 Boost::mp11 safeint_interface + flatbuffers::flatbuffers ${GSL_TARGET} ${ABSEIL_LIBS} date::date + ${ONNXRUNTIME_CLOG_TARGET_NAME}) + # The source code of onnx_proto is generated, we must build this lib first before starting to compile the other source code that uses ONNX protobuf types. # The other libs do not have the problem. All the sources are already there. We can compile them in any order. set(onnxruntime_EXTERNAL_DEPENDENCIES onnx_proto flatbuffers::flatbuffers) @@ -675,91 +680,31 @@ if (onnxruntime_USE_WEBGPU) set(DAWN_ENABLE_VULKAN OFF CACHE BOOL "" FORCE) endif() - if (ANDROID) - endif() - onnxruntime_fetchcontent_makeavailable(dawn) -endif() -message(STATUS "Finished fetching external dependencies") - -set(onnxruntime_LINK_DIRS ) - -if (onnxruntime_USE_CUDA) - find_package(CUDAToolkit REQUIRED) - - if(onnxruntime_CUDNN_HOME) - file(TO_CMAKE_PATH ${onnxruntime_CUDNN_HOME} onnxruntime_CUDNN_HOME) - set(CUDNN_PATH ${onnxruntime_CUDNN_HOME}) - endif() - include(cuDNN) + # Add with dependencies in reverse order as new values are added at the front in each call + add_dependencies_to_external_libs(dawn::dawn_proc onnxruntime_EXTERNAL_LIBRARIES "${onnxruntime_EXTERNAL_LIBRARIES}") + add_dependencies_to_external_libs(dawn::native onnxruntime_EXTERNAL_LIBRARIES "${onnxruntime_EXTERNAL_LIBRARIES}") endif() +set(onnxruntime_LINK_DIRS) if(onnxruntime_USE_SNPE) - include(external/find_snpe.cmake) + include(external/find_snpe.cmake) + list(APPEND onnxruntime_EXTERNAL_LIBRARIES ${SNPE_NN_LIBS}) endif() -# add dependencies to the list of external libraries and populate onnxruntime_EXTERNAL_LIBRARIES with the result -function(add_dependencies_to_external_libs output_var) - set (external_libs ${ARGN}) - set(extended_deps) - - function(get_dependencies input_target) - message(STATUS "get_dependencies: ${input_target}") - get_target_property(alias ${input_target} ALIASED_TARGET) - if(TARGET ${alias}) - set(input_target ${alias}) - endif() - - if(${input_target} IN_LIST all_dependencies) - return() - endif() - - list(APPEND all_dependencies ${input_target}) - - get_target_property(link_libraries ${input_target} LINK_LIBRARIES) - foreach(dependency IN LISTS link_libraries) - if(TARGET ${dependency}) - get_dependencies(${dependency}) - endif() - endforeach() - - # get_target_property(link_libraries ${input_target} INTERFACE_LINK_LIBRARIES) - # foreach(dependency IN LISTS link_libraries) - # if(TARGET ${dependency}) - # get_dependencies(${dependency}) - # endif() - # endforeach() - - set(all_dependencies ${all_dependencies} PARENT_SCOPE) - endfunction() - - foreach(external_lib IN LISTS external_libs) - message(STATUS "### Getting dependencies for : ${external_lib}") - get_dependencies(${external_lib}) - endforeach() - - foreach(dependency IN LISTS all_dependencies) - get_target_property(type ${dependency} TYPE) - if((${type} STREQUAL "STATIC_LIBRARY" OR ${type} STREQUAL "OBJECT_LIBRARY") AND - NOT ${dependency} IN_LIST external_libs_extended) - list(APPEND extended_deps ${dependency}) - endif() - endforeach() - - set(${output_var} ${extended_deps} PARENT_SCOPE) -endfunction() +if (onnxruntime_USE_CUDA) + find_package(CUDAToolkit REQUIRED) -# Create list of external libraries potentially added in this file. -set(_external_libraries ${onnxruntime_EXTERNAL_LIBRARIES_XNNPACK} ${SNPE_NN_LIBS} ${WIL_TARGET} - dawn::dawn_native dawn::dawn_proc nlohmann_json::nlohmann_json - onnx onnx_proto ${PROTOBUF_LIB} re2::re2 Boost::mp11 safeint_interface - flatbuffers::flatbuffers ${GSL_TARGET} ${ABSEIL_LIBS} date::date - ${ONNXRUNTIME_CLOG_TARGET_NAME}) + if(onnxruntime_CUDNN_HOME) + file(TO_CMAKE_PATH ${onnxruntime_CUDNN_HOME} onnxruntime_CUDNN_HOME) + set(CUDNN_PATH ${onnxruntime_CUDNN_HOME}) + endif() -# add the dependencies as well. this is need in some places where we have to process the full list of libraries -# e.g. iOS pre-linking. -add_dependencies_to_external_libs(onnxruntime_EXTERNAL_LIBRARIES "${_external_libraries}") + include(cuDNN) +endif() FILE(TO_NATIVE_PATH ${CMAKE_BINARY_DIR} ORT_BINARY_DIR) FILE(TO_NATIVE_PATH ${PROJECT_SOURCE_DIR} ORT_SOURCE_DIR) + +message(STATUS "Finished fetching external dependencies")