Remove old CVS $Id$ keyword
[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   #+(or allegro openmcl digitool sbcl) (declare (ignore module supporting-libraries))
78   #+(or cmu scl) (declare (ignore module))
79   #+lispworks (declare (ignore supporting-libraries))
80
81   (flet ((load-failure ()
82            (error "Unable to load foreign library \"~A\"." filename)))
83     (when (and filename (or (null (pathname-directory filename))
84                             (probe-file filename)))
85       (if (pathnamep filename)    ;; ensure filename is a string to check if already loaded
86           (setq filename (namestring (if (null (pathname-directory filename))
87                                          filename
88                                          ;; lispworks treats as UNC, so use truename
89                                          #+(and lispworks mswindows) (truename filename)
90                                          #-(and lispworks mswindows) filename))))
91
92       (if (and (not force-load)
93                (find filename *loaded-libraries* :test #'string-equal))
94           t ;; return T, but don't reload library
95       (progn
96         #+cmu
97         (let ((type (pathname-type (parse-namestring filename))))
98           (if (string-equal type "so")
99               (unless
100                   (sys::load-object-file filename)
101                 (load-failure))
102               (alien:load-foreign filename
103                                   :libraries
104                                   (convert-supporting-libraries-to-string
105                                    supporting-libraries))))
106         #+scl
107         (let ((type (pathname-type (parse-namestring filename))))
108           (alien:load-foreign filename
109                               :libraries
110                               (convert-supporting-libraries-to-string
111                                supporting-libraries)))
112         #+sbcl
113         (handler-case (sb-alien::load-1-foreign filename)
114           (sb-int:unsupported-operator (c)
115             (if (fboundp (intern "LOAD-SHARED-OBJECT" :sb-alien))
116                 (funcall (intern "LOAD-SHARED-OBJECT" :sb-alien) filename)
117                 (error c))))
118
119         #+lispworks (fli:register-module module :real-name filename
120                                          :connection-style :immediate)
121         #+allegro (load filename)
122         #+openmcl (ccl:open-shared-library filename)
123         #+digitool (ccl:add-to-shared-library-search-path filename t)
124
125         (push filename *loaded-libraries*)
126         t)))))
127
128 (defun convert-supporting-libraries-to-string (libs)
129   (let (lib-load-list)
130     (dolist (lib libs)
131       (push (format nil "-l~A" lib) lib-load-list))
132     (nreverse lib-load-list)))