-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile-cxx.rkt
executable file
·262 lines (240 loc) · 9.67 KB
/
compile-cxx.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env racket
#lang racket
(define verbose-mode (make-parameter #f))
(define bindir (make-parameter #f))
(define clang-flags (make-parameter null))
(define skeleton-search-paths (make-parameter null))
(define header-search-paths (make-parameter null))
(define skeleton-pairs (make-parameter null))
(define config-file (make-parameter #f))
(define delete-temps (make-parameter #t))
(define lang-ext (make-parameter "c"))
(define make-absolute
(lambda (path)
(if (relative-path? path)
(normalize-path (build-path (current-directory) path))
path)))
(define file-to-compile
(command-line
#:once-each
[("-v" "--verbose") "Compile with verbose messages"
(verbose-mode #t)]
[("-b" "--bin-dir") dir
"The clang bin directory"
(bindir dir)]
[("-t" "--preserve-temps")
"Temporary files are not deleted"
(delete-temps #f)]
#:once-any
[("-c" "--dot-c") "Sets the file extension of expanded source to .c"
(lang-ext "c")]
[("-u" "--dot-cuda") "Sets the file extension of expanded source to .cu"
(display "cu") (newline)
(lang-ext "cu")]
[("-p" "--dot-cplusplus") "Sets the file extension of expanded source to .cpp"
(lang-ext "cpp")]
[("-x" "--dot-x") x
"Sets the file extension of expanded source to a user supplied value"
(lang-ext x)]
#:multi
[("-S" "--skeleton-search-dir") dir
"Add a directory for skeleton search"
(skeleton-search-paths (cons dir (skeleton-search-paths)))]
[("-@" "--skeleton-pair") sym file
"Bind a skeleton lookup to a filename"
(skeleton-pairs (cons (cons sym file) (skeleton-pairs)))]
[("-f" "--clang-flag") cf
"Add a flag for clang to use at compile time"
(clang-flags (cons cf (clang-flags)))]
[("-I" "--include-search-dir") isd
"Add an include search dir. Also added to the compile time flags"
(let
([flag (string-append "-I" isd)])
(header-search-paths (cons flag (header-search-paths)))
(clang-flags (cons flag (clang-flags))))]
#:args (cfg src)
(config-file (path->string (make-absolute cfg)))
(path->string
(make-absolute src))))
(define-values (clang-path clang-fmt-path)
(let*
([env (current-environment-variables)]
[PATH (string->bytes/locale "PATH")]
[old-path (environment-variables-ref env PATH)]
[new-path
(if (bindir)
(string->bytes/locale (bindir))
old-path)])
(environment-variables-set! env PATH new-path)
(let
([clang-path (find-executable-path "clang")]
[clang-fmt-path (find-executable-path "clang-format")])
(environment-variables-set! env PATH old-path)
(values clang-path clang-fmt-path))))
(define sexp-src-raw-port
(let*-values
([(stdout stdin stderr)
(values #f (open-input-file file-to-compile) #f)]
[(command-line)
(list* "-cc1" "-ast-sexp" "-fdiagnostics-show-option" "-fcolor-diagnostics" "-x" "skeletons" (reverse (header-search-paths)))] ; (string-join (reverse ())) need you later
[(sexp-process
stdout _ stderr) ; safe to discard results of stdin because we supplied our own ports
(apply subprocess stdout stdin stderr clang-path command-line)]
[(exit-status error-text)
(begin
(subprocess-wait sexp-process)
(values (subprocess-status sexp-process) (string-trim (port->string stderr))))])
(close-input-port stderr)
(close-input-port stdin)
(unless (equal? error-text "")
(display error-text (current-error-port))
(newline (current-error-port)))
(unless (eq? 0 exit-status)
(exit exit-status))
stdout))
(define full-pairs
(map
(lambda (sk-pair)
(let*-values
([(sym file) (values (car sk-pair) (cdr sk-pair))]
[(full-pair)
(let loop
([work (skeleton-search-paths)])
(if (null? work)
#f
(let*-values
([(head rest) (values (car work) (cdr work))]
[(full-path) (build-path head file)])
(if (file-exists? full-path)
(cons sym
(if (relative-path? full-path)
(build-path (current-directory) full-path)
full-path))
(loop rest)))))])
(if full-pair
full-pair
(begin
(display (~a (list "Unable to find requested file" file "in paths:\n" (skeleton-search-paths))) (current-error-port))
(newline (current-error-port))
(exit 1)))))
(skeleton-pairs)))
(define-values (template-header-port template-footer-port)
(apply
values
(map open-input-string
(list
(string-append
"#lang Cxx\n"
"(translation-unit\n(skeletons:\n"
(string-join
(map
(lambda (pair)
(string-append
"("
(car pair)
" \""
(path->string (cdr pair))
"\")\n"))
full-pairs))
"\"" (config-file) "\")\n")
")"))))
(display "Constructed S-Exp") (newline)
(let* ([syntax-port (input-port-append #t template-header-port sexp-src-raw-port template-footer-port)]
[tmp-file (make-temporary-file (string-append file-to-compile "~a.rkt"))]
[sexp-contents
(call-with-output-file tmp-file
(lambda (out)
(let ([copy-string (open-output-string "SExp Source")])
(copy-port syntax-port out copy-string)
(get-output-string copy-string)))
#:exists 'replace)])
(define expanded-src-port
(let*-values
([(stdout stdin stderr) (values #f #f #f)]
[(racket-path command-line)
(values (find-executable-path "racket");(apply build-path (map find-system-path '(orig-dir exec-file)))
(list tmp-file))]
[(sexp-process
stdout stdin stderr)
(apply subprocess stdout stdin stderr racket-path command-line)]
[(exit-status error-text)
(begin
(subprocess-wait sexp-process)
(values (subprocess-status sexp-process) (string-trim (port->string stderr))))])
(close-input-port stderr)
(close-output-port stdin)
(when (delete-temps)
(delete-file tmp-file))
(unless (equal? error-text "")
(display error-text (current-error-port))
(newline (current-error-port)))
(unless (eq? 0 exit-status)
(display "
Failed to expand the following program:
=======================================\n"
(current-error-port))
(display sexp-contents (current-error-port))
(newline (current-error-port))
(exit exit-status))
stdout))
(define pretty-src-port
(let*-values
([(stdout stdin stderr) (values #f expanded-src-port #f)]
[(command-line) null]
[(sexp-process
stdout _ stderr)
(apply subprocess stdout stdin stderr clang-fmt-path command-line)]
[(exit-status error-text)
(begin
(subprocess-wait sexp-process)
(values (subprocess-status sexp-process) (string-trim (port->string stderr))))])
(close-input-port stderr)
(close-input-port stdin)
(unless (equal? error-text "")
(display error-text (current-error-port))
(newline (current-error-port)))
(unless (eq? 0 exit-status)
(exit exit-status))
stdout))
(define clang-finished-port
(let*-values
([(tmp-file) (make-temporary-file (string-append file-to-compile "~a." (lang-ext)))]
[(pretty-contents)
(call-with-output-file tmp-file
(lambda (out)
(let ([copy-string (open-output-string "Pretty Source")])
(copy-port pretty-src-port out copy-string)
(get-output-string copy-string)))
#:exists 'replace)]
[(stdout stdin stderr) (values #f #f #f)]
[(command-line) (reverse (cons tmp-file (clang-flags)))]
[(sexp-process
stdout stdin stderr)
(begin
(display clang-path) (display " ") (display (~a command-line)) (newline)
(apply subprocess stdout stdin stderr clang-path command-line))]
[(exit-status error-text)
(begin
(subprocess-wait sexp-process)
(values (subprocess-status sexp-process) (string-trim (port->string stderr))))])
(close-input-port stderr)
(close-output-port stdin)
(when (delete-temps)
(delete-file tmp-file))
(unless (equal? error-text "")
(display error-text (current-error-port))
(newline (current-error-port)))
(unless (eq? 0 exit-status)
(display "
Failed to compile the following program:
=======================================\n"
(current-error-port))
(display pretty-contents (current-error-port))
(newline (current-error-port))
(exit exit-status))
stdout))
(display (port->string clang-finished-port))
(newline)
(close-input-port clang-finished-port))
(display "Done")
(newline)