1
0
forked from alex-eg/sex

check sexc's command line on a sexc process

The -- split, the -o - spelling and the dependency rule were exported to
be unit tested, as lists and strings sexc might never hand on; a stand-in
cc and the depfile show what it does hand on.
This commit is contained in:
2026-10-20 11:51:31 +03:00
parent ab881e4c31
commit 600b4801de
8 changed files with 126 additions and 91 deletions

View File

@@ -1,83 +0,0 @@
;;; Splitting the command line at `--'.
;;;
;;; getopt-long cannot do this: it consumes the separator and merges
;;; everything after it into `@' alongside the input file. So sexc
;;; splits the raw argv first, and only the head is parsed as options.
;;; Everything else reaches the C compiler exactly as written --
;;; including the words that do not start with a dash, which a previous
;;; leading-dash heuristic used to drop.
(import sexc)
(define full '("foo.sex" "-o" "bar" "--" "-framework" "OpenGL" "-Wall"))
(test-group "argument separator"
(test "options and input file stay with sexc"
'("foo.sex" "-o" "bar")
(args-before-separator full))
(test "the tail reaches the compiler verbatim"
'("-framework" "OpenGL" "-Wall")
(args-after-separator full))
;; The case that motivated this: `OpenGL' has no leading dash and was
;; silently dropped, leaving `-framework' to swallow whatever flag
;; came next.
(test "a word without a dash survives"
'("-framework" "OpenGL")
(args-after-separator '("x.sex" "--" "-framework" "OpenGL")))
(test "no separator means nothing for the compiler"
'()
(args-after-separator '("foo.sex" "-o" "bar")))
(test "no separator leaves every argument with sexc"
'("foo.sex" "-o" "bar")
(args-before-separator '("foo.sex" "-o" "bar")))
(test "a trailing separator is allowed"
'()
(args-after-separator '("foo.sex" "--")))
(test "a leading separator leaves no input file"
'()
(args-before-separator '("--" "-lm")))
;; Only the first `--' separates; a later one is an ordinary compiler
;; argument (ld takes several).
(test "only the first separator counts"
'("-Wl,--as-needed" "--" "-lm")
(args-after-separator '("x.sex" "--" "-Wl,--as-needed" "--" "-lm")))
(test "an empty command line is handled"
'()
(args-before-separator '()))
;; getopt-long reads a lone `-' as an option and refused `-o -', so
;; it is handed the one spelling it takes a dash in
(test "-o - reaches getopt-long as a value"
'("x.sex" "--output=-" "-C")
(spell-stdout-output '("x.sex" "-o" "-" "-C")))
(test "and so does --output -"
'("--output=-")
(spell-stdout-output '("--output" "-")))
(test "any other output is left alone"
'("-o" "out" "-")
(spell-stdout-output '("-o" "out" "-"))))
;;; --depfile: an import pastes another file's forms into the output, so
;;; the output depends on that file as a C object does on a header
(test-group "dependency rule"
(test "the target, the source and the modules, then a rule per module"
"app.o: app.sex /m/greet.sex /m/ledger.sex\n\n/m/greet.sex:\n\n/m/ledger.sex:\n"
(dependency-rule "app.o" "app.sex" '("/m/greet.sex" "/m/ledger.sex")))
(test "no modules, no rules of their own"
"app.o: app.sex\n"
(dependency-rule "app.o" "app.sex" '()))
(test "a space is escaped, the way make reads it"
"a\\ b.o: a\\ b.sex\n"
(dependency-rule "a b.o" "a b.sex" '()))
(test "stdin is no file to depend on"
"app.o: /m/greet.sex\n\n/m/greet.sex:\n"
(dependency-rule "app.o" #f '("/m/greet.sex"))))

4
tests/command-line/cc Executable file
View File

@@ -0,0 +1,4 @@
#!/bin/sh
# Stands in for the C compiler: writes the arguments it was given, one
# to a line, to $CC_ARGUMENTS
printf '%s\n' "$@" > "$CC_ARGUMENTS"

View File

@@ -0,0 +1,108 @@
;;; 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)

View File

@@ -0,0 +1,2 @@
(pub fn main () int
(return 0))

View File

@@ -72,6 +72,14 @@
(irregex-match ".*/greet-app: greet-app\\.sex .*/greet\\.sex .*/ledger\\.sex"
(call-with-input-file (built "greet-app.d") read-line)))
;;; Each module has a rule of its own, so make takes one deleted since as
;;; out of date rather than as a file it has no rule to make
(test "and gives each a rule of its own"
'("greet.sex:" "ledger.sex:")
(filter-map (lambda (line)
(and (irregex-match ".*\\.sex:" line) (pathname-strip-directory line)))
(call-with-input-file (built "greet-app.d") read-lines)))
(test "the app runs through both"
'("hello, world"
"hello, sex"

View File

@@ -10,7 +10,6 @@
(include-relative "line-directives.scm")
(include-relative "codegen.scm")
(include-relative "encapsulation.scm")
(include-relative "args.scm")
(include-relative "module-path.scm")
(include-relative "types.scm")
(include-relative "infer.scm")