[elephant-devel] BDB Constants generation

Glenn Tarcea gtarcea at umich.edu
Fri Jun 6 11:18:06 UTC 2008


I <think> this will work to remove the dependency on hand generating  
the berkeley-constants.lisp file constants (but not the structures).  
As long as the structures don't change between BDB releases, it  
should remove the explicit dependency on a version of BDB.

=======================
asdf file:

(defvar *gcc* "gcc")

(defclass grovel-file (cl-source-file) ())

(defmethod perform ((o compile-op) (c grovel-file))
   (let* ((output-file (car (output-files o c)))
        (filename (component-pathname c))
        (c-source (merge-pathnames "tmp.c" output-file))
        (a-dot-out (merge-pathnames "a.out" output-file))
        (constants (merge-pathnames "grovel.lisp-temp" output-file))
        (*grovel*))
      (declare (special *grovel*))
      (load filename)
      (and (funcall (the function *grovel*) c-source a-dot-out  
constants)
          (compile-file constants :output-file output-file))))

(defsystem xxxx
    ...
    :components ((:file "defpackage"
                        (:grovel-file "grovel-constants"
                        ....))
  :depends-on (:uffi ....))

=======================

grovel-constants.lisp:

(defun write-groveler (file constants)
     (with-open-file (f file :direction :output :if-exists :supersede)
        (let ((*print-case* :upcase))
          (format f "
#include <...>

void defconstant(char *lisp_name, long unix_number)
{
     printf(\"(defconstant %s %ld); #x%lx\\n\", lisp_name, unix_number);
}

int main()
{
    printf(\"(in-package : THE PACKAGE WE ARE DEFINING)\\n\") ;")
       (dolist (c constants)
          (format f "~&    defconstant(\"~A\", ~A);~%" (car c) (cdr c)))
       (format f "
     return 0 ;
}"))))


(unless (boundp *grovel*)
    (error "No GROVEL hook!"))

(defvar *grovel*)

(setf *grovel*
     (lambda (c obj lisp)
         (write-groveler c
              '(;; BDB Constants
                  (db_create . DB_CREATE)
                ...)))
            (and (zerop (run-shell-command "~A ~A -o ~A"
                          *gcc*
                          (namestring c)
                          (namestring obj)))
                    (zerop (run-shell-command "~A > ~A"
                              (namestring obj)
                              (namestring lisp))))))

======================

This will create a c file and corresponding binary for generating the  
lisp defining the constants. I am using this to generate all the  
constant declarations I need for System V IPC access (eg, IPC_STAT,  
IPC_RMID, etc...). I got much of the structure for this from OSICAT  
with some cleanup and changes on my part.

Glenn







More information about the elephant-devel mailing list