From 0e355cdef2693e2e21c0d9507200fa0d39792d21 Mon Sep 17 00:00:00 2001
From: Bart Verhagen <bart@kapernikov.com>
Date: Fri, 6 Oct 2023 15:33:13 +0200
Subject: [PATCH 1/4] onnxruntime: enable cuda provider

---
 recipes/onnxruntime/all/conandata.yml                |  6 ++++++
 recipes/onnxruntime/all/conanfile.py                 |  5 ++++-
 .../all/patches/1.14.1-0006-cuda-gsl.patch           | 12 ++++++++++++
 .../all/patches/1.15.1-0002-cuda-gsl.patch           | 12 ++++++++++++
 4 files changed, 34 insertions(+), 1 deletion(-)
 create mode 100644 recipes/onnxruntime/all/patches/1.14.1-0006-cuda-gsl.patch
 create mode 100644 recipes/onnxruntime/all/patches/1.15.1-0002-cuda-gsl.patch

diff --git a/recipes/onnxruntime/all/conandata.yml b/recipes/onnxruntime/all/conandata.yml
index abfa25a1b938f..98eb34ee8025f 100644
--- a/recipes/onnxruntime/all/conandata.yml
+++ b/recipes/onnxruntime/all/conandata.yml
@@ -33,6 +33,9 @@ patches:
     - patch_file: "patches/1.14.1-0004-abseil-no-string-view.patch"
       patch_description: "allow to build with abseil built without c++17 support"
       patch_type: "portability"
+    - patch_file: "patches/1.15.1-0002-cuda-gsl.patch"
+      patch_description: "CMake: fix GSL and cuda"
+      patch_type: "conan"
   "1.14.1":
     - patch_file: "patches/1.14.1-0001-cmake-dependencies.patch"
       patch_description: "CMake: ensure conan dependencies are used (upstreamed future versions)"
@@ -52,3 +55,6 @@ patches:
       patch_description: "Ensures the forward compatibility with the newest versions of re2 library."
       patch_type: "portability"
       patch_source: "https://github.com/microsoft/onnxruntime/commit/126e7bf15fa4af8621814b82a3f7bd0d786f0239.patch"
+    - patch_file: "patches/1.14.1-0006-cuda-gsl.patch"
+      patch_description: "CMake: fix GSL and cuda"
+      patch_type: "conan"
diff --git a/recipes/onnxruntime/all/conanfile.py b/recipes/onnxruntime/all/conanfile.py
index 7413919fad0ee..d9c5a19e98295 100644
--- a/recipes/onnxruntime/all/conanfile.py
+++ b/recipes/onnxruntime/all/conanfile.py
@@ -27,11 +27,13 @@ class OnnxRuntimeConan(ConanFile):
         "shared": [True, False],
         "fPIC": [True, False],
         "with_xnnpack": [True, False],
+        "use_cuda": [True, False],
     }
     default_options = {
         "shared": False,
         "fPIC": True,
         "with_xnnpack": False,
+        "use_cuda": False,
     }
     short_paths = True
 
@@ -135,6 +137,7 @@ def generate(self):
         tc.variables["onnxruntime_USE_FULL_PROTOBUF"] = not self.dependencies["protobuf"].options.lite
         tc.variables["onnxruntime_USE_XNNPACK"] = self.options.with_xnnpack
 
+        tc.variables["onnxruntime_USE_CUDA"] = self.options.use_cuda
         tc.variables["onnxruntime_BUILD_UNIT_TESTS"] = False
         tc.variables["onnxruntime_RUN_ONNX_TESTS"] = False
         tc.variables["onnxruntime_GENERATE_TEST_REPORTS"] = False
@@ -159,7 +162,7 @@ def generate(self):
         tc.variables["onnxruntime_TVM_CUDA_RUNTIME"] = False
         tc.variables["onnxruntime_TVM_USE_HASH"] = False
         tc.variables["onnxruntime_CROSS_COMPILING"] = False
-        tc.variables["onnxruntime_DISABLE_CONTRIB_OPS"] = False
+        tc.variables["onnxruntime_DISABLE_CONTRIB_OPS"] = self.options.use_cuda
         tc.variables["onnxruntime_DISABLE_ML_OPS"] = False
         tc.variables["onnxruntime_DISABLE_RTTI"] = False
         tc.variables["onnxruntime_DISABLE_EXCEPTIONS"] = False
diff --git a/recipes/onnxruntime/all/patches/1.14.1-0006-cuda-gsl.patch b/recipes/onnxruntime/all/patches/1.14.1-0006-cuda-gsl.patch
new file mode 100644
index 0000000000000..965b24fe9d601
--- /dev/null
+++ b/recipes/onnxruntime/all/patches/1.14.1-0006-cuda-gsl.patch
@@ -0,0 +1,12 @@
+diff --git a/cmake/external/onnxruntime_external_deps.cmake b/cmake/external/onnxruntime_external_deps.cmake
+index b051ff0be1..19ce4107de 100644
+--- a/cmake/external/onnxruntime_external_deps.cmake
++++ b/cmake/external/onnxruntime_external_deps.cmake
+@@ -251,6 +251,7 @@ if(onnxruntime_USE_CUDA)
+     URL ${DEP_URL_microsoft_gsl}
+     URL_HASH SHA1=${DEP_SHA1_microsoft_gsl}
+     PATCH_COMMAND ${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/gsl/1064.patch
++    FIND_PACKAGE_ARGS 4.0 NAMES Microsoft.GSL
+   )
+ else()
+   FetchContent_Declare(
diff --git a/recipes/onnxruntime/all/patches/1.15.1-0002-cuda-gsl.patch b/recipes/onnxruntime/all/patches/1.15.1-0002-cuda-gsl.patch
new file mode 100644
index 0000000000000..dd1d64e3fdb8e
--- /dev/null
+++ b/recipes/onnxruntime/all/patches/1.15.1-0002-cuda-gsl.patch
@@ -0,0 +1,12 @@
+diff --git a/cmake/external/onnxruntime_external_deps.cmake b/cmake/external/onnxruntime_external_deps.cmake
+index 9effd1a2db..3284c35424 100644
+--- a/cmake/external/onnxruntime_external_deps.cmake
++++ b/cmake/external/onnxruntime_external_deps.cmake
+@@ -288,6 +288,7 @@ if(onnxruntime_USE_CUDA)
+     URL ${DEP_URL_microsoft_gsl}
+     URL_HASH SHA1=${DEP_SHA1_microsoft_gsl}
+     PATCH_COMMAND ${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/gsl/1064.patch
++    FIND_PACKAGE_ARGS 4.0 NAMES Microsoft.GSL
+   )
+ else()
+   FetchContent_Declare(

From b09d0e76f139faf93beda0bdd8ceee51190bbb45 Mon Sep 17 00:00:00 2001
From: Bart Verhagen <bart@kapernikov.com>
Date: Mon, 9 Oct 2023 14:59:59 +0200
Subject: [PATCH 2/4] onnxruntime: patch 1.16.0 build too

---
 recipes/onnxruntime/all/conandata.yml                |  3 +++
 .../all/patches/1.16.0-0002-cuda-gsl.patch           | 12 ++++++++++++
 2 files changed, 15 insertions(+)
 create mode 100644 recipes/onnxruntime/all/patches/1.16.0-0002-cuda-gsl.patch

diff --git a/recipes/onnxruntime/all/conandata.yml b/recipes/onnxruntime/all/conandata.yml
index 98eb34ee8025f..1fe5abb9fac76 100644
--- a/recipes/onnxruntime/all/conandata.yml
+++ b/recipes/onnxruntime/all/conandata.yml
@@ -26,6 +26,9 @@ patches:
     - patch_file: "patches/1.14.1-0004-abseil-no-string-view.patch"
       patch_description: "allow to build with abseil built without c++17 support"
       patch_type: "portability"
+    - patch_file: "patches/1.16.0-0002-cuda-gsl.patch"
+      patch_description: "CMake: fix GSL and cuda"
+      patch_type: "conan"
   "1.15.1":
     - patch_file: "patches/1.15.1-0001-cmake-dependencies.patch"
       patch_description: "CMake: ensure conan dependencies are used"
diff --git a/recipes/onnxruntime/all/patches/1.16.0-0002-cuda-gsl.patch b/recipes/onnxruntime/all/patches/1.16.0-0002-cuda-gsl.patch
new file mode 100644
index 0000000000000..7659e55bcec80
--- /dev/null
+++ b/recipes/onnxruntime/all/patches/1.16.0-0002-cuda-gsl.patch
@@ -0,0 +1,12 @@
+diff --git a/cmake/external/onnxruntime_external_deps.cmake b/cmake/external/onnxruntime_external_deps.cmake
+index 8e412c7847..d9edf2a101 100644
+--- a/cmake/external/onnxruntime_external_deps.cmake
++++ b/cmake/external/onnxruntime_external_deps.cmake
+@@ -302,6 +302,7 @@ if(onnxruntime_USE_CUDA)
+     URL ${DEP_URL_microsoft_gsl}
+     URL_HASH SHA1=${DEP_SHA1_microsoft_gsl}
+     PATCH_COMMAND ${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/gsl/1064.patch
++    FIND_PACKAGE_ARGS 4.0 NAMES Microsoft.GSL
+   )
+ else()
+   FetchContent_Declare(

From 8416420aa3eb76a0c10430355d649dba216ab96e Mon Sep 17 00:00:00 2001
From: Bart Verhagen <bart@kapernikov.com>
Date: Fri, 13 Oct 2023 09:57:29 +0200
Subject: [PATCH 3/4] onnxruntime: patch 1.16.1 build too

---
 recipes/onnxruntime/all/conandata.yml                |  3 +++
 .../all/patches/1.16.1-0002-cuda-gsl.patch           | 12 ++++++++++++
 2 files changed, 15 insertions(+)
 create mode 100644 recipes/onnxruntime/all/patches/1.16.1-0002-cuda-gsl.patch

diff --git a/recipes/onnxruntime/all/conandata.yml b/recipes/onnxruntime/all/conandata.yml
index 1fe5abb9fac76..50264f9eed6b2 100644
--- a/recipes/onnxruntime/all/conandata.yml
+++ b/recipes/onnxruntime/all/conandata.yml
@@ -19,6 +19,9 @@ patches:
     - patch_file: "patches/1.14.1-0004-abseil-no-string-view.patch"
       patch_description: "allow to build with abseil built without c++17 support"
       patch_type: "portability"
+    - patch_file: "patches/1.16.1-0002-cuda-gsl.patch"
+      patch_description: "CMake: fix GSL and cuda"
+      patch_type: "conan"
   "1.16.0":
     - patch_file: "patches/1.16.0-0001-cmake-dependencies.patch"
       patch_description: "CMake: ensure conan dependencies are used"
diff --git a/recipes/onnxruntime/all/patches/1.16.1-0002-cuda-gsl.patch b/recipes/onnxruntime/all/patches/1.16.1-0002-cuda-gsl.patch
new file mode 100644
index 0000000000000..7659e55bcec80
--- /dev/null
+++ b/recipes/onnxruntime/all/patches/1.16.1-0002-cuda-gsl.patch
@@ -0,0 +1,12 @@
+diff --git a/cmake/external/onnxruntime_external_deps.cmake b/cmake/external/onnxruntime_external_deps.cmake
+index 8e412c7847..d9edf2a101 100644
+--- a/cmake/external/onnxruntime_external_deps.cmake
++++ b/cmake/external/onnxruntime_external_deps.cmake
+@@ -302,6 +302,7 @@ if(onnxruntime_USE_CUDA)
+     URL ${DEP_URL_microsoft_gsl}
+     URL_HASH SHA1=${DEP_SHA1_microsoft_gsl}
+     PATCH_COMMAND ${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/gsl/1064.patch
++    FIND_PACKAGE_ARGS 4.0 NAMES Microsoft.GSL
+   )
+ else()
+   FetchContent_Declare(

From 5cb32e7b2c9518246b84f831450315c53370fbbc Mon Sep 17 00:00:00 2001
From: Bart Verhagen <bart@kapernikov.com>
Date: Tue, 17 Oct 2023 11:43:38 +0200
Subject: [PATCH 4/4] onnxruntime: avoid gsl being patched when cuda is active

---
 recipes/onnxruntime/all/conanfile.py                       | 4 ++--
 recipes/onnxruntime/all/patches/1.14.1-0006-cuda-gsl.patch | 7 ++++---
 recipes/onnxruntime/all/patches/1.15.1-0002-cuda-gsl.patch | 7 ++++---
 recipes/onnxruntime/all/patches/1.16.0-0002-cuda-gsl.patch | 7 ++++---
 recipes/onnxruntime/all/patches/1.16.1-0002-cuda-gsl.patch | 7 ++++---
 5 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/recipes/onnxruntime/all/conanfile.py b/recipes/onnxruntime/all/conanfile.py
index d9c5a19e98295..fc8c7daa58c83 100644
--- a/recipes/onnxruntime/all/conanfile.py
+++ b/recipes/onnxruntime/all/conanfile.py
@@ -119,8 +119,8 @@ def validate_build(self):
             )
 
     def build_requirements(self):
-        # Required by upstream https://github.com/microsoft/onnxruntime/blob/v1.14.1/cmake/CMakeLists.txt#L5
-        self.tool_requires("cmake/[>=3.24 <4]")
+        # Required by upstream https://github.com/microsoft/onnxruntime/blob/v1.16.1/cmake/CMakeLists.txt#L5
+        self.tool_requires("cmake/[>=3.26 <4]")
 
     def source(self):
         get(self, **self.conan_data["sources"][self.version], strip_root=True)
diff --git a/recipes/onnxruntime/all/patches/1.14.1-0006-cuda-gsl.patch b/recipes/onnxruntime/all/patches/1.14.1-0006-cuda-gsl.patch
index 965b24fe9d601..de12d90d18497 100644
--- a/recipes/onnxruntime/all/patches/1.14.1-0006-cuda-gsl.patch
+++ b/recipes/onnxruntime/all/patches/1.14.1-0006-cuda-gsl.patch
@@ -1,11 +1,12 @@
 diff --git a/cmake/external/onnxruntime_external_deps.cmake b/cmake/external/onnxruntime_external_deps.cmake
-index b051ff0be1..19ce4107de 100644
+index 0c41945778..9bb5ffc63b 100644
 --- a/cmake/external/onnxruntime_external_deps.cmake
 +++ b/cmake/external/onnxruntime_external_deps.cmake
-@@ -251,6 +251,7 @@ if(onnxruntime_USE_CUDA)
+@@ -250,7 +250,7 @@ if(onnxruntime_USE_CUDA)
+     GSL
      URL ${DEP_URL_microsoft_gsl}
      URL_HASH SHA1=${DEP_SHA1_microsoft_gsl}
-     PATCH_COMMAND ${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/gsl/1064.patch
+-    PATCH_COMMAND ${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/gsl/1064.patch
 +    FIND_PACKAGE_ARGS 4.0 NAMES Microsoft.GSL
    )
  else()
diff --git a/recipes/onnxruntime/all/patches/1.15.1-0002-cuda-gsl.patch b/recipes/onnxruntime/all/patches/1.15.1-0002-cuda-gsl.patch
index dd1d64e3fdb8e..f8e107029bd12 100644
--- a/recipes/onnxruntime/all/patches/1.15.1-0002-cuda-gsl.patch
+++ b/recipes/onnxruntime/all/patches/1.15.1-0002-cuda-gsl.patch
@@ -1,11 +1,12 @@
 diff --git a/cmake/external/onnxruntime_external_deps.cmake b/cmake/external/onnxruntime_external_deps.cmake
-index 9effd1a2db..3284c35424 100644
+index 9effd1a2db..db30f7a0d4 100644
 --- a/cmake/external/onnxruntime_external_deps.cmake
 +++ b/cmake/external/onnxruntime_external_deps.cmake
-@@ -288,6 +288,7 @@ if(onnxruntime_USE_CUDA)
+@@ -287,7 +287,7 @@ if(onnxruntime_USE_CUDA)
+     GSL
      URL ${DEP_URL_microsoft_gsl}
      URL_HASH SHA1=${DEP_SHA1_microsoft_gsl}
-     PATCH_COMMAND ${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/gsl/1064.patch
+-    PATCH_COMMAND ${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/gsl/1064.patch
 +    FIND_PACKAGE_ARGS 4.0 NAMES Microsoft.GSL
    )
  else()
diff --git a/recipes/onnxruntime/all/patches/1.16.0-0002-cuda-gsl.patch b/recipes/onnxruntime/all/patches/1.16.0-0002-cuda-gsl.patch
index 7659e55bcec80..d66ee9bb957f9 100644
--- a/recipes/onnxruntime/all/patches/1.16.0-0002-cuda-gsl.patch
+++ b/recipes/onnxruntime/all/patches/1.16.0-0002-cuda-gsl.patch
@@ -1,11 +1,12 @@
 diff --git a/cmake/external/onnxruntime_external_deps.cmake b/cmake/external/onnxruntime_external_deps.cmake
-index 8e412c7847..d9edf2a101 100644
+index 8e412c7847..efd599e5b3 100644
 --- a/cmake/external/onnxruntime_external_deps.cmake
 +++ b/cmake/external/onnxruntime_external_deps.cmake
-@@ -302,6 +302,7 @@ if(onnxruntime_USE_CUDA)
+@@ -301,7 +301,7 @@ if(onnxruntime_USE_CUDA)
+     GSL
      URL ${DEP_URL_microsoft_gsl}
      URL_HASH SHA1=${DEP_SHA1_microsoft_gsl}
-     PATCH_COMMAND ${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/gsl/1064.patch
+-    PATCH_COMMAND ${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/gsl/1064.patch
 +    FIND_PACKAGE_ARGS 4.0 NAMES Microsoft.GSL
    )
  else()
diff --git a/recipes/onnxruntime/all/patches/1.16.1-0002-cuda-gsl.patch b/recipes/onnxruntime/all/patches/1.16.1-0002-cuda-gsl.patch
index 7659e55bcec80..103e16e300545 100644
--- a/recipes/onnxruntime/all/patches/1.16.1-0002-cuda-gsl.patch
+++ b/recipes/onnxruntime/all/patches/1.16.1-0002-cuda-gsl.patch
@@ -1,11 +1,12 @@
 diff --git a/cmake/external/onnxruntime_external_deps.cmake b/cmake/external/onnxruntime_external_deps.cmake
-index 8e412c7847..d9edf2a101 100644
+index 0e2482d7de..4210e0bc5c 100644
 --- a/cmake/external/onnxruntime_external_deps.cmake
 +++ b/cmake/external/onnxruntime_external_deps.cmake
-@@ -302,6 +302,7 @@ if(onnxruntime_USE_CUDA)
+@@ -301,7 +301,7 @@ if(onnxruntime_USE_CUDA)
+     GSL
      URL ${DEP_URL_microsoft_gsl}
      URL_HASH SHA1=${DEP_SHA1_microsoft_gsl}
-     PATCH_COMMAND ${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/gsl/1064.patch
+-    PATCH_COMMAND ${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/gsl/1064.patch
 +    FIND_PACKAGE_ARGS 4.0 NAMES Microsoft.GSL
    )
  else()