Patch from Peter De Wachter to improve the SBCL version detection
[clg] / tools / config.lisp
1 (defpackage #:pkg-config
2 (:use #:common-lisp #:clg-utils #+(or cmu clisp) #:ext #+sbcl #:sb-ext)
3 #+sbcl
4 (:import-from #:sb-int #:featurep)
5 (:export #:pkg-cflags #:pkg-libs #:pkg-exists-p #:pkg-version #:pkg-variable)
6 (:export #:featurep #:sbcl>=))
7
8 (in-package #:pkg-config)
9
10 (defparameter *pkg-config* "/usr/bin/pkg-config")
11
12
13 #+(or sbcl cmu)
14 (defun run-pkg-config (package error-p &rest options)
15 (let ((process
16 (run-program
17 *pkg-config* (cons package options) :wait t :output :stream)))
18 (unless process
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))
22 (error
23 (or
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)))))
30
31 #+clisp
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))))
35 (unwind-protect
36 (let ((exit-code
37 (run-shell-command
38 (format nil "~A ~A ~{~A ~}2>~A"
39 *pkg-config* package
40 (mapcar #'(lambda (option)
41 (format nil "'~A'" option))
42 options)
43 errfile)
44 :output outfile :if-output-exists :overwrite)))
45 (cond
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)))
50 (error
51 (if (not errmsg)
52 (format nil "~A terminated with exit code ~A" *pkg-config* exit-code)
53 (format nil "~A: ~{~A~%~}" *pkg-config* errmsg))))))
54 (t
55 (values
56 (with-open-file (output outfile)
57 (read-lines output))
58 exit-code))))
59 (progn
60 (delete-file outfile)
61 (delete-file errfile)))))
62
63
64 (defun pkg-cflags (package)
65 (split-string (first (run-pkg-config package t "--cflags"))))
66
67 (defun pkg-libs (package)
68 (split-string (first (run-pkg-config package t "--libs"))))
69
70
71 (defun pkg-exists-p (package &key version atleast-version max-version
72 ( error t))
73 (let ((version-check
74 (cond
75 (version (format nil "= ~A" version))
76 (atleast-version (format nil ">= ~A" atleast-version))
77 (max-version (format nil "<= ~A" max-version))
78 (t ""))))
79 (if error
80 (progn
81 (run-pkg-config package t "--print-errors" "--exists" version-check)
82 t)
83 (multiple-value-bind (output exit-code)
84 (run-pkg-config package nil "--exists" version-check)
85 (declare (ignore output))
86 (zerop exit-code)))))
87
88
89 (defun pkg-version (package)
90 (first (run-pkg-config package t "--modversion")))
91
92
93 (defun pkg-variable (package variable)
94 (first (run-pkg-config package t "--variable" variable)))
95
96
97 (defun |#?-reader| (stream subchar arg)
98 (declare (ignore subchar arg))
99 (let ((not-p (when (char= (peek-char nil stream) #\-)
100 (read-char stream)))
101 (conditional (read stream t nil t)))
102 (cond
103 (*read-suppress* (read stream t nil t))
104 ((not *read-eval*)
105 (error 'reader-error
106 :format-control "Attempt to read #? while *READ-EVAL* is bound to NIL."
107 :format-arguments nil :stream stream))
108 ((if not-p
109 (eval conditional)
110 (not (eval conditional)))
111 (let ((*read-suppress* t))
112 (read stream t nil t)))))
113 (values))
114
115 (set-dispatch-macro-character #\# #\? #'|#?-reader|)
116
117
118 #+sbcl
119 (progn
120 (defun sbcl-version ()
121 (let* ((dot1 (position #\. (lisp-implementation-version)))
122 (dot2 (position #\. (lisp-implementation-version) :start (1+ dot1))))
123 (values
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) :junk-allowed t))))
127 (defun sbcl>= (req-major req-minor req-micro)
128 (multiple-value-bind (major minor micro) (sbcl-version)
129 (or
130 (> major req-major)
131 (and (= major req-major) (> minor req-minor))
132 (and (= major req-major) (= minor req-minor) (>= micro req-micro))))))
133
134 #-sbcl
135 (defun sbcl>= (req-major req-minor req-micro)
136 (declare (ignore req-major req-minor req-micro))
137 nil)