r1712: *** 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.9 2002/04/01 05:12:02 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   #+freebsd "a"
29   #+linux "so")
30
31 (defun find-foreign-library (names directories &key type 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 type
36     (setq type (default-foreign-library-type)))
37   (unless (listp names)
38     (setq names (list names)))
39   (unless (listp directories)
40     (setq directories (list directories)))
41   #+(or win32 mswindows)
42   (unless (listp drive-letters)
43     (setq drive-letters (list drive-letters)))
44   #-(or win32 mswindows)
45   (setq drive-letters '(nil))
46   (dolist (drive-letter drive-letters)
47     (dolist (name names)
48       (dolist (dir directories)
49         (let ((path (make-pathname 
50                      #+lispworks :host
51                      #+lispworks (when drive-letter drive-letter)
52                      #-lispworks :device
53                      #-lispworks (when drive-letter drive-letter)
54                      :name name 
55                      :type type
56                      :directory 
57                      (etypecase dir
58                        (pathname
59                         (pathname-directory dir))
60                        (list
61                         dir)
62                        (string
63                         (pathname-directory 
64                          (parse-namestring dir)))))))
65           (when (probe-file path)
66             (return-from find-foreign-library path))))))
67    nil)
68
69
70 (defun load-foreign-library (filename &key module supporting-libraries
71                                            force-load)
72   #+allegro (declare (ignore module supporting-libraries))
73   #+lispworks  (declare (ignore supporting-libraries))
74   #+cmu (declare (ignore module))
75   
76   (when (and filename (probe-file filename))
77     (if (pathnamep filename)    ;; ensure filename is a string to check if
78         (setq filename (namestring filename)))  ; already loaded
79
80     (if (and (not force-load)
81              (find filename *loaded-libraries* :test #'string-equal))
82         t ;; return T, but don't reload library
83       (progn
84         #+cmu (alien:load-foreign filename 
85                                   :libraries
86                                   (convert-supporting-libraries-to-string
87                                    supporting-libraries))
88         #+lispworks (fli:register-module module 
89                                          :connection-style :automatic 
90                                          :real-name filename)
91         #+allegro (load filename)
92         
93         (push filename *loaded-libraries*)
94         t)))
95   )
96
97 (defun convert-supporting-libraries-to-string (libs)
98   (let (lib-load-list)
99     (dolist (lib libs)
100       (push (format nil "-l~A" lib) lib-load-list))
101     (nreverse lib-load-list)))