1 (defpackage #:pkg-config
2 (:use #:common-lisp #:clg-utils #+(or cmu clisp) #:ext #+sbcl #:sb-ext)
4 (:import-from #:sb-int #:featurep)
5 (:export #:pkg-cflags #:pkg-libs #:pkg-exists-p #:pkg-version #:pkg-variable)
6 (:export #:featurep #:sbcl>=))
8 (in-package #:pkg-config)
10 (defparameter *pkg-config* "/usr/bin/pkg-config")
14 (defun run-pkg-config (package error-p &rest options)
17 *pkg-config* (cons package options) :wait t :output :stream)))
19 (error "Unable to run ~A" *pkg-config*))
20 (let ((exit-code (process-exit-code process)))
21 (unless (or (not error-p) (zerop exit-code))
24 (format nil "~A: ~{~A~%~}" *pkg-config* (read-lines (process-error process)))
25 (format nil "~A terminated with exit code ~A"
26 *pkg-config* exit-code))))
27 (let ((output (read-lines (process-output process))))
28 (process-close process)
29 (values output exit-code)))))
32 (defun run-pkg-config (package error-p &rest options)
33 (let ((outfile (format nil "/tmp/clg-pkg-config-~A-output" (os:process-id)))
34 (errfile (format nil "/tmp/clg-pkg-config-~A-error" (os:process-id))))
38 (format nil "~A ~A ~{~A ~}2>~A"
40 (mapcar #'(lambda (option)
41 (format nil "'~A'" option))
44 :output outfile :if-output-exists :overwrite)))
46 ((= exit-code 127) (error "Unable to run ~A" *pkg-config*))
47 ((and error-p (not (zerop exit-code)))
48 (with-open-file (output errfile)
49 (let ((errmsg (read-lines output)))
52 (format nil "~A terminated with exit code ~A" *pkg-config* exit-code)
53 (format nil "~A: ~{~A~%~}" *pkg-config* errmsg))))))
56 (with-open-file (output outfile)
61 (delete-file errfile)))))
64 (defun pkg-cflags (package)
65 (split-string (first (run-pkg-config package t "--cflags"))))
67 (defun pkg-libs (package)
68 (split-string (first (run-pkg-config package t "--libs"))))
71 (defun pkg-exists-p (package &key version atleast-version max-version
75 (version (format nil "= ~A" version))
76 (atleast-version (format nil ">= ~A" atleast-version))
77 (max-version (format nil "<= ~A" max-version))
81 (run-pkg-config package t "--print-errors" "--exists" version-check)
83 (multiple-value-bind (output exit-code)
84 (run-pkg-config package nil "--exists" version-check)
85 (declare (ignore output))
89 (defun pkg-version (package)
90 (first (run-pkg-config package t "--modversion")))
93 (defun pkg-variable (package variable)
94 (first (run-pkg-config package t "--variable" variable)))
97 (defun |#?-reader| (stream subchar arg)
98 (declare (ignore subchar arg))
99 (let ((not-p (when (char= (peek-char nil stream) #\-)
101 (conditional (read stream t nil t)))
103 (*read-suppress* (read stream t nil t))
106 :format-control "Attempt to read #? while *READ-EVAL* is bound to NIL."
107 :format-arguments nil :stream stream))
110 (not (eval conditional)))
111 (let ((*read-suppress* t))
112 (read stream t nil t)))))
115 (set-dispatch-macro-character #\# #\? #'|#?-reader|)
120 (defun sbcl-version ()
121 (let ((dot1 (position #\. (lisp-implementation-version)))
122 (dot2 (position #\. (lisp-implementation-version) :from-end t)))
124 (parse-integer (lisp-implementation-version) :end dot1)
125 (parse-integer (lisp-implementation-version) :start (1+ dot1) :end dot2)
126 (parse-integer (lisp-implementation-version) :start (1+ dot2)))))
127 (defun sbcl>= (req-major req-minor req-micro)
128 (multiple-value-bind (major minor micro) (sbcl-version)
131 (and (= major req-major) (> minor req-minor))
132 (and (= major req-major) (= minor req-minor) (>= micro req-micro))))))
135 (defun sbcl>= (req-major req-minor req-micro)
136 (declare (ignore req-major req-minor req-micro))