-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage.lisp
More file actions
165 lines (147 loc) · 6.64 KB
/
package.lisp
File metadata and controls
165 lines (147 loc) · 6.64 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
(uiop:define-package :linux-packaging/package
(:use :cl)
(:import-from :asdf
#:system
#:component-entry-point
#:component-name
#:perform
#:program-op
#:system
#:system-author
#:system-description
#:system-license
#:system-version)
(:import-from :asdf/system #:component-build-pathname)
(:import-from :cffi
#:close-foreign-library
#:foreign-library-load-state
#:foreign-library-name
#:foreign-library-pathname
#:foreign-library-type
#:list-foreign-libraries)
(:import-from :cffi-toolchain #:static-program-op)
(:import-from :uiop
#:getenv
#:register-image-dump-hook
#:run-program)
(:import-from :uiop #:strcat #:run-program #:*image-entry-point*)
(:import-from :cl-ppcre #:split)
(:export #:linux-package
#:system-dependencies
#:package-type
#:path->package
#:build-op))
(in-package :linux-packaging/package)
(defmacro d (system &rest args)
`(when (verbose ,system)
(funcall #'format t ,@args)))
(defun last-elt (sequence)
(subseq sequence (1- (length sequence))))
(defun right-pad (char haystack)
(check-type char string)
(check-type haystack string)
(strcat haystack
(unless (string= char (last-elt haystack))
char)))
(defun additional-file->argument (additional-file)
(format nil "~a=~a"
(let ((src (first additional-file)))
(if (pathnamep src)
(namestring src)
src))
(let ((dst (rest additional-file)))
(if (pathnamep dst)
(namestring dst)
(right-pad "/" dst)))))
(defclass linux-package (system)
((package-name :initarg :package-name :initform nil :reader pkg-name)
(homepage :initarg :homepage :initform nil :reader homepage)
(ignored-libraries :initarg :ignored-libraries :initform nil :reader ignored-libraries)
(additional-files :initarg :additional-files :initform nil :reader additional-files)
(additional-dependencies :initarg :additional-dependencies :initform nil :reader additional-dependencies)
(verbose :initarg :verbose :initform nil :reader verbose)
(package-type :reader package-type)))
(defmethod make-instance :after ((s linux-package) &key &allow-other-keys)
(setf (slot-value s 'ignored-libraries)
(mapcar #'string-downcase (ignored-libraries s))))
(defgeneric system-dependencies (linux-package)
(:documentation "Returns the dependencies that every Lisp image relies on."))
(defgeneric path->package (linux-package path)
(:documentation "Returns the package a file belongs to."))
(defmethod find-dependencies ((system linux-package))
(let ((libraries-to-paths (ldconfig)))
(remove-duplicates
(append
(additional-dependencies system)
(let ((system-deps (system-dependencies system)))
(d system "System dependencies: ~a~%" system-deps)
system-deps)
(reduce (lambda (packages library)
(append
packages
(unless (or (is-ignored system library)
(eq (foreign-library-type library) :grovel-wrapper))
(list
(let* ((path (or (gethash (namestring (foreign-library-pathname library))
libraries-to-paths)
(error "Unable to find the library for ~a"
(foreign-library-name library))))
(package (path->package system path)))
(d system "Package for ~a: ~a~%" path package)
(or package
(error "Unable to find a package for ~a"
(foreign-library-name library))))))))
(list-foreign-libraries)
:initial-value nil)))))
(defclass build-op (static-program-op) ())
(defmethod perform :before ((o program-op) (s system))
;;; Make sure we close statically linked libraries.
;;; Remove when this or similar is done in cffi: https://github.com/cffi/cffi/pull/163
(register-image-dump-hook
(lambda ()
(dolist (library (list-foreign-libraries))
(d s "Detecting library ~a of type ~a~%" library (foreign-library-type library))
(when (eql (foreign-library-type library) :grovel-wrapper)
(d s "Closing ~a~%" library)
(close-foreign-library library)))))
(setf *image-entry-point* (component-entry-point s)))
(defmethod perform ((o build-op) (s system))
(call-next-method o s)
(let* ((deps (find-dependencies s))
(command (delete nil
`("fpm" "-s" "dir"
"-t" ,(package-type s)
,(let ((maintainer (system-author s)))
(when maintainer (strcat "--maintainer=" maintainer)))
,(let ((license (system-license s)))
(when license (strcat "--license=" license)))
,(let ((description (system-description s)))
(when description (strcat "--description=" description)))
,(let ((homepage (homepage s)))
(when homepage (strcat "--url=" homepage)))
,@(mapcar (lambda (dep)
(strcat "--depends=" dep))
deps)
"-n" ,(or (pkg-name s) (component-name s))
"-v" ,(or (system-version s) (getenv "VERSION") "1.0.0")
,(format nil "~a=/usr/bin/" (component-build-pathname s))
,@(mapcar #'additional-file->argument
(additional-files s))))))
(d s "Running command: ~a~%" command)
(run-program command :output :interactive :error-output :interactive)))
(defun ldconfig ()
(let ((libraries->paths (make-hash-table :test #'equal)))
(with-input-from-string (s (run-program '("ldconfig" "-p") :output '(:string)))
;; first line is pointless
(read-line s)
(loop
(let ((line (read-line s nil 'eof)))
(when (eq line 'eof)
(return-from ldconfig libraries->paths))
(let ((parts (split " " (subseq line 1))))
(setf (gethash (first parts) libraries->paths)
(first (last parts)))))))))
(defun is-ignored (system library)
(member (string-downcase (symbol-name (foreign-library-name library)))
(ignored-libraries system)
:test #'string=))