1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: libraries.lisp
6 ;;;; Purpose: UFFI source to load foreign libraries
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
12 ;;;; This file, part of UFFI, is Copyright (c) 2002-2005 by Kevin M. Rosenberg
14 ;;;; *************************************************************************
18 (defvar *loaded-libraries* nil
19 "List of foreign libraries loaded. Used to prevent reloading a library")
21 (defun default-foreign-library-type ()
22 "Returns string naming default library type for platform"
23 #+(or win32 win64 cygwin mswindows windows) "dll"
24 #+(or macosx darwin ccl-5.0) "dylib"
25 #-(or win32 win64 cygwin mswindows windows macosx darwin ccl-5.0) "so"
28 (defun foreign-library-types ()
29 "Returns list of string naming possible library types for platform, sorted by preference"
30 #+(or win32 win64 windows mswindows) '("dll" "lib")
31 #+(or macosx darwin ccl-5.0) '("dylib" "bundle")
32 #-(or win32 win64 windows mswindows macosx darwin ccl-5.0) '("so" "a" "o")
35 (defun find-foreign-library (names directories &key types drive-letters)
36 "Looks for a foreign library. directories can be a single
37 string or a list of strings of candidate directories. Use default
38 library type if type is not specified."
40 (setq types (foreign-library-types)))
42 (setq types (list types)))
44 (setq names (list names)))
45 (unless (listp directories)
46 (setq directories (list directories)))
47 #+(or win32 win64 windows mswindows)
48 (unless (listp drive-letters)
49 (setq drive-letters (list drive-letters)))
50 #-(or win32 win64 windows mswindows)
51 (setq drive-letters '(nil))
52 (dolist (drive-letter drive-letters)
54 (dolist (dir directories)
56 (let ((path (make-pathname
58 #+lispworks (when drive-letter drive-letter)
60 #-lispworks (when drive-letter drive-letter)
66 (pathname-directory dir))
71 (parse-namestring dir)))))))
72 (when (probe-file path)
73 (return-from find-foreign-library path)))))))
77 (defun load-foreign-library (filename &key module supporting-libraries
79 #+(or allegro openmcl digitool sbcl) (declare (ignore module supporting-libraries))
80 #+(or cmu scl) (declare (ignore module))
81 #+lispworks (declare (ignore supporting-libraries))
83 (flet ((load-failure ()
84 (error "Unable to load foreign library \"~A\"." filename)))
85 (when (and filename (or (null (pathname-directory filename))
86 (probe-file filename)))
87 (if (pathnamep filename) ;; ensure filename is a string to check if already loaded
88 (setq filename (namestring (if (null (pathname-directory filename))
90 ;; lispworks treats as UNC, so use truename
91 #+(and lispworks mswindows) (truename filename)
92 #-(and lispworks mswindows) filename))))
94 (if (and (not force-load)
95 (find filename *loaded-libraries* :test #'string-equal))
96 t ;; return T, but don't reload library
99 (let ((type (pathname-type (parse-namestring filename))))
100 (if (string-equal type "so")
102 (sys::load-object-file filename)
104 (alien:load-foreign filename
106 (convert-supporting-libraries-to-string
107 supporting-libraries))))
109 (let ((type (pathname-type (parse-namestring filename))))
110 (alien:load-foreign filename
112 (convert-supporting-libraries-to-string
113 supporting-libraries)))
115 (handler-case (sb-alien::load-1-foreign filename)
116 (sb-int:unsupported-operator (c)
117 (if (fboundp (intern "LOAD-SHARED-OBJECT" :sb-alien))
118 (funcall (intern "LOAD-SHARED-OBJECT" :sb-alien) filename)
121 #+lispworks (fli:register-module module :real-name filename
122 :connection-style :immediate)
123 #+allegro (load filename)
124 #+openmcl (ccl:open-shared-library filename)
125 #+digitool (ccl:add-to-shared-library-search-path filename t)
127 (push filename *loaded-libraries*)
130 (defun convert-supporting-libraries-to-string (libs)
133 (push (format nil "-l~A" lib) lib-load-list))
134 (nreverse lib-load-list)))