| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
|
|
|
|
| (define-module (system foreign-library) |
| #:use-module (ice-9 match) |
| #:use-module (srfi srfi-9) |
| #:use-module (system foreign) |
| #:export (guile-extensions-path |
| ltdl-library-path |
| guile-system-extensions-path |
|
|
| lib->cyg |
| load-foreign-library |
| foreign-library? |
| foreign-library-pointer |
| foreign-library-function)) |
|
|
| (define-record-type <foreign-library> |
| (make-foreign-library filename handle) |
| foreign-library? |
| (filename foreign-library-filename) |
| (handle foreign-library-handle set-foreign-library-handle!)) |
|
|
| (eval-when (expand load eval) |
| (load-extension (string-append "libguile-" (effective-version)) |
| "scm_init_system_foreign_library")) |
|
|
| (define system-library-extensions |
| (cond |
| ((string-contains %host-type "-darwin") |
| '(".bundle" ".so" ".dylib")) |
| ((or (string-contains %host-type "cygwin") |
| (string-contains %host-type "mingw") |
| (string-contains %host-type "msys")) |
| '(".dll")) |
| (else |
| '(".so")))) |
|
|
| (define (has-extension? head exts) |
| (match exts |
| (() #f) |
| ((ext . exts) |
| (or (string-contains head ext) |
| (has-extension? head exts))))) |
|
|
| (define (file-exists-with-extension head exts) |
| (if (has-extension? head exts) |
| (and (file-exists? head) head) |
| (let lp ((exts exts)) |
| (match exts |
| (() #f) |
| ((ext . exts) |
| (let ((head (string-append head ext))) |
| (if (file-exists? head) |
| head |
| (lp exts)))))))) |
|
|
| (define (file-exists-in-path-with-extension basename path exts) |
| (match path |
| (() #f) |
| ((dir . path) |
| (or (file-exists-with-extension (in-vicinity dir basename) exts) |
| (file-exists-in-path-with-extension basename path exts))))) |
|
|
| (define path-separator |
| (case (system-file-name-convention) |
| ((posix) #\:) |
| ((windows) #\;) |
| (else (error "unreachable")))) |
|
|
| (define (parse-path var) |
| (match (getenv var) |
| (#f #f) |
| |
| ("" '()) |
| (val (string-split val path-separator)))) |
|
|
| (define guile-extensions-path |
| (make-parameter |
| (or (parse-path "GUILE_EXTENSIONS_PATH") '()))) |
|
|
| (define ltdl-library-path |
| (make-parameter |
| (or (parse-path "LTDL_LIBRARY_PATH") '()))) |
|
|
| (define guile-system-extensions-path |
| (make-parameter |
| (or (parse-path "GUILE_SYSTEM_EXTENSIONS_PATH") |
| (list (assq-ref %guile-build-info 'libdir) |
| (assq-ref %guile-build-info 'extensiondir))))) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| (define (augment-ltdl-library-path path) |
| (match path |
| (() '()) |
| ((dir . path) |
| (cons* dir (in-vicinity dir ".libs") |
| (augment-ltdl-library-path path))))) |
|
|
| (define (default-search-path search-ltdl-library-path?) |
| (append |
| (guile-extensions-path) |
| (if search-ltdl-library-path? |
| (augment-ltdl-library-path (ltdl-library-path)) |
| '()) |
| (guile-system-extensions-path))) |
|
|
| (define (lib->cyg name) |
| "Convert a standard shared library name to a Cygwin shared library |
| name." |
| (if (not name) |
| #f |
| (let ((start (1+ (or (string-index-right |
| name |
| (lambda (c) (or (char=? #\\ c) (char=? #\/ c)))) |
| -1)))) |
| (cond |
| ((>= (+ 3 start) (string-length name)) |
| name) |
| ((string= name "lib" start (+ start 3)) |
| (string-append (substring name 0 start) |
| "cyg" |
| (substring name (+ start 3)))) |
| (else |
| name))))) |
|
|
| (define* (load-foreign-library #:optional filename #:key |
| (extensions system-library-extensions) |
| (search-ltdl-library-path? #t) |
| (search-path (default-search-path |
| search-ltdl-library-path?)) |
| (search-system-paths? #t) |
| (lazy? #t) (global? #f) (rename-on-cygwin? #t)) |
| (define (error-not-found) |
| (scm-error 'misc-error "load-foreign-library" |
| "file: ~S, message: ~S" |
| (list filename "file not found") |
| #f)) |
| (define flags |
| (logior (if lazy? RTLD_LAZY RTLD_NOW) |
| (if global? RTLD_GLOBAL RTLD_LOCAL))) |
| (define (dlopen* name) (dlopen name flags)) |
| (if (and rename-on-cygwin? (string-contains %host-type "cygwin")) |
| (set! filename (lib->cyg filename))) |
| (make-foreign-library |
| filename |
| (cond |
| ((not filename) |
| |
| (dlopen* #f)) |
| ((or (absolute-file-name? filename) |
| (string-any file-name-separator? filename)) |
| (cond |
| ((or (file-exists-with-extension filename extensions) |
| (and search-ltdl-library-path? |
| (file-exists-with-extension |
| (in-vicinity (in-vicinity (dirname filename) ".libs") |
| (basename filename)) |
| extensions))) |
| => dlopen*) |
| (else |
| (error-not-found)))) |
| ((file-exists-in-path-with-extension filename search-path extensions) |
| => dlopen*) |
| (search-system-paths? |
| (if (or (null? extensions) (has-extension? filename extensions)) |
| (dlopen* filename) |
| (let lp ((extensions extensions)) |
| (match extensions |
| ((extension) |
| |
| (dlopen* (string-append filename extension))) |
| ((extension . extensions) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| (or (false-if-exception |
| (dlopen* (string-append filename extension))) |
| (lp extensions))))))) |
| (else |
| (error-not-found))))) |
|
|
| (define (->foreign-library lib) |
| (if (foreign-library? lib) |
| lib |
| (load-foreign-library lib))) |
|
|
| (define* (foreign-library-pointer lib name) |
| (let ((handle (foreign-library-handle (->foreign-library lib)))) |
| (dlsym handle name))) |
|
|
| (define* (foreign-library-function lib name |
| #:key |
| (return-type void) |
| (arg-types '()) |
| (return-errno? #f)) |
| (let ((pointer (foreign-library-pointer lib name))) |
| (pointer->procedure return-type pointer arg-types |
| #:return-errno? return-errno?))) |
|
|