mirror of
https://github.com/logos-messaging/libchat.git
synced 2026-05-18 16:09:31 +00:00
chore(nim-bindings): replace dynlib dlopen with plain importc
The dynlib pragma hard-coded a library path and resolved it via dlopen() at
runtime, preventing static linking and forcing a specific load-time path.
Using bare {.importc.} lets consumers choose: link liblibchat dynamically
at link time (--passL:-llibchat) or link it statically into their binary.
This commit is contained in:
parent
960bb195a1
commit
803a11ce27
@ -18,4 +18,4 @@ before build:
|
|||||||
exec "cargo build --release --manifest-path ../Cargo.toml"
|
exec "cargo build --release --manifest-path ../Cargo.toml"
|
||||||
|
|
||||||
task pingpong, "Run pingpong example":
|
task pingpong, "Run pingpong example":
|
||||||
exec "nim c -r --path:src examples/pingpong.nim"
|
exec "nim c -r --path:src --passL:../target/release/liblibchat.a examples/pingpong.nim"
|
||||||
|
|||||||
@ -1,29 +1,5 @@
|
|||||||
# Nim FFI bindings for libchat conversations library
|
# Nim FFI bindings for libchat conversations library
|
||||||
|
|
||||||
import std/[os]
|
|
||||||
|
|
||||||
# Dynamic library path resolution
|
|
||||||
# Can be overridden at compile time with -d:CONVERSATIONS_LIB:"path/to/lib"
|
|
||||||
# Or at runtime via LIBCHAT_LIB environment variable
|
|
||||||
when defined(macosx):
|
|
||||||
const DEFAULT_LIB_NAME = "liblibchat.dylib"
|
|
||||||
elif defined(linux):
|
|
||||||
const DEFAULT_LIB_NAME = "liblibchat.so"
|
|
||||||
elif defined(windows):
|
|
||||||
const DEFAULT_LIB_NAME = "libchat.dll"
|
|
||||||
else:
|
|
||||||
const DEFAULT_LIB_NAME = "libchat"
|
|
||||||
|
|
||||||
# Try to find the library relative to the source file location at compile time
|
|
||||||
const
|
|
||||||
thisDir = currentSourcePath().parentDir()
|
|
||||||
projectRoot = thisDir.parentDir().parentDir()
|
|
||||||
releaseLibPath = projectRoot / "target" / "release" / DEFAULT_LIB_NAME
|
|
||||||
debugLibPath = projectRoot / "target" / "debug" / DEFAULT_LIB_NAME
|
|
||||||
|
|
||||||
# Default to release path, can be overridden with -d:CONVERSATIONS_LIB:"..."
|
|
||||||
const CONVERSATIONS_LIB* {.strdefine.} = releaseLibPath
|
|
||||||
|
|
||||||
# Error codes (must match Rust ErrorCode enum)
|
# Error codes (must match Rust ErrorCode enum)
|
||||||
const
|
const
|
||||||
ErrNone* = 0'i32
|
ErrNone* = 0'i32
|
||||||
@ -97,23 +73,23 @@ type
|
|||||||
|
|
||||||
## Creates a new libchat Context
|
## Creates a new libchat Context
|
||||||
## Returns: Opaque handle to the context. Must be freed with destroy_context()
|
## Returns: Opaque handle to the context. Must be freed with destroy_context()
|
||||||
proc create_context*(name: ReprCString): ContextHandle {.importc, dynlib: CONVERSATIONS_LIB.}
|
proc create_context*(name: ReprCString): ContextHandle {.importc.}
|
||||||
|
|
||||||
## Returns the friendly name of the context's identity
|
## Returns the friendly name of the context's identity
|
||||||
## The result must be freed by the caller (repr_c::String ownership transfers)
|
## The result must be freed by the caller (repr_c::String ownership transfers)
|
||||||
proc installation_name*(ctx: ContextHandle): ReprCString {.importc, dynlib: CONVERSATIONS_LIB.}
|
proc installation_name*(ctx: ContextHandle): ReprCString {.importc.}
|
||||||
|
|
||||||
## Destroys a context and frees its memory
|
## Destroys a context and frees its memory
|
||||||
## - handle must be a valid pointer from create_context()
|
## - handle must be a valid pointer from create_context()
|
||||||
## - handle must not be used after this call
|
## - handle must not be used after this call
|
||||||
proc destroy_context*(ctx: ContextHandle) {.importc, dynlib: CONVERSATIONS_LIB.}
|
proc destroy_context*(ctx: ContextHandle) {.importc.}
|
||||||
|
|
||||||
## Creates an intro bundle for sharing with other users
|
## Creates an intro bundle for sharing with other users
|
||||||
## Returns: CreateIntroResult struct - check error_code field (0 = success, negative = error)
|
## Returns: CreateIntroResult struct - check error_code field (0 = success, negative = error)
|
||||||
## The result must be freed with destroy_intro_result()
|
## The result must be freed with destroy_intro_result()
|
||||||
proc create_intro_bundle*(
|
proc create_intro_bundle*(
|
||||||
ctx: ContextHandle,
|
ctx: ContextHandle,
|
||||||
): CreateIntroResult {.importc, dynlib: CONVERSATIONS_LIB.}
|
): CreateIntroResult {.importc.}
|
||||||
|
|
||||||
## Creates a new private conversation
|
## Creates a new private conversation
|
||||||
## Returns: NewConvoResult struct - check error_code field (0 = success, negative = error)
|
## Returns: NewConvoResult struct - check error_code field (0 = success, negative = error)
|
||||||
@ -122,7 +98,7 @@ proc create_new_private_convo*(
|
|||||||
ctx: ContextHandle,
|
ctx: ContextHandle,
|
||||||
bundle: SliceUint8,
|
bundle: SliceUint8,
|
||||||
content: SliceUint8,
|
content: SliceUint8,
|
||||||
): NewConvoResult {.importc, dynlib: CONVERSATIONS_LIB.}
|
): NewConvoResult {.importc.}
|
||||||
|
|
||||||
## Sends content to an existing conversation
|
## Sends content to an existing conversation
|
||||||
## Returns: SendContentResult struct - check error_code field (0 = success, negative = error)
|
## Returns: SendContentResult struct - check error_code field (0 = success, negative = error)
|
||||||
@ -131,7 +107,7 @@ proc send_content*(
|
|||||||
ctx: ContextHandle,
|
ctx: ContextHandle,
|
||||||
convo_id: ReprCString,
|
convo_id: ReprCString,
|
||||||
content: SliceUint8,
|
content: SliceUint8,
|
||||||
): SendContentResult {.importc, dynlib: CONVERSATIONS_LIB.}
|
): SendContentResult {.importc.}
|
||||||
|
|
||||||
## Handles an incoming payload
|
## Handles an incoming payload
|
||||||
## Returns: HandlePayloadResult struct - check error_code field (0 = success, negative = error)
|
## Returns: HandlePayloadResult struct - check error_code field (0 = success, negative = error)
|
||||||
@ -141,19 +117,19 @@ proc send_content*(
|
|||||||
proc handle_payload*(
|
proc handle_payload*(
|
||||||
ctx: ContextHandle,
|
ctx: ContextHandle,
|
||||||
payload: SliceUint8,
|
payload: SliceUint8,
|
||||||
): HandlePayloadResult {.importc, dynlib: CONVERSATIONS_LIB.}
|
): HandlePayloadResult {.importc.}
|
||||||
|
|
||||||
## Free the result from create_intro_bundle
|
## Free the result from create_intro_bundle
|
||||||
proc destroy_intro_result*(result: CreateIntroResult) {.importc, dynlib: CONVERSATIONS_LIB.}
|
proc destroy_intro_result*(result: CreateIntroResult) {.importc.}
|
||||||
|
|
||||||
## Free the result from create_new_private_convo
|
## Free the result from create_new_private_convo
|
||||||
proc destroy_convo_result*(result: NewConvoResult) {.importc, dynlib: CONVERSATIONS_LIB.}
|
proc destroy_convo_result*(result: NewConvoResult) {.importc.}
|
||||||
|
|
||||||
## Free the result from send_content
|
## Free the result from send_content
|
||||||
proc destroy_send_content_result*(result: SendContentResult) {.importc, dynlib: CONVERSATIONS_LIB.}
|
proc destroy_send_content_result*(result: SendContentResult) {.importc.}
|
||||||
|
|
||||||
## Free the result from handle_payload
|
## Free the result from handle_payload
|
||||||
proc destroy_handle_payload_result*(result: HandlePayloadResult) {.importc, dynlib: CONVERSATIONS_LIB.}
|
proc destroy_handle_payload_result*(result: HandlePayloadResult) {.importc.}
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
# Helper functions
|
# Helper functions
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user