From d66ffd95a803787eee800824df34260a8a090a38 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Mon, 19 Oct 2026 14:48:43 +0300 Subject: [PATCH] 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. --- Readme.org | 3 ++- sexc.scm | 22 +++++++++++++--------- tests/exit-code/check.scm | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/Readme.org b/Readme.org index 251a2f6..31eec62 100644 --- a/Readme.org +++ b/Readme.org @@ -90,7 +90,8 @@ sexc ./examples/hello-world.sex -o hello 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~, 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: diff --git a/sexc.scm b/sexc.scm index 601a6ca..8c459f9 100644 --- a/sexc.scm +++ b/sexc.scm @@ -217,9 +217,11 @@ (define (compile-to-file sex-forms output args cc-args) "Hand the generated C to the C compiler. Returns the compiler's exit status, which is ours to pass on." - (let ((compiler (or (get-arg args 'c-compiler #f) - (get-env-var "SEX_CC") - "cc")) + ;; named with its own options, as make's CC is: "ccache cc", "cc -O2" + (let ((compiler (let ((words (string-split (or (get-arg args 'c-compiler #f) + (get-env-var "SEX_CC") + "cc")))) + (if (null? words) (list "cc") words))) (out-file (if (eq? output 'default) "a.out" output)) @@ -233,12 +235,14 @@ status, which is ours to pass on." (begin (delete-file* c-file) (abort exn)) (with-output-to-file c-file (lambda () (emit-c sex-forms))) - (let ((proc (process compiler (append (list "-o" out-file) - (if (get-arg args 'compile-object #f) - (list "-c") - (list)) - (list c-file) - cc-args)))) + (let ((proc (process (car compiler) + (append (cdr compiler) + (list "-o" out-file) + (if (get-arg args 'compile-object #f) + (list "-c") + (list)) + (list c-file) + cc-args)))) (call-with-values (lambda () (process-wait proc)) (lambda (pid normal-exit? status) (delete-file* c-file) diff --git a/tests/exit-code/check.scm b/tests/exit-code/check.scm index b0259f0..de01690 100644 --- a/tests/exit-code/check.scm +++ b/tests/exit-code/check.scm @@ -45,6 +45,14 @@ (process-wait proc) (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 (define (sexc-succeeds? . args) (let ((proc (process* sexc args))) @@ -105,6 +113,17 @@ (test "and the source is as it was" hello-source (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 ;;; working on sexc itself, and asked for by SEXC_DEBUG (test-assert "a missing input says so"