X-Git-Url: https://git.distorted.org.uk/~mdw/clg/blobdiff_plain/560af5c515eb5b6206040a9334de4254d2650147..53a94300685e9c34f59fa8889b22b0cbdd612428:/tools/config.lisp diff --git a/tools/config.lisp b/tools/config.lisp index fa335fa..1623a57 100644 --- a/tools/config.lisp +++ b/tools/config.lisp @@ -1,16 +1,83 @@ -(defun configure-cflags (config-program) +(defparameter *pkg-config* "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))))) + + +(defun run-pkg-config (package error &rest options) (let ((process (run-program - config-program '("--cflags") :wait t :output :stream))) + *pkg-config* (cons package options) :wait t :output :stream))) (unless process - (error "Unable to run %A" config-program)) - (labels ((split (string) - (let ((position (position #\sp string))) - (if position - (cons - (subseq string 0 position) - (split (subseq string (1+ position)))) - (list string))))) - (prog1 - (split (read-line (process-output process))) - (process-close process))))) + (error "Unable to run ~A" *pkg-config*)) + (let ((exit-code (process-exit-code process))) + (unless (or (not error) (zerop exit-code)) + (error + (or + (read-string (process-error process) nil) + (format nil "~A terminated with exit code ~A" + *pkg-config* exit-code)))) + (let ((output (read-lines (process-output process)))) + (process-close process) + (values output exit-code))))) + + +(defun pkg-cflags (package) + (split-string (first (run-pkg-config package t "--cflags")))) + + +(defun pkg-exists-p (package &key version atleast-version max-version + ( error t)) + (let ((version-check + (cond + (version (format nil "= ~A" version)) + (atleast-version (format nil ">= ~A" atleast-version)) + (max-version (format nil "<= ~A" max-version)) + (t "")))) + (if error + (progn + (run-pkg-config package t "--print-errors" "--exists" version-check) + t) + (multiple-value-bind (output exit-code) + (run-pkg-config package nil "--exists" version-check) + (declare (ignore output)) + (zerop exit-code))))) + + +(defun pkg-version (package) + (first (run-pkg-config package t "--modversion"))) + + +(defun pkg-variable (package variable) + (first (run-pkg-config package t "--variable" variable)))