r3090: fix load path
[clsql.git] / db-mysql / mysql-loader.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          mysql-loader.sql
6 ;;;; Purpose:       MySQL library loader using UFFI
7 ;;;; Programmers:   Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id: mysql-loader.lisp,v 1.4 2002/10/18 01:16:26 kevin Exp $
11 ;;;;
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002 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 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :mysql)
21
22 ;;;; Modified by Kevin Rosenberg 
23 ;;;;  - probe potential directories to find library
24 ;;;;  - Changed from CMUCL functions to UFFI to
25 ;;;;      -- prevent library from being loaded multiple times
26 ;;;;      -- support Allegro CL and Lispworks
27
28 (defvar *clsql-mysql-library-filename* 
29   (uffi:find-foreign-library
30    "clsql-mysql"
31    `(,(make-pathname :directory (pathname-directory *load-truename*))
32      "/usr/lib/clsql/"
33      "/home/kevin/debian/src/clsql/db-mysql/")
34    :drive-letters '("C" "D" "E" "F" "G")))
35   
36 (defvar *mysql-library-filename*
37     (cond
38      ((probe-file "/opt/mysql/lib/mysql/libmysqlclient.so")
39       "/opt/mysql/lib/mysql/libmysqlclient.so")
40      ((probe-file "/usr/local/lib/libmysqlclient.so")
41       "/usr/local/lib/libmysqlclient.so")
42      ((probe-file "/usr/local/lib/mysql/libmysqlclient.so")
43       "/usr/local/lib/mysql/libmysqlclient.so")
44      ((probe-file "/usr/lib/libmysqlclient.so")
45       "/usr/lib/libmysqlclient.so")
46      ((probe-file "/usr/lib/mysql/libmysqlclient.so")
47       "/usr/lib/mysql/libmysqlclient.so")
48      #+(or win32 mswindows) 
49      ((probe-file "c:/mysql/lib/opt/libmysql.dll")
50       "c:/mysql/lib/opt/libmysql.dll")
51      (t
52       (error "Can't find MySQL client library to load.")))
53   "Location where the MySQL client library is to be found.")
54
55 (defvar *mysql-library-candidate-names*
56     '("libmysqlclient" "libmysql"))
57
58 (defvar *mysql-library-candidate-directories*
59     '("/opt/mysql/lib/mysql/" "/usr/local/lib/" "/usr/lib/" "/usr/local/lib/mysql/" "/usr/lib/mysql/" "/mysql/lib/opt/"))
60
61 (defvar *mysql-library-candidate-drive-letters* '("C" "D" "E"))
62
63 (defvar *mysql-supporting-libraries* '("c")
64   "Used only by CMU. List of library flags needed to be passed to ld to
65 load the MySQL client library succesfully.  If this differs at your site,
66 set to the right path before compiling or loading the system.")
67
68 (defvar *mysql-library-loaded* nil
69   "T if foreign library was able to be loaded successfully")
70
71 (defmethod clsql-base-sys:database-type-library-loaded ((database-type (eql :mysql)))
72   *mysql-library-loaded*)
73                                       
74 (defmethod clsql-base-sys:database-type-load-foreign ((database-type (eql :mysql)))
75   (let ((mysql-path
76          (uffi:find-foreign-library *mysql-library-candidate-names*
77                                     *mysql-library-candidate-directories*
78                                     :drive-letters
79                                     *mysql-library-candidate-drive-letters*))
80         (zlib-path
81          (uffi:find-foreign-library '("libz" "zlib")
82                                     '("/usr/lib/" "/usr/local/lib/" "/lib/"))))
83     (unless (probe-file mysql-path)
84       (error "Can't find mysql client library to load"))
85     (unless (probe-file zlib-path)
86       (error "Can't find zlib client library to load"))
87     
88     (uffi:load-foreign-library zlib-path) 
89     (if (and
90          (uffi:load-foreign-library mysql-path
91                                     :module "mysql" 
92                                     :supporting-libraries 
93                                     *mysql-supporting-libraries*)
94          (uffi:load-foreign-library *clsql-mysql-library-filename* 
95                                     :module "clsql-mysql" 
96                                     :supporting-libraries 
97                                     (append *mysql-supporting-libraries*)))
98         (setq *mysql-library-loaded* t)
99       (error "Unable to load MySQL client library ~A or CLSQL-MySQL library ~A"
100             mysql-path *clsql-mysql-library-filename*))))
101
102
103 (clsql-base-sys:database-type-load-foreign :mysql)
104