r5565: *** empty log message ***
[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.13 2003/08/27 20:02:59 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 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")
36 )
37
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."
42   (unless types
43     (setq types (foreign-library-types)))
44   (unless (listp types)
45     (setq types (list types)))
46   (unless (listp names)
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)
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 mcl) (declare (ignore module supporting-libraries))
83   #+(or cmu scl sbcl) (declare (ignore module))
84   
85   (when (and filename (probe-file filename))
86     (if (pathnamep filename)    ;; ensure filename is a string to check if
87         (setq filename (namestring filename)))  ; already loaded
88
89     (if (and (not force-load)
90              (find filename *loaded-libraries* :test #'string-equal))
91         t ;; return T, but don't reload library
92       (progn
93         #+cmu
94         (let ((type (pathname-type (parse-namestring filename))))
95           (if (string-equal type "so")
96               (sys::load-object-file filename)
97               (alien:load-foreign filename 
98                                   :libraries
99                                   (convert-supporting-libraries-to-string
100                                    supporting-libraries))))
101         #+scl
102         (let ((type (pathname-type (parse-namestring filename))))
103           (alien:load-foreign filename 
104                               :libraries
105                               (convert-supporting-libraries-to-string
106                                supporting-libraries)))
107         #+sbcl
108         (let ((type (pathname-type (parse-namestring filename))))
109           (if (or (string-equal type "so")
110                   (string-equal type "bundle")
111                   (string-equal type "dylib"))
112               (sb-alien::load-1-foreign filename)
113               (sb-alien:load-foreign filename 
114                                      :libraries
115                                      (convert-supporting-libraries-to-string
116                                       supporting-libraries))))
117         #+lispworks (fli:register-module module :real-name filename)
118         #+allegro (load filename)
119         #+openmcl (ccl:open-shared-library filename)
120         #+(and mcl (not openmcl)) (ccl:add-to-shared-library-search-path filename t)
121         
122         (push filename *loaded-libraries*)
123         t))))
124
125 (defun convert-supporting-libraries-to-string (libs)
126   (let (lib-load-list)
127     (dolist (lib libs)
128       (push (format nil "-l~A" lib) lib-load-list))
129     (nreverse lib-load-list)))