Skip to content

Refactor props and targets of Microsoft.ML.OnnxRuntime and DllImport#27351

Open
tianleiwu wants to merge 4 commits intomainfrom
tlwu/20260212/ort_nuget_props_linux_macos_v2
Open

Refactor props and targets of Microsoft.ML.OnnxRuntime and DllImport#27351
tianleiwu wants to merge 4 commits intomainfrom
tlwu/20260212/ort_nuget_props_linux_macos_v2

Conversation

@tianleiwu
Copy link
Contributor

This PR refactors the MSBuild assets and the C# DllImportResolver to simplify native library deployment and improve cross-platform support (Windows, Linux, macOS). It also adds build-time verification to ensure native binaries are automatically copied to the output directory.

Summary of Changes

1. Refactored MSBuild Assets (props.xml and targets.xml)

  • Simplified props.xml: Removed complex conditional logic for linking native libraries in Windows. The logic now focuses on providing the correct include and import library paths based on the target architecture (x64, ARM64).
  • Enhanced targets.xml:
    • Added a platform compatibility check to explicitly block unsupported x86 builds, suggesting x64 or ARM64 instead.
    • Improved the legacy .NET Framework (non-SDK) fallback logic to automatically copy native binaries from the NuGet package to the output directory using a cleaner, glob-based approach.

2. Streamlined DllImportResolver

  • Simplified Resolution Logic: Removed the manual runtime probing of runtimes/ subfolders in NativeMethods.shared.cs.
  • Platform-Specific Mapping: Instead of searching multiple paths, the resolver now maps the generic onnxruntime library name to its platform-specific filename:
    • Windows: onnxruntime.dll
    • Linux: libonnxruntime.so
    • macOS: libonnxruntime.dylib
  • This relies on the standard .NET library loading mechanism to find the files in the output directory or standard search paths, ensuring consistency with how other .NET libraries behave.

3. End-to-End Build Verification

  • Updated runtest.bat and runtest.sh to perform a dotnet build followed by a check for the existence of the native library (onnxruntime.dll or libonnxruntime.so/dylib) in the bin/ directory.
  • This ensures that any changes to the packaging or build logic that might break "automatic copying" are caught during CI.

Benefits Compared to Original Files

Feature Original Implementation New Implementation
Complexity Heavily nested MSBuild logic for specific file links. Consolidated, architecture-aware logic using standard paths.
Cross-Platform Resolver had hardcoded RID probing logic which was hard to maintain. Resolver uses explicit mapping to platform-native names, leveraging .NET's built-in loader.
Reliability Native library loading might fail if RIDs didn't match exactly. Simplified mapping reduces edge cases and follows OS conventions (lib prefix).
Verification Tests relied on dotnet test which might obscure build-time copy failures. Explicit build and file check ensures native assets are correctly deployed.
Support Implicit x86 failures could be confusing. Explicit error message blocking x86 and guiding users to supported architectures.

Tests

Verified in Nuget Test and Foundry Local Package Pipelines

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

You can commit the suggested changes from lintrunner.

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.

1 participant