Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【第三方库离线编译】add onnxruntime cache #54346

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 59 additions & 13 deletions cmake/external/onnxruntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,32 @@ set(ONNXRUNTIME_LIB_DIR
CACHE PATH "onnxruntime lib directory." FORCE)
set(CMAKE_BUILD_RPATH "${CMAKE_BUILD_RPATH}" "${ONNXRUNTIME_LIB_DIR}")

set(ONNXRUNTIME_DOWNLOAD_DIR
${PADDLE_SOURCE_DIR}/third_party/onnxruntime/${CMAKE_SYSTEM_NAME})

if(WIN32)
set(ONNXRUNTIME_URL
"https://github.com/microsoft/onnxruntime/releases/download/v${ONNXRUNTIME_VERSION}/onnxruntime-win-x64-${ONNXRUNTIME_VERSION}.zip"
)
set(ONNXRUNTIME_URL_MD5 f21d6bd1feef15935a5f4e1007797593)
set(ONNXRUNTIME_CACHE_EXTENSION "zip")
elseif(APPLE)
set(ONNXRUNTIME_URL
"https://github.com/microsoft/onnxruntime/releases/download/v${ONNXRUNTIME_VERSION}/onnxruntime-osx-x86_64-${ONNXRUNTIME_VERSION}.tgz"
)
set(ONNXRUNTIME_URL_MD5 6a6f6b7df97587da59976042f475d3f4)
set(ONNXRUNTIME_CACHE_EXTENSION "tgz")
else()
set(ONNXRUNTIME_URL
"https://github.com/microsoft/onnxruntime/releases/download/v${ONNXRUNTIME_VERSION}/onnxruntime-linux-x64-${ONNXRUNTIME_VERSION}.tgz"
)
set(ONNXRUNTIME_URL_MD5 ce3f2376854b3da4b483d6989666995a)
set(ONNXRUNTIME_CACHE_EXTENSION "tgz")
endif()

set(ONNXRUNTIME_CACHE_FILENAME
"${ONNXRUNTIME_VERSION}.${ONNXRUNTIME_CACHE_EXTENSION}")

# For ONNXRUNTIME code to include internal headers.
include_directories(${ONNXRUNTIME_INC_DIR})

Expand Down Expand Up @@ -96,38 +108,72 @@ else()
CACHE FILEPATH "ONNXRUNTIME shared library." FORCE)
endif()

function(download_onnxruntime)
message(
STATUS
"Downloading ${ONNXRUNTIME_URL} to ${ONNXRUNTIME_DOWNLOAD_DIR}/${ONNXRUNTIME_CACHE_FILENAME}"
)
# NOTE: If the version is updated, consider emptying the folder; maybe add timeout
file(
DOWNLOAD ${ONNXRUNTIME_URL}
${ONNXRUNTIME_DOWNLOAD_DIR}/${ONNXRUNTIME_CACHE_FILENAME}
EXPECTED_MD5 ${ONNXRUNTIME_URL_MD5}
STATUS ERR)
if(ERR EQUAL 0)
message(STATUS "Download ${ONNXRUNTIME_CACHE_FILENAME} success")
else()
message(
FATAL_ERROR
"Download failed, error: ${ERR}\n You can try downloading ${ONNXRUNTIME_CACHE_FILENAME} again"
)
endif()
endfunction()

find_file(
LOCAL_ONNXRUNTIME_LIB_ZIP
NAMES ${ONNXRUNTIME_CACHE_FILENAME}
PATHS ${ONNXRUNTIME_DOWNLOAD_DIR}
NO_DEFAULT_PATH)

if(LOCAL_ONNXRUNTIME_LIB_ZIP)
file(MD5 ${ONNXRUNTIME_DOWNLOAD_DIR}/${ONNXRUNTIME_CACHE_FILENAME}
ONNXRUNTIME_MD5)
if(NOT ONNXRUNTIME_MD5 EQUAL ONNXRUNTIME_URL_MD5)
download_onnxruntime()
endif()
else()
download_onnxruntime()
endif()

if(WIN32)
ExternalProject_Add(
${ONNXRUNTIME_PROJECT}
${EXTERNAL_PROJECT_LOG_ARGS}
URL ${ONNXRUNTIME_URL}
URL ${ONNXRUNTIME_DOWNLOAD_DIR}/${ONNXRUNTIME_CACHE_FILENAME}
URL_MD5 ${ONNXRUNTIME_URL_MD5}
PREFIX ${ONNXRUNTIME_PREFIX_DIR}
DOWNLOAD_NO_PROGRESS 1
DOWNLOAD_DIR ${ONNXRUNTIME_DOWNLOAD_DIR}
SOURCE_DIR ${ONNXRUNTIME_INSTALL_DIR}
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
UPDATE_COMMAND ""
INSTALL_COMMAND
${CMAKE_COMMAND} -E copy ${ONNXRUNTIME_SOURCE_LIB}
${ONNXRUNTIME_SHARED_LIB} && ${CMAKE_COMMAND} -E copy
${ONNXRUNTIME_SOURCE_DIR}/lib/onnxruntime.lib ${ONNXRUNTIME_LIB} &&
${CMAKE_COMMAND} -E copy_directory ${ONNXRUNTIME_SOURCE_DIR}/include
${ONNXRUNTIME_INC_DIR}
INSTALL_COMMAND ""
BUILD_BYPRODUCTS ${ONNXRUNTIME_LIB})
else()
ExternalProject_Add(
${ONNXRUNTIME_PROJECT}
${EXTERNAL_PROJECT_LOG_ARGS}
URL ${ONNXRUNTIME_URL}
URL ${ONNXRUNTIME_DOWNLOAD_DIR}/${ONNXRUNTIME_CACHE_FILENAME}
URL_MD5 ${ONNXRUNTIME_URL_MD5}
PREFIX ${ONNXRUNTIME_PREFIX_DIR}
DOWNLOAD_NO_PROGRESS 1
DOWNLOAD_DIR ${ONNXRUNTIME_DOWNLOAD_DIR}
SOURCE_DIR ${ONNXRUNTIME_INSTALL_DIR}
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
UPDATE_COMMAND ""
INSTALL_COMMAND
${CMAKE_COMMAND} -E copy ${ONNXRUNTIME_SOURCE_LIB} ${ONNXRUNTIME_LIB} &&
${CMAKE_COMMAND} -E copy_directory ${ONNXRUNTIME_SOURCE_DIR}/include
${ONNXRUNTIME_INC_DIR} && ${CMAKE_COMMAND} -E create_symlink
${ONNXRUNTIME_LIB_NAME} ${ONNXRUNTIME_LIB_DIR}/${ONNXRUNTIME_LIB_NEW_NAME}
INSTALL_COMMAND ""
BUILD_BYPRODUCTS ${ONNXRUNTIME_LIB})
endif()

Expand Down