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

Es0m/add static runtime linking #469

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

es0m
Copy link

@es0m es0m commented Jun 11, 2023

  • current main of onnxruntime-extensions depends on msvc runtime (vcruntime)
  • static linkage is desired to reduce downstream dependency
  • OCOS_ENABLE_STATIC_LIB is an existing option which only seems to be used for webassembly checks atm
  • added add_compile_options(/MT$<$CONFIG:Debug:d>) and CMAKE_MSVC_RUNTIME_LIBRARY for static linkage if OCOS_ENABLE_STATIC_LIB is specified

@es0m es0m requested a review from a team as a code owner June 11, 2023 14:38
CMakeLists.txt Outdated Show resolved Hide resolved
Copy link
Member

@wenbingl wenbingl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Win32, there is few usage of using static runtime link because ORT custom op API is using dynamic loading a DLL. Now the latest API can support ort-extensions to be a static library, but I think the changes should be more than the changes in this PR.

CMakeLists.txt Outdated
message(STATUS "Build type not set - using RelWithDebInfo")
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose build type: Debug Release RelWithDebInfo." FORCE)
endif()
if (OCOS_ENABLE_STATIC_LIB)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (MSVC)

CMakeLists.txt Show resolved Hide resolved
@wenbingl
Copy link
Member

/azp run extensions.ci

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@es0m-msft
Copy link
Collaborator

/azp run extensions.ci

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@wenbingl
Copy link
Member

wenbingl commented Jun 12, 2023

Can you add the failed build case that you are fixing in ort-extensions CI pipeline to make sure the static link build won't be broken by any future change?

@snnn , can you review this PR as well?

@@ -105,6 +105,11 @@ if(NOT CC_OPTIMIZE)
endif()

if (MSVC)
if (OCOS_ENABLE_STATIC_LIB)
add_compile_options(/MT$<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:d>)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:Debug>")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can set the CMAKE_MSVC_RUNTIME_LIBRARY variable from cmake command line. So this change is unnecessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OCOS_ENABLE_STATIC_LIB means we want to build this repo's code as a static library. However, it doesn't mean they need to static link to VC Runtime.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@es0m-msft, According to the description, this PR is to enable static linking to VCRuntime, but OCOS_STATIC_LIBRARY flag was created for generating the static library of ort-extensions.lib instead of DLL. Can you try @snnn suggestion to see if it meets your requirements?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OCOS_STATIC_LIBRARY is different from OCOS_ENABLE_STATIC_LIBRARY?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snnn unfortunately, you can't if you want to respect generator expressions (I tried!)
Would you like a different OPTION for this usecase?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I searched the code, the only occurrence of "OCOS_ENABLE_STATIC_LIB" is:

if(NOT OCOS_ENABLE_STATIC_LIB AND CMAKE_SYSTEM_NAME STREQUAL "Emscripten")

So, it has nothing to do with VC Runtime. OCOS_ENABLE_STATIC_LIB has effects only when CMAKE_SYSTEM_NAME equals to "Emscripten", which does not use vc runtime at all. So I don't think the variable is used to control which VC Runtime to use. And we should not give the variable a new meaning(which will surprise the person who created this cmake option).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ONNX Runtime's build.py has a build option: --enable_msvc_static_runtime. It works by setting CMAKE_MSVC_RUNTIME_LIBRARY cmake variable to MultiThreaded$<$<CONFIG:Debug>:Debug. If you build this repo with the main ONNX Runtime repo, you can use that build option. Otherwise you can do the same thing when you invoke cmake. Therefore I think there is no need to introduce a new cmake variable to control this. Every cmake project should respect the CMAKE_MSVC_RUNTIME_LIBRARY cmake variable's value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, as CMAKE_MSVC_RUNTIME_LIBRARY means "the default value of MSVC_RUNTIME_LIBRARY". In cmake files, we should not set CMAKE_MSVC_RUNTIME_LIBRARY as it will change the default value instead of the actual value: MSVC_RUNTIME_LIBRARY.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html#variable:CMAKE_MSVC_RUNTIME_LIBRARY
If you follow the docs, you're supposed to set CMAKE_MSVC_RUNTIME_LIBRARY not MSVC_RUNTIME_LIBRARY.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regarding your use of OCOS_ENABLE_STATIC_LIB -- I'd suggest writing some documentation on how to use the flag. It is unclear as you can see by it's usage. It's related to WASM only apparently.
Also, instead of having some command line collection parameters assembled as per build.py, having a clear option to enable the required functionality might be better.

@wenbingl
Copy link
Member

@es0m, I verified -DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$CONFIG:Debug:Debug>" parameter in ort-extensions CI pipeline and fixed the issue in protobuf, it works now: #472

Is that what you needed?

@es0m
Copy link
Author

es0m commented Jun 15, 2023

Hi @wenbingl let me check the output on my end. I believe more needs to be set (pthreads_enable_static, etc.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants