run-test.in 16 KB

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