d7075c13cb0bfdab714dab82bd8e074386aca844
[uffi.git] / src-mcl / 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 ;;;; Programmers:   Kevin M. Rosenberg and John DeSoi
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id: libraries.cl,v 1.1 2002/09/16 17:57:43 kevin Exp $
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;; and John DeSoi
14 ;;;;
15 ;;;; UFFI users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
17 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
18 ;;;; *************************************************************************
19
20 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
21 (in-package :uffi)
22
23 (defvar *loaded-libraries* nil
24   "List of foreign libraries loaded. Used to prevent reloading a library")
25
26 ;in MCL calling this more than once for the same library does not do anything
27 (defmacro load-foreign-library (filename &key module supporting-libraries)
28   (declare (ignore module supporting-libraries))
29   `(eval-when (:compile-toplevel :load-toplevel :execute)
30      (when (ccl:add-to-shared-library-search-path ,filename t) 
31        (pushnew ,filename *loaded-libraries*))))
32
33 ;; Copied directly from main source without MCL specializations
34 (defun find-foreign-library (names directories &key types drive-letters)  
35   "Looks for a foreign library. directories can be a single
36 string or a list of strings of candidate directories. Use default
37 library type if type is not specified."
38   (unless types
39     (setq types (default-foreign-library-type)))
40   (unless (listp types)
41     (setq types (list types)))
42   (unless (listp names)
43     (setq names (list names)))
44   (unless (listp directories)
45     (setq directories (list directories)))
46   #+(or win32 mswindows)
47   (unless (listp drive-letters)
48     (setq drive-letters (list drive-letters)))
49   #-(or win32 mswindows)
50   (setq drive-letters '(nil))
51   (dolist (drive-letter drive-letters)
52     (dolist (name names)
53       (dolist (dir directories)
54         (dolist (type types)
55           (let ((path (make-pathname 
56                        #+lispworks :host
57                        #+lispworks (when drive-letter drive-letter)
58                        #-lispworks :device
59                        #-lispworks (when drive-letter drive-letter)
60                        :name name 
61                        :type type
62                        :directory 
63                        (etypecase dir
64                          (pathname
65                           (pathname-directory dir))
66                          (list
67                           dir)
68                          (string
69                           (pathname-directory 
70                            (parse-namestring dir)))))))
71             (when (probe-file path)
72               (return-from find-foreign-library path)))))))
73    nil)
74
75
76 ;; Copied directly from main source without MCL specializations
77 (defun default-foreign-library-type ()
78   "Returns string naming default library type for platform"
79   #+(or win32 mswindows) "dll"
80   #-(or win32 mswindows) "so")