| 1 | (import scheme) |
| 2 | (import (chicken base)) |
| 3 | (import (chicken file)) |
| 4 | (import (chicken io)) |
| 5 | (import (chicken format)) |
| 6 | (import (chicken pathname)) |
| 7 | (import (chicken platform)) |
| 8 | (import (chicken process)) |
| 9 | (import (chicken process-context)) |
| 10 | (import (chicken string)) |
| 11 | |
| 12 | (define blackhole-device |
| 13 | (cond-expand |
| 14 | ((or unix cygwin) ">/dev/null 2>/dev/null") |
| 15 | (windows ">NUL 2>NUL"))) |
| 16 | |
| 17 | (define object-extension |
| 18 | (cond-expand |
| 19 | (unix "o") |
| 20 | (windows "obj"))) |
| 21 | |
| 22 | (define executable-extension |
| 23 | (cond-expand |
| 24 | (unix "") |
| 25 | (windows "exe"))) |
| 26 | |
| 27 | ;; adapted from egg-compile.scm |
| 28 | (define (system+ str) |
| 29 | (system (if (and (eq? (software-type) 'windows) |
| 30 | (not (eq? (software-version) 'cygwin)) |
| 31 | (positive? (string-length str)) |
| 32 | (char=? #\" (string-ref str 0))) |
| 33 | (string-append "\"" str "\"") |
| 34 | str))) |
| 35 | |
| 36 | (define cflags #f) |
| 37 | (define ldflags #f) |
| 38 | |
| 39 | (define (capture command) |
| 40 | (string-chomp |
| 41 | (with-input-from-pipe command (cut read-string #f)))) |
| 42 | |
| 43 | ;; Determine if pkg-config exists and knows about libpq |
| 44 | (cond |
| 45 | ((and (get-environment-variable "PG_CFLAGS") |
| 46 | (get-environment-variable "PG_LDFLAGS")) |
| 47 | (set! cflags (get-environment-variable "PG_CFLAGS")) |
| 48 | (set! ldflags (get-environment-variable "PG_LDFLAGS"))) |
| 49 | ((zero? (system+ (format "pkg-config --exists libpq ~a" blackhole-device))) |
| 50 | (set! cflags (capture "pkg-config --cflags libpq")) |
| 51 | (set! ldflags (capture "pkg-config --libs libpq"))) |
| 52 | ((zero? (system+ (format "pg_config --version ~a" blackhole-device))) |
| 53 | (set! cflags (format "-I~a" (capture "pg_config --includedir"))) |
| 54 | (set! ldflags (format "-L~a -Wl,-rpath ~a -lpq" |
| 55 | (capture "pg_config --libdir") |
| 56 | (capture "pg_config --libdir")))) |
| 57 | (else |
| 58 | (print "Please ensure that libpq and its headers are installed. " |
| 59 | "Either install `pkg-config` or `pg_config` to auto-detect " |
| 60 | "flags or set them manually in PG_CFLAGS and PG_LDFLAGS.") |
| 61 | (exit 1))) |
| 62 | |
| 63 | (define csc (get-environment-variable "CHICKEN_CSC")) |
| 64 | (define known-features '("escape-identifier" "connectdb-params" |
| 65 | "diag-query-position" "diag-schema-info")) |
| 66 | (define feature-flags "") |
| 67 | |
| 68 | (for-each |
| 69 | (lambda (feature) |
| 70 | (when (zero? (system+ (format "~a -C ~a -L ~a ~a ~a" |
| 71 | (qs csc) (qs cflags) (qs ldflags) |
| 72 | (qs (make-pathname "feature-tests" feature "c")) |
| 73 | blackhole-device))) |
| 74 | (set! feature-flags (string-append feature-flags " -Dhas-" feature))) |
| 75 | (delete-file* |
| 76 | (make-pathname "feature-tests" feature object-extension)) |
| 77 | (delete-file* |
| 78 | (make-pathname "feature-tests" feature executable-extension))) |
| 79 | known-features) |
| 80 | |
| 81 | (define cmdline |
| 82 | (format "~a ~a -C ~a -L ~a ~a" |
| 83 | (qs csc) feature-flags (qs cflags) (qs ldflags) |
| 84 | (string-intersperse (map qs (command-line-arguments)) " "))) |
| 85 | |
| 86 | (let ((status (system+ cmdline))) |
| 87 | (when (not (zero? status)) |
| 88 | (error "shell invocation failed with non-zero return status" cmdline status))) |