forked from qitab/cl-protobufs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asdf.lisp
153 lines (132 loc) · 7.24 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
152
153
;;; Copyright 2012-2020 Google LLC
;;;
;;; Use of this source code is governed by an MIT-style
;;; license that can be found in the LICENSE file or at
;;; https://opensource.org/licenses/MIT.
(defpackage #:protobuf-config
(:documentation "Configuration information for PROTOBUF.")
(:use #:common-lisp)
(:export *protoc-relative-path*))
(in-package #:protobuf-config)
(defvar *protoc-relative-path* nil
"Supply relative proto file paths to protoc, the protobuf compiler?")
(defpackage #:protobuf-system
(:documentation "System definitions for protocol buffer code.")
(:use #:common-lisp
#:asdf
#:protobuf-config)
(:export #:protobuf-source-file
#:proto-pathname
#:search-path))
(in-package #:protobuf-system)
(defclass protobuf-source-file (cl-source-file)
((relative-proto-pathname :initarg :proto-pathname
:initform nil
:reader proto-pathname
:documentation
"Relative pathname that specifies the location of a .proto file.")
(search-path :initform ()
:initarg :proto-search-path
:reader search-path
:documentation
"List containing directories where the protocol buffer compiler should search
for imported protobuf files. Non-absolute pathnames are treated as relative to
the directory containing the DEFSYSTEM form in which they appear."))
(:documentation "A protocol buffer definition file."))
(setf (find-class 'asdf::protobuf-source-file) (find-class 'protobuf-source-file))
(defclass proto-to-lisp (downward-operation selfward-operation)
((selfward-operation :initform 'prepare-op :allocation :class))
(:documentation
"An ASDF operation that compiles a .proto file containing protocol buffer
definitions into a Lisp source file."))
(defmethod component-depends-on ((operation compile-op) (proto-def-file protobuf-source-file))
"Specifies the dependencies of a compile OPERATION on PROTO-DEF-FILE.
Compiling a protocol buffer file depends on generating Lisp source code for
the protobuf, but also on loading package definitions and in-line function
definitions that the machine-generated protobuf Lisp code uses."
`((proto-to-lisp ,(component-name proto-def-file))
,@(call-next-method)))
(defmethod component-depends-on ((operation load-op) (proto-def-file protobuf-source-file))
"Specifies the dependencies of a load OPERATION on PROTO-DEF-FILE.
Loading a protocol buffer file depends on generating Lisp source code for the
protobuf, but also on loading package definitions and in-line function
definitions that the machine-generated protobuf Lisp code uses."
`((proto-to-lisp ,(component-name proto-def-file))
,@(call-next-method)))
(defun proto-input (protobuf-source-file)
"Returns the pathname of PROTOBUF-SOURCE-FILE which must be
translated into Lisp source code for this PROTO-FILE component."
(if (proto-pathname protobuf-source-file)
;; Path of the protobuf file was specified with :PROTO-PATHNAME.
(merge-pathnames
(make-pathname :type "proto")
(merge-pathnames (pathname (proto-pathname protobuf-source-file))
(component-pathname (component-parent protobuf-source-file))))
;; No :PROTO-PATHNAME was specified, so the path of the protobuf
;; defaults to that of the Lisp file, but with a ".proto" suffix.
(let ((lisp-pathname (component-pathname protobuf-source-file)))
(merge-pathnames (make-pathname :type "proto") lisp-pathname))))
(defmethod input-files ((operation proto-to-lisp) (component protobuf-source-file))
(list (proto-input component)))
(defmethod output-files ((operation proto-to-lisp) (component protobuf-source-file))
"Arranges for the Lisp output file of a proto-to-lisp OPERATION on a
PROTOBUF-SOURCE-FILE COMPONENT to be stored where fasl files are located."
(values (list (component-pathname component))
nil)) ; allow around methods to translate
(defun resolve-relative-pathname (path parent-path)
"When PATH doesn't have an absolute directory component, treat it as relative
to PARENT-PATH."
(let* ((pathname (pathname path))
(directory (pathname-directory pathname)))
(if (and (list directory) (eq (car directory) :absolute))
pathname
(let ((resolved-path (merge-pathnames pathname parent-path)))
(make-pathname :directory (pathname-directory resolved-path)
:name nil
:type nil
:defaults resolved-path)))))
(defun resolve-search-path (protobuf-source-file)
"Resolves the search path of PROTOBUF-SOURCE-FILE."
(let ((search-path (search-path protobuf-source-file)))
(let ((parent-path (component-pathname (component-parent protobuf-source-file))))
(mapcar (lambda (path)
(resolve-relative-pathname path parent-path))
search-path))))
(define-condition protobuf-compile-failed (compile-failed-error)
()
(:documentation "Condition signalled when translating a .proto file into Lisp code fails."))
(defmethod perform :before ((operation proto-to-lisp) (component protobuf-source-file))
(map nil #'ensure-directories-exist (output-files operation component)))
(defmethod perform ((operation proto-to-lisp) (component protobuf-source-file))
(let* ((source-file (first (input-files operation component)))
(source-file-argument (if *protoc-relative-path*
(file-namestring source-file)
(namestring source-file)))
;; Around methods on output-file may globally redirect output products, so we must call
;; that method instead of executing (component-pathname component).
(output-file (first (output-files operation component)))
(search-path (cons (directory-namestring source-file) (resolve-search-path component)))
(command (format nil "protoc --proto_path=~{~A~^:~} --cl-pb_out=output-file=~A:~A ~A ~
--experimental_allow_proto3_optional"
search-path
(file-namestring output-file)
(directory-namestring output-file)
source-file-argument)))
(multiple-value-bind (output error-output status)
(uiop:run-program command :output t :error-output :output :ignore-error-status t)
(declare (ignore output error-output))
(unless (zerop status)
(error 'protobuf-compile-failed
:description (format nil "Failed to compile proto file. Command: ~S" command)
:context-format "~/asdf-action::format-action/"
:context-arguments `((,operation . ,component)))))))
(defmethod asdf::component-self-dependencies :around ((op load-op) (c protobuf-source-file))
"Removes PROTO-TO-LISP operations from self dependencies. Otherwise, the Lisp
output files of PROTO-TO-LISP are considered to be input files for LOAD-OP,
which means ASDF loads both the .lisp file and the .fasl file."
(remove-if (lambda (x)
(eq (car x) 'proto-to-lisp))
(call-next-method)))
(defmethod input-files ((operation compile-op) (c protobuf-source-file))
(output-files 'proto-to-lisp c))
(pushnew :cl-protobufs *features*)