;;; postgresql.setup -*- Scheme -*- (use files utils) (define pkg-config-available? (handle-exceptions exn (begin (print "pkg-config is NOT available. Will use pg_config.") #f) (print "Checking if pkg-config is available...") (system* "pkg-config --version") (print "pkg-config is available.") #t)) (define libpq-flags (if pkg-config-available? '("`pkg-config --cflags libpq`" "`pkg-config --libs libpq`") '(-C -I -C "\"`pg_config --includedir`\"" "-L\"`pg_config --libdir`\"" -rpath "`pg_config --libdir`" -lpq))) (define PQescapeIdentifier-presence-checker #< int main(void) { PGconn *conn = NULL; /* Bork */ return PQescapeIdentifier(conn, "test", 4) != NULL; } EOF ) (define PQconnectdbParams-presence-checker #< int main(void) { const char *kw[] = {"a", NULL}; const char *val[] = {"b", NULL}; return PQconnectdbParams(kw, val, 0) != NULL; } EOF ) (define PG_DIAG_INTERNAL_QUERY+POSITION-checker #< int main(void) { int foo = PG_DIAG_INTERNAL_QUERY; int bar = PG_DIAG_INTERNAL_POSITION; return foo + bar; } EOF ) (define PG_DIAG_SCHEMA_INFO-checker #< int main(void) { int s = PG_DIAG_SCHEMA_NAME; int t = PG_DIAG_TABLE_NAME; int col = PG_DIAG_COLUMN_NAME; int d = PG_DIAG_DATATYPE_NAME; int con = PG_DIAG_CONSTRAINT_NAME; return s + t + col + d + con; } EOF ) (define feature-checks `((has-PQescapeIdentifier . ,PQescapeIdentifier-presence-checker) (has-PQconnectdbParams . ,PQconnectdbParams-presence-checker) (has-PG_DIAG_INTERNAL_QUERY+POSITION . ,PG_DIAG_INTERNAL_QUERY+POSITION-checker) (has-PG_DIAG_SCHEMA_INFO . ,PG_DIAG_SCHEMA_INFO-checker))) (define-syntax try-compile-pgsql-prog (syntax-rules () ((_ str) (begin (with-output-to-file "attempt.c" (lambda () (display str) (newline))) (let ((result (handle-exceptions ex #f (compile "attempt.c" ,@libpq-flags)))) (delete-file* "attempt.c") (delete-file* "attempt.o") (delete-file* "attempt") result))))) (define (check-features) (fold (lambda (feature/test features) (if (try-compile-pgsql-prog (cdr feature/test)) (append! `(-feature ,(car feature/test)) features) features)) '() feature-checks)) (compile -s -O2 -d0 postgresql.scm -j postgresql ,@(check-features) ,@libpq-flags) (compile postgresql.import.scm -s -O2 -d0) (install-extension 'postgresql '("postgresql.so" "postgresql.import.so") '((version "3.9.3") (documentation "postgresql.html")))