r10612: 05 Jul 2005 Kevin Rosenberg <kevin@rosenberg.net>
[clsql.git] / uffi / clsql-uffi-loader.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:     clsql-uffi-loader.sql
6 ;;;; Purpose:  Library loader using CLSQL UFFI helper library
7 ;;;; Author:   Kevin M. Rosenberg
8 ;;;; Created:  Mar 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2004 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; CLSQL 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 (in-package #:clsql-uffi)
20
21 (defun find-and-load-foreign-library (filenames &key module supporting-libraries (errorp t))
22   (setq filenames (if (listp filenames) filenames (list filenames))
23         filenames
24           (append
25            (loop for search-path in clsql:*foreign-library-search-paths*
26                  nconc (loop for filename in filenames
27                              collect (merge-pathnames filename search-path)))
28            filenames))
29   (or (loop for type in (uffi:foreign-library-types)
30             for suffix = (make-pathname :type type)
31             thereis (loop for filename in filenames
32                           thereis (handler-case
33                                     (uffi:load-foreign-library (merge-pathnames filename suffix)
34                                                                :module module
35                                                                :supporting-libraries supporting-libraries)
36                                     (error (c)
37                                       (warn "~A" c)
38                                       nil))))
39       (when errorp
40         (error "Couldn't load foreign librar~@P ~{~S~^, ~}."
41                (length filenames) filenames))))
42
43 (defvar *clsql-uffi-library-filenames*
44     (list #+(or 64bit x86-64) "clsql_uffi64"
45           #+(or 64bit x86-64) (make-pathname :name "clsql_uffi64"
46                                              :directory clsql-uffi-system::*library-file-dir*)
47           "clsql_uffi"
48           (make-pathname :name "clsql_uffi"
49                          :directory clsql-uffi-system::*library-file-dir*)))
50
51 (defvar *clsql-uffi-supporting-libraries* '("c")
52   "Used only by CMU. List of library flags needed to be passed to ld to
53 load the MySQL client library succesfully.  If this differs at your site,
54 set to the right path before compiling or loading the system.")
55
56 (defvar *uffi-library-loaded* nil
57   "T if foreign library was able to be loaded successfully")
58
59 (defun load-uffi-foreign-library ()
60   (find-and-load-foreign-library *clsql-uffi-library-filenames*
61                                  :module "clsql-uffi"
62                                  :supporting-libraries
63                                  *clsql-uffi-supporting-libraries*)
64   (setq *uffi-library-loaded* t))
65
66 (load-uffi-foreign-library)
67