r3428: openmcl fixes
[uffi.git] / src / libraries.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          libraries.cl
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.5 2002/11/18 04:53:32 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 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :uffi)
21
22 (defvar *loaded-libraries* nil
23   "List of foreign libraries loaded. Used to prevent reloading a library")
24
25 (defun default-foreign-library-type ()
26   "Returns string naming default library type for platform"
27   #+(or win32 mswindows) "dll"
28   #+(or macosx darwin) "dylib"
29   #-(or win32 mswindows macosx darwin) "so"
30 )
31
32 (defun find-foreign-library (names directories &key types drive-letters)  
33   "Looks for a foreign library. directories can be a single
34 string or a list of strings of candidate directories. Use default
35 library type if type is not specified."
36   (unless types
37     (setq types (default-foreign-library-type)))
38   (unless (listp types)
39     (setq types (list types)))
40   (unless (listp names)
41     (setq names (list names)))
42   (unless (listp directories)
43     (setq directories (list directories)))
44   #+(or win32 mswindows)
45   (unless (listp drive-letters)
46     (setq drive-letters (list drive-letters)))
47   #-(or win32 mswindows)
48   (setq drive-letters '(nil))
49   (dolist (drive-letter drive-letters)
50     (dolist (name names)
51       (dolist (dir directories)
52         (dolist (type types)
53           (let ((path (make-pathname 
54                        #+lispworks :host
55                        #+lispworks (when drive-letter drive-letter)
56                        #-lispworks :device
57                        #-lispworks (when drive-letter drive-letter)
58                        :name name 
59                        :type type
60                        :directory 
61                        (etypecase dir
62                          (pathname
63                           (pathname-directory dir))
64                          (list
65                           dir)
66                          (string
67                           (pathname-directory 
68                            (parse-namestring dir)))))))
69             (when (probe-file path)
70               (return-from find-foreign-library path)))))))
71    nil)
72
73
74 (defun load-foreign-library (filename &key module supporting-libraries
75                                            force-load)
76   #+(or allegro lispworks openmcl) (declare (ignore module supporting-libraries))
77   #+(or cmu scl sbcl) (declare (ignore module))
78   
79   (when (and filename (probe-file filename))
80     (if (pathnamep filename)    ;; ensure filename is a string to check if
81         (setq filename (namestring filename)))  ; already loaded
82
83     (if (and (not force-load)
84              (find filename *loaded-libraries* :test #'string-equal))
85         t ;; return T, but don't reload library
86       (progn
87         #+cmu
88         (let ((type (pathname-type (parse-namestring filename))))
89           (if (equal type "so")
90               (sys::load-object-file filename)
91             (alien:load-foreign filename 
92                                 :libraries
93                                 (convert-supporting-libraries-to-string
94                                  supporting-libraries))))
95         #+scl
96         (let ((type (pathname-type (parse-namestring filename))))
97           (alien:load-foreign filename 
98                               :libraries
99                               (convert-supporting-libraries-to-string
100                                supporting-libraries)))
101         #+sbcl
102         (sb-alien:load-foreign filename 
103                                :libraries
104                                  (convert-supporting-libraries-to-string
105                                   supporting-libraries))
106         #+lispworks (fli:register-module module :real-name filename)
107         #+allegro (load filename)
108         #+openmcl (ccl:open-shared-library filename)
109         #+(and mcl (not openmcl)) (ccl:add-to-shared-library-search-path filename t)
110         
111         (push filename *loaded-libraries*)
112         t))))
113
114 (defun convert-supporting-libraries-to-string (libs)
115   (let (lib-load-list)
116     (dolist (lib libs)
117       (push (format nil "-l~A" lib) lib-load-list))
118     (nreverse lib-load-list)))