e7245064532bf6491e53b448ed46c36f6e669a24
[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 prcoedure 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   set tmp  [file join Output $tmpFile]
50
51   # Substitute all Tcl variables.
52   set new_line [subst $line ]
53
54   #replace %prcontext with prcontext.tcl (Must replace before %p)
55   regsub -all {%prcontext} $new_line $prcontext new_line
56   #replace %llvmgcc with actual path to llvmgcc
57   regsub -all {%llvmgcc} $new_line "$llvmgcc -emit-llvm" new_line
58   #replace %llvmgxx with actual path to llvmg++
59   regsub -all {%llvmgxx} $new_line "$llvmgxx -emit-llvm" new_line
60   #replace %compile_c with C compilation command
61   regsub -all {%compile_c} $new_line "$compile_c" new_line
62   #replace %compile_cxx with C++ compilation command
63   regsub -all {%compile_cxx} $new_line "$compile_cxx" new_line
64   #replace %link with C++ link command
65   regsub -all {%link} $new_line "$link" new_line
66   #replace %shlibext with shared library extension
67   regsub -all {%shlibext} $new_line "$shlibext" new_line
68   #replace %llvmlibsdir with configure library directory
69   regsub -all {%llvmlibsdir} $new_line "$llvmlibsdir" new_line
70   #replace %p with path to source, 
71   regsub -all {%p} $new_line [file join $srcdir $subdir] new_line
72   #replace %s with filename
73   regsub -all {%s} $new_line $test new_line
74   #replace %t with temp filenames
75   regsub -all {%t} $new_line [file join Output $tmpFile] new_line
76   #replace %% with %
77   regsub -all {%%} $new_line % new_line
78   return $new_line
79 }
80
81 # This procedure runs the set of tests for the test_source_files array.
82 proc RunLLVMTests { test_source_files } {
83   global srcroot objroot srcdir objdir subdir target_triplet llvmgcc_version
84   set timeout 60
85
86   set path [file join $objdir $subdir]
87   
88   #Make Output Directory if it does not exist already
89   if { [file exists path] } {
90     cd $path
91   } else {
92     file mkdir $path
93     cd $path
94   }
95   
96   file mkdir 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   if { [ string first "$lang" "$llvmgcc_langs" ] >= 0 } {
211     # FIXME: Knowing it is configured is not enough. We should do two more
212     # checks here. First, we need to run llvm-gcc -print-prog-name=$file to get
213     # the path to the compiler. If we don't get a path, the language isn't
214     # properly configured or built. If we do get a path, we should check to make
215     # sure that it is executable and perhaps even try executing it.
216     return 1;
217   }
218   return 0;
219 }
220
221 # This procedure provides an interface to check the TARGETS_TO_BUILD makefile
222 # variable to see if a particular target has been configured to build. This
223 # helps avoid running tests for targets that aren't available.
224 proc llvm_supports_target { tgtName } {
225   global TARGETS_TO_BUILD
226   foreach target [split $TARGETS_TO_BUILD] {
227     if { [regexp $tgtName $target match] } {
228       return 1
229     }
230   }
231   return 0
232 }