Skip to content

Commit

Permalink
[onnx-optimizer] prepare 'python' feature support
Browse files Browse the repository at this point in the history
  • Loading branch information
luncliff committed Nov 23, 2024
1 parent 416db39 commit 8051f68
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
25 changes: 9 additions & 16 deletions ports/onnx-optimizer/fix-cmakelists.patch
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,7 @@ diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0063385..e9ad9ff 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,6 +12,13 @@ set(CMAKE_CXX_STANDARD 17)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

+function(add_msvc_runtime_flag lib)
+ if(${ONNX_USE_MSVC_STATIC_RUNTIME})
+ target_compile_options(${lib} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/MT> $<$<CONFIG:Debug>:/MTd>)
+ else()
+ target_compile_options(${lib} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/MD> $<$<CONFIG:Debug>:/MDd>)
+ endif()
+endfunction()
include(cmake/utils.cmake)

# For integration with onnxruntime_webassembly etc.
@@ -26,7 +33,7 @@ if(NOT ONNX_OPT_USE_SYSTEM_PROTOBUF)
@@ -26,7 +26,7 @@ if(NOT ONNX_OPT_USE_SYSTEM_PROTOBUF)
add_subdirectory_if_no_target(${PROJECT_SOURCE_DIR}/third_party/protobuf/cmake libprotobuf)
endif()

Expand All @@ -29,8 +15,15 @@ diff --git a/cmake/utils.cmake b/cmake/utils.cmake
index 6cca9f3..a735434 100644
--- a/cmake/utils.cmake
+++ b/cmake/utils.cmake
@@ -1,4 +1,3 @@
@@ -1,4 +1,10 @@
-include(${PROJECT_SOURCE_DIR}/third_party/onnx/cmake/Utils.cmake)
+function(add_msvc_runtime_flag lib)
+ if(ONNX_USE_MSVC_STATIC_RUNTIME)
+ target_compile_options(${lib} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/MT> $<$<CONFIG:Debug>:/MTd>)
+ else()
+ target_compile_options(${lib} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/MD> $<$<CONFIG:Debug>:/MDd>)
+ endif()
+endfunction()

# Poor man's FetchContent
function(add_subdirectory_if_no_target dir target)
35 changes: 34 additions & 1 deletion ports/onnx-optimizer/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,37 @@ vcpkg_from_github(
fix-cmakelists.patch
)

vcpkg_check_features(OUT_FEATURE_OPTIONS FEATURE_OPTIONS
FEATURES
python BUILD_ONNX_PYTHON
)

if("python" IN_LIST FEATURES)
x_vcpkg_get_python_packages(
PYTHON_VERSION 3
PACKAGES typing-extensions pyyaml numpy pybind11
OUT_PYTHON_VAR PYTHON3
)
message(STATUS "Using Python3: ${PYTHON3}")

function(get_python_site_packages PYTHON OUT_PATH)
execute_process(
COMMAND "${PYTHON}" -c "import site; print(site.getsitepackages()[0])"
OUTPUT_VARIABLE output OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(${OUT_PATH} "${output}" PARENT_SCOPE)
endfunction()
get_python_site_packages("${PYTHON3}" SITE_PACKAGES_DIR)

get_filename_component(pybind11_DIR "${SITE_PACKAGES_DIR}/pybind11/share/cmake/pybind11" ABSOLUTE)
message(STATUS "Using pybind11: ${pybind11_DIR}")

list(APPEND FEATURE_OPTIONS
"-DPython_EXECUTABLE:FILEPATH=${PYTHON3}"
"-Dpybind11_DIR:PATH=${pybind11_DIR}"
)
endif()

string(COMPARE EQUAL "${VCPKG_CRT_LINKAGE}" "static" USE_STATIC_RUNTIME)

vcpkg_cmake_configure(
Expand All @@ -18,7 +49,9 @@ vcpkg_cmake_configure(
-DONNX_USE_MSVC_STATIC_RUNTIME=${USE_STATIC_RUNTIME}
-DONNX_OPT_USE_SYSTEM_PROTOBUF=ON
-DONNX_TARGET_NAME=ONNX::onnx
-DBUILD_ONNX_PYTHON=OFF
${FEATURE_OPTIONS}
MAYBE_UNUSED_VARIABLES
ONNX_USE_MSVC_STATIC_RUNTIME # use in add_msvc_runtime_flag
)
vcpkg_cmake_install()
vcpkg_copy_pdbs()
Expand Down

0 comments on commit 8051f68

Please sign in to comment.