pass all args after '--' to C compiler verbatim
This commit is contained in:
@@ -49,6 +49,12 @@ 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.
|
||||||
|
|
||||||
|
Everything after ~--~ is handed to the C compiler exactly as written:
|
||||||
|
|
||||||
|
#+begin_src shell
|
||||||
|
sexc example/sdl3-triangle.sex -o triangle -- `pkg-config --cflags --libs sdl3` -framework OpenGL
|
||||||
|
#+end_src
|
||||||
|
|
||||||
** Example
|
** Example
|
||||||
An example of Sex source:
|
An example of Sex source:
|
||||||
#+begin_src scheme
|
#+begin_src scheme
|
||||||
|
|||||||
29
sexc.scm
29
sexc.scm
@@ -16,7 +16,6 @@
|
|||||||
reader
|
reader
|
||||||
semen
|
semen
|
||||||
srfi-1 ; list routines
|
srfi-1 ; list routines
|
||||||
srfi-13
|
|
||||||
utils)
|
utils)
|
||||||
|
|
||||||
;;; Main function facilities
|
;;; Main function facilities
|
||||||
@@ -73,12 +72,24 @@
|
|||||||
(if arg (cdr arg)
|
(if arg (cdr arg)
|
||||||
default)))
|
default)))
|
||||||
|
|
||||||
|
;;; Everything before the first `--' is ours to parse, everything after
|
||||||
|
;;; is handed to the C compiler verbatim
|
||||||
|
|
||||||
|
(define (separator? a)
|
||||||
|
(string=? a "--"))
|
||||||
|
|
||||||
|
(define (args-before-separator argv)
|
||||||
|
(take-while (complement separator?) argv))
|
||||||
|
|
||||||
|
(define (args-after-separator argv)
|
||||||
|
(let ((tail (drop-while (complement separator?) argv)))
|
||||||
|
(if (null? tail)
|
||||||
|
(list)
|
||||||
|
(cdr tail))))
|
||||||
|
|
||||||
(define (get-rest-args args)
|
(define (get-rest-args args)
|
||||||
(cdr (assoc '@ args)))
|
(cdr (assoc '@ args)))
|
||||||
|
|
||||||
(define (get-c-compiler-args args)
|
|
||||||
(filter (fn (string-prefix? "-" x)) (get-rest-args args)))
|
|
||||||
|
|
||||||
(define (line-directives-arg args)
|
(define (line-directives-arg args)
|
||||||
(let ((v (get-arg args 'line-directives "statement")))
|
(let ((v (get-arg args 'line-directives "statement")))
|
||||||
(cond ((equal? v "statement") 'statement)
|
(cond ((equal? v "statement") 'statement)
|
||||||
@@ -105,7 +116,7 @@
|
|||||||
(map pp sex-forms)
|
(map pp sex-forms)
|
||||||
(emit-c sex-forms)))))
|
(emit-c sex-forms)))))
|
||||||
|
|
||||||
(define (compile-to-file sex-forms output args)
|
(define (compile-to-file sex-forms output args cc-args)
|
||||||
(let ((compiler (or (get-arg args 'c-compiler #f)
|
(let ((compiler (or (get-arg args 'c-compiler #f)
|
||||||
(get-env-var "SEX_CC")
|
(get-env-var "SEX_CC")
|
||||||
"cc"))
|
"cc"))
|
||||||
@@ -120,7 +131,7 @@
|
|||||||
(list "-c")
|
(list "-c")
|
||||||
(list))
|
(list))
|
||||||
(list "-") ; read stdin
|
(list "-") ; read stdin
|
||||||
(get-c-compiler-args args))))
|
cc-args)))
|
||||||
(cc-stdin (process-input-port proc)))
|
(cc-stdin (process-input-port proc)))
|
||||||
(with-output-to-port cc-stdin
|
(with-output-to-port cc-stdin
|
||||||
(lambda () (emit-c sex-forms)))
|
(lambda () (emit-c sex-forms)))
|
||||||
@@ -146,7 +157,9 @@
|
|||||||
(typedef i64 int64-t)))
|
(typedef i64 int64-t)))
|
||||||
|
|
||||||
(define (main)
|
(define (main)
|
||||||
(let* ((raw-args (command-line-arguments))
|
(let* ((argv (command-line-arguments))
|
||||||
|
(raw-args (args-before-separator argv))
|
||||||
|
(cc-args (args-after-separator argv))
|
||||||
(args (getopt-long raw-args
|
(args (getopt-long raw-args
|
||||||
opts-grammar))
|
opts-grammar))
|
||||||
(output (get-arg args 'output 'default))
|
(output (get-arg args 'output 'default))
|
||||||
@@ -185,4 +198,4 @@
|
|||||||
;; Emit processed and macro-expanded sex code, or emit C code
|
;; Emit processed and macro-expanded sex code, or emit C code
|
||||||
(emit-c-or-sex sex-forms output args)
|
(emit-c-or-sex sex-forms output args)
|
||||||
;; Compile file!
|
;; Compile file!
|
||||||
(compile-to-file sex-forms output args)))))))
|
(compile-to-file sex-forms output args cc-args)))))))
|
||||||
|
|||||||
Reference in New Issue
Block a user