run-test.in 16 KB

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