From 99870c83b3686d14f82443a200f8fad129b5ec2b Mon Sep 17 00:00:00 2001 From: jiangjiajun Date: Thu, 28 Jul 2022 21:56:13 +0800 Subject: [PATCH 1/4] Polish compilation on Windows --- CMakeLists.txt | 31 ++++++++++++++++++++----- FastDeploy.cmake.in | 36 ++++++++++++++--------------- external/opencv.cmake | 7 ++++++ external/utils.cmake | 4 ++-- third_party/yaml-cpp/CMakeLists.txt | 16 ++++--------- 5 files changed, 56 insertions(+), 38 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5aef0b56ed3..8a57c2a9de3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,6 +71,13 @@ if(WIN32) endif() endif() +if(WIN32 AND ENABLE_VISION) + add_definitions(-DYAML_CPP_DLL) + set(YAML_BUILD_SHARED_LIBS ON) + set(YAML_CPP_INSTALL ON) + set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) +endif() + if(ENABLE_DEBUG) add_definitions(-DFASTDEPLOY_DEBUG) endif() @@ -180,6 +187,7 @@ if(ENABLE_VISION) list(APPEND ALL_DEPLOY_SRCS ${DEPLOY_VISION_SRCS}) include_directories(${PROJECT_SOURCE_DIR}/third_party/yaml-cpp/include) include(external/opencv.cmake) + list(APPEND DEPEND_LIBS ${OpenCV_LIBS}) if(ENABLE_VISION_VISUALIZE) add_definitions(-DENABLE_VISION_VISUALIZE) @@ -207,7 +215,6 @@ set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS_RELEASE -s) file(READ "${PROJECT_SOURCE_DIR}/VERSION_NUMBER" FASTDEPLOY_VERSION) string(STRIP "${FASTDEPLOY_VERSION}" FASTDEPLOY_VERSION) if (APPLE) -# set_target_properties(fastdeploy PROPERTIES LINK_FLAGS "-undefined dynamic_lookup") set_target_properties(${LIBRARY_NAME} PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") elseif(MSVC) else() @@ -221,6 +228,10 @@ endif() # list(APPEND DEPEND_LIBS OpenMP::OpenMP_CXX) #endif() set_target_properties(${LIBRARY_NAME} PROPERTIES VERSION ${FASTDEPLOY_VERSION}) +if(MSVC) + # disable warnings for dll export + target_compile_options(${LIBRARY_NAME} PRIVATE /wd4251) +endif() target_link_libraries(${LIBRARY_NAME} ${DEPEND_LIBS}) # add examples after prepare include paths for third-parties @@ -232,11 +243,19 @@ endif() include(external/summary.cmake) fastdeploy_summary() - -install( - TARGETS ${LIBRARY_NAME} - LIBRARY DESTINATION lib -) +if(WIN32) + install( + TARGETS ${LIBRARY_NAME} + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + RUNTIME DESTINATION lib + ) +else() + install( + TARGETS ${LIBRARY_NAME} + LIBRARY DESTINATION lib + ) +endif() install( DIRECTORY ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy DESTINATION ${CMAKE_INSTALL_PREFIX}/include diff --git a/FastDeploy.cmake.in b/FastDeploy.cmake.in index ccf2a574b3b..531b6b85a68 100644 --- a/FastDeploy.cmake.in +++ b/FastDeploy.cmake.in @@ -52,13 +52,6 @@ if(WITH_GPU) list(APPEND FASTDEPLOY_LIBS ${CUDA_LIB}) if (ENABLE_TRT_BACKEND) -# if (NOT TRT_DIRECTORY) -# message(FATAL_ERROR "[FastDeploy] Please define TRT_DIRECTORY, e.g -DTRT_DIRECTORY=/usr/downloads/TensorRT-8.4.1.0") -# endif() -# find_library(TRT_INFER_LIB nvinfer ${TRT_DIRECTORY}/lib) -# find_library(TRT_ONNX_LIB nvonnxparser ${TRT_DIRECTORY}/lib) -# find_library(TRT_CAFFE_LIB nvcaffe_parser ${TRT_DIRECTORY}/lib) -# find_library(TRT_PLUGIN_LIB nvinfer_plugin ${TRT_DIRECTORY}/lib) find_library(TRT_INFER_LIB nvinfer ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tensorrt/lib) find_library(TRT_ONNX_LIB nvonnxparser ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tensorrt/lib) find_library(TRT_CAFFE_LIB nvcaffe_parser ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tensorrt/lib) @@ -68,18 +61,25 @@ if(WITH_GPU) endif() if(ENABLE_VISION) - find_library(OPENCV_CORE_LIB opencv_core ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/lib) - find_library(OPENCV_HIGHGUI_LIB opencv_highgui ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/lib) - find_library(OPENCV_IMGPROC_LIB opencv_imgproc ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/lib) - find_library(OPENCV_IMGCODESC_LIB opencv_imgcodecs ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/lib) - list(APPEND FASTDEPLOY_LIBS ${OPENCV_CORE_LIB} ${OPENCV_HIGHGUI_LIB} ${OPENCV_IMGPROC_LIB} ${OPENCV_IMGCODESC_LIB}) - list(APPEND FASTDEPLOY_INCS ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/include) + if(WIN32) + set(OpenCV_DIR ${CMAKE_CURRENT_LIST_DIR}/install/opencv-win-x64-3.4.16/build) + find_package(OpenCV REQUIRED PATHS ${OpenCV_DIR}) + list(APPEND FASTDEPLOY_INCS ${OpenCV_INCLUDE_DIRS}) + list(APPEND FASTDEPLOY_LIBS ${OpenCV_LIBS}) + else() + find_library(OPENCV_CORE_LIB opencv_core ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/lib) + find_library(OPENCV_HIGHGUI_LIB opencv_highgui ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/lib) + find_library(OPENCV_IMGPROC_LIB opencv_imgproc ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/lib) + find_library(OPENCV_IMGCODESC_LIB opencv_imgcodecs ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/lib) + list(APPEND FASTDEPLOY_LIBS ${OPENCV_CORE_LIB} ${OPENCV_HIGHGUI_LIB} ${OPENCV_IMGPROC_LIB} ${OPENCV_IMGCODESC_LIB}) + list(APPEND FASTDEPLOY_INCS ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/include) - if(ENABLE_OPENCV_CUDA) - find_library(OPENCV_CUDAARITHM_LIB opencv_core ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/lib) - find_library(OPENCV_CUDAIMGPROC_LIB opencv_core ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/lib) - find_library(OPENCV_CUDAWARPING_LIB opencv_core ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/lib) - list(APPEND FASTDEPLOY_LIBS ${OPENCV_CUDAARITHM_LIB} ${OPENCV_CUDAIMGPROC_LIB} ${OPENCV_CUDAWARPING_LIB}) + if(ENABLE_OPENCV_CUDA) + find_library(OPENCV_CUDAARITHM_LIB opencv_core ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/lib) + find_library(OPENCV_CUDAIMGPROC_LIB opencv_core ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/lib) + find_library(OPENCV_CUDAWARPING_LIB opencv_core ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/opencv/lib) + list(APPEND FASTDEPLOY_LIBS ${OPENCV_CUDAARITHM_LIB} ${OPENCV_CUDAIMGPROC_LIB} ${OPENCV_CUDAWARPING_LIB}) + endif() endif() endif() diff --git a/external/opencv.cmake b/external/opencv.cmake index 6762e0640b7..975e9974262 100644 --- a/external/opencv.cmake +++ b/external/opencv.cmake @@ -12,8 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. + +set(OPENCV_INSTALL_DIR ${THIRD_PARTY_PATH}/install/) + if(WIN32) + set(OPENCV_URL https://bj.bcebos.com/paddle2onnx/libs/opencv-win-x64-3.4.16.zip) + download_and_decompress(${OPENCV_URL} ${CMAKE_CURRENT_BINARY_DIR}/opencv-win-x64-3.4.16.zip ${THIRD_PARTY_PATH}/install/) + set(OpenCV_DIR ${THIRD_PARTY_PATH}/install/opencv-win-x64-3.4.16/build) find_package(OpenCV REQUIRED PATHS ${OpenCV_DIR}) + include_directories(${OpenCV_INCLUDE_DIRS}) list(APPEND DEPEND_LIBS ${OpenCV_LIBS}) else() diff --git a/external/utils.cmake b/external/utils.cmake index f5d69720423..1a203ee96a3 100644 --- a/external/utils.cmake +++ b/external/utils.cmake @@ -16,8 +16,8 @@ endfunction() function(download_and_decompress url filename decompress_dir) if(NOT EXISTS ${filename}) - message("Downloading file from ${url} ...") - file(DOWNLOAD ${url} "${filename}.tmp" SHOW_PROGRESS) + message("Downloading file from ${url} to ${filename} ...") + file(DOWNLOAD ${url} "${filename}.tmp") file(RENAME "${filename}.tmp" ${filename}) endif() if(NOT EXISTS ${decompress_dir}) diff --git a/third_party/yaml-cpp/CMakeLists.txt b/third_party/yaml-cpp/CMakeLists.txt index 94771d7cd35..ffd18b316d8 100644 --- a/third_party/yaml-cpp/CMakeLists.txt +++ b/third_party/yaml-cpp/CMakeLists.txt @@ -168,20 +168,12 @@ configure_file(yaml-cpp.pc.in yaml-cpp.pc @ONLY) if (YAML_CPP_INSTALL) install(TARGETS yaml-cpp EXPORT yaml-cpp-targets - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) + RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/third_libs/install/yaml-cpp/lib + LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/third_libs/install/yaml-cpp/lib + ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/third_libs/install/yaml-cpp/lib) install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + DESTINATION ${CMAKE_INSTALL_PREFIX}/third_libs/install/yaml-cpp/include FILES_MATCHING PATTERN "*.h") - install(EXPORT yaml-cpp-targets - DESTINATION "${CONFIG_EXPORT_DIR}") - install(FILES - "${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake" - "${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake" - DESTINATION "${CONFIG_EXPORT_DIR}") - install(FILES "${PROJECT_BINARY_DIR}/yaml-cpp.pc" - DESTINATION ${CMAKE_INSTALL_DATADIR}/pkgconfig) endif() unset(CONFIG_EXPORT_DIR) From d0aeef4f1f573fdcceabc571e862a6a74f96e8a1 Mon Sep 17 00:00:00 2001 From: jiangjiajun Date: Sun, 31 Jul 2022 16:49:19 +0800 Subject: [PATCH 2/4] Fix python usage on Windows --- fastdeploy/__init__.py | 25 ++++++++++++++++++++++--- requirements.txt | 1 + setup.py | 10 ++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/fastdeploy/__init__.py b/fastdeploy/__init__.py index 17858f7e9a9..69384cea4b1 100644 --- a/fastdeploy/__init__.py +++ b/fastdeploy/__init__.py @@ -13,13 +13,31 @@ # limitations under the License. from __future__ import absolute_import import logging +import os +import sys + +def add_dll_search_dir(dir_path): + os.environ["path"] = dir_path + ";" + os.environ["path"] + sys.path.insert(0, dir_path) + if sys.version_info[:2] >= (3, 8): + os.add_dll_directory(dir_path) + +if os.name == "nt": + current_path = os.path.abspath(__file__) + dirname = os.path.dirname(current_path) + third_libs_dir = os.path.join(dirname, "libs") + add_dll_search_dir(third_libs_dir) + for root, dirs, filenames in os.walk(third_libs_dir): + for d in dirs: + if d == "lib": + add_dll_search_dir(os.path.join(dirname, root, d)) + from .fastdeploy_main import Frontend, Backend, FDDataType, TensorInfo, Device from .fastdeploy_runtime import * from . import fastdeploy_main as C from . import vision from .download import download, download_and_decompress - def TensorInfoStr(tensor_info): message = "TensorInfo(name : '{}', dtype : '{}', shape : '{}')".format( tensor_info.name, tensor_info.dtype, tensor_info.shape) @@ -28,7 +46,7 @@ def TensorInfoStr(tensor_info): class RuntimeOption: def __init__(self): - self._option = C.RuntimeOption() + self._option = C.RuntimeOption() def set_model_path(self, model_path, params_path="", model_format="paddle"): return self._option.set_model_path(model_path, params_path, model_format) @@ -103,5 +121,6 @@ def RuntimeOptionStr(runtime_option): message.strip("\n") message += ")" return message + C.TensorInfo.__repr__ = TensorInfoStr -C.RuntimeOption.__repr__ = RuntimeOptionStr +C.RuntimeOption.__repr__ = RuntimeOptionStr \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index f2293605cf1..2c8b74ebd07 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ requests +tqdm \ No newline at end of file diff --git a/setup.py b/setup.py index fcaaa9f3767..12cc65d98ac 100644 --- a/setup.py +++ b/setup.py @@ -362,6 +362,16 @@ def run(self): "fastdeploy/libs/third_libs", symlinks=True) + if platform.system().lower() == "windows": + release_dir = os.path.join(".setuptools-cmake-build", "Release") + for f in os.listdir(release_dir): + filename = os.path.join(release_dir, f) + if not os.path.isfile(filename): + continue + if filename.endswith(".pyd"): + continue + shutil.copy(filename, "fastdeploy/libs") + if platform.system().lower() == "linux": rpaths = ["$ORIGIN:$ORIGIN/libs"] for root, dirs, files in os.walk( From 4e15db8005064798160a6c12fcc74d119e773be6 Mon Sep 17 00:00:00 2001 From: jiangjiajun Date: Sun, 31 Jul 2022 17:51:29 +0800 Subject: [PATCH 3/4] fix compile on windows --- fastdeploy/__init__.py | 2 +- setup.py | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/fastdeploy/__init__.py b/fastdeploy/__init__.py index 69384cea4b1..f9b9f686e0c 100644 --- a/fastdeploy/__init__.py +++ b/fastdeploy/__init__.py @@ -29,7 +29,7 @@ def add_dll_search_dir(dir_path): add_dll_search_dir(third_libs_dir) for root, dirs, filenames in os.walk(third_libs_dir): for d in dirs: - if d == "lib": + if d == "lib" or d == "bin": add_dll_search_dir(os.path.join(dirname, root, d)) from .fastdeploy_main import Frontend, Backend, FDDataType, TensorInfo, Device diff --git a/setup.py b/setup.py index 12cc65d98ac..cf850f24481 100644 --- a/setup.py +++ b/setup.py @@ -362,6 +362,18 @@ def run(self): "fastdeploy/libs/third_libs", symlinks=True) + third_party_path = os.path.join(".setuptools-cmake-build", "third_party") + for f in os.listdir(third_party_path): + lib_dir_name = os.path.join(third_party_path, f) + if os.path.isfile(lib_dir_name): + continue + for f1 in os.listdir(lib_dir_name): + release_dir = os.path.join(lib_dir_name, f1) + if f1 == "Release" and not os.path.isfile(release_dir): + if os.path.exists(os.path.join("fastdeploy/libs/third_libs", f)): + shutil.rmtree(os.path.join("fastdeploy/libs/third_libs", f)) + shutil.copytree(release_dir, os.path.join("fastdeploy/libs/third_libs", f, "lib")) + if platform.system().lower() == "windows": release_dir = os.path.join(".setuptools-cmake-build", "Release") for f in os.listdir(release_dir): @@ -428,7 +440,6 @@ def run(self): all_files = get_all_files("fastdeploy/libs") for f in all_files: package_data[PACKAGE_NAME].append(os.path.relpath(f, "fastdeploy")) - setuptools.setup( name=PACKAGE_NAME, version=VersionInfo.version, From 0aacf0061723bf7858fc14b93693fd42d7ffb4fc Mon Sep 17 00:00:00 2001 From: Jason Date: Sun, 31 Jul 2022 18:07:29 +0800 Subject: [PATCH 4/4] Update setup.py --- setup.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/setup.py b/setup.py index cf850f24481..e57dcd49338 100644 --- a/setup.py +++ b/setup.py @@ -363,16 +363,17 @@ def run(self): symlinks=True) third_party_path = os.path.join(".setuptools-cmake-build", "third_party") - for f in os.listdir(third_party_path): - lib_dir_name = os.path.join(third_party_path, f) - if os.path.isfile(lib_dir_name): - continue - for f1 in os.listdir(lib_dir_name): - release_dir = os.path.join(lib_dir_name, f1) - if f1 == "Release" and not os.path.isfile(release_dir): - if os.path.exists(os.path.join("fastdeploy/libs/third_libs", f)): - shutil.rmtree(os.path.join("fastdeploy/libs/third_libs", f)) - shutil.copytree(release_dir, os.path.join("fastdeploy/libs/third_libs", f, "lib")) + if os.path.exists(third_party_path): + for f in os.listdir(third_party_path): + lib_dir_name = os.path.join(third_party_path, f) + if os.path.isfile(lib_dir_name): + continue + for f1 in os.listdir(lib_dir_name): + release_dir = os.path.join(lib_dir_name, f1) + if f1 == "Release" and not os.path.isfile(release_dir): + if os.path.exists(os.path.join("fastdeploy/libs/third_libs", f)): + shutil.rmtree(os.path.join("fastdeploy/libs/third_libs", f)) + shutil.copytree(release_dir, os.path.join("fastdeploy/libs/third_libs", f, "lib")) if platform.system().lower() == "windows": release_dir = os.path.join(".setuptools-cmake-build", "Release")