1 module bindbc.onnxruntime.v12.bind; 2 3 import bindbc.loader; 4 import bindbc.onnxruntime.config; 5 import bindbc.onnxruntime.v12.types; 6 7 version (BindONNXRuntime_Static) 8 { 9 extern (C) OrtApiBase* OrtGetApiBase(); 10 } 11 else 12 { 13 private __gshared SharedLib lib; 14 private __gshared ONNXRuntimeSupport loadedVersion; 15 16 extern (C) @nogc nothrow: 17 18 __gshared const(OrtApiBase)* function() OrtGetApiBase; 19 20 ONNXRuntimeSupport loadedONNXVersion() 21 { 22 return loadedVersion; 23 } 24 25 bool isONNXLoaded() 26 { 27 return lib != invalidHandle; 28 } 29 30 ONNXRuntimeSupport loadONNXRuntime() 31 { 32 version (Windows) 33 { 34 const(char)[][1] libNames = ["onnxruntime.dll"]; 35 } 36 else version (OSX) 37 { 38 const(char)[][2] libNames = [ 39 "libonnxruntime.dylib", "libonnxruntime.1.2.0.dylib" 40 ]; 41 } 42 else version (Posix) 43 { 44 const(char)[][2] libNames = ["onnxruntime.so", "onnxruntime.so.1.2"]; 45 } 46 else 47 static assert(false, 48 "bindbc-onnxruntime support for ONNX Runtime 1.2 is not implemented on this platform."); 49 50 ONNXRuntimeSupport ret; 51 foreach (libName; libNames) 52 { 53 ret = loadONNXRuntimeByLibName(libName.ptr); 54 if (ret != ONNXRuntimeSupport.noLibrary) 55 { 56 return ret; 57 } 58 } 59 60 return ONNXRuntimeSupport.noLibrary; 61 } 62 63 ONNXRuntimeSupport loadONNXRuntimeByLibName(const char* libName) 64 in(libName !is null) 65 { 66 lib = load(libName); 67 if (lib == invalidHandle) 68 { 69 return ONNXRuntimeSupport.noLibrary; 70 } 71 72 const errCount = errorCount(); 73 74 lib.bindSymbol(cast(void**)&OrtGetApiBase, "OrtGetApiBase"); 75 76 if (errCount != errorCount()) 77 { 78 return ONNXRuntimeSupport.badLibrary; 79 } 80 loadedVersion = ONNXRuntimeSupport.onnx12; 81 82 return ONNXRuntimeSupport.onnx12; 83 } 84 85 void unloadONNXRuntime() 86 { 87 if (lib != invalidHandle) 88 { 89 lib.unload(); 90 } 91 } 92 }