1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; Free Software published under an MIT-like license. See LICENSE ;;;
5 ;;; Copyright (c) 2012 Google, Inc. All rights reserved. ;;;
7 ;;; Original author: Scott McKay ;;;
8 ;;; Based on work by: Robert Brown, Francois-Rene Rideau ;;;
10 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15 ;;; ASDF support for CL-Protobufs
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
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
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
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."))
40 (defclass proto-to-lisp (compile-op) ()
42 "The ASDF operation that compiles a .proto file containing Protocol Buffers
43 definitions into a .lisp source file."))
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)
49 `((proto-to-lisp ,(component-name component))
50 ,@(call-next-method))))
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)))
57 (defmethod component-self-dependencies :around ((op load-op) (component protobuf-file))
58 (remove-if #'(lambda (x)
59 (eq (car x) 'proto-to-lisp))
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)
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)))
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))
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)))
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)))
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))
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)
117 (proto-impl:parse-protobuf-file proto lisp imports
118 :conc-name (proto-conc-name component)))))))))
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))))
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))
129 (defmethod perform ((op compile-op) (component protobuf-file))
130 (destructuring-bind (lisp-file imports-file) (input-files op component)
131 (destructuring-bind (fasl-file
134 #+(or ecl mkcl) object-file
135 #+asdf3 warnings-file)
136 (output-files op component)
137 (let* ((proto-file (protobuf-input-file component))
138 (paths (cons (directory-namestring proto-file)
139 (resolve-search-path component)))
140 (proto-impl:*protobuf-search-path* paths)
141 (proto-impl:*protobuf-output-path* fasl-file)
142 (*compile-file-warnings-behaviour* (operation-on-warnings op))
143 (*compile-file-failure-behaviour* (operation-on-failure op)))
144 (proto-impl:process-imports-from-file imports-file)
145 (multiple-value-bind (output warnings-p failure-p)
146 (apply #'compile-file* lisp-file
147 :output-file fasl-file
149 :warnings-file warnings-file
151 #+clisp (list :lib-file lib-file)
152 #+(or ecl mkcl) (list :object-file object-file)
153 (compile-op-flags op)))
155 (check-lisp-compile-results output warnings-p failure-p
156 "~/asdf-action::format-action/" (list (cons op component)))
160 (case (operation-on-warnings op)
161 (:warn (warn "~@<COMPILE-FILE warned while performing ~A on ~A.~@:>" op component))
162 (:error (error 'compile-warned
163 :component component :operation op))
166 (case (operation-on-failure op)
167 (:warn (warn "~@<COMPILE-FILE failed while performing ~A on ~A.~@:>" op component))
168 (:error (error 'compile-failed
169 :component component :operation op))
172 (error 'compile-error
173 :component component :operation op))))))))
175 (defmethod input-files ((op load-op) (component protobuf-file))
176 "The input files are the .fasl and .proto-imports files."
177 (declare (ignorable op))
178 (list (first (output-files (make-instance 'compile-op) component)) ;fasl
179 (second (output-files (make-instance 'proto-to-lisp) component)))) ;proto-imports
181 (defmethod perform ((op load-op) (component protobuf-file))
182 (let* ((input (protobuf-input-file component))
183 (paths (cons (directory-namestring input) (resolve-search-path component)))
184 (proto-impl:*protobuf-search-path* paths)
185 (proto-impl:*protobuf-output-path* (first (input-files op component))))
186 (destructuring-bind (fasl proto-imports)
187 (input-files op component)
188 (proto-impl:process-imports-from-file proto-imports)
191 (defmethod operation-description ((op compile-op) (component protobuf-file))
192 (format nil (compatfmt "~@<compiling ~3i~_~A~@:>")
193 (first (input-files op component))))
196 ;;; Processing of imports
198 (in-package "PROTO-IMPL")
200 (defun parse-protobuf-file (protobuf-file lisp-file imports-file &key (conc-name ""))
201 (let ((schema (parse-schema-from-file protobuf-file :conc-name conc-name)))
202 (with-open-file (stream lisp-file
204 :if-exists :supersede
205 :external-format :utf-8
206 :element-type 'character)
207 (write-schema schema :stream stream :type :lisp))
208 (with-open-file (stream imports-file
210 :if-exists :supersede
211 :external-format :utf-8
212 :element-type 'character)
213 (with-standard-io-syntax
214 (format stream "~W~%" (proto-imports schema)))))
217 ;; Process 'import' lines
218 (defun process-imports (schema imports)
219 "Imports all of the files given by 'imports'.
220 If the file is a .proto file, it first parses it and writes a .lisp file.
221 The .lisp file is the compiled and loaded."
222 (dolist (import imports)
224 (let* ((import (pathname import))
225 (import-name (pathname-name import))
226 (imported (find-schema (class-name->proto import-name))))
227 ;; If this schema has already been imported somewhere else,
228 ;; mark it as imported here and carry on
230 (setf (proto-imported-schemas schema)
231 (nconc (proto-imported-schemas schema) (list imported)))
232 (return-from import-one))
233 (do-process-import import import-name)
234 (let* ((imported (find-schema (class-name->proto import-name))))
236 (setf (proto-imported-schemas schema)
237 (nconc (proto-imported-schemas schema) (list imported))))
238 (return-from import-one))))))
240 (defun process-imports-from-file (imports-file)
241 (when (probe-file imports-file)
242 (let ((imports (with-open-file (stream imports-file
244 :external-format :utf-8
245 :element-type 'character)
246 (with-standard-io-syntax (read stream)))))
247 (dolist (import imports)
248 (let* ((import (pathname import))
249 (import-name (pathname-name import)))
250 ;; If this schema has already been loaded, we're done.
251 (unless (find-schema (class-name->proto import-name))
252 (do-process-import import import-name)))))))
254 (defun do-process-import (import import-name
255 &key (search-path *protobuf-search-path*)
256 (output-path *protobuf-output-path*))
257 (dolist (path search-path (error "Could not import ~S" import))
258 (let* ((base-path (asdf::merge-pathnames* import path))
259 (proto-file (make-pathname :name import-name :type "proto"
260 :defaults base-path))
261 (lisp-file (asdf::lispize-pathname
263 (make-pathname :name import-name
264 :directory (pathname-directory output-path))
266 (imports-file (make-pathname :type "proto-imports"
267 :defaults lisp-file))
268 (fasl-file (compile-file-pathname lisp-file))
269 (asdf:*asdf-verbose* nil) ;for safe-file-write-date
270 (proto-date (asdf::safe-file-write-date proto-file))
271 (lisp-date (asdf::safe-file-write-date lisp-file))
272 (fasl-date (asdf::safe-file-write-date fasl-file))
273 (imports-date (asdf::safe-file-write-date imports-file)))
274 (when (probe-file proto-file)
275 (let ((*protobuf-pathname* proto-file))
276 (when (string= (pathname-type base-path) "proto")
277 ;; The user asked to import a .proto file
278 ;; If there's no .lisp file or an older .lisp file, or no
279 ;; .proto-imports file or an older .proto-imports file parse
280 ;; the .proto file now
281 ;; If we did not parse the .proto file, process the generated
282 ;; .proto-imports file now.
283 (cond ((not proto-date)
284 (warn "Could not find the .proto file to be imported: ~A" proto-file))
285 ((or (not (and lisp-date imports-date))
286 (< lisp-date proto-date)
287 (< imports-date proto-date))
288 (parse-protobuf-file proto-file lisp-file imports-file)
289 (setq lisp-date (file-write-date lisp-file))
290 (setq imports-date (file-write-date imports-file)))
292 (process-imports-from-file imports-file))))
293 ;; Compile the .lisp file, if necessary
294 (cond ((not lisp-date)
295 (unless (string= (pathname-type base-path) "proto")
296 (warn "Could not find the .lisp file to be compiled: ~A" lisp-file)))
298 (when (or (not fasl-date)
299 (< fasl-date lisp-date))
300 (let ((*compile-file-pathname* lisp-file)
301 (*load-pathname* nil))
302 (setq fasl-file (compile-file lisp-file)))
303 (setq fasl-date (file-write-date fasl-file)))))
304 ;; Load the .fasl file
305 (cond ((not fasl-date)
306 (unless (string= (pathname-type base-path) "proto")
307 (warn "Could not find the .fasl file to be loaded: ~A" fasl-file)))
309 (let ((*compile-file-pathname* nil)
310 (*load-pathname* fasl-file))
312 (return (values))))))