r10811: fix warning for sbcl. remove setf getenv
[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 (listp drive-letters)
49     (setq drive-letters (list drive-letters)))
50   #-(or win32 mswindows)
51   (setq drive-letters '(nil))
52   (dolist (drive-letter drive-letters)
53     (dolist (name names)
54       (dolist (dir directories)
55         (dolist (type types)
56           (let ((path (make-pathname 
57                        #+lispworks :host
58                        #+lispworks (when drive-letter drive-letter)
59                        #-lispworks :device
60                        #-lispworks (when drive-letter drive-letter)
61                        :name name 
62                        :type type
63                        :directory 
64                        (etypecase dir
65                          (pathname
66                           (pathname-directory dir))
67                          (list
68                           dir)
69                          (string
70                           (pathname-directory 
71                            (parse-namestring dir)))))))
72             (when (probe-file path)
73               (return-from find-foreign-library path)))))))
74    nil)
75
76
77 (defun load-foreign-library (filename &key module supporting-libraries
78                                            force-load)
79   #+(or allegro mcl sbcl) (declare (ignore module supporting-libraries))
80   #+(or cmu scl) (declare (ignore module))
81   #+lispworks (declare (ignore supporting-libraries))
82
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 win32) (truename filename)
90                                        #-(and lispworks win32) 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               (sys::load-object-file filename)
100               (alien:load-foreign filename 
101                                   :libraries
102                                   (convert-supporting-libraries-to-string
103                                    supporting-libraries))))
104         #+scl
105         (let ((type (pathname-type (parse-namestring filename))))
106           (alien:load-foreign filename 
107                               :libraries
108                               (convert-supporting-libraries-to-string
109                                supporting-libraries)))
110         #+sbcl
111         (handler-case (sb-alien::load-1-foreign filename)
112           (sb-int:unsupported-operator (c)
113             (if (fboundp (intern "LOAD-SHARED-OBJECT" :sb-alien))
114                 (funcall (intern "LOAD-SHARED-OBJECT" :sb-alien) filename)
115                 (error c))))
116
117         #+lispworks (fli:register-module module :real-name filename
118                                                 :connection-style :immediate)
119         #+allegro (load filename)
120         #+openmcl (ccl:open-shared-library filename)
121         #+(and mcl (not openmcl)) (ccl:add-to-shared-library-search-path filename t)
122         
123         (push filename *loaded-libraries*)
124         t))))
125
126 (defun convert-supporting-libraries-to-string (libs)
127   (let (lib-load-list)
128     (dolist (lib libs)
129       (push (format nil "-l~A" lib) lib-load-list))
130     (nreverse lib-load-list)))