Skip to content

Commit

Permalink
go search paths
Browse files Browse the repository at this point in the history
  • Loading branch information
fundthmcalculus committed May 19, 2022
1 parent 5a5fccd commit 4f4626c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
37 changes: 32 additions & 5 deletions go/okapi/native.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package okapi

import (
"errors"
"fmt"
"google.golang.org/protobuf/proto"
"os"
"path"
"runtime"
"strings"
"unsafe"
)

Expand Down Expand Up @@ -38,20 +42,43 @@ func (d *DidError) Error() string {
return fmt.Sprintf("Error on call: %s() return code=%d message=%s", d.FunctionName, d.Code, d.Message)
}

func getLibraryName() string {
func getLibraryName() (string, string) {
os := runtime.GOOS
// TODO - ARM support
switch os {
case "windows":
return "okapi.dll"
return "windows", "okapi.dll"
case "darwin":
return "libokapi.dylib"
return "macos", "libokapi.dylib"
case "linux":
return "libokapi.so"
return "linux", "libokapi.so"
default:
return "okapi"
return "", "okapi"
}
}

func getLibraryPath() string {
libFolder, libName := getLibraryName()
ldLibPath := os.Getenv("LD_LIBRARY_PATH")
// TODO - Check the appropriate folders
checkPaths := []string{
path.Join(".", libName),
path.Join(".", libFolder, libName),
path.Join(ldLibPath, libName),
path.Join(ldLibPath, libFolder, libName),
}

for _, checkPath := range checkPaths {
if _, err := os.Stat(checkPath); errors.Is(err, os.ErrNotExist) {
continue
} else {
// Return this path
return checkPath
}
}
panic(fmt.Sprintf("could not find necessary okapi binary. paths searched:\n%s", strings.Join(checkPaths, "\n")))
}

func createBuffersFromMessage(requestMessage proto.Message) (ByteBuffer, ByteBuffer, ExternError, error) {
out, e := proto.Marshal(requestMessage)
if e != nil {
Expand Down
4 changes: 2 additions & 2 deletions go/okapi/native_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func callOkapiNative(request proto.Message, response proto.Message, funcName str
}

func getFunctionPointer(funcName string) (*dlopen.LibHandle, unsafe.Pointer) {
libPtr, err := dlopen.GetHandle([]string{getLibraryName()})
libPtr, err := dlopen.GetHandle([]string{getLibraryPath()})
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func cByteBufferFree(cRespBuf C.ByteBuffer) error {
}

func okapiStringFree(s uintptr) error {
dll := syscall.MustLoadDLL(getLibraryName())
dll := syscall.MustLoadDLL(getLibraryPath())
okapiFunc := dll.MustFindProc("okapi_string_free")
_, _, err := okapiFunc.Call(s)
if err != syscall.Errno(0x0) {
Expand Down
6 changes: 3 additions & 3 deletions go/okapi/native_call_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func callOkapiNative(request proto.Message, response proto.Message, funcName str
if err != nil {
return wrapError("Failed to create buffers", err)
}
dll := syscall.MustLoadDLL(getLibraryName())
dll := syscall.MustLoadDLL(getLibraryPath())
okapiFunc := dll.MustFindProc(funcName)
_, _, err = okapiFunc.Call(uintptr(unsafe.Pointer(&requestBuffer)), uintptr(unsafe.Pointer(&responseBuffer)), uintptr(unsafe.Pointer(&errorBuffer)))
if err != syscall.Errno(0x0) {
Expand All @@ -26,7 +26,7 @@ func callOkapiNative(request proto.Message, response proto.Message, funcName str
}

func byteBufferFree(responseBuffer ByteBuffer) error {
dll := syscall.MustLoadDLL(getLibraryName())
dll := syscall.MustLoadDLL(getLibraryPath())
okapiFunc := dll.MustFindProc("okapi_bytebuffer_free")
_, _, err := okapiFunc.Call(uintptr(unsafe.Pointer(&responseBuffer)))
if err != syscall.Errno(0x0) {
Expand All @@ -37,7 +37,7 @@ func byteBufferFree(responseBuffer ByteBuffer) error {
}

func okapiStringFree(s uintptr) error {
dll := syscall.MustLoadDLL(getLibraryName())
dll := syscall.MustLoadDLL(getLibraryPath())
okapiFunc := dll.MustFindProc("okapi_string_free")
_, _, err := okapiFunc.Call(s)
if err != syscall.Errno(0x0) {
Expand Down

0 comments on commit 4f4626c

Please sign in to comment.