;;; What sexc makes of its command line. ;;; ;;; Everything after the first `--' is the C compiler's, word for word; ;;; `-o -' is stdout; --depfile writes a make rule. Each shows only in ;;; what a sexc process hands on, so each is checked on one. `cc' here ;;; stands in for the compiler and writes down the arguments it got. ;;; ;;; make check-command-line (import scheme (chicken base) (chicken file) (chicken io) (chicken pathname) (chicken process) (chicken process-context) (chicken string) srfi-1 test) (define sexc (or (get-environment-variable "SEXC") "../../sexc")) (define scratch (create-temporary-directory)) (define (built name) (make-pathname scratch name)) (define cc-arguments-file (built "cc-arguments")) (set-environment-variable! "CC_ARGUMENTS" cc-arguments-file) (define cc-option (string-append "--c-compiler=" (make-pathname (current-directory) "cc"))) ;;; The lines sexc prints when run with ARGS and INPUT on its stdin, or ;;; #f when it fails (define (output-with-input input . args) (let ((proc (process sexc args))) (display input (process-input-port proc)) (close-output-port (process-input-port proc)) (let ((lines (read-lines (process-output-port proc)))) (process-wait proc) (and (process-returned-normally? proc) (zero? (process-exit-status proc)) lines)))) (define (output-of . args) (apply output-with-input "" args)) (define (cc-arguments) (call-with-input-file cc-arguments-file read-lines)) ;;; ("-o" "a" "/tmp/x.c" "-lm") -> ("-lm") (define (past-c-file arguments) (cdr (find-tail (lambda (a) (equal? (pathname-extension a) "c")) arguments))) ;;; What the compiler got past sexc's own -o and .c file, when sexc ;;; compiles with ARGS (define (cc-tail . args) (and (apply output-of cc-option args) (past-c-file (cc-arguments)))) (define hello (string-intersperse (call-with-input-file "hello.sex" read-lines) "\n")) (test-group "--" ;; `OpenGL' has no dash, and was once dropped for it, leaving ;; -framework to swallow whatever came next (test "what follows reaches the compiler as written" '("-framework" "OpenGL" "-Wall") (cc-tail "hello.sex" "-o" (built "a") "--" "-framework" "OpenGL" "-Wall")) (test "what precedes stays with sexc" (list "-o" (built "a")) (begin (cc-tail "hello.sex" "-o" (built "a") "--" "-lm") (take (cc-arguments) 2))) (test "without one the compiler gets nothing more" '() (cc-tail "hello.sex" "-o" (built "a"))) (test "nor after a trailing one" '() (cc-tail "hello.sex" "-o" (built "a") "--")) ;; ld takes a `--' of its own (test "only the first one separates" '("-Wl,--as-needed" "--" "-lm") (cc-tail "hello.sex" "-o" (built "a") "--" "-Wl,--as-needed" "--" "-lm")) (test "a file after it is the compiler's, and the source is stdin" '("extra.o") (and (output-with-input hello cc-option "-o" (built "a") "--" "extra.o") (past-c-file (cc-arguments))))) ;;; getopt-long reads a lone `-' as an option, and refused `-o -' (test-group "-o -" (test-assert "writes the C to stdout" (member "int main (void) {" (or (output-of "hello.sex" "-C" "-o" "-") '()))) (test-assert "and so does --output -" (member "int main (void) {" (or (output-of "hello.sex" "-C" "--output" "-") '())))) ;;; The rules for the modules a unit imports are checked in tests/modules (test-group "--depfile" (copy-file "hello.sex" (built "a b.sex")) (test "a space is escaped, the way make reads it" (list (string-append (built "a\\ b.c") ": " (built "a\\ b.sex"))) (and (output-of (built "a b.sex") "-C" "-o" (built "a b.c") (string-append "--depfile=" (built "a b.d"))) (call-with-input-file (built "a b.d") read-lines))) (test "stdin is no file to depend on" (list (string-append (built "hello.c") ":")) (and (output-with-input hello "-C" "-o" (built "hello.c") (string-append "--depfile=" (built "hello.d"))) (call-with-input-file (built "hello.d") read-lines)))) (delete-directory scratch #t) (test-exit)