Skip to content
Open
Show file tree
Hide file tree
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
77 changes: 3 additions & 74 deletions csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -889,8 +889,8 @@ internal class NativeLib
#if !NETSTANDARD2_0 && !__ANDROID__ && !__IOS__
/// <summary>
/// Custom DllImportResolver to handle platform-specific library loading.
/// On Windows, it explicitly loads the library with a lowercase .dll extension to handle
/// case-sensitive filesystems.
/// It handles the addition of "lib" prefix and file extensions (.so/.dylib) for Linux/macOS,
/// and .dll extension for Windows (handling case-sensitivity).
/// </summary>
private static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
Expand All @@ -915,89 +915,18 @@ private static IntPtr DllImportResolver(string libraryName, Assembly assembly, D

if (mappedName != null)
{
// 1. Try default loading (name only)
if (NativeLibrary.TryLoad(mappedName, assembly, searchPath, out IntPtr handle))
{
return handle;
}

// 2. Try relative to assembly location (look into runtimes subfolders)
string assemblyLocation = null;
try { assemblyLocation = assembly.Location; } catch { }
if (!string.IsNullOrEmpty(assemblyLocation))
Comment on lines -924 to -927
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need this for development scenarios using an AnyCPU build? If that is the case dotnet might not copy from the runtimes directory to the build output as I don't think there's a specific RID at that point. It would do the copy when publishing though.

{
string assemblyDir = System.IO.Path.GetDirectoryName(assemblyLocation);
string rid = RuntimeInformation.RuntimeIdentifier;

// Probe the specific RID first, then common fallbacks for the current OS
string[] ridsToTry;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
ridsToTry = new[] { rid, "win-x64", "win-arm64" };
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
ridsToTry = new[] { rid, "linux-x64", "linux-arm64" };
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
// We no longer provide osx-x64 in official package since 1.24.
// However, we keep it in the list for build-from-source users.
ridsToTry = new[] { rid, "osx-arm64", "osx-x64" };
}
else
{
ridsToTry = new[] { rid };
}

foreach (var tryRid in ridsToTry)
{
string probePath = System.IO.Path.Combine(assemblyDir, "runtimes", tryRid, "native", mappedName);
if (System.IO.File.Exists(probePath) && NativeLibrary.TryLoad(probePath, assembly, searchPath, out handle))
{
LogLibLoad($"[DllImportResolver] Loaded {mappedName} from: {probePath}");
return handle;
}
}
}

// 3. Try AppContext.BaseDirectory as a fallback
string baseDir = AppContext.BaseDirectory;
if (!string.IsNullOrEmpty(baseDir))
{
string probePath = System.IO.Path.Combine(baseDir, mappedName);
if (NativeLibrary.TryLoad(probePath, assembly, searchPath, out handle))
{
LogLibLoad($"[DllImportResolver] Loaded {mappedName} from: {probePath}");
return handle;
}

string rid = RuntimeInformation.RuntimeIdentifier;
probePath = System.IO.Path.Combine(baseDir, "runtimes", rid, "native", mappedName);
if (NativeLibrary.TryLoad(probePath, assembly, searchPath, out handle))
{
LogLibLoad($"[DllImportResolver] Loaded {mappedName} from: {probePath}");
return handle;
}
}

LogLibLoad($"[DllImportResolver] Failed loading {mappedName} (RID: {RuntimeInformation.RuntimeIdentifier}, Assembly: {assemblyLocation})");

}
}

// Fall back to default resolution
return IntPtr.Zero;
}

private static void LogLibLoad(string message)
{
System.Diagnostics.Trace.WriteLine(message);
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("ORT_LOADER_VERBOSITY")))
{
Console.WriteLine(message);
}
}

#endif

[DllImport(NativeLib.DllName, CharSet = CharSet.Ansi)]
Expand Down
37 changes: 37 additions & 0 deletions csharp/src/Microsoft.ML.OnnxRuntime/targets/native/props.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<!-- ========================================================= -->
<!-- Native include path -->
<!-- ========================================================= -->
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>
$(MSBuildThisFileDirectory)include;
%(AdditionalIncludeDirectories)
</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>

<!-- ========================================================= -->
<!-- Windows native linking -->
<!-- ========================================================= -->
<ItemDefinitionGroup Condition="'$(PlatformTarget)' == 'x64'">
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this condition also check the target is Windows or do we not have x64/arm64 as platform targets on Linux?

<Link>
<AdditionalDependencies>
$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\onnxruntime.lib;
%(AdditionalDependencies)
</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>

<ItemDefinitionGroup Condition="'$(PlatformTarget)' == 'ARM64'">
<Link>
<AdditionalDependencies>
$(MSBuildThisFileDirectory)..\..\runtimes\win-arm64\native\onnxruntime.lib;
%(AdditionalDependencies)
</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>

</Project>
11 changes: 11 additions & 0 deletions csharp/src/Microsoft.ML.OnnxRuntime/targets/native/targets.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- ========================================================= -->
<!-- Block unsupported x86 -->
<!-- ========================================================= -->
<Target Name="OnnxRuntime_BlockX86"
BeforeTargets="Build"
Condition="'$(PlatformTarget)' == 'x86'">
<Error Text="Microsoft.ML.OnnxRuntime does not support x86. Please use x64 or ARM64." />
</Target>
</Project>
151 changes: 21 additions & 130 deletions csharp/src/Microsoft.ML.OnnxRuntime/targets/netstandard/props.xml
Original file line number Diff line number Diff line change
@@ -1,146 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<!-- ========================================================= -->
<!-- Native include path -->
<!-- ========================================================= -->
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)../../build/native/include/;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>
$(MSBuildThisFileDirectory)..\native\include;
%(AdditionalIncludeDirectories)
</AdditionalIncludeDirectories>
</ClCompile>
<ResourceCompile>
<AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)../../build/native/include/;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ResourceCompile>
</ItemDefinitionGroup>

<ItemDefinitionGroup Condition="'$(PlatformTarget)' == 'ARM64'">
<!-- ========================================================= -->
<!-- Windows native linking -->
<!-- ========================================================= -->
<ItemDefinitionGroup Condition="'$(PlatformTarget)' == 'x64'">
<Link>
<AdditionalDependencies>$(MSBuildThisFileDirectory)../../runtimes/win-arm64/native/onnxruntime.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>
$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\onnxruntime.lib;
%(AdditionalDependencies)
</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>

<ItemDefinitionGroup Condition="'$(PlatformTarget)' == 'ARM'">
<ItemDefinitionGroup Condition="'$(PlatformTarget)' == 'ARM64'">
<Link>
<AdditionalDependencies>$(MSBuildThisFileDirectory)../../runtimes/win-arm/native/onnxruntime.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>
$(MSBuildThisFileDirectory)..\..\runtimes\win-arm64\native\onnxruntime.lib;
%(AdditionalDependencies)
</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>

<ItemDefinitionGroup Condition="'$(PlatformTarget)' == 'x64' OR ('$(PlatformTarget)' == 'AnyCPU' AND '$(Prefer32Bit)' != 'true')">
<Link>
<AdditionalDependencies>$(MSBuildThisFileDirectory)../../runtimes/win-x64/native/onnxruntime.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>

<PropertyGroup>
<EnginePlatform Condition="'$(Platform)' == 'ARM64'">arm64</EnginePlatform>
<EnginePlatform Condition="'$(Platform)' == 'ARM'">arm</EnginePlatform>
<EnginePlatform Condition="'$(Platform)' != 'Win32' AND '$(Platform)' != 'ARM64'">$(Platform)</EnginePlatform>
</PropertyGroup>

<PropertyGroup>
<OnnxRuntimeBinary>$(MSBuildThisFileDirectory)..\..\runtimes\win-$(EnginePlatform)\native\onnxruntime.dll</OnnxRuntimeBinary>
</PropertyGroup>

<!-- Assume apps using the Microsoft.ML.OnnxRuntime.DirectML package only want the DirectML binaries (no need for a build dependency). -->
<PropertyGroup Label="Globals" Condition="Exists('$(MSBuildThisFileDirectory)include\dml_provider_factory.h')">
<Microsoft_AI_DirectML_SkipDebugLayerCopy>true</Microsoft_AI_DirectML_SkipDebugLayerCopy>
<Microsoft_AI_DirectML_SkipLink>true</Microsoft_AI_DirectML_SkipLink>
<Microsoft_AI_DirectML_SkipIncludeDir>true</Microsoft_AI_DirectML_SkipIncludeDir>
</PropertyGroup>

<ItemGroup>
<!-- x64 -->
<None Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\onnxruntime.dll"
Condition="('$(PlatformTarget)' == 'x64' OR ('$(PlatformTarget)' == 'AnyCPU' AND '$(Prefer32Bit)' != 'true')) AND
Exists('$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\onnxruntime.dll')">
<Link>onnxruntime.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</None>
<None Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\onnxruntime_providers_shared.dll"
Condition="('$(PlatformTarget)' == 'x64' OR ('$(PlatformTarget)' == 'AnyCPU' AND '$(Prefer32Bit)' != 'true')) AND
Exists('$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\onnxruntime_providers_shared.dll')">
<Link>onnxruntime_providers_shared.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</None>
<None Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\onnxruntime_providers_cuda.dll"
Condition="('$(PlatformTarget)' == 'x64' OR ('$(PlatformTarget)' == 'AnyCPU' AND '$(Prefer32Bit)' != 'true')) AND
Exists('$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\onnxruntime_providers_cuda.dll')">
<Link>onnxruntime_providers_cuda.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</None>
<None Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\onnxruntime_providers_dnnl.dll"
Condition="('$(PlatformTarget)' == 'x64' OR ('$(PlatformTarget)' == 'AnyCPU' AND '$(Prefer32Bit)' != 'true')) AND
Exists('$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\onnxruntime_providers_dnnl.dll')">
<Link>onnxruntime_providers_dnnl.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</None>
<None Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\onnxruntime_providers_tensorrt.dll"
Condition="('$(PlatformTarget)' == 'x64' OR ('$(PlatformTarget)' == 'AnyCPU' AND '$(Prefer32Bit)' != 'true')) AND
Exists('$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\onnxruntime_providers_tensorrt.dll')">
<Link>onnxruntime_providers_tensorrt.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</None>
<None Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\onnxruntime_providers_openvino.dll"
Condition="('$(PlatformTarget)' == 'x64' OR ('$(PlatformTarget)' == 'AnyCPU' AND '$(Prefer32Bit)' != 'true')) AND
Exists('$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\onnxruntime_providers_openvino.dll')">
<Link>onnxruntime_providers_openvino.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</None>
<None Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\dnnl.dll"
Condition="('$(PlatformTarget)' == 'x64' OR ('$(PlatformTarget)' == 'AnyCPU' AND '$(Prefer32Bit)' != 'true')) AND
Exists('$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\dnnl.dll')">
<Link>dnnl.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</None>
<None Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\mklml.dll"
Condition="('$(PlatformTarget)' == 'x64' OR ('$(PlatformTarget)' == 'AnyCPU' AND '$(Prefer32Bit)' != 'true')) AND
Exists('$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\mklml.dll')">
<Link>mklml.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</None>
<None Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\libiomp5md.dll"
Condition="('$(PlatformTarget)' == 'x64' OR ('$(PlatformTarget)' == 'AnyCPU' AND '$(Prefer32Bit)' != 'true')) AND
Exists('$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\libiomp5md.dll')">
<Link>libiomp5md.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</None>

<!-- arm64 -->
<None Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-arm64\native\onnxruntime.dll"
Condition="'$(PlatformTarget)' == 'ARM64' AND
Exists('$(MSBuildThisFileDirectory)..\..\runtimes\win-arm64\native\onnxruntime.dll')">
<Link>onnxruntime.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</None>
<None Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-arm64\native\onnxruntime_providers_shared.dll"
Condition="'$(PlatformTarget)' == 'ARM64' AND
Exists('$(MSBuildThisFileDirectory)..\..\runtimes\win-arm64\native\onnxruntime_providers_shared.dll')">
<Link>onnxruntime_providers_shared.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</None>

<!-- arm -->
<None Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-arm\native\onnxruntime.dll"
Condition="'$(PlatformTarget)' == 'ARM' AND
Exists('$(MSBuildThisFileDirectory)..\..\runtimes\win-arm\native\onnxruntime.dll')">
<Link>onnxruntime.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</None>
<None Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-arm\native\onnxruntime_providers_shared.dll"
Condition="'$(PlatformTarget)' == 'ARM' AND
Exists('$(MSBuildThisFileDirectory)..\..\runtimes\win-arm\native\onnxruntime_providers_shared.dll')">
<Link>onnxruntime_providers_shared.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</None>
</ItemGroup>
</Project>
</Project>
52 changes: 40 additions & 12 deletions csharp/src/Microsoft.ML.OnnxRuntime/targets/netstandard/targets.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Microsoft_ML_OnnxRuntime_CheckPrerequisites" BeforeTargets="BeforeBuild">
<!--
Special case .NET Core portable applications. When building a portable .NET Core app,
the PlatformTarget is empty, and you don't know until runtime (i.e. which dotnet.exe)
what processor architecture will be used.
-->
<Error Condition="('$(PlatformTarget)' != 'x64' AND '$(PlatformTarget)' != 'arm32' AND '$(PlatformTarget)' != 'arm64' AND '$(PlatformTarget)' != 'x86' AND '$(PlatformTarget)' != 'AnyCPU') AND
('$(OutputType)' == 'Exe' OR '$(OutputType)'=='WinExe') AND
!('$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND '$(PlatformTarget)' == '') AND
$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) != 'ios' AND
'$(SuppressOnnxRuntimePlatformCompatibilityError)' != 'true'"
Text="Microsoft.ML.OnnxRuntime only supports the AnyCPU, x64, arm32, arm64 and x86 platforms at this time."/>
<!-- ========================================================= -->
<!-- 1. Block unsupported x86 -->
<!-- ========================================================= -->
<Target Name="OnnxRuntime_BlockUnsupportedPlatform"
BeforeTargets="Build"
Condition="
'$(SuppressOnnxRuntimePlatformCompatibilityError)' != 'true' AND
$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) != 'ios' AND
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIK there are no 32-bit iOS builds.

$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) != 'android' AND
'$(PlatformTarget)' == 'x86'">
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need a check for an AnyCPU build (based on the previous implementation I'm guessing PlatformTarget may not have a value in that case) with Prefer32Bit set to true?


<Error Text="Microsoft.ML.OnnxRuntime does not support x86. Please use x64 or ARM64." />

</Target>


<!-- ========================================================= -->
<!-- 2. Legacy .NET Framework fallback copy logic -->
<!-- (ONLY for non-SDK projects, which only run on Windows.) -->
<!-- ========================================================= -->
<PropertyGroup Condition="'$(UsingMicrosoftNETSdk)' != 'true' AND '$(OS)' == 'Windows_NT'">
<!-- Normalize architecture. Default to x64 for AnyCPU/unspecified as we don't support x86. -->
<_OrtArch>x64</_OrtArch>
<_OrtArch Condition="'$(PlatformTarget)' == 'ARM64' OR '$(Platform)' == 'ARM64'">arm64</_OrtArch>
</PropertyGroup>

<ItemGroup Condition="'$(UsingMicrosoftNETSdk)' != 'true' AND '$(_OrtArch)' != '' AND '$(OS)' == 'Windows_NT'">
<_OrtNativeFiles Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-$(_OrtArch)\native\*.dll" />
</ItemGroup>

<Target Name="OnnxRuntime_CopyNativeFallback"
AfterTargets="Build"
Condition="'$(UsingMicrosoftNETSdk)' != 'true' AND '@(_OrtNativeFiles)' != ''">

<Message Importance="Low"
Text="Copying ONNX Runtime native binaries for legacy project." />

<Copy SourceFiles="@(_OrtNativeFiles)"
DestinationFolder="$(OutDir)"
SkipUnchangedFiles="true" />
</Target>
</Project>
Loading
Loading