Porting to CLISP
[clg] / tools / config.lisp
1 (defpackage #:pkg-config
2 (:use #:common-lisp #+cmu #:ext #+sbcl #:sb-ext)
3 (:export #:pkg-cflags #:pkg-libs #:pkg-exists-p #:pkg-version
4 #:pkg-variable))
5
6 (in-package #:pkg-config)
7
8 (defparameter *pkg-config* "/usr/bin/pkg-config")
9
10 (defun split-string (string &key (start 0) (end (length string)))
11 (let ((position (position #\sp string :start start :end end)))
12 (cond
13 ((zerop (- end start)) nil)
14 ((not position) (list (subseq string start end)))
15 ((= position start) (split-string string :start (1+ start) :end end))
16 (t (cons
17 (subseq string start position)
18 (split-string string :start (1+ position) :end end))))))
19
20
21 (defun read-lines (&optional (stream *standard-input*))
22 (let ((line (read-line stream nil)))
23 (when line
24 (cons line (read-lines stream)))))
25
26
27 (defun read-string (&optional (stream *standard-input*)
28 (delimiter #\newline) (eof-error-p t) eof-value)
29 (let ((string (make-array 0 :element-type 'character
30 :fill-pointer t :adjustable t)))
31 ;; I really need to learn how to use the loop facility
32 (labels ((read-chars ()
33 (let ((char (read-char stream (and eof-error-p delimiter))))
34 (when char
35 (vector-push-extend char string)
36 (unless (eq char delimiter)
37 (read-chars))))))
38 (read-chars))
39 (cond
40 ((not (zerop (length string))) string)
41 ((not eof-error-p) eof-value)
42 ((error 'end-of-file :stream stream)))))
43
44
45 #+(or sbcl cmu)
46 (defun run-pkg-config (package error &rest options)
47 (let ((process
48 (run-program
49 *pkg-config* (cons package options) :wait t :output :stream)))
50 (unless process
51 (error "Unable to run ~A" *pkg-config*))
52 (let ((exit-code (process-exit-code process)))
53 (unless (or (not error) (zerop exit-code))
54 (error
55 (or
56 (read-string (process-error process) nil)
57 (format nil "~A terminated with exit code ~A"
58 *pkg-config* exit-code))))
59 (let ((output (read-lines (process-output process))))
60 (process-close process)
61 (values output exit-code)))))
62
63 #+clisp
64 ;; I haven't figured out how to do error checking with CLISP's run-program
65 (defun run-pkg-config (package error &rest options)
66 (declare (ignore error))
67 (let ((stream (ext:run-program *pkg-config* :arguments (cons package options) :output :stream)))
68 (read-lines stream)))
69
70 (defun pkg-cflags (package)
71 (split-string (first (run-pkg-config package t "--cflags"))))
72
73 (defun pkg-libs (package)
74 (split-string (first (run-pkg-config package t "--libs"))))
75
76
77 (defun pkg-exists-p (package &key version atleast-version max-version
78 ( error t))
79 (let ((version-check
80 (cond
81 (version (format nil "= ~A" version))
82 (atleast-version (format nil ">= ~A" atleast-version))
83 (max-version (format nil "<= ~A" max-version))
84 (t ""))))
85 (if error
86 (progn
87 (run-pkg-config package t "--print-errors" "--exists" version-check)
88 t)
89 (multiple-value-bind (output exit-code)
90 (run-pkg-config package nil "--exists" version-check)
91 (declare (ignore output))
92 (zerop exit-code)))))
93
94
95 (defun pkg-version (package)
96 (first (run-pkg-config package t "--modversion")))
97
98
99 (defun pkg-variable (package variable)
100 (first (run-pkg-config package t "--variable" variable)))