(defpackage #:pkg-config
- (:use #:common-lisp #+cmu #:ext #+sbcl #:sb-ext)
- (:export #:pkg-cflags #:pkg-libs #:pkg-exists-p #:pkg-version
- #:pkg-variable))
+ (:use #:common-lisp #:clg-utils #+(or cmu clisp) #:ext #+sbcl #:sb-ext)
+ #+sbcl
+ (:import-from #:sb-int #:featurep)
+ (:export #:pkg-cflags #:pkg-libs #:pkg-exists-p #:pkg-version #:pkg-variable)
+ (:export #:featurep #:sbcl>=))
(in-package #:pkg-config)
(defparameter *pkg-config* "/usr/bin/pkg-config")
-(defun split-string (string &key (start 0) (end (length string)))
- (let ((position (position #\sp string :start start :end end)))
- (cond
- ((zerop (- end start)) nil)
- ((not position) (list (subseq string start end)))
- ((= position start) (split-string string :start (1+ start) :end end))
- (t (cons
- (subseq string start position)
- (split-string string :start (1+ position) :end end))))))
-
-
-(defun read-lines (&optional (stream *standard-input*))
- (let ((line (read-line stream nil)))
- (when line
- (cons line (read-lines stream)))))
-
-
-(defun read-string (&optional (stream *standard-input*)
- (delimiter #\newline) (eof-error-p t) eof-value)
- (let ((string (make-array 0 :element-type 'character
- :fill-pointer t :adjustable t)))
- ;; I really need to learn how to use the loop facility
- (labels ((read-chars ()
- (let ((char (read-char stream (and eof-error-p delimiter))))
- (when char
- (vector-push-extend char string)
- (unless (eq char delimiter)
- (read-chars))))))
- (read-chars))
- (cond
- ((not (zerop (length string))) string)
- ((not eof-error-p) eof-value)
- ((error 'end-of-file :stream stream)))))
-
#+(or sbcl cmu)
-(defun run-pkg-config (package error &rest options)
+(defun run-pkg-config (package error-p &rest options)
(let ((process
(run-program
*pkg-config* (cons package options) :wait t :output :stream)))
(unless process
(error "Unable to run ~A" *pkg-config*))
(let ((exit-code (process-exit-code process)))
- (unless (or (not error) (zerop exit-code))
+ (unless (or (not error-p) (zerop exit-code))
(error
(or
- (read-string (process-error process) nil)
+ (format nil "~A: ~{~A~%~}" *pkg-config* (read-lines (process-error process)))
(format nil "~A terminated with exit code ~A"
*pkg-config* exit-code))))
(let ((output (read-lines (process-output process))))
(values output exit-code)))))
#+clisp
-;; I haven't figured out how to do error checking with CLISP's run-program
-(defun run-pkg-config (package error &rest options)
- (declare (ignore error))
- (let ((stream (ext:run-program *pkg-config* :arguments (cons package options) :output :stream)))
- (read-lines stream)))
+(defun run-pkg-config (package error-p &rest options)
+ (let ((outfile (format nil "/tmp/clg-pkg-config-~A-output" (os:process-id)))
+ (errfile (format nil "/tmp/clg-pkg-config-~A-error" (os:process-id))))
+ (unwind-protect
+ (let ((exit-code
+ (run-shell-command
+ (format nil "~A ~A ~{~A ~}2>~A"
+ *pkg-config* package
+ (mapcar #'(lambda (option)
+ (format nil "'~A'" option))
+ options)
+ errfile)
+ :output outfile :if-output-exists :overwrite)))
+ (cond
+ ((= exit-code 127) (error "Unable to run ~A" *pkg-config*))
+ ((and error-p (not (zerop exit-code)))
+ (with-open-file (output errfile)
+ (let ((errmsg (read-lines output)))
+ (error
+ (if (not errmsg)
+ (format nil "~A terminated with exit code ~A" *pkg-config* exit-code)
+ (format nil "~A: ~{~A~%~}" *pkg-config* errmsg))))))
+ (t
+ (values
+ (with-open-file (output outfile)
+ (read-lines output))
+ exit-code))))
+ (progn
+ (delete-file outfile)
+ (delete-file errfile)))))
+
(defun pkg-cflags (package)
(split-string (first (run-pkg-config package t "--cflags"))))
(defun pkg-variable (package variable)
(first (run-pkg-config package t "--variable" variable)))
+
+
+(defun |#?-reader| (stream subchar arg)
+ (declare (ignore subchar arg))
+ (let ((not-p (when (char= (peek-char nil stream) #\-)
+ (read-char stream)))
+ (conditional (read stream t nil t)))
+ (cond
+ (*read-suppress* (read stream t nil t))
+ ((not *read-eval*)
+ (error 'reader-error
+ :format-control "Attempt to read #? while *READ-EVAL* is bound to NIL."
+ :format-arguments nil :stream stream))
+ ((if not-p
+ (eval conditional)
+ (not (eval conditional)))
+ (let ((*read-suppress* t))
+ (read stream t nil t)))))
+ (values))
+
+(set-dispatch-macro-character #\# #\? #'|#?-reader|)
+
+
+#+sbcl
+(progn
+ (defun sbcl-version ()
+ (let ((dot1 (position #\. (lisp-implementation-version)))
+ (dot2 (position #\. (lisp-implementation-version) :from-end t)))
+ (values
+ (parse-integer (lisp-implementation-version) :end dot1)
+ (parse-integer (lisp-implementation-version) :start (1+ dot1) :end dot2)
+ (parse-integer (lisp-implementation-version) :start (1+ dot2)))))
+ (defun sbcl>= (req-major req-minor req-micro)
+ (multiple-value-bind (major minor micro) (sbcl-version)
+ (or
+ (> major req-major)
+ (and (= major req-major) (> minor req-minor))
+ (and (= major req-major) (= minor req-minor) (>= micro req-micro))))))
+
+#-sbcl
+(defun sbcl>= (req-major req-minor req-micro)
+ (declare (ignore req-major req-minor req-micro))
+ nil)