-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmds.rkt
380 lines (329 loc) · 16.2 KB
/
cmds.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
;; The MIT License (MIT)
;;
;; Copyright (c) 2015 Matthew C. Jadud
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
#lang racket
(require "util.rkt"
"debug.rkt"
"syscalls.rkt"
"config.rkt"
"constants.rkt")
(provide compile
binhex
output-exists?
exe-in-session
compile
plink
ihexmerge
)
;; RUNNING COMMANDS
;; We'll define commands as S-expressions. The language
;; looks like
;; (cmd -flag0 (-flag1 value1) (= -p1 value2))
;; which becomes
;; "cmd -flag0 -flag1 value1 -p1=value2"
;; Note we don't insert hyphens, but we make sure
;; spaces come out right.
;
;
;
;
;
;
;
;
; ;;;;; ;;;; ;;; ;;; ;;;;;;; ;; ;; ;;;;;;;;
; ;; ; ;;; ;; ;;; ;;; ;; ;; ;; ;; ;;
; ;; ; ;; ;;; ;;;; ;; ; ;; ;; ;;
; ;; ;; ;; ;;;; ; ;; ;; ; ;; ;; ;;
; ;; ;; ;; ;; ; ; ;; ;; ;; ;; ;; ;;
; ;; ; ;; ;; ;; ;; ;; ;;;;;;; ;; ;; ;;;;;;;
; ;; ;; ;; ;; ; ; ;; ;; ;; ;; ;;
; ;; ;; ;; ;; ; ; ;; ;; ;; ;; ;;
; ;; ; ;; ;; ;;; ;; ;; ;; ;; ;;
; ;; ; ;;; ;; ;; ;; ;; ;; ;; ;; ;;
; ;;;;; ;;;; ;; ;; ;; ;; ;; ;;;;;;; ;;;;;;;;
;
;
;
;
;
(define (compile conf)
(debug 'COMPILE "***** COMPILE *****")
(define cmd (compile-cmd conf (hash-ref conf "main")))
(define session-dir (hash-ref conf "session-id"))
(parameterize ([current-directory (build-path (conf-get "temp-dir") session-dir)])
;; Give us a place to put everything.
(unless (directory-exists? (current-directory))
(make-directory* (current-directory)))
;; Dump all the files in the configuration
(for ([file (hash-ref conf "files")])
(with-output-to-file
(build-path (current-directory) file)
(λ ()
(printf "~a" (hash-ref conf file)))))
(debug 'COMPILE "Current directory: ~a~n" (current-directory))
(debug 'COMPILE "~n****~n~a~n****~n" cmd)
(let-values ([(stdout stdin pid stderr control)
(apply values (process cmd))])
(define result (make-hash))
(let loop ([status (control 'status)])
(case status
[(running) (sleep 1)
(debug 'COMPILE "Running...")
(loop (control 'status))]
[(done-ok)
;; FIXME Some kind of response
(hash-set! result "code" OK.COMPILE)
(close-input-port stdout)
(close-input-port stderr)
(close-output-port stdin)
(debug 'COMPILE "DONE-OK~n")
]
[(done-error)
(let ([err-msg (read-all stdout)]
[details (read-all stderr)])
(close-input-port stdout)
(close-input-port stderr)
(close-output-port stdin)
(control 'kill)
;; Build an error response
(hash-set! result "err-msg" err-msg)
(hash-set! result "details" details)
(hash-set! result "code" ERR.COMPILE)
(debug 'COMPILE:ERR "msg: ~a~ndetails: ~a~n" err-msg details)
;; Clean up the temporary space.
(debug 'DELETE "Deleting ~a" session-dir)
;; (delete-directory/files (build-path (conf-get "temp-dir") session-dir))
)]
[else
(debug 'COMPILE "LOOP ERR: ~a~n" (control 'status))]
))
(debug 'COMPILERESULT (~a result))
result
)))
;; NEED TO PARAMETERIZE
#|
avr-occbuild --program fastblink.occ
--search /home/jupiter/git/kroc/tvm/arduino/occam/include/
--search /home/jupiter/git/kroc/tvm/arduino/occam/include/arch/common/
--search /home/jupiter/git/kroc/tvm/arduino/occam/include/arch/m328p/
--search /home/jupiter/git/kroc/tvm/arduino/occam/include/platforms/arduino
-D F.CPU=16000000
|#
;; FIXME : check the defaults in the hash-gets...
(define (compile-cmd config main)
(system-call
(conf-get "occbuild")
`(--verbose
,@(if (conf-get "docker")
`(--occ21 ,(format "~a/avr-occ21" (conf-get "docker"))
--plinker.pl ,(format "~a/avr-plinker.pl" (conf-get "docker"))
--tce-dump.pl ,(format "~a/avr-tce-dump.pl" (conf-get "docker")))
'())
--search ,(conf-get "include")
,@(map (λ (lib)
`(--search ,(build-path (conf-get "include") lib)))
'("convert" "forall" "hostio" "hostsp" "maths" "streamio" "string"))
--search ,(build-path (conf-get "include") "plumbing")
--search
,(build-path (conf-get "include")
"plumbing" "arch" "common")
--search
,(build-path (conf-get "include")
"plumbing" "arch"
(hash-ref config "cpu" "m328p"))
--search
,(build-path (conf-get "include")
"plumbing" "platforms"
(hash-ref config "platform" "arduino"))
-D ,(format "F.CPU=~a" (hash-ref config 'mhz "16000000"))
;; --program needs to come last
--program
;; ,(build-path (conf-get 'temp-dir) (hash-ref config "session-id") main))))
,main)))
(define (output-exists? session-dir names ext)
(parameterize ([current-directory (build-path (conf-get 'temp-dir) session-dir)])
(file-exists? (hash-ref names ext))))
(define (plink conf)
(define result (exe-in-session conf (plinker-cmd conf)))
(cond
[(zero? result)
(make-hash `(("code" . OK.LINK)))]
[else
(make-hash `(("code" . ERR.LINK)))])
)
(define (replace-extension file orig new)
(regexp-replace (format "~a$" orig) (extract-filename file) new))
;
;
;
;
;
;
;
;
; ;;;;;;; ;; ;; ;;; ;; ;; ;; ;;;;;;;; ;;;;;;;
; ;; ;; ;; ;; ;;; ;; ;; ;; ;; ;; ;;
; ;; ; ;; ;; ;;;; ;; ;; ;; ;; ;; ;
; ;; ; ;; ;; ;; ; ;; ;; ;; ;; ;; ;;
; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;;
; ;;;;;;; ;; ;; ;; ; ;; ;;;;;; ;;;;;;; ;;;;;;;
; ;; ;; ;; ;; ;; ;; ;;; ;; ;; ;; ;
; ;; ;; ;; ;; ; ;; ;; ;; ;; ;; ;;
; ;; ;; ;; ;; ;;;; ;; ;; ;; ;; ;
; ;; ;; ;; ;; ;;; ;; ;; ;; ;; ;;
; ;; ;;;;;;; ;; ;; ;;; ;; ;; ;;;;;;;; ;; ;;
;
;
;
;
;
(define (plinker-cmd conf)
(define tbc (replace-extension (hash-ref conf "main") "occ" "tbc"))
(define tce (replace-extension (hash-ref conf "main") "occ" "tce"))
(system-call
(conf-get "linker")
`(-s -o ,tbc
,(->string (build-path (conf-get "include") "forall" "forall.lib"))
,tce)))
(define (binhex conf)
(define result (exe-in-session conf (binhex-cmd conf)))
(cond
[(zero? result)
(make-hash `(("code" . OK.BINHEX)))]
[else
(make-hash `(("code" . ERR.BINHEX)))])
)
;
;
;
;
;
;
;
;
; ;;;;;;; ;; ;;; ;; ;; ;; ;;;;;;;; ;; ;;
; ;; ;; ;; ;;; ;; ;; ;; ;; ;; ;;
; ;; ; ;; ;;;; ;; ;; ;; ;; ;; ;;
; ;; ; ;; ;; ; ;; ;; ;; ;; ;; ;
; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;;;
; ;;;;;;; ;; ;; ; ;; ;;;;;;;;; ;;;;;;; ;;
; ;; ; ;; ;; ;; ;; ;; ;; ;; ;;;;
; ;; ;; ;; ;; ; ;; ;; ;; ;; ; ;;
; ;; ;; ;; ;; ;;;; ;; ;; ;; ;; ;;
; ;; ;; ;; ;; ;;; ;; ;; ;; ;; ;
; ;;;;;;; ;; ;; ;;; ;; ;; ;;;;;;;; ;; ;;;
;
;
;
;
;
;;FIXME : Magic Number (bytecode location)
(define (binhex-cmd conf)
(define tbc (replace-extension (hash-ref conf "main") "occ" "tbc"))
(define hex (replace-extension (hash-ref conf "main") "occ" "hex"))
(system-call
(conf-get "binhex")
`(,(format "0x~a" (hash-ref (hash-ref (hash-ref conf "client-config") "board") "start-address"))
,tbc
,hex)))
;
;
;
;
;
;
;
;
; ;; ;; ;; ;;;;;;;; ;; ;; ;;; ;;; ;;;;;;;; ;;;;;;; ;;;;; ;;;;;;;;
; ;; ;; ;; ;; ;; ;; ;;; ;;; ;; ;; ;; ;; ; ;;
; ;; ;; ;; ;; ;; ;; ;;; ;;;; ;; ;; ; ;; ;;
; ;; ;; ;; ;; ;; ; ;;;; ; ;; ;; ;; ;; ;; ;;
; ;; ;; ;; ;; ;;; ;; ; ; ;; ;; ;; ;; ;; ;;
; ;; ;;;;;;;;; ;;;;;;; ;; ;; ;; ;; ;; ;;;;;;; ;;;;;;; ;; ;;;;;;;
; ;; ;; ;; ;; ;;;; ;; ; ; ;; ;; ;; ; ;; ;;;; ;;
; ;; ;; ;; ;; ; ;; ;; ; ; ;; ;; ;; ;; ;; ;; ;;
; ;; ;; ;; ;; ;; ;; ;; ;;; ;; ;; ;; ; ;; ;; ;;
; ;; ;; ;; ;; ;; ; ;; ;; ;; ;; ;; ;; ;; ;; ;;
; ;; ;; ;; ;;;;;;;; ;; ;;; ;; ;; ;; ;;;;;;;; ;; ;; ;;;;; ;;;;;;;;
;
;
;
;
;
(define (ihexmerge conf)
(define result (ihexmerge-cmd conf))
(hash-set! result "code" OK.IHEXMERGE)
result)
(define (ihexmerge-cmd conf)
(parameterize ([current-directory (build-path (conf-get "temp-dir")
(hash-ref conf "session-id"))])
(define tvm (build-path
(conf-get "install")
"firmwares"
(hash-ref (hash-ref (hash-ref conf "client-config") "board") "firmware")))
(debug 'IHEX "Firmware path: ~a" tvm)
(define hex
(replace-extension (hash-ref conf "main") "occ" "hex"))
(debug 'IHEX "Target hex to merge: ~a" hex)
(define cmd
(system-call
(conf-get "ihexmerge")
`(,tvm
,hex
"> plumbware.hex")))
(define result (make-hash))
(let-values ([(stdout stdin pid stderr control)
(apply values (process cmd))])
(let loop ([status (control 'status)])
(case status
[(running)
(debug 'IHEX "STATE: ~a" status)
(sleep 1)
(loop (control 'status))]
[(done-ok)
(debug 'IHEX "STATE: ~a" status)
(let (#;[hex (read-all stdout)])
(debug 'IHEX "STATE: ~a" status)
(close-input-port stdout)
(close-input-port stderr)
(close-output-port stdin)
(control 'kill)
;; Build an ok response
(define hex (file->string "plumbware.hex"))
(debug 'IHEX "Read plumbware: ~a bytes" (string-length hex))
(hash-set! result "hex" hex)
(hash-set! result "code" OK.IHEXMERGE)
;; Clean up the temporary space.
;;(debug 'DELETE "Deleting ~a" session-dir)
;; (delete-directory/files (build-path (conf-get "temp-dir") session-dir))
)
]
[(done-error)
(debug 'IHEX "STATE: ~a" status)
(close-input-port stdout)
(close-input-port stderr)
(close-output-port stdin)
(control 'kill)
(hash-set! result "code" ERR.IHEXMERGE)
])))
result))
;; avrdude -V -F -p atmega328p -c arduino -U flash:w:firmware.hex:i -b 57600 -P /dev/tty.usbserial-A901N6UH