-
Notifications
You must be signed in to change notification settings - Fork 133
/
asdf.lisp
152 lines (129 loc) · 6.19 KB
/
asdf.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
;;;
;;; asdf.lisp --- ASDF components for cffi-grovel.
;;;
;;; Copyright (C) 2005-2006, Dan Knap <dankna@accela.net>
;;; Copyright (C) 2005-2006, Emily Backes <lucca@accela.net>
;;; Copyright (C) 2007, Stelian Ionescu <sionescu@cddr.org>
;;; Copyright (C) 2007, Luis Oliveira <loliveira@common-lisp.net>
;;;
;;; Permission is hereby granted, free of charge, to any person
;;; obtaining a copy of this software and associated documentation
;;; files (the "Software"), to deal in the Software without
;;; restriction, including without limitation the rights to use, copy,
;;; modify, merge, publish, distribute, sublicense, and/or sell copies
;;; of the Software, and to permit persons to whom the Software is
;;; furnished to do so, subject to the following conditions:
;;;
;;; The above copyright notice and this permission notice shall be
;;; included in all copies or substantial portions of the Software.
;;;
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;;; DEALINGS IN THE SOFTWARE.
;;;
(in-package #:cffi-grovel)
(defclass cc-flags-mixin ()
((cc-flags :initform nil :accessor cc-flags-of :initarg :cc-flags)))
(defclass process-op (downward-operation)
()
(:documentation "This ASDF operation performs the steps necessary
to generate a compilable and loadable lisp file from a
PROCESS-OP-INPUT component."))
(defclass process-op-input (cl-source-file)
((generated-lisp-file-type
:initarg :generated-lisp-file-type
:accessor generated-lisp-file-type
:documentation "The :TYPE argument to use for the generated lisp file."))
(:default-initargs
:generated-lisp-file-type "generated-lisp-file")
(:documentation "This ASDF component represents a file that is
used as input to a function that generates lisp source file. This
component acts as if it is a CL-SOURCE-FILE by applying the
COMPILE-OP and LOAD-SOURCE-OP operations to the file generated by
PROCESS-OP."))
(defmethod perform :around ((op process-op) (file cc-flags-mixin))
(let ((*cc-flags* (append (ensure-list (cc-flags-of file))
*cc-flags*)))
(call-next-method)))
(defmethod input-files ((op process-op) (c process-op-input))
(list (component-pathname c)))
(defmethod input-files ((op compile-op) (c process-op-input))
(list (first (output-files 'process-op c))))
(defmethod component-depends-on ((op process-op) (c process-op-input))
`((prepare-op ,c) ,@(call-next-method)))
(defmethod component-depends-on ((op compile-op) (c process-op-input))
`((process-op ,c) ,@(call-next-method)))
(defmethod component-depends-on ((op load-source-op) (c process-op-input))
`((process-op ,c) ,@(call-next-method)))
;;;# ASDF component: GROVEL-FILE
(defclass grovel-file (process-op-input cc-flags-mixin)
()
(:default-initargs
:generated-lisp-file-type "processed-grovel-file")
(:documentation
"This ASDF component represents an input file that is processed
by PROCESS-GROVEL-FILE."))
(defmethod output-files ((op process-op) (c grovel-file))
(let* ((input-file (first (input-files op c)))
(output-file (make-pathname :type (generated-lisp-file-type c)
:defaults input-file))
(c-file (make-c-file-name output-file "__grovel")))
(list output-file
c-file
(make-exe-file-name c-file))))
(defmethod perform ((op process-op) (c grovel-file))
(let* ((output-file (first (output-files op c)))
(input-file (first (input-files op c)))
(tmp-file (process-grovel-file input-file output-file)))
(rename-file-overwriting-target tmp-file output-file)))
;;;# ASDF component: WRAPPER-FILE
(defclass wrapper-file (process-op-input cc-flags-mixin)
((soname :initform nil :initarg :soname :accessor soname-of))
(:default-initargs
:generated-lisp-file-type "processed-wrapper-file")
(:documentation
"This ASDF component represents an input file that is processed
by PROCESS-WRAPPER-FILE. This generates a foreign library and
matching CFFI bindings that are subsequently compiled and
loaded."))
(defun wrapper-soname (c)
(or (soname-of c)
(component-name c)))
(defmethod output-files ((op process-op) (c wrapper-file))
(let* ((input-file (first (input-files op c)))
(output-file (make-pathname :type (generated-lisp-file-type c)
:defaults input-file))
(c-file (make-c-file-name output-file "__wrapper"))
(o-file (make-o-file-name output-file "__wrapper"))
(lib-soname (wrapper-soname c)))
(list output-file
(make-so-file-name (make-soname lib-soname output-file))
c-file
o-file)))
;;; Declare the .o and .so files as compilation outputs,
;;; so they get picked up by bundle operations.
#.(when (version<= "3.1.6" (asdf-version))
'(defmethod output-files ((op compile-op) (c wrapper-file))
(destructuring-bind (generated-lisp lib-file c-file o-file) (output-files 'process-op c)
(declare (ignore generated-lisp c-file))
(multiple-value-bind (files translatedp) (call-next-method)
(values (append files (list lib-file o-file)) translatedp)))))
(defmethod perform ((op process-op) (c wrapper-file))
(let* ((output-file (first (output-files op c)))
(input-file (first (input-files op c)))
(tmp-file (process-wrapper-file
input-file
:output-defaults output-file
:lib-soname (wrapper-soname c))))
(unwind-protect
(alexandria:copy-file tmp-file output-file :if-to-exists :supersede)
(delete-file tmp-file))))
;; Allow for naked :cffi-grovel-file and :cffi-wrapper-file in asdf definitions.
(setf (find-class 'asdf::cffi-grovel-file) (find-class 'grovel-file))
(setf (find-class 'asdf::cffi-wrapper-file) (find-class 'wrapper-file))