r1714: *** empty log message ***
[uffi.git] / src / libraries.cl
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
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.cl,v 1.10 2002/04/01 17:16:15 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 win32 mswindows) "so")
29
30 (defun find-foreign-library (names directories &key type drive-letters)  
31   "Looks for a foreign library. directories can be a single
32 string or a list of strings of candidate directories. Use default
33 library type if type is not specified."
34   (unless type
35     (setq type (default-foreign-library-type)))
36   (unless (listp names)
37     (setq names (list names)))
38   (unless (listp directories)
39     (setq directories (list directories)))
40   #+(or win32 mswindows)
41   (unless (listp drive-letters)
42     (setq drive-letters (list drive-letters)))
43   #-(or win32 mswindows)
44   (setq drive-letters '(nil))
45   (dolist (drive-letter drive-letters)
46     (dolist (name names)
47       (dolist (dir directories)
48         (let ((path (make-pathname 
49                      #+lispworks :host
50                      #+lispworks (when drive-letter drive-letter)
51                      #-lispworks :device
52                      #-lispworks (when drive-letter drive-letter)
53                      :name name 
54                      :type type
55                      :directory 
56                      (etypecase dir
57                        (pathname
58                         (pathname-directory dir))
59                        (list
60                         dir)
61                        (string
62                         (pathname-directory 
63                          (parse-namestring dir)))))))
64           (when (probe-file path)
65             (return-from find-foreign-library path))))))
66    nil)
67
68
69 (defun load-foreign-library (filename &key module supporting-libraries
70                                            force-load)
71   #+allegro (declare (ignore module supporting-libraries))
72   #+lispworks  (declare (ignore supporting-libraries))
73   #+cmu (declare (ignore module))
74   
75   (when (and filename (probe-file filename))
76     (if (pathnamep filename)    ;; ensure filename is a string to check if
77         (setq filename (namestring filename)))  ; already loaded
78
79     (if (and (not force-load)
80              (find filename *loaded-libraries* :test #'string-equal))
81         t ;; return T, but don't reload library
82       (progn
83         #+cmu (alien:load-foreign filename 
84                                   :libraries
85                                   (convert-supporting-libraries-to-string
86                                    supporting-libraries))
87         #+lispworks (fli:register-module module 
88                                          :connection-style :automatic 
89                                          :real-name filename)
90         #+allegro (load filename)
91         
92         (push filename *loaded-libraries*)
93         t)))
94   )
95
96 (defun convert-supporting-libraries-to-string (libs)
97   (let (lib-load-list)
98     (dolist (lib libs)
99       (push (format nil "-l~A" lib) lib-load-list))
100     (nreverse lib-load-list)))