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
10 ;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
12 ;;;; *************************************************************************
16 (defvar *loaded-libraries* nil
17 "List of foreign libraries loaded. Used to prevent reloading a library")
19 (defun default-foreign-library-type ()
20 "Returns string naming default library type for platform"
21 #+(or win32 win64 cygwin mswindows windows) "dll"
22 #+(or macosx darwin ccl-5.0) "dylib"
23 #-(or win32 win64 cygwin mswindows windows macosx darwin ccl-5.0) "so"
26 (defun foreign-library-types ()
27 "Returns list of string naming possible library types for platform, sorted by preference"
28 #+(or win32 win64 windows mswindows) '("dll" "lib")
29 #+(or macosx darwin ccl-5.0) '("dylib" "bundle")
30 #-(or win32 win64 windows mswindows macosx darwin ccl-5.0) '("so" "a" "o")
33 (defun find-foreign-library (names directories &key types drive-letters)
34 "Looks for a foreign library. directories can be a single
35 string or a list of strings of candidate directories. Use default
36 library type if type is not specified."
38 (setq types (foreign-library-types)))
40 (setq types (list types)))
42 (setq names (list names)))
43 (unless (listp directories)
44 (setq directories (list directories)))
45 #+(or win32 win64 windows mswindows)
46 (unless (listp drive-letters)
47 (setq drive-letters (list drive-letters)))
48 #-(or win32 win64 windows mswindows)
49 (setq drive-letters '(nil))
50 (dolist (drive-letter drive-letters)
52 (dolist (dir directories)
54 (let ((path (make-pathname
56 #+lispworks (when drive-letter drive-letter)
58 #-lispworks (when drive-letter drive-letter)
64 (pathname-directory dir))
69 (parse-namestring dir)))))))
70 (when (probe-file path)
71 (return-from find-foreign-library path)))))))
75 (defun load-foreign-library (filename &key module supporting-libraries
77 (declare (ignorable module supporting-libraries))
79 (flet ((load-failure ()
80 (error "Unable to load foreign library \"~A\"." filename)))
81 (declare (ignorable #'load-failure))
82 (when (and filename (or (null (pathname-directory filename))
83 (probe-file filename)))
84 (if (pathnamep filename) ;; ensure filename is a string to check if already loaded
85 (setq filename (namestring (if (null (pathname-directory filename))
87 ;; lispworks treats as UNC, so use truename
88 #+(and lispworks mswindows) (truename filename)
89 #-(and lispworks mswindows) filename))))
91 (if (and (not force-load)
92 (find filename *loaded-libraries* :test #'string-equal))
93 t ;; return T, but don't reload library
96 (let ((type (pathname-type (parse-namestring filename))))
97 (if (string-equal type "so")
99 (sys::load-object-file filename)
101 (alien:load-foreign filename
103 (convert-supporting-libraries-to-string
104 supporting-libraries))))
106 (alien:load-foreign filename
108 (convert-supporting-libraries-to-string
109 supporting-libraries))
111 (handler-case (sb-alien::load-1-foreign filename)
112 (sb-int:unsupported-operator (c)
113 (if (fboundp (intern "LOAD-SHARED-OBJECT" :sb-alien))
114 (funcall (intern "LOAD-SHARED-OBJECT" :sb-alien) filename)
117 #+lispworks (fli:register-module module :real-name filename
118 :connection-style :immediate)
119 #+allegro (load filename)
120 #+openmcl (ccl:open-shared-library filename)
121 #+digitool (ccl:add-to-shared-library-search-path filename t)
123 (push filename *loaded-libraries*)
126 (defun convert-supporting-libraries-to-string (libs)
129 (push (format nil "-l~A" lib) lib-load-list))
130 (nreverse lib-load-list)))