run-test.in 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #!/bin/sh
  2. # -*- mode: scheme; coding: utf-8; -*-
  3. GUILE_AUTO_COMPILE=0
  4. export GUILE_AUTO_COMPILE
  5. main='(@ (run-test) build/run)'
  6. exec "${GUILE-@GUILE@}" -l "$0" \
  7. -c "(apply $main (cdr (command-line)))" "$@"
  8. !#
  9. ;;; GCC-StarPU
  10. ;;; Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique
  11. ;;;
  12. ;;; GCC-StarPU is free software: you can redistribute it and/or modify
  13. ;;; it under the terms of the GNU General Public License as published by
  14. ;;; the Free Software Foundation, either version 3 of the License, or
  15. ;;; (at your option) any later version.
  16. ;;;
  17. ;;; GCC-StarPU is distributed in the hope that it will be useful,
  18. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;;; GNU General Public License for more details.
  21. ;;;
  22. ;;; You should have received a copy of the GNU General Public License
  23. ;;; along with GCC-StarPU. If not, see <http://www.gnu.org/licenses/>.
  24. ;;;
  25. ;;; Written by Ludovic Courtès <ludovic.courtes@inria.fr>.
  26. ;;;
  27. (define-module (run-test)
  28. #:use-module (ice-9 regex)
  29. #:use-module (ice-9 popen)
  30. #:use-module (ice-9 rdelim)
  31. #:use-module (ice-9 format)
  32. #:use-module (ice-9 match)
  33. #:use-module (srfi srfi-1)
  34. #:use-module (srfi srfi-9)
  35. #:use-module (srfi srfi-11)
  36. #:use-module (srfi srfi-13)
  37. #:use-module (srfi srfi-14)
  38. #:use-module (srfi srfi-26)
  39. #:export (build/run))
  40. ;;; Commentary:
  41. ;;;
  42. ;;; Test machinery similar to the DejaGNU-based test framework used in GCC.
  43. ;;; In a nutshell, this program compiles code with GCC and makes sure
  44. ;;; warnings and errors are as appear in source code comments.
  45. ;;;
  46. ;;; This module should work with both Guile 1.8 and Guile 2.0.
  47. ;;;
  48. ;;; Code:
  49. ;; Make sure the reader gets position information.
  50. (read-enable 'positions)
  51. (define %log-port
  52. ;; Output port for debugging messages.
  53. (current-output-port))
  54. (define (log fmt . args)
  55. "Write an informational message."
  56. (apply format %log-port (string-append fmt "\n") args))
  57. ;;;
  58. ;;; Compiling code.
  59. ;;;
  60. (define %srcdir "@srcdir@")
  61. (define %builddir "@builddir@")
  62. (define %gcc "@CC@")
  63. (define %default-cflags
  64. `("-I" ,%srcdir
  65. "-I" ,(string-append %srcdir "/../../include")
  66. "-I" ,(string-append %builddir "/../../include")
  67. "-I" ,(string-append %builddir "/../..")
  68. ;; Unfortunately `libtool --mode=execute' doesn't help here, so hard-code
  69. ;; the real file name.
  70. ,(string-append "-fplugin=" %builddir "/../src/.libs/starpu.so")
  71. "-fdump-tree-gimple" "-Wall"))
  72. (define %default-ldflags
  73. `(,(string-append "-L" %srcdir "/../../src")))
  74. (define %libtool
  75. (string-append %builddir "/../../libtool"))
  76. (define (compile-starpu-code file cc cflags ldflags)
  77. "Compile and link FILE with CC, using CFLAGS and LDFLAGS. Return the
  78. compiler status and the list of lines printed on stdout/stderr."
  79. (let* ((compile? (member "-c" cflags))
  80. (ldflags (if compile?
  81. (remove (cut string-prefix? "-L" <>) ldflags)
  82. ldflags))
  83. (mode (if compile?
  84. "compile"
  85. "link"))
  86. (command (format #f "LANG=C ~a --mode=~a ~a ~{~a ~} \"~a\" ~{~a ~} 2>&1"
  87. %libtool mode cc cflags file ldflags))
  88. (pipe (begin
  89. (log "running `~a'" command)
  90. (open-input-pipe command))))
  91. (let loop ((line (read-line pipe))
  92. (result '()))
  93. (if (eof-object? line)
  94. (values (close-pipe pipe) (reverse result))
  95. (loop (read-line pipe)
  96. (cons line result))))))
  97. (define (run-starpu-code executable)
  98. "Run EXECUTABLE using Libtool; return its exit status."
  99. (let* ((exe (if (string-index executable #\/)
  100. executable
  101. (string-append (getcwd) "/" executable)))
  102. (command (string-append %libtool " --mode=execute "
  103. exe)))
  104. (log "running `~a'" command)
  105. (system command)))
  106. ;;;
  107. ;;; GCC diagnostics.
  108. ;;;
  109. (define-record-type <location>
  110. (make-location file line column)
  111. location?
  112. (file location-file)
  113. (line location-line)
  114. (column location-column))
  115. (define (location=? loc1 loc2)
  116. "Return #t if LOC1 and LOC2 refer roughly to the same file and line
  117. number."
  118. (and (location-file loc1) (location-file loc2)
  119. (string=? (basename (location-file loc1))
  120. (basename (location-file loc2)))
  121. (= (location-line loc1) (location-line loc2))))
  122. (define-record-type <diagnostic>
  123. (make-diagnostic location kind message)
  124. diagnostic?
  125. (location diagnostic-location)
  126. (kind diagnostic-kind)
  127. (message diagnostic-message))
  128. (define %diagnostic-with-location-rx
  129. ;; "FILE:LINE:COL: KIND: MESSAGE..."
  130. (make-regexp "^(.+):([[:digit:]]+):([[:digit:]]+): ([^:]+): (.*)$"))
  131. (define (string->diagnostic str)
  132. "Parse STR and return the corresponding `diagnostic' object."
  133. (cond ((regexp-exec %diagnostic-with-location-rx str)
  134. =>
  135. (lambda (m)
  136. (let ((loc (make-location (match:substring m 1)
  137. (string->number (match:substring m 2))
  138. (string->number (match:substring m 3))))
  139. (kind (string->symbol (match:substring m 4))))
  140. (make-diagnostic loc kind (match:substring m 5)))))
  141. (else
  142. (make-diagnostic #f #f str))))
  143. ;;;
  144. ;;; Reading test directives.
  145. ;;;
  146. (define (read-test-directives port)
  147. "Read test directives from PORT. Return a list of location/directive
  148. pairs."
  149. (define (consume-whitespace p)
  150. (let loop ((chr (peek-char p)))
  151. (cond ((char-set-contains? char-set:whitespace chr)
  152. (read-char p) ;; consume CHR
  153. (loop (peek-char p)))
  154. (else chr))))
  155. (define (read-until-*/ p)
  156. (let loop ((chr (read-char p)))
  157. (cond ((eof-object? chr)
  158. (error "unterminated C comment"))
  159. ((eq? chr #\*)
  160. (let ((next (peek-char p)))
  161. (if (eq? next #\/)
  162. (read-char p) ;; consume CHR
  163. (loop (read-char p)))))
  164. (else
  165. (loop (read-char p))))))
  166. (let loop ((chr (read-char port))
  167. (directives '()))
  168. (cond ((eof-object? chr)
  169. (reverse directives))
  170. ((eq? chr #\/)
  171. (let ((chr (read-char port)))
  172. (if (eq? chr #\*)
  173. (let ((chr (consume-whitespace port)))
  174. (if (eq? chr #\()
  175. (let ((loc (make-location (port-filename port)
  176. (1+ (port-line port))
  177. (port-column port)))
  178. (sexp (read port)))
  179. (read-until-*/ port)
  180. (loop (peek-char port)
  181. (cons (cons loc sexp)
  182. directives)))
  183. (begin
  184. (read-until-*/ port)
  185. (loop (peek-char port) directives))))
  186. (loop chr directives))))
  187. (else
  188. (loop (read-char port) directives)))))
  189. (define (diagnostic-matches-directive? diagnostic directive location)
  190. "Return #t if DIAGNOSTIC matches DIRECTIVE, which is at LOCATION."
  191. (and (location? (diagnostic-location diagnostic))
  192. (location=? (diagnostic-location diagnostic) location)
  193. (match directive
  194. ((kind message)
  195. (and (eq? kind (diagnostic-kind diagnostic))
  196. (string-match message (diagnostic-message diagnostic)))))))
  197. ;;;
  198. ;;; Compiling and matching diagnostics against directives.
  199. ;;;
  200. (define (compile/match* file directives cc cflags ldflags)
  201. "Compile FILE and check whether GCC's diagnostics match DIRECTIVES. Return
  202. 3 values: the compiler's status code, the unmatched diagnostics, and the
  203. unsatisfied directives."
  204. (let-values (((status diagnostics)
  205. (compile-starpu-code file cc cflags ldflags)))
  206. (let loop ((diagnostics (map string->diagnostic diagnostics))
  207. (directives directives)
  208. (unsatisfied '()))
  209. (if (null? directives)
  210. (values status diagnostics unsatisfied)
  211. (let* ((dir (car directives))
  212. (diag (find (cute diagnostic-matches-directive?
  213. <> (cdr dir) (car dir))
  214. diagnostics)))
  215. (if diag
  216. (loop (delq diag diagnostics)
  217. (cdr directives)
  218. unsatisfied)
  219. (loop diagnostics
  220. (cdr directives)
  221. (cons dir unsatisfied))))))))
  222. (define (executable-file source)
  223. "Return the name of the executable file corresponding to SOURCE."
  224. (let ((dot (string-rindex source #\.)))
  225. (if dot
  226. (substring source 0 dot)
  227. (string-append source ".exe"))))
  228. (define (compile/match file cc cflags ldflags)
  229. "Read directives from FILE, and compiler/link/run it. Make sure directives
  230. are matched, and report any errors otherwise. Return #t on success and #f
  231. otherwise."
  232. (define directives
  233. (call-with-input-file file read-test-directives))
  234. (define exe
  235. (executable-file file))
  236. (log "~a directives found in `~a'" (length directives) file)
  237. (let*-values (((error-expected?)
  238. (find (lambda (l+d)
  239. (match l+d
  240. (((? location?) 'error _)
  241. #t)
  242. (_ #f)))
  243. directives))
  244. ((instructions)
  245. (or (any (lambda (l+d)
  246. (match l+d
  247. (((? location?) 'instructions x ...)
  248. x)
  249. (_ #f)))
  250. directives)
  251. '(run)))
  252. ((goal)
  253. (if error-expected?
  254. 'compile
  255. (car instructions)))
  256. ((cflags)
  257. `(,@cflags
  258. ,@(or (and=> (assq-ref (cdr instructions) 'cflags) cadr)
  259. '())
  260. ,@(if (memq goal '(link run))
  261. `("-o" ,exe)
  262. '("-c"))))
  263. ((ldflags)
  264. `(,@ldflags
  265. ,@(or (assq-ref (cdr instructions) 'ldflags)
  266. '())))
  267. ((directives)
  268. (remove (lambda (l+d)
  269. (match l+d
  270. (((? location?) 'instructions _ ...)
  271. #t)
  272. (_ #f)))
  273. directives))
  274. ((status diagnostics unsatisfied)
  275. (compile/match* file directives cc cflags ldflags))
  276. ((unmatched)
  277. ;; Consider unmatched only diagnostics that have a kind, to
  278. ;; avoid taking into account messages like "In file included
  279. ;; from", "In function 'main'", etc.
  280. (filter diagnostic-kind diagnostics)))
  281. (or (null? unmatched)
  282. (begin
  283. (format (current-error-port)
  284. "error: ~a unmatched GCC diagnostics:~%"
  285. (length unmatched))
  286. (for-each (lambda (d)
  287. (format (current-error-port)
  288. " ~a:~a:~a: ~a: ~a~%"
  289. (and=> (diagnostic-location d)
  290. location-file)
  291. (and=> (diagnostic-location d)
  292. location-line)
  293. (and=> (diagnostic-location d)
  294. location-column)
  295. (diagnostic-kind d)
  296. (diagnostic-message d)))
  297. unmatched)
  298. #f))
  299. (if (null? unsatisfied)
  300. (or (null? directives)
  301. (log "~a directives satisfied" (length directives)))
  302. (begin
  303. (format (current-error-port) "error: ~a unsatisfied directives:~%"
  304. (length unsatisfied))
  305. (for-each (lambda (l+d)
  306. (let ((loc (car l+d))
  307. (dir (cdr l+d)))
  308. (format (current-error-port)
  309. " ~a:~a:~a: ~a: ~s~%"
  310. (location-file loc)
  311. (location-line loc)
  312. (location-column loc)
  313. (car dir)
  314. (cadr dir))))
  315. unsatisfied)
  316. #f))
  317. (if error-expected?
  318. (if (= 0 status)
  319. (format (current-error-port) "error: compilation succeeded~%"))
  320. (if (= 0 status)
  321. (or (eq? goal 'compile)
  322. (file-exists? exe)
  323. (begin
  324. (format (current-error-port)
  325. "error: executable file `~a' not found~%" exe)
  326. #f))
  327. (format (current-error-port)
  328. "error: compilation failed (compiler exit code ~a)~%~{ ~a~%~}"
  329. status
  330. (map diagnostic-message diagnostics))))
  331. (and (null? unmatched)
  332. (null? unsatisfied)
  333. (if error-expected?
  334. (not (= 0 status))
  335. (and (= 0 status)
  336. (or (eq? goal 'compile) (file-exists? exe))
  337. (or (not (eq? goal 'run))
  338. (let ((status (run-starpu-code exe)))
  339. (or (= 0 status)
  340. (begin
  341. (format (current-error-port)
  342. "error: program `~a' failed (exit code ~a)~%"
  343. exe status)
  344. #f)))))))))
  345. ;;;
  346. ;;; Entry point.
  347. ;;;
  348. (define (build/run . file)
  349. (exit (every (cut compile/match <> %gcc %default-cflags %default-ldflags) file)))
  350. ;;; run-test.in ends here