faa5f12c2ef44efab56d6f4f4a7bcd5bb3507352
[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 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002-2005 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; *************************************************************************
15
16 (in-package #:uffi)
17
18 (defvar *loaded-libraries* nil
19   "List of foreign libraries loaded. Used to prevent reloading a library")
20
21 (defun default-foreign-library-type ()
22   "Returns string naming default library type for platform"
23   #+(or win32 mswindows) "dll"
24   #+(or macosx darwin ccl-5.0) "dylib"
25   #-(or win32 mswindows macosx darwin ccl-5.0) "so"
26 )
27
28 (defun foreign-library-types ()
29   "Returns list of string naming possible library types for platform, sorted by preference"
30   #+(or win32 mswindows) '("dll" "lib")
31   #+(or macosx darwin ccl-5.0) '("dylib" "bundle")
32   #-(or win32 mswindows macosx darwin ccl-5.0) '("so" "a" "o")
33 )
34
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."
39   (unless types
40     (setq types (foreign-library-types)))
41   (unless (listp types)
42     (setq types (list types)))
43   (unless (listp names)
44     (setq names (list names)))
45   (unless (listp directories)
46     (setq directories (list directories)))
47   #+(or win32 mswindows)
48   (unless drive-letters
49     (setq drive-letters (list (pathname-device 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)
56     (dolist (name names)
57       (dolist (dir directories)
58         (dolist (type types)
59           (let ((path (make-pathname
60                        #+lispworks :host
61                        #+lispworks (when drive-letter drive-letter)
62                        #-lispworks :device
63                        #-lispworks (when drive-letter drive-letter)
64                        :name name
65                        :type type
66                        :directory
67                        (etypecase dir
68                          (pathname
69                           (pathname-directory dir))
70                          (list
71                           dir)
72                          (string
73                           (pathname-directory
74                            (parse-namestring dir)))))))
75             (when (probe-file path)
76               (return-from find-foreign-library path)))))))
77    nil)
78
79
80 (defun load-foreign-library (filename &key module supporting-libraries
81                                            force-load)
82   #+(or allegro openmcl digitool sbcl) (declare (ignore module supporting-libraries))
83   #+(or cmu scl) (declare (ignore module))
84   #+lispworks (declare (ignore supporting-libraries))
85
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))
90                                        filename
91                                        ;; lispworks treats as UNC, so use truename
92                                        #+(and lispworks win32) (truename filename)
93                                        #-(and lispworks win32) filename))))
94
95     (if (and (not force-load)
96              (find filename *loaded-libraries* :test #'string-equal))
97         t ;; return T, but don't reload library
98       (progn
99         #+cmu
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
104                                   :libraries
105                                   (convert-supporting-libraries-to-string
106                                    supporting-libraries))))
107         #+scl
108         (let ((type (pathname-type (parse-namestring filename))))
109           (alien:load-foreign filename
110                               :libraries
111                               (convert-supporting-libraries-to-string
112                                supporting-libraries)))
113         #+sbcl
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)
118                 (error c))))
119
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         #+digitool (ccl:add-to-shared-library-search-path filename t)
125
126         (push filename *loaded-libraries*)
127         t))))
128
129 (defun convert-supporting-libraries-to-string (libs)
130   (let (lib-load-list)
131     (dolist (lib libs)
132       (push (format nil "-l~A" lib) lib-load-list))
133     (nreverse lib-load-list)))