084f66ae160fded631dbe9e4e33d623844c3d511
[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   "Attempt to load a foreign library. This will search for any of the filenames, as
23 well as any of the filenames in any of the clsql:*foreign-library-search-paths*"
24   (setq filenames (if (listp filenames) filenames (list filenames)))
25
26   (flet ((try-load (testpath)
27            (handler-case
28                (uffi:load-foreign-library testpath
29                                           :module module
30                                           :supporting-libraries supporting-libraries)
31              (error (c) (warn "~A" c) nil))))
32     (or
33      (loop for type in (uffi:foreign-library-types)
34            thereis
35            (loop for name in filenames
36                  for pn = (make-pathname :name name :type type)
37                  thereis (or
38                           (loop for search-path in clsql:*foreign-library-search-paths*
39                                 thereis (try-load (merge-pathnames pn search-path)))
40                           (try-load pn))))
41      (when errorp
42        (error "Couldn't load foreign librar~@P ~{~S~^, ~}. (searched ~S)"
43               (length filenames) filenames
44               'clsql:*foreign-library-search-paths*)))))
45
46 (defvar *clsql-uffi-library-filenames*
47   (list #+(or 64bit x86-64) "clsql_uffi64"
48         "clsql_uffi"))
49
50 (defvar *clsql-uffi-supporting-libraries* '("c")
51   "Used only by CMU. List of library flags needed to be passed to ld to
52 load the MySQL client library succesfully.  If this differs at your site,
53 set to the right path before compiling or loading the system.")
54
55 (defvar *uffi-library-loaded* nil
56   "T if foreign library was able to be loaded successfully")
57
58 (defun load-uffi-foreign-library ()
59   (clsql:push-library-path clsql-uffi-system::*clsql-uffi-library-dir*)
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)