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 by Kevin M. Rosenberg
14 ;;;; UFFI users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
21 (defvar *loaded-libraries* nil
22 "List of foreign libraries loaded. Used to prevent reloading a library")
24 (defun default-foreign-library-type ()
25 "Returns string naming default library type for platform"
26 #+(or win32 mswindows) "dll"
27 #+(or macosx darwin ccl-5.0) "dylib"
28 #-(or win32 mswindows macosx darwin ccl-5.0) "so"
31 (defun foreign-library-types ()
32 "Returns list of string naming possible library types for platform, sorted by preference"
33 #+(or win32 mswindows) '("dll" "lib")
34 #+(or macosx darwin ccl-5.0) '("dylib" "bundle")
35 #-(or win32 mswindows macosx darwin ccl-5.0) '("so" "a" "o")
38 (defun find-foreign-library (names directories &key types drive-letters)
39 "Looks for a foreign library. directories can be a single
40 string or a list of strings of candidate directories. Use default
41 library type if type is not specified."
43 (setq types (foreign-library-types)))
45 (setq types (list types)))
47 (setq names (list names)))
48 (unless (listp directories)
49 (setq directories (list directories)))
50 #+(or win32 mswindows)
51 (unless (listp drive-letters)
52 (setq drive-letters (list drive-letters)))
53 #-(or win32 mswindows)
54 (setq drive-letters '(nil))
55 (dolist (drive-letter drive-letters)
57 (dolist (dir directories)
59 (let ((path (make-pathname
61 #+lispworks (when drive-letter drive-letter)
63 #-lispworks (when drive-letter drive-letter)
69 (pathname-directory dir))
74 (parse-namestring dir)))))))
75 (when (probe-file path)
76 (return-from find-foreign-library path)))))))
80 (defun load-foreign-library (filename &key module supporting-libraries
82 #+(or allegro mcl) (declare (ignore module supporting-libraries))
83 #+(or cmu scl sbcl) (declare (ignore module))
84 #+lispworks (declare (ignore supporting-libraries))
86 (when (and filename (or (null (pathname-directory filename))
87 (probe-file filename)))
88 (if (pathnamep filename) ;; ensure filename is a string to check if already loaded
89 (setq filename (namestring (if (null (pathname-directory filename))
91 ;; lispworks treats as UNC, so use truename
92 #+(and lispworks win32 mswindows) (truename filename)
93 #-(and lispworks win32 mswindows) filename))))
95 (if (and (not force-load)
96 (find filename *loaded-libraries* :test #'string-equal))
97 t ;; return T, but don't reload library
100 (let ((type (pathname-type (parse-namestring filename))))
101 (if (string-equal type "so")
102 (sys::load-object-file filename)
103 (alien:load-foreign filename
105 (convert-supporting-libraries-to-string
106 supporting-libraries))))
108 (let ((type (pathname-type (parse-namestring filename))))
109 (alien:load-foreign filename
111 (convert-supporting-libraries-to-string
112 supporting-libraries)))
114 (handler-case (sb-alien::load-1-foreign filename)
115 (sb-int:unsupported-operator (c)
116 (if (fboundp (intern "LOAD-SHARED-OBJECT" :sb-alien))
117 (funcall (intern "LOAD-SHARED-OBJECT" :sb-alien) filename)
120 #+lispworks (fli:register-module module :real-name filename
121 :connection-style :immediate)
122 #+allegro (load filename)
123 #+openmcl (ccl:open-shared-library filename)
124 #+(and mcl (not openmcl)) (ccl:add-to-shared-library-search-path filename t)
126 (push filename *loaded-libraries*)
129 (defun convert-supporting-libraries-to-string (libs)
132 (push (format nil "-l~A" lib) lib-load-list))
133 (nreverse lib-load-list)))