Version 1.8.3: patch from Stelian Ionescu
[uffi.git] / src / libraries.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          libraries.lisp
6 ;;;; Purpose:       UFFI source to load foreign libraries
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
11 ;;;;
12 ;;;; *************************************************************************
13
14 (in-package #:uffi)
15
16 (defvar *loaded-libraries* nil
17   "List of foreign libraries loaded. Used to prevent reloading a library")
18
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"
24 )
25
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")
31 )
32
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."
37   (unless types
38     (setq types (foreign-library-types)))
39   (unless (listp types)
40     (setq types (list types)))
41   (unless (listp names)
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)
51     (dolist (name names)
52       (dolist (dir directories)
53         (dolist (type types)
54           (let ((path (make-pathname
55                        #+lispworks :host
56                        #+lispworks (when drive-letter drive-letter)
57                        #-lispworks :device
58                        #-lispworks (when drive-letter drive-letter)
59                        :name name
60                        :type type
61                        :directory
62                        (etypecase dir
63                          (pathname
64                           (pathname-directory dir))
65                          (list
66                           dir)
67                          (string
68                           (pathname-directory
69                            (parse-namestring dir)))))))
70             (when (probe-file path)
71               (return-from find-foreign-library path)))))))
72    nil)
73
74
75 (defun load-foreign-library (filename &key module supporting-libraries
76                                            force-load)
77   (declare (ignorable module supporting-libraries))
78
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))
86                                          filename
87                                          ;; lispworks treats as UNC, so use truename
88                                          #+(and lispworks mswindows) (truename filename)
89                                          #-(and lispworks mswindows) filename))))
90
91       (if (and (not force-load)
92                (find filename *loaded-libraries* :test #'string-equal))
93           t ;; return T, but don't reload library
94       (progn
95         #+cmu
96         (let ((type (pathname-type (parse-namestring filename))))
97           (if (string-equal type "so")
98               (unless
99                   (sys::load-object-file filename)
100                 (load-failure))
101               (alien:load-foreign filename
102                                   :libraries
103                                   (convert-supporting-libraries-to-string
104                                    supporting-libraries))))
105         #+scl
106         (alien:load-foreign filename
107                             :libraries
108                             (convert-supporting-libraries-to-string
109                              supporting-libraries))
110         #+sbcl
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)
115                 (error c))))
116
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)
122
123         (push filename *loaded-libraries*)
124         t)))))
125
126 (defun convert-supporting-libraries-to-string (libs)
127   (let (lib-load-list)
128     (dolist (lib libs)
129       (push (format nil "-l~A" lib) lib-load-list))
130     (nreverse lib-load-list)))