From bfe1eca10c607bfcbc005889d91b5db1d8346686 Mon Sep 17 00:00:00 2001 From: Scott McKay Date: Sat, 2 Jul 2022 02:13:55 +1000 Subject: [PATCH] Add targets files for new .net6 frameworks (#12016) * Add net6 targets. Remove maccatalyst as we don't have a native build targetting that. * Set platform in macos targets * Add targetFramework entries * Move NativeLib.DllName definition and set using preprocessor values for simplicity. Couldn't get it to build with the preprocessor based setup when it was in a separate file. Update the nuspec generation to set platform version for .net6 targets. TODO: Validate versions. I copied them from the managed nuget package the packaging pipeline generated prior to adding targets. Possibly w could/should lower some of the versions. Hopefully the need to specify a version goes away when the release version of VS2022 supports .net6. * Try android 31.1 as https://github.com/actions/virtual-environments/blob/main/images/win/Windows2022-Readme.md suggests that should be available on the CI machines * Fix patch version mismatch Add some extra debug info in case it helps * Debug nuget location in CI * Add workspace entry back in * Add steps * One more attempt with hardcoded nuget.exe path and original android31.0 version * Better fix - found explicit nuget download and updated version there. * flake8 fixes * Fix black complaints. * Exit Microsoft_ML_OnnxRuntime_CheckPrerequisites for net6 iOS. * Removed outdated comment --- .../Microsoft.ML.OnnxRuntime.csproj | 2 +- .../NativeLib.android.cs | 8 -- .../Microsoft.ML.OnnxRuntime/NativeLib.ios.cs | 8 -- .../Microsoft.ML.OnnxRuntime/NativeLib.net.cs | 8 -- .../NativeLib.netstandard.cs | 8 -- .../NativeMethods.shared.cs | 15 ++++ .../targets/net6.0-android/targets.xml | 8 ++ .../targets/net6.0-ios/targets.xml | 21 +++++ .../targets/net6.0-macos/targets.xml | 21 +++++ .../targets/netstandard/targets.xml | 3 +- .../c-api-noopenmp-packaging-pipelines.yml | 6 +- .../azure-pipelines/templates/c-api-cpu.yml | 8 +- .../nuget/generate_nuspec_for_native_nuget.py | 81 ++++++++++++++++++- 13 files changed, 155 insertions(+), 42 deletions(-) delete mode 100644 csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.android.cs delete mode 100644 csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.ios.cs delete mode 100644 csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.net.cs delete mode 100644 csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.netstandard.cs create mode 100644 csharp/src/Microsoft.ML.OnnxRuntime/targets/net6.0-android/targets.xml create mode 100644 csharp/src/Microsoft.ML.OnnxRuntime/targets/net6.0-ios/targets.xml create mode 100644 csharp/src/Microsoft.ML.OnnxRuntime/targets/net6.0-macos/targets.xml diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/Microsoft.ML.OnnxRuntime.csproj b/csharp/src/Microsoft.ML.OnnxRuntime/Microsoft.ML.OnnxRuntime.csproj index 3711978cff..661a9e8cba 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/Microsoft.ML.OnnxRuntime.csproj +++ b/csharp/src/Microsoft.ML.OnnxRuntime/Microsoft.ML.OnnxRuntime.csproj @@ -37,7 +37,7 @@ we can add .net6 support to other packages later as needed --> - net6.0;net6.0-android;net6.0-ios;net6.0-maccatalyst;net6.0-macos + net6.0;net6.0-android;net6.0-ios;net6.0-macos diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.android.cs b/csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.android.cs deleted file mode 100644 index 4d5a05da4f..0000000000 --- a/csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.android.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Microsoft.ML.OnnxRuntime -{ - internal class NativeLib - { - // define the library name required for android - internal const string DllName = "libonnxruntime.so"; - } -} \ No newline at end of file diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.ios.cs b/csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.ios.cs deleted file mode 100644 index ad5cbcb413..0000000000 --- a/csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.ios.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Microsoft.ML.OnnxRuntime -{ - internal class NativeLib - { - // define the library name required for iOS - internal const string DllName = "__Internal"; - } -} \ No newline at end of file diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.net.cs b/csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.net.cs deleted file mode 100644 index 0514be6775..0000000000 --- a/csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.net.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Microsoft.ML.OnnxRuntime -{ - internal class NativeLib - { - // define the library name required for net targets - internal const string DllName = "onnxruntime"; - } -} \ No newline at end of file diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.netstandard.cs b/csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.netstandard.cs deleted file mode 100644 index 832de058a2..0000000000 --- a/csharp/src/Microsoft.ML.OnnxRuntime/NativeLib.netstandard.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Microsoft.ML.OnnxRuntime -{ - internal class NativeLib - { - // define the library name required for netstandard targets - internal const string DllName = "onnxruntime"; - } -} \ No newline at end of file diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs b/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs index 70e46c286e..f008d2fbff 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs @@ -425,6 +425,21 @@ namespace Microsoft.ML.OnnxRuntime typeof(DSessionOptionsAppendExecutionProvider)); } + internal class NativeLib + { +#if __ANDROID__ + // define the library name required for android + internal const string DllName = "libonnxruntime.so"; +#elif __IOS__ + // define the library name required for iOS + internal const string DllName = "__Internal"; +#else + internal const string DllName = "onnxruntime"; +#endif + // TODO: Does macos need special handling or will 'onnxruntime' -> libonnxruntime.dylib? + } + + [DllImport(NativeLib.DllName, CharSet = CharSet.Ansi)] public static extern ref OrtApiBase OrtGetApiBase(); diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/targets/net6.0-android/targets.xml b/csharp/src/Microsoft.ML.OnnxRuntime/targets/net6.0-android/targets.xml new file mode 100644 index 0000000000..54576a67d2 --- /dev/null +++ b/csharp/src/Microsoft.ML.OnnxRuntime/targets/net6.0-android/targets.xml @@ -0,0 +1,8 @@ + + + + + %(Filename)%(Extension) + + + \ No newline at end of file diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/targets/net6.0-ios/targets.xml b/csharp/src/Microsoft.ML.OnnxRuntime/targets/net6.0-ios/targets.xml new file mode 100644 index 0000000000..02043bcad2 --- /dev/null +++ b/csharp/src/Microsoft.ML.OnnxRuntime/targets/net6.0-ios/targets.xml @@ -0,0 +1,21 @@ + + + + + Framework + True + True + True + -lc++ + CoreML + + + Framework + True + True + True + -lc++ + CoreML + + + \ No newline at end of file diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/targets/net6.0-macos/targets.xml b/csharp/src/Microsoft.ML.OnnxRuntime/targets/net6.0-macos/targets.xml new file mode 100644 index 0000000000..63d4461ad1 --- /dev/null +++ b/csharp/src/Microsoft.ML.OnnxRuntime/targets/net6.0-macos/targets.xml @@ -0,0 +1,21 @@ + + + + + Framework + True + True + True + -lc++ + CoreML + + + Framework + True + True + True + -lc++ + CoreML + + + \ No newline at end of file diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/targets/netstandard/targets.xml b/csharp/src/Microsoft.ML.OnnxRuntime/targets/netstandard/targets.xml index b2a3149434..b8bb5efc7c 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/targets/netstandard/targets.xml +++ b/csharp/src/Microsoft.ML.OnnxRuntime/targets/netstandard/targets.xml @@ -9,7 +9,8 @@ diff --git a/tools/ci_build/github/azure-pipelines/c-api-noopenmp-packaging-pipelines.yml b/tools/ci_build/github/azure-pipelines/c-api-noopenmp-packaging-pipelines.yml index 530ad9177e..d07d6ac1e5 100644 --- a/tools/ci_build/github/azure-pipelines/c-api-noopenmp-packaging-pipelines.yml +++ b/tools/ci_build/github/azure-pipelines/c-api-noopenmp-packaging-pipelines.yml @@ -485,16 +485,16 @@ jobs: displayName: 'Create models link' - task: NuGetToolInstaller@0 - displayName: Use Nuget 5.7.0 + displayName: Use Nuget 6.2.1 inputs: - versionSpec: 5.7.0 + versionSpec: 6.2.1 - task: PowerShell@2 displayName: Install .NET 6 workloads inputs: targetType: 'inline' script: | - dotnet workload install android ios maccatalyst macos + dotnet workload install android ios macos workingDirectory: '$(Build.SourcesDirectory)\csharp' - task: PowerShell@2 diff --git a/tools/ci_build/github/azure-pipelines/templates/c-api-cpu.yml b/tools/ci_build/github/azure-pipelines/templates/c-api-cpu.yml index 8ed4d01ac8..1ee5868047 100644 --- a/tools/ci_build/github/azure-pipelines/templates/c-api-cpu.yml +++ b/tools/ci_build/github/azure-pipelines/templates/c-api-cpu.yml @@ -423,18 +423,18 @@ jobs: workingDirectory: '$(Build.BinariesDirectory)' displayName: 'Create models link' - task: NuGetToolInstaller@0 - displayName: Use Nuget 5.7.0 + displayName: Use Nuget 6.2.1 inputs: - versionSpec: 5.7.0 + versionSpec: 6.2.1 - task: PowerShell@2 displayName: Install .NET 6 workloads inputs: targetType: 'inline' script: | - dotnet workload install android ios maccatalyst macos + dotnet workload install android ios macos workingDirectory: '$(Build.SourcesDirectory)\csharp' - + - task: PowerShell@2 displayName: Build Microsoft.ML.OnnxRuntime .NET 6 targets using dotnet inputs: diff --git a/tools/nuget/generate_nuspec_for_native_nuget.py b/tools/nuget/generate_nuspec_for_native_nuget.py index 974598ca58..46af293e8d 100644 --- a/tools/nuget/generate_nuspec_for_native_nuget.py +++ b/tools/nuget/generate_nuspec_for_native_nuget.py @@ -240,6 +240,18 @@ def generate_dependencies(xml_text, package_name, version, dependency_id, depend xml_text.append('') xml_text.append('') xml_text.append("") + # Support net6.0-android + xml_text.append('') + xml_text.append('') + xml_text.append("") + # Support net6.0-ios + xml_text.append('') + xml_text.append('') + xml_text.append("") + # Support net6.0-macos + xml_text.append('') + xml_text.append('') + xml_text.append("") # Support Native C++ if include_dml: xml_text.append('') @@ -861,7 +873,6 @@ def generate_files(list, args): "monoandroid11.0", args.package_name + ".targets", ) - os.system(copy_command + " " + monoandroid_source_targets + " " + monoandroid_target_targets) xamarinios_source_targets = os.path.join( args.sources_path, "csharp", "src", "Microsoft.ML.OnnxRuntime", "targets", "xamarinios10", "targets.xml" @@ -875,17 +886,85 @@ def generate_files(list, args): "xamarinios10", args.package_name + ".targets", ) + + net6_android_source_targets = os.path.join( + args.sources_path, + "csharp", + "src", + "Microsoft.ML.OnnxRuntime", + "targets", + "net6.0-android", + "targets.xml", + ) + net6_android_target_targets = os.path.join( + args.sources_path, + "csharp", + "src", + "Microsoft.ML.OnnxRuntime", + "targets", + "net6.0-android", + args.package_name + ".targets", + ) + + net6_ios_source_targets = os.path.join( + args.sources_path, "csharp", "src", "Microsoft.ML.OnnxRuntime", "targets", "net6.0-ios", "targets.xml" + ) + net6_ios_target_targets = os.path.join( + args.sources_path, + "csharp", + "src", + "Microsoft.ML.OnnxRuntime", + "targets", + "net6.0-ios", + args.package_name + ".targets", + ) + + net6_macos_source_targets = os.path.join( + args.sources_path, "csharp", "src", "Microsoft.ML.OnnxRuntime", "targets", "net6.0-macos", "targets.xml" + ) + net6_macos_target_targets = os.path.join( + args.sources_path, + "csharp", + "src", + "Microsoft.ML.OnnxRuntime", + "targets", + "net6.0-macos", + args.package_name + ".targets", + ) + + os.system(copy_command + " " + monoandroid_source_targets + " " + monoandroid_target_targets) os.system(copy_command + " " + xamarinios_source_targets + " " + xamarinios_target_targets) + os.system(copy_command + " " + net6_android_source_targets + " " + net6_android_target_targets) + os.system(copy_command + " " + net6_ios_source_targets + " " + net6_ios_target_targets) + os.system(copy_command + " " + net6_macos_source_targets + " " + net6_macos_target_targets) files_list.append("') files_list.append( "' ) + files_list.append("') files_list.append( "' ) + files_list.append( + "' + ) + files_list.append( + "' + ) + + files_list.append("') + files_list.append( + "' + ) + + files_list.append("') + files_list.append( + "' + ) + # Process License, ThirdPartyNotices, Privacy files_list.append("') files_list.append(