]> asedeno.scripts.mit.edu Git - cl-protobufs.git/blob - asdf-support.lisp
generalize CCL fasl ignores
[cl-protobufs.git] / asdf-support.lisp
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;;                                                                  ;;;
3 ;;; Free Software published under an MIT-like license. See LICENSE   ;;;
4 ;;;                                                                  ;;;
5 ;;; Copyright (c) 2012 Google, Inc.  All rights reserved.            ;;;
6 ;;;                                                                  ;;;
7 ;;; Original author: Scott McKay                                     ;;;
8 ;;; Based on work by: Robert Brown, Francois-Rene Rideau             ;;;
9 ;;;                                                                  ;;;
10 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11
12 (in-package "ASDF")
13
14
15 ;;; ASDF support for CL-Protobufs
16
17 (defclass protobuf-file (cl-source-file)
18   ((type :initform "proto")             ;default file type
19    ;; If non-nil, use this relative pathname
20    (proto-pathname :accessor proto-relative-pathname
21                    :initform nil
22                    :initarg :proto-pathname
23                    :documentation "Relative pathname giving the location of the .proto file")
24    ;; A search path to try when looking for system-provided .proto files
25    (search-path :accessor proto-search-path
26                 :initform ()
27                 :initarg :search-path
28                 :documentation
29                 "List of directories where the protocol buffer compiler should search
30                  for imported protobuf files.  Relative pathnames are treated as relative
31                  to the directory containing the DEFSYSTEM form in which they appear.")
32    (conc-name :accessor proto-conc-name
33               :initform ""
34               :initarg :conc-name))
35   (:documentation
36    "This ASDF component defines PROTO-TO-LISP, COMPILE-OP and LOAD-OP
37     operations that compile the .proto file into a .lisp file. The .lisp
38     file is then compiled, and possibly loaded, as well."))
39
40 (defclass proto-to-lisp (compile-op) ()
41   (:documentation
42    "The ASDF operation that compiles a .proto file containing Protocol Buffers
43     definitions into a .lisp source file."))
44
45 (defmethod component-depends-on ((op compile-op) (component protobuf-file))
46   "Compiling a protocol buffer file depends on generating Lisp source code for it."
47   (if (typep op 'proto-to-lisp)
48     (call-next-method)
49     `((proto-to-lisp ,(component-name component))
50       ,@(call-next-method))))
51
52 (defmethod component-depends-on ((op load-op) (component protobuf-file))
53   "Loading a protocol buffer file depends on generating Lisp source code for it."
54   `((proto-to-lisp ,(component-name component))
55     ,@(call-next-method)))
56
57 (defmethod component-self-dependencies :around ((op load-op) (component protobuf-file))
58   (remove-if #'(lambda (x)
59                  (eq (car x) 'proto-to-lisp))
60              (call-next-method)))
61
62 (defun protobuf-input-file (component)
63   "Returns the pathname of the protocol buffer definition file that must be
64    translated into Lisp source code for this PROTO-FILE component."
65   (check-type component protobuf-file)
66   (if (proto-relative-pathname component)
67     ;; Path was specified with ':proto-pathname'
68     (subpathname (component-pathname (component-parent component))
69                  (proto-relative-pathname component)
70                  :type "proto")
71     ;; No ':proto-pathname', the path of the protobuf file
72     ;; defaults to the component-pathname, with its automatic type "proto"
73     (component-pathname component)))
74
75 (defun resolve-search-path (component)
76   (check-type component protobuf-file)
77   (let* ((search-path (proto-search-path component))
78          (parent-path (component-pathname (component-parent component))))
79     (mapcar #'(lambda (path)
80                 (resolve-relative-pathname path parent-path))
81             search-path)))
82
83 (defun resolve-relative-pathname (path parent-path)
84   "When 'path' doesn't have an absolute directory component,
85    treat it as relative to 'parent-path'."
86   (pathname-directory-pathname
87    (merge-pathnames* path parent-path)))
88
89 (defmethod input-files ((op proto-to-lisp) (component protobuf-file))
90   "The input file is just the .proto file."
91   (declare (ignorable op))
92   (list (protobuf-input-file component)))
93
94 (defmethod output-files ((op proto-to-lisp) (component protobuf-file))
95   "The output file is a .lisp file and a .proto-imports file with dependency data,
96    stored where .fasl files are stored"
97   (declare (ignorable op))
98   (let ((lisp-file (lispize-pathname (component-pathname component))))
99     (values (list lisp-file
100                   (make-pathname :type "proto-imports"
101                                  :defaults lisp-file))
102             nil)))
103
104 (defmethod perform ((op proto-to-lisp) (component protobuf-file))
105   (let* ((input  (protobuf-input-file component))
106          (output (first (output-files op component)))
107          (paths  (cons (directory-namestring input) (resolve-search-path component)))
108          (proto-impl:*protobuf-search-path* paths)
109          (proto-impl:*protobuf-output-path* output))
110     (dolist (path paths (error 'compile-failed
111                           :component component :operation op))
112       (let ((proto (make-pathname :type "proto" :defaults (merge-pathnames* path (pathname input)))))
113         (destructuring-bind (lisp imports)
114             (output-files op component)
115           (when (probe-file proto)
116             (return-from perform
117               (proto-impl:parse-protobuf-file proto lisp imports
118                                               :conc-name (proto-conc-name component)))))))))
119
120 (defmethod operation-description ((op proto-to-lisp) (component protobuf-file))
121   (format nil (compatfmt "~@<proto-compiling ~3i~_~A~@:>")
122           (first (input-files op component))))
123
124 (defmethod input-files ((op compile-op) (component protobuf-file))
125   "The input files are the .lisp and .proto-imports files."
126   (declare (ignorable op))
127   (output-files (make-instance 'proto-to-lisp) component))
128
129 (defmethod perform ((op compile-op) (component protobuf-file))
130   (let* ((input  (protobuf-input-file component))
131          (output (output-file op component))
132          (lisp   (first (input-files op component)))
133          (fasl   output)
134          (paths  (cons (directory-namestring input) (resolve-search-path component)))
135          (proto-impl:*protobuf-search-path* paths)
136          (proto-impl:*protobuf-output-path* output)
137          (*compile-file-warnings-behaviour* (operation-on-warnings op))
138          (*compile-file-failure-behaviour* (operation-on-failure op)))
139     (proto-impl:process-imports-from-file
140      (make-pathname :type "proto-imports"
141                     :defaults output))
142     (multiple-value-bind (output warnings-p failure-p)
143         (apply #'compile-file* lisp
144                :output-file fasl
145                (compile-op-flags op))
146       (when warnings-p
147         (case (operation-on-warnings op)
148           (:warn  (warn "~@<COMPILE-FILE warned while performing ~A on ~A.~@:>" op component))
149           (:error (error 'compile-warned
150                          :component component :operation op))
151           (:ignore nil)))
152       (when failure-p
153         (case (operation-on-failure op)
154           (:warn  (warn "~@<COMPILE-FILE failed while performing ~A on ~A.~@:>" op component))
155           (:error (error 'compile-failed
156                          :component component :operation op))
157           (:ignore nil)))
158       (unless output
159         (error 'compile-error
160                :component component :operation op)))))
161
162 (defmethod input-files ((op load-op) (component protobuf-file))
163   "The input files are the .fasl and .proto-imports files."
164   (declare (ignorable op))
165   (append (output-files (make-instance 'compile-op) component)            ;fasl
166           (cdr (output-files (make-instance 'proto-to-lisp) component)))) ;proto-imports
167
168 (defmethod perform ((op load-op) (component protobuf-file))
169   (let* ((input  (protobuf-input-file component))
170          (paths  (cons (directory-namestring input) (resolve-search-path component)))
171          (proto-impl:*protobuf-search-path* paths)
172          (proto-impl:*protobuf-output-path* (first (input-files op component))))
173     (destructuring-bind (fasl proto-imports)
174         (input-files op component)
175       (proto-impl:process-imports-from-file proto-imports)
176       (load fasl))))
177
178 (defmethod operation-description ((op compile-op) (component protobuf-file))
179   (format nil (compatfmt "~@<compiling ~3i~_~A~@:>")
180           (first (input-files op component))))
181
182 \f
183 ;;; Processing of imports
184
185 (in-package "PROTO-IMPL")
186
187 (defun parse-protobuf-file (protobuf-file lisp-file imports-file &key (conc-name ""))
188   (let ((schema (parse-schema-from-file protobuf-file :conc-name conc-name)))
189     (with-open-file (stream lisp-file
190                      :direction :output
191                      :if-exists :supersede
192                      :external-format :utf-8
193                      :element-type 'character)
194       (write-schema schema :stream stream :type :lisp))
195     (with-open-file (stream imports-file
196                      :direction :output
197                      :if-exists :supersede
198                      :external-format :utf-8
199                      :element-type 'character)
200       (with-standard-io-syntax
201         (format stream "~W~%" (proto-imports schema)))))
202   lisp-file)
203
204 ;; Process 'import' lines
205 (defun process-imports (schema imports)
206   "Imports all of the files given by 'imports'.
207    If the file is a .proto file, it first parses it and writes a .lisp file.
208    The .lisp file is the compiled and loaded."
209   (dolist (import imports)
210     (block import-one
211       (let* ((import      (pathname import))
212              (import-name (pathname-name import))
213              (imported    (find-schema (class-name->proto import-name))))
214         ;; If this schema has already been imported somewhere else,
215         ;; mark it as imported here and carry on
216         (when imported
217           (setf (proto-imported-schemas schema)
218                 (nconc (proto-imported-schemas schema) (list imported)))
219           (return-from import-one))
220         (do-process-import import import-name)
221         (let* ((imported (find-schema (class-name->proto import-name))))
222           (when imported
223             (setf (proto-imported-schemas schema)
224                   (nconc (proto-imported-schemas schema) (list imported))))
225           (return-from import-one))))))
226
227 (defun process-imports-from-file (imports-file)
228   (when (probe-file imports-file)
229     (let ((imports (with-open-file (stream imports-file
230                                     :direction :input
231                                     :external-format :utf-8
232                                     :element-type 'character)
233                      (with-standard-io-syntax (read stream)))))
234       (dolist (import imports)
235         (let* ((import      (pathname import))
236                (import-name (pathname-name import)))
237           ;; If this schema has already been loaded, we're done.
238           (unless (find-schema (class-name->proto import-name))
239             (do-process-import import import-name)))))))
240
241 (defun do-process-import (import import-name
242                           &key (search-path *protobuf-search-path*)
243                                (output-path *protobuf-output-path*))
244   (dolist (path search-path (error "Could not import ~S" import))
245     (let* ((base-path  (asdf::merge-pathnames* import path))
246            (proto-file (make-pathname :name import-name :type "proto"
247                                       :defaults base-path))
248            (lisp-file  (asdf::lispize-pathname
249                         (if output-path
250                             (make-pathname :name import-name
251                                            :directory (pathname-directory output-path))
252                             base-path)))
253            (imports-file (make-pathname :type "proto-imports"
254                                         :defaults lisp-file))
255            (fasl-file  (compile-file-pathname lisp-file))
256            (asdf:*asdf-verbose* nil)    ;for safe-file-write-date
257            (proto-date (asdf::safe-file-write-date proto-file))
258            (lisp-date  (asdf::safe-file-write-date lisp-file))
259            (fasl-date  (asdf::safe-file-write-date fasl-file))
260            (imports-date  (asdf::safe-file-write-date imports-file)))
261       (when (probe-file proto-file)
262         (let ((*protobuf-pathname* proto-file))
263           (when (string= (pathname-type base-path) "proto")
264             ;; The user asked to import a .proto file
265             ;; If there's no .lisp file or an older .lisp file, or no
266             ;; .proto-imports file or an older .proto-imports file parse
267             ;; the .proto file now
268             ;; If we did not parse the .proto file, process the generated
269             ;; .proto-imports file now.
270             (cond ((not proto-date)
271                    (warn "Could not find the .proto file to be imported: ~A" proto-file))
272                   ((or (not (and lisp-date imports-date))
273                        (< lisp-date proto-date)
274                        (< imports-date proto-date))
275                    (parse-protobuf-file proto-file lisp-file imports-file)
276                    (setq lisp-date (file-write-date lisp-file))
277                    (setq imports-date (file-write-date imports-file)))
278                   (t
279                    (process-imports-from-file imports-file))))
280           ;; Compile the .lisp file, if necessary
281           (cond ((not lisp-date)
282                  (unless (string= (pathname-type base-path) "proto")
283                    (warn "Could not find the .lisp file to be compiled: ~A" lisp-file)))
284                 (t
285                  (when (or (not fasl-date)
286                            (< fasl-date lisp-date))
287                    (let ((*compile-file-pathname* lisp-file)
288                          (*load-pathname* nil))
289                      (setq fasl-file (compile-file lisp-file)))
290                    (setq fasl-date (file-write-date fasl-file)))))
291           ;; Load the .fasl file
292           (cond ((not fasl-date)
293                  (unless (string= (pathname-type base-path) "proto")
294                    (warn "Could not find the .fasl file to be loaded: ~A" fasl-file)))
295                 (t
296                  (let ((*compile-file-pathname* nil)
297                        (*load-pathname* fasl-file))
298                    (load fasl-file)))))
299         (return (values))))))