9a85a1c87ae7184a6abf053d83c16059a72e7116
[oota-llvm.git] / test / lib / llvm.exp
1 # This procedure executes one line of a test case's execution script.
2 proc execOneLine { test PRS outcome lineno line } {
3   set status 0
4   set resultmsg ""
5   set retval [ catch { eval exec -keepnewline -- $line } errmsg ]
6   if { $retval != 0 } {
7     set code [lindex $::errorCode 0]
8     set lineno [expr $lineno + 1]
9     if { $PRS != ""} {
10       set PRS " for $PRS" 
11     }
12     set errmsg " at line $lineno\nwhile running: $line\n$errmsg"
13     switch "$code" {
14       CHILDSTATUS {
15         set status [lindex $::errorCode 2]
16         if { $status != 0 } {
17           set resultmsg "$test$PRS\nFailed with exit($status)$errmsg"
18         }
19       }
20       CHILDKILLED {
21         set signal [lindex $::errorCode 2]
22         set resultmsg "$test$PRS\nFailed with signal($signal)$errmsg"
23       }
24       CHILDSUSP {
25         set signal [lindex $::errorCode 2]
26         set resultmsg "$test$PRS\nFailed with suspend($signal)$errmsg"
27       }
28       POSIX {
29         set posixNum [lindex $::errorCode 1]
30         set posixMsg [lindex $::errorCode 2]
31         set resultmsg "$test$PRS\nFailed with posix($posixNum,$posixMsg)$errmsg"
32       }
33       NONE {
34       }
35       default {
36       }
37     }
38   } 
39   return $resultmsg
40 }
41
42 # This procedure performs variable substitutions on the RUN: lines of a test
43 # cases.
44 proc substitute { line test tmpFile } {
45   global srcroot objroot srcdir objdir subdir target_triplet prcontext 
46   global llvmgcc llvmgxx llvmgcc_version llvmgccmajvers 
47   global gccpath gxxpath compile_c compile_cxx link shlibext llvmlibsdir
48   set path [file join $srcdir $subdir]
49
50   # Substitute all Tcl variables.
51   set new_line [subst $line ]
52
53   #replace %prcontext with prcontext.tcl (Must replace before %p)
54   regsub -all {%prcontext} $new_line $prcontext new_line
55   #replace %llvmgcc with actual path to llvmgcc
56   regsub -all {%llvmgcc} $new_line "$llvmgcc -emit-llvm" new_line
57   #replace %llvmgxx with actual path to llvmg++
58   regsub -all {%llvmgxx} $new_line "$llvmgxx -emit-llvm" new_line
59   #replace %compile_c with C compilation command
60   regsub -all {%compile_c} $new_line "$compile_c" new_line
61   #replace %compile_cxx with C++ compilation command
62   regsub -all {%compile_cxx} $new_line "$compile_cxx" new_line
63   #replace %link with C++ link command
64   regsub -all {%link} $new_line "$link" new_line
65   #replace %shlibext with shared library extension
66   regsub -all {%shlibext} $new_line "$shlibext" new_line
67   #replace %llvmlibsdir with configure library directory
68   regsub -all {%llvmlibsdir} $new_line "$llvmlibsdir" new_line
69   #replace %p with path to source, 
70   regsub -all {%p} $new_line [file join $srcdir $subdir] new_line
71   #replace %s with filename
72   regsub -all {%s} $new_line $test new_line
73   #replace %t with temp filenames
74   regsub -all {%t} $new_line $tmpFile new_line
75   #replace %% with %
76   regsub -all {%%} $new_line % new_line
77   return $new_line
78 }
79
80 # This procedure runs the set of tests for the test_source_files array.
81 proc RunLLVMTests { test_source_files } {
82   global srcroot objroot srcdir objdir subdir target_triplet llvmgcc_version
83   set timeout 60
84
85   set path [file join $objdir $subdir]
86   
87   #Make Output Directory if it does not exist already
88   if { [file exists path] } {
89     cd $path
90   } else {
91     file mkdir $path
92     cd $path
93   }
94   
95   file mkdir Output
96   cd Output
97
98   foreach test $test_source_files {
99     #Should figure out best way to set the timeout
100     #set timeout 40
101     
102     set filename [file tail $test]
103     set outcome PASS
104     set tmpFile "$filename.tmp"
105
106     #set hasRunline bool to check if testcase has a runline
107     set numLines 0
108
109     # Open the test file and start reading lines
110     set testFileId [ open $test r]
111     set runline ""
112     set PRNUMS ""
113     foreach line [split [read $testFileId] \n] {
114
115       # if its the END. line then stop parsing (optimization for big files)
116       if {[regexp {END.[ *]$} $line match endofscript]} {
117         break
118
119       # if the line is continued, concatenate and continue the loop
120       } elseif {[regexp {RUN: *(.+)(\\)$} $line match oneline suffix]} {
121         set runline "$runline$oneline "
122
123       # if its a terminating RUN: line then do substitution on the whole line
124       # and then save the line.
125       } elseif {[regexp {RUN: *([^&]+)(&&)?} $line match oneline suffix]} {
126         set runline "$runline$oneline"
127         set runline [ substitute $runline $test $tmpFile ]
128         set lines($numLines) $runline
129         set numLines [expr $numLines + 1]
130         set runline ""
131
132       # if its an PR line, save the problem report number
133       } elseif {[regexp {PR([0-9]+)} $line match prnum]} {
134         if {$PRNUMS == ""} {
135           set PRNUMS "PR$prnum"
136         } else {
137           set PRNUMS "$PRNUMS,$prnum"
138         }
139       # if its an XFAIL line, see if we should be XFAILing or not.
140       } elseif {[regexp {XFAIL:[ *](.+)} $line match targets]} {
141         set targets
142
143         #split up target if more then 1 specified
144         foreach target [split $targets ,] {
145           if { [regexp {\*} $target match] } {
146               set outcome XFAIL
147           } elseif { [regexp $target $target_triplet match] } {
148               set outcome XFAIL
149           } elseif { [regexp {llvmgcc(([0-9]+)|([0-9]+[.][0-9]+))} $target match submatch submatch2]  } {
150             if { [regexp ^($submatch)$|^(($submatch)(\.)) $llvmgcc_version match] } {
151               set outcome XFAIL
152             }
153           }
154         }
155       }
156     }
157     
158     # Done reading the script
159     close $testFileId
160     
161     
162     if { $numLines == 0 } {
163       fail "$test: \nDoes not have a RUN line\n"
164     } else {
165       set failed 0
166       for { set i 0 } { $i < $numLines } { set i [ expr $i + 1 ] } {
167         regsub ^.*RUN:(.*) $lines($i) \1 theLine
168         set resultmsg [execOneLine $test $PRNUMS $outcome $i $theLine ]
169         if { $resultmsg != "" } {
170           if { $outcome == "XFAIL" } {
171             xfail "$resultmsg"
172           } else {
173             fail "$resultmsg"
174           }
175           set failed 1
176           break
177         }
178       }
179       if { $failed } {
180         continue
181       } else {
182         if { $PRNUMS != "" } {
183           set PRNUMS " for $PRNUMS"
184         }
185         if { $outcome == "XFAIL" } {
186           xpass "$test$PRNUMS"
187         } else {
188           pass "$test$PRNUMS"
189         }
190       }
191     }
192   }
193 }
194
195 # This procedure provides an interface to check the LLVMGCC_LANGS makefile
196 # variable to see if llvm-gcc supports compilation of a particular language.
197 proc llvm_gcc_supports { lang } {
198   global llvmgcc llvmgcc_langs
199   # validate the language choices and determine the name of the compiler
200   # component responsible for determining if the compiler has been built.
201   switch "$lang" {
202     ada     { set file gnat1 }
203     c       { set file cc1 }
204     c++     { set file cc1plus }
205     objc    { set file cc1 }
206     objc++  { set file cc1 }
207     fortran { set file fcc1 }
208     default { return 0 }
209   }
210   foreach supported_lang [split "$llvmgcc_langs" ,] {
211     if { "$lang" == "$supported_lang" } {
212       # FIXME: Knowing it is configured is not enough. We should do two more
213       # checks here. First, we need to run llvm-gcc -print-prog-name=$file to 
214       # get the path to the compiler. If we don't get a path, the language isn't
215       # properly configured or built. If we do get a path, we should check to 
216       # make sure that it is executable and perhaps even try executing it.
217       return 1;
218     }
219   }
220   return 0;
221 }
222
223 # This procedure provides an interface to check the TARGETS_TO_BUILD makefile
224 # variable to see if a particular target has been configured to build. This
225 # helps avoid running tests for targets that aren't available.
226 proc llvm_supports_target { tgtName } {
227   global TARGETS_TO_BUILD
228   foreach target [split $TARGETS_TO_BUILD] {
229     if { [regexp $tgtName $target match] } {
230       return 1
231     }
232   }
233   return 0
234 }