Make IOLIB/GROVEL:PROCESS-OP an ASDF:DOWNWARD-OPERATION
[iolib.git] / src / grovel / asdf.lisp
blob27bc325d0e0e4105bc475fe60b73bf25443d39c3
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 (ignorable 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:downward-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 (ignorable 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 `(#-asdf3 (asdf:load-op ,@(asdf::component-load-dependencies c))
74 #+asdf3 (asdf:prepare-op ,c)
75 ,@(call-next-method)))
77 (defmethod asdf:component-depends-on ((op asdf:compile-op) (c process-op-input))
78 (declare (ignorable op))
79 `((process-op ,(asdf:component-name c))
80 ,@(call-next-method)))
82 (defmethod asdf:component-depends-on ((op asdf:load-source-op) (c process-op-input))
83 (declare (ignorable op))
84 `((process-op ,(asdf:component-name c))
85 ,@(call-next-method)))
87 (defmethod asdf:perform ((op asdf:compile-op) (c process-op-input))
88 (let ((generated-lisp-file (asdf:output-file (make-instance 'process-op) c)))
89 (asdf:perform op (make-instance 'asdf:cl-source-file
90 :name (asdf:component-name c)
91 :parent (asdf:component-parent c)
92 :pathname generated-lisp-file))))
94 (defmethod asdf:perform ((op asdf:load-source-op) (c process-op-input))
95 (let ((generated-lisp-file (asdf:output-file (make-instance 'process-op) c)))
96 (asdf:perform op (make-instance 'asdf:cl-source-file
97 :name (asdf:component-name c)
98 :parent (asdf:component-parent c)
99 :pathname generated-lisp-file))))
101 ;;;# ASDF component: GROVEL-FILE
103 (eval-when (:compile-toplevel :load-toplevel :execute)
104 (defclass asdf::iolib-grovel-file (process-op-input cc-flags-mixin)
106 (:default-initargs
107 :generated-lisp-file-type "processed-iolib-grovel-file")
108 (:documentation
109 "This ASDF component represents an input file that is processed
110 by PROCESS-GROVEL-FILE.")))
112 (defmethod asdf:perform ((op process-op) (c asdf::iolib-grovel-file))
113 (let ((output-file (asdf:output-file op c))
114 (input-file (asdf:component-pathname c)))
115 (ensure-directories-exist (directory-namestring output-file))
116 (let ((tmp-file (process-grovel-file input-file (ensure-pathname output-file))))
117 (unwind-protect
118 (alexandria:copy-file tmp-file output-file :if-to-exists :supersede)
119 (delete-file tmp-file)))))
121 ;;;# ASDF component: WRAPPER-FILE
123 (eval-when (:compile-toplevel :load-toplevel :execute)
124 (defclass asdf::iolib-wrapper-file (process-op-input cc-flags-mixin)
125 ((soname :initform nil :initarg :soname :accessor soname-of))
126 (:default-initargs
127 :generated-lisp-file-type "processed-iolib-wrapper-file")
128 (:documentation
129 "This ASDF component represents an input file that is processed
130 by PROCESS-WRAPPER-FILE. This generates a foreign library and
131 matching CFFI bindings that are subsequently compiled and
132 loaded.")))
134 (defmethod asdf:perform ((op process-op) (c asdf::iolib-wrapper-file))
135 (let ((output-file (asdf:output-file op c))
136 (input-file (asdf:component-pathname c)))
137 (ensure-directories-exist (directory-namestring output-file))
138 (let ((tmp-file (process-wrapper-file input-file output-file (or (soname-of c)
139 (asdf:component-name c)))))
140 (unwind-protect
141 (alexandria:copy-file tmp-file output-file :if-to-exists :supersede)
142 (delete-file tmp-file)))))