Groveler ASDF integration: import new build system from CFFI
[iolib.git] / src / grovel / asdf.lisp
blobbfe106681aeefc7076057aaa48730ff908176c4b
1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; asdf.lisp --- ASDF components for iolib-grovel.
4 ;;;
5 ;;; Copyright (C) 2005-2006, Dan Knap <dankna@accela.net>
6 ;;; Copyright (C) 2005-2006, Emily Backes <lucca@accela.net>
7 ;;; Copyright (C) 2007, Stelian Ionescu <sionescu@cddr.org>
8 ;;; Copyright (C) 2007, Luis Oliveira <loliveira@common-lisp.net>
9 ;;;
10 ;;; Permission is hereby granted, free of charge, to any person
11 ;;; obtaining a copy of this software and associated documentation
12 ;;; files (the "Software"), to deal in the Software without
13 ;;; restriction, including without limitation the rights to use, copy,
14 ;;; modify, merge, publish, distribute, sublicense, and/or sell copies
15 ;;; of the Software, and to permit persons to whom the Software is
16 ;;; furnished to do so, subject to the following conditions:
17 ;;;
18 ;;; The above copyright notice and this permission notice shall be
19 ;;; included in all copies or substantial portions of the Software.
20 ;;;
21 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 ;;; DEALINGS IN THE SOFTWARE.
29 ;;;
31 (in-package :iolib-grovel)
33 (defun ensure-pathname (thing)
34 (if (typep thing 'logical-pathname)
35 (translate-logical-pathname thing)
36 (pathname thing)))
38 (defclass cc-flags-mixin ()
39 ((cc-flags :initform nil :accessor cc-flags-of :initarg :cc-flags)))
41 (defmethod asdf:perform :around ((op asdf:compile-op) (file cc-flags-mixin))
42 (declare (ignore op))
43 (let ((*cc-flags* (append (ensure-list (cc-flags-of file))
44 *cc-flags*)))
45 (call-next-method)))
47 (eval-when (:compile-toplevel :load-toplevel :execute)
48 (defclass process-op (asdf:operation)
50 (:documentation "This ASDF operation performs the steps necessary
51 to generate a compilable and loadable lisp file from a
52 PROCESS-OP-INPUT component."))
54 (defclass process-op-input (asdf:cl-source-file)
55 ((generated-lisp-file-type
56 :initarg :generated-lisp-file-type
57 :accessor generated-lisp-file-type
58 :documentation "The :TYPE argument to use for the generated lisp file."))
59 (:default-initargs
60 :generated-lisp-file-type "generated-lisp-file")
61 (:documentation "This ASDF component represents a file that is
62 used as input to a function that generates lisp source file. This
63 component acts as if it is a CL-SOURCE-FILE by applying the
64 COMPILE-OP and LOAD-SOURCE-OP operations to the file generated by
65 PROCESS-OP.")))
67 (defmethod asdf:output-files ((op process-op) (c process-op-input))
68 (declare (ignore op))
69 (list (make-pathname :type (generated-lisp-file-type c)
70 :defaults (asdf:component-pathname c))))
72 (defmethod asdf:component-depends-on ((op process-op) (c process-op-input))
73 (append (call-next-method)
74 (list (cons 'asdf:load-op (asdf::component-load-dependencies c)))))
76 (defmethod asdf:component-depends-on ((op asdf:compile-op) (c process-op-input))
77 (declare (ignore op))
78 (append (call-next-method)
79 (list (list 'process-op (asdf:component-name c)))))
81 (defmethod asdf:component-depends-on ((op asdf:load-source-op) (c process-op-input))
82 (declare (ignore op))
83 (append (call-next-method)
84 (list (list 'process-op (asdf:component-name c)))))
86 (defmethod asdf:perform ((op asdf:compile-op) (c process-op-input))
87 (let ((generated-lisp-file (asdf:output-file (make-instance 'process-op) c)))
88 (asdf:perform op (make-instance 'asdf:cl-source-file
89 :name (asdf:component-name c)
90 :parent (asdf:component-parent c)
91 :pathname generated-lisp-file))))
93 (defmethod asdf:perform ((op asdf:load-source-op) (c process-op-input))
94 (let ((generated-lisp-file (asdf:output-file (make-instance 'process-op) c)))
95 (asdf:perform op (make-instance 'asdf:cl-source-file
96 :name (asdf:component-name c)
97 :parent (asdf:component-parent c)
98 :pathname generated-lisp-file))))
100 ;;;# ASDF component: GROVEL-FILE
102 (eval-when (:compile-toplevel :load-toplevel :execute)
103 (defclass asdf::iolib-grovel-file (process-op-input cc-flags-mixin)
105 (:default-initargs
106 :generated-lisp-file-type "processed-iolib-grovel-file")
107 (:documentation
108 "This ASDF component represents an input file that is processed
109 by PROCESS-GROVEL-FILE.")))
111 (defmethod asdf:perform ((op process-op) (c asdf::iolib-grovel-file))
112 (let ((output-file (asdf:output-file op c))
113 (input-file (asdf:component-pathname c)))
114 (ensure-directories-exist (directory-namestring output-file))
115 (let ((tmp-file (process-grovel-file input-file (ensure-pathname output-file))))
116 (unwind-protect
117 (alexandria:copy-file tmp-file output-file :if-to-exists :supersede)
118 (delete-file tmp-file)))))
120 ;;;# ASDF component: WRAPPER-FILE
122 (eval-when (:compile-toplevel :load-toplevel :execute)
123 (defclass asdf::iolib-wrapper-file (process-op-input cc-flags-mixin)
124 ((soname :initform nil :initarg :soname :accessor soname-of))
125 (:default-initargs
126 :generated-lisp-file-type "processed-iolib-wrapper-file")
127 (:documentation
128 "This ASDF component represents an input file that is processed
129 by PROCESS-WRAPPER-FILE. This generates a foreign library and
130 matching CFFI bindings that are subsequently compiled and
131 loaded.")))
133 (defmethod asdf:perform ((op process-op) (c asdf::iolib-wrapper-file))
134 (let ((output-file (asdf:output-file op c))
135 (input-file (asdf:component-pathname c)))
136 (ensure-directories-exist (directory-namestring output-file))
137 (let ((tmp-file (process-wrapper-file input-file output-file (or (soname-of c)
138 (asdf:component-name c)))))
139 (unwind-protect
140 (alexandria:copy-file tmp-file output-file :if-to-exists :supersede)
141 (delete-file tmp-file)))))