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