split the C compiler's name from its options

SEX_CC="ccache cc" went looking for an executable of that whole name;
it is read as make reads CC now.
This commit is contained in:
2026-10-19 14:48:43 +03:00
parent a53c358113
commit 7973a4331d
3 changed files with 34 additions and 10 deletions

View File

@@ -90,7 +90,8 @@ sexc ./examples/hello-world.sex -o hello
That's it. Now you should have executable named ~hello~ in your That's it. Now you should have executable named ~hello~ in your
directory. Sex uses C under the hood, the default C compiler is ~cc~, directory. Sex uses C under the hood, the default C compiler is ~cc~,
but you can pass any using ~--c-compiler~ option, or by setting but you can pass any using ~--c-compiler~ option, or by setting
~SEX_CC~ environment variable. ~SEX_CC~ environment variable. Either may carry the compiler's own
options, as ~make~'s ~CC~ does: ~SEX_CC="ccache cc"~.
Everything after ~--~ is handed to the C compiler exactly as written: Everything after ~--~ is handed to the C compiler exactly as written:

View File

@@ -213,9 +213,11 @@
(define (compile-to-file sex-forms output args cc-args) (define (compile-to-file sex-forms output args cc-args)
"Hand the generated C to the C compiler. Returns the compiler's exit "Hand the generated C to the C compiler. Returns the compiler's exit
status, which is ours to pass on." status, which is ours to pass on."
(let ((compiler (or (get-arg args 'c-compiler #f) ;; named with its own options, as make's CC is: "ccache cc", "cc -O2"
(get-env-var "SEX_CC") (let ((compiler (let ((words (string-split (or (get-arg args 'c-compiler #f)
"cc")) (get-env-var "SEX_CC")
"cc"))))
(if (null? words) (list "cc") words)))
(out-file (if (eq? output 'default) (out-file (if (eq? output 'default)
"a.out" "a.out"
output)) output))
@@ -229,12 +231,14 @@ status, which is ours to pass on."
(begin (delete-file* c-file) (abort exn)) (begin (delete-file* c-file) (abort exn))
(with-output-to-file c-file (with-output-to-file c-file
(lambda () (emit-c sex-forms))) (lambda () (emit-c sex-forms)))
(let ((proc (process compiler (append (list "-o" out-file) (let ((proc (process (car compiler)
(if (get-arg args 'compile-object #f) (append (cdr compiler)
(list "-c") (list "-o" out-file)
(list)) (if (get-arg args 'compile-object #f)
(list c-file) (list "-c")
cc-args)))) (list))
(list c-file)
cc-args))))
(call-with-values (lambda () (process-wait proc)) (call-with-values (lambda () (process-wait proc))
(lambda (pid normal-exit? status) (lambda (pid normal-exit? status)
(delete-file* c-file) (delete-file* c-file)

View File

@@ -47,6 +47,14 @@
(process-wait proc) (process-wait proc)
(list (process-exit-status proc) out err)))) (list (process-exit-status proc) out err))))
;;; (status) of PROGRAM run on its own
(define (sexc-run-program program)
(let ((proc (process* program (list))))
(close-output-port (process-input-port proc))
(read-string #f (process-output-port proc))
(process-wait proc)
(list (process-exit-status proc))))
;;; Whether sexc exits 0 when run with ARGS, saying nothing of it ;;; Whether sexc exits 0 when run with ARGS, saying nothing of it
(define (sexc-succeeds? . args) (define (sexc-succeeds? . args)
(zero? (system (string-append (string-intersperse (map qs (cons sexc args))) (zero? (system (string-append (string-intersperse (map qs (cons sexc args)))
@@ -108,6 +116,17 @@
(test "and the source is as it was" hello-source (test "and the source is as it was" hello-source
(call-with-input-file "victim.sex" (lambda (p) (read-string #f p)))) (call-with-input-file "victim.sex" (lambda (p) (read-string #f p))))
;;; A compiler is named with its own options, as make's CC is: SEX_CC
;;; "cc -DCODE=7" went looking for an executable of that whole name
(call-with-output-file "code.sex" (lambda (p) (display "(pub fn main () int (return CODE))" p)))
(test "a compiler given with options takes them"
7
(begin
(set-environment-variable! "SEX_CC" "cc -DCODE=7")
(let ((built (sexc-succeeds? "code.sex" "-o" "code")))
(unset-environment-variable! "SEX_CC")
(and built (car (sexc-run-program "./code"))))))
;;; An error is its message: CHICKEN's call history after it is for ;;; An error is its message: CHICKEN's call history after it is for
;;; working on sexc itself, and asked for by SEXC_DEBUG ;;; working on sexc itself, and asked for by SEXC_DEBUG
(test-assert "a missing input says so" (test-assert "a missing input says so"