r5062: return from san diego
[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: libraries.lisp,v 1.7 2003/06/06 21:59:18 kevin Exp $
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
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 ;;;; *************************************************************************
18
19 (in-package #:uffi)
20
21 (defvar *loaded-libraries* nil
22   "List of foreign libraries loaded. Used to prevent reloading a library")
23
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"
29 )
30
31 (defun find-foreign-library (names directories &key types drive-letters)  
32   "Looks for a foreign library. directories can be a single
33 string or a list of strings of candidate directories. Use default
34 library type if type is not specified."
35   (unless types
36     (setq types (default-foreign-library-type)))
37   (unless (listp types)
38     (setq types (list types)))
39   (unless (listp names)
40     (setq names (list names)))
41   (unless (listp directories)
42     (setq directories (list directories)))
43   #+(or win32 mswindows)
44   (unless (listp drive-letters)
45     (setq drive-letters (list drive-letters)))
46   #-(or win32 mswindows)
47   (setq drive-letters '(nil))
48   (dolist (drive-letter drive-letters)
49     (dolist (name names)
50       (dolist (dir directories)
51         (dolist (type types)
52           (let ((path (make-pathname 
53                        #+lispworks :host
54                        #+lispworks (when drive-letter drive-letter)
55                        #-lispworks :device
56                        #-lispworks (when drive-letter drive-letter)
57                        :name name 
58                        :type type
59                        :directory 
60                        (etypecase dir
61                          (pathname
62                           (pathname-directory dir))
63                          (list
64                           dir)
65                          (string
66                           (pathname-directory 
67                            (parse-namestring dir)))))))
68             (when (probe-file path)
69               (return-from find-foreign-library path)))))))
70    nil)
71
72
73 (defun load-foreign-library (filename &key module supporting-libraries
74                                            force-load)
75   #+(or allegro lispworks mcl) (declare (ignore module supporting-libraries))
76   #+(or cmu scl sbcl) (declare (ignore module))
77   
78   (when (and filename (probe-file filename))
79     (if (pathnamep filename)    ;; ensure filename is a string to check if
80         (setq filename (namestring filename)))  ; already loaded
81
82     (if (and (not force-load)
83              (find filename *loaded-libraries* :test #'string-equal))
84         t ;; return T, but don't reload library
85       (progn
86         #+cmu
87         (let ((type (pathname-type (parse-namestring filename))))
88           (if (equal type "so")
89               (sys::load-object-file filename)
90             (alien:load-foreign filename 
91                                 :libraries
92                                 (convert-supporting-libraries-to-string
93                                  supporting-libraries))))
94         #+scl
95         (let ((type (pathname-type (parse-namestring filename))))
96           (alien:load-foreign filename 
97                               :libraries
98                               (convert-supporting-libraries-to-string
99                                supporting-libraries)))
100         #+sbcl
101         (sb-alien:load-foreign filename 
102                                :libraries
103                                  (convert-supporting-libraries-to-string
104                                   supporting-libraries))
105         #+lispworks (fli:register-module module :real-name filename)
106         #+allegro (load filename)
107         #+openmcl (ccl:open-shared-library filename)
108         #+(and mcl (not openmcl)) (ccl:add-to-shared-library-search-path filename t)
109         
110         (push filename *loaded-libraries*)
111         t))))
112
113 (defun convert-supporting-libraries-to-string (libs)
114   (let (lib-load-list)
115     (dolist (lib libs)
116       (push (format nil "-l~A" lib) lib-load-list))
117     (nreverse lib-load-list)))