run-test.in 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 %cuda-cppflags
  64. (string-tokenize "@STARPU_CUDA_CPPFLAGS@"))
  65. (define %default-cflags
  66. `("-I" ,%srcdir
  67. "-I" ,(string-append %srcdir "/../../include")
  68. "-I" ,(string-append %builddir "/../../include")
  69. "-I" ,(string-append %builddir "/../..")
  70. ,@%cuda-cppflags
  71. ;; Unfortunately `libtool --mode=execute' doesn't help here, so hard-code
  72. ;; the real file name.
  73. ,(string-append "-fplugin=" %builddir "/../src/.libs/starpu.so")
  74. "-fdump-tree-gimple" "-Wall"))
  75. (define %default-ldflags
  76. `(,(string-append "-L" %srcdir "/../../src")))
  77. (define %libtool
  78. (string-append %builddir "/../../libtool"))
  79. (define (compile-starpu-code file cc cflags ldflags)
  80. "Compile and link FILE with CC, using CFLAGS and LDFLAGS. Return the
  81. compiler status and the list of lines printed on stdout/stderr."
  82. (let* ((compile? (member "-c" cflags))
  83. (ldflags (if compile?
  84. (remove (cut string-prefix? "-L" <>) ldflags)
  85. ldflags))
  86. (mode (if compile?
  87. "compile"
  88. "link"))
  89. (command (format #f "LANG=C ~a --mode=~a ~a ~{~a ~} \"~a\" ~{~a ~} 2>&1"
  90. %libtool mode cc cflags file ldflags))
  91. (pipe (begin
  92. (log "running `~a'" command)
  93. (open-input-pipe command))))
  94. (let loop ((line (read-line pipe))
  95. (result '()))
  96. (if (eof-object? line)
  97. (values (close-pipe pipe) (reverse result))
  98. (loop (read-line pipe)
  99. (cons line result))))))
  100. (define (run-starpu-code executable)
  101. "Run EXECUTABLE using Libtool; return its exit status."
  102. (let* ((exe (if (string-index executable #\/)
  103. executable
  104. (string-append (getcwd) "/" executable)))
  105. (command (string-append %libtool " --mode=execute "
  106. exe)))
  107. (log "running `~a'" command)
  108. (system command)))
  109. ;;;
  110. ;;; GCC diagnostics.
  111. ;;;
  112. (define-record-type <location>
  113. (make-location file line column)
  114. location?
  115. (file location-file)
  116. (line location-line)
  117. (column location-column))
  118. (define (location=? loc1 loc2)
  119. "Return #t if LOC1 and LOC2 refer roughly to the same file and line
  120. number."
  121. (and (location-file loc1) (location-file loc2)
  122. (string=? (basename (location-file loc1))
  123. (basename (location-file loc2)))
  124. (= (location-line loc1) (location-line loc2))))
  125. (define-record-type <diagnostic>
  126. (make-diagnostic location kind message)
  127. diagnostic?
  128. (location diagnostic-location)
  129. (kind diagnostic-kind)
  130. (message diagnostic-message))
  131. (define %diagnostic-with-location-rx
  132. ;; "FILE:LINE:COL: KIND: MESSAGE..."
  133. (make-regexp "^(.+):([[:digit:]]+):([[:digit:]]+): ([^:]+): (.*)$"))
  134. (define (string->diagnostic str)
  135. "Parse STR and return the corresponding `diagnostic' object."
  136. (cond ((regexp-exec %diagnostic-with-location-rx str)
  137. =>
  138. (lambda (m)
  139. (let ((loc (make-location (match:substring m 1)
  140. (string->number (match:substring m 2))
  141. (string->number (match:substring m 3))))
  142. (kind (string->symbol (match:substring m 4))))
  143. (make-diagnostic loc kind (match:substring m 5)))))
  144. (else
  145. (make-diagnostic #f #f str))))
  146. ;;;
  147. ;;; Reading test directives.
  148. ;;;
  149. (define (read-test-directives port)
  150. "Read test directives from PORT. Return a list of location/directive
  151. pairs."
  152. (define (consume-whitespace p)
  153. (let loop ((chr (peek-char p)))
  154. (cond ((char-set-contains? char-set:whitespace chr)
  155. (read-char p) ;; consume CHR
  156. (loop (peek-char p)))
  157. (else chr))))
  158. (define (read-until-*/ p)
  159. (let loop ((chr (read-char p)))
  160. (cond ((eof-object? chr)
  161. (error "unterminated C comment"))
  162. ((eq? chr #\*)
  163. (let ((next (peek-char p)))
  164. (if (eq? next #\/)
  165. (read-char p) ;; consume CHR
  166. (loop (read-char p)))))
  167. (else
  168. (loop (read-char p))))))
  169. (let loop ((chr (read-char port))
  170. (directives '()))
  171. (cond ((eof-object? chr)
  172. (reverse directives))
  173. ((eq? chr #\/)
  174. (let ((chr (read-char port)))
  175. (if (eq? chr #\*)
  176. (let ((chr (consume-whitespace port)))
  177. (if (eq? chr #\()
  178. (let ((loc (make-location (port-filename port)
  179. (1+ (port-line port))
  180. (port-column port)))
  181. (sexp (read port)))
  182. (read-until-*/ port)
  183. (loop (peek-char port)
  184. (cons (cons loc sexp)
  185. directives)))
  186. (begin
  187. (read-until-*/ port)
  188. (loop (peek-char port) directives))))
  189. (loop chr directives))))
  190. (else
  191. (loop (read-char port) directives)))))
  192. (define (diagnostic-matches-directive? diagnostic directive location)
  193. "Return #t if DIAGNOSTIC matches DIRECTIVE, which is at LOCATION."
  194. (and (location? (diagnostic-location diagnostic))
  195. (location=? (diagnostic-location diagnostic) location)
  196. (match directive
  197. ((kind message)
  198. (and (eq? kind (diagnostic-kind diagnostic))
  199. (string-match message (diagnostic-message diagnostic)))))))
  200. ;;;
  201. ;;; Compiling and matching diagnostics against directives.
  202. ;;;
  203. (define (compile/match* file directives cc cflags ldflags)
  204. "Compile FILE and check whether GCC's diagnostics match DIRECTIVES. Return
  205. 3 values: the compiler's status code, the unmatched diagnostics, and the
  206. unsatisfied directives."
  207. (let-values (((status diagnostics)
  208. (compile-starpu-code file cc cflags ldflags)))
  209. (let loop ((diagnostics (map string->diagnostic diagnostics))
  210. (directives directives)
  211. (unsatisfied '()))
  212. (if (null? directives)
  213. (values status diagnostics unsatisfied)
  214. (let* ((dir (car directives))
  215. (diag (find (cute diagnostic-matches-directive?
  216. <> (cdr dir) (car dir))
  217. diagnostics)))
  218. (if diag
  219. (loop (delq diag diagnostics)
  220. (cdr directives)
  221. unsatisfied)
  222. (loop diagnostics
  223. (cdr directives)
  224. (cons dir unsatisfied))))))))
  225. (define (executable-file source)
  226. "Return the name of the executable file corresponding to SOURCE."
  227. (let ((dot (string-rindex source #\.)))
  228. (if dot
  229. (substring source 0 dot)
  230. (string-append source ".exe"))))
  231. (define (compile/match file cc cflags ldflags)
  232. "Read directives from FILE, and compiler/link/run it. Make sure directives
  233. are matched, and report any errors otherwise. Return #t on success and #f
  234. otherwise."
  235. (define directives
  236. (call-with-input-file file read-test-directives))
  237. (define exe
  238. (executable-file file))
  239. (log "~a directives found in `~a'" (length directives) file)
  240. (let*-values (((error-expected?)
  241. (find (lambda (l+d)
  242. (match l+d
  243. (((? location?) 'error _)
  244. #t)
  245. (_ #f)))
  246. directives))
  247. ((instructions)
  248. (or (any (lambda (l+d)
  249. (match l+d
  250. (((? location?) 'instructions x ...)
  251. x)
  252. (_ #f)))
  253. directives)
  254. '(run)))
  255. ((goal)
  256. (if error-expected?
  257. 'compile
  258. (car instructions)))
  259. ((cflags)
  260. `(,@cflags
  261. ,@(or (and=> (assq-ref (cdr instructions) 'cflags) cadr)
  262. '())
  263. ,@(if (memq goal '(link run))
  264. `("-o" ,exe)
  265. '("-c"))))
  266. ((ldflags)
  267. `(,@ldflags
  268. ,@(or (assq-ref (cdr instructions) 'ldflags)
  269. '())))
  270. ((directives)
  271. (remove (lambda (l+d)
  272. (match l+d
  273. (((? location?) 'instructions _ ...)
  274. #t)
  275. (_ #f)))
  276. directives))
  277. ((status diagnostics unsatisfied)
  278. (compile/match* file directives cc cflags ldflags))
  279. ((unmatched)
  280. ;; Consider unmatched only diagnostics that have a kind, to
  281. ;; avoid taking into account messages like "In file included
  282. ;; from", "In function 'main'", etc.
  283. (filter diagnostic-kind diagnostics)))
  284. (or (null? unmatched)
  285. (begin
  286. (format (current-error-port)
  287. "error: ~a unmatched GCC diagnostics:~%"
  288. (length unmatched))
  289. (for-each (lambda (d)
  290. (format (current-error-port)
  291. " ~a:~a:~a: ~a: ~a~%"
  292. (and=> (diagnostic-location d)
  293. location-file)
  294. (and=> (diagnostic-location d)
  295. location-line)
  296. (and=> (diagnostic-location d)
  297. location-column)
  298. (diagnostic-kind d)
  299. (diagnostic-message d)))
  300. unmatched)
  301. #f))
  302. (if (null? unsatisfied)
  303. (or (null? directives)
  304. (log "~a directives satisfied" (length directives)))
  305. (begin
  306. (format (current-error-port) "error: ~a unsatisfied directives:~%"
  307. (length unsatisfied))
  308. (for-each (lambda (l+d)
  309. (let ((loc (car l+d))
  310. (dir (cdr l+d)))
  311. (format (current-error-port)
  312. " ~a:~a:~a: ~a: ~s~%"
  313. (location-file loc)
  314. (location-line loc)
  315. (location-column loc)
  316. (car dir)
  317. (cadr dir))))
  318. unsatisfied)
  319. #f))
  320. (if error-expected?
  321. (if (= 0 status)
  322. (format (current-error-port) "error: compilation succeeded~%"))
  323. (if (= 0 status)
  324. (or (eq? goal 'compile)
  325. (file-exists? exe)
  326. (begin
  327. (format (current-error-port)
  328. "error: executable file `~a' not found~%" exe)
  329. #f))
  330. (format (current-error-port)
  331. "error: compilation failed (compiler exit code ~a)~%~{ ~a~%~}"
  332. status
  333. (map diagnostic-message diagnostics))))
  334. (and (null? unmatched)
  335. (null? unsatisfied)
  336. (if error-expected?
  337. (not (= 0 status))
  338. (and (= 0 status)
  339. (or (eq? goal 'compile) (file-exists? exe))
  340. (or (not (eq? goal 'run))
  341. (let ((status (run-starpu-code exe)))
  342. (or (= 0 status)
  343. (begin
  344. (format (current-error-port)
  345. "error: program `~a' failed (exit code ~a)~%"
  346. exe status)
  347. #f)))))))))
  348. ;;;
  349. ;;; Entry point.
  350. ;;;
  351. (define (build/run . file)
  352. (exit (every (cut compile/match <> %gcc %default-cflags %default-ldflags) file)))
  353. ;;; run-test.in ends here