run-test.in 16 KB

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