fbab246aae0089e05214429883b8940e1b61a98f
[oota-llvm.git] / test / lib / llvm.exp
1 proc execOneLine { test PRS outcome lineno line } {
2   set status 0
3   set resultmsg ""
4   set retval [ catch { eval exec -keepnewline -- $line } errmsg ]
5   if { $retval != 0 } {
6     set code [lindex $::errorCode 0]
7     set lineno [expr $lineno + 1]
8     if { $PRS != ""} {
9       set PRS " for $PRS" 
10     }
11     set errmsg " at line $lineno$PRS\nwhile running: $line\n$errmsg"
12     switch "$code" {
13       CHILDSTATUS {
14         set status [lindex $::errorCode 2]
15         if { $status != 0 } {
16           set resultmsg "$test\nFailed with exit($status)$errmsg"
17         }
18       }
19       CHILDKILLED {
20         set signal [lindex $::errorCode 2]
21         set resultmsg "$test\nFailed with signal($signal)$errmsg"
22       }
23       CHILDSUSP {
24         set signal [lindex $::errorCode 2]
25         set resultmsg "$test\nFailed with suspend($signal)$errmsg"
26       }
27       POSIX {
28         set posixNum [lindex $::errorCode 1]
29         set posixMsg [lindex $::errorCode 2]
30         set resultmsg "$test\nFailed with posix($posixNum,$posixMsg)$errmsg"
31       }
32       NONE {
33       }
34       default {
35       }
36     }
37   } 
38   return $resultmsg
39 }
40
41 proc substitute { line test tmpFile } {
42   global srcroot objroot srcdir objdir subdir target_triplet prcontext 
43   global llvmgcc llvmgxx llvmgcc_version llvmgccmajvers 
44   global gccpath gxxpath compile_c compile_cxx link shlibext llvmlibsdir
45   set path [file join $srcdir $subdir]
46   set tmp  [file join Output $tmpFile]
47
48   # Substitute all Tcl variables.
49   set new_line [subst $line ]
50
51   #replace %prcontext with prcontext.tcl (Must replace before %p)
52   regsub -all {%prcontext} $new_line $prcontext new_line
53   #replace %llvmgcc with actual path to llvmgcc
54   regsub -all {%llvmgcc} $new_line "$llvmgcc -emit-llvm" new_line
55   #replace %llvmgxx with actual path to llvmg++
56   regsub -all {%llvmgxx} $new_line "$llvmgxx -emit-llvm" new_line
57   #replace %compile_c with C compilation command
58   regsub -all {%compile_c} $new_line "$compile_c" new_line
59   #replace %compile_cxx with C++ compilation command
60   regsub -all {%compile_cxx} $new_line "$compile_cxx" new_line
61   #replace %link with C++ link command
62   regsub -all {%link} $new_line "$link" new_line
63   #replace %shlibext with shared library extension
64   regsub -all {%shlibext} $new_line "$shlibext" new_line
65   #replace %llvmlibsdir with configure library directory
66   regsub -all {%llvmlibsdir} $new_line "$llvmlibsdir" new_line
67   #replace %p with path to source, 
68   regsub -all {%p} $new_line [file join $srcdir $subdir] new_line
69   #replace %s with filename
70   regsub -all {%s} $new_line $test new_line
71   #replace %t with temp filenames
72   regsub -all {%t} $new_line [file join Output $tmpFile] new_line
73   #replace %% with %
74   regsub -all {%%} $new_line % new_line
75   return $new_line
76 }
77
78 proc RunLLVMTests { test_source_files } {
79   global srcroot objroot srcdir objdir subdir target_triplet llvmgcc_version
80   set timeout 60
81
82   set path [file join $objdir $subdir]
83   
84   #Make Output Directory if it does not exist already
85   if { [file exists path] } {
86     cd $path
87   } else {
88     file mkdir $path
89     cd $path
90   }
91   
92   file mkdir Output
93
94   foreach test $test_source_files {
95     #Should figure out best way to set the timeout
96     #set timeout 40
97     
98     set filename [file tail $test]
99     set outcome PASS
100     set tmpFile "$filename.tmp"
101
102     #set hasRunline bool to check if testcase has a runline
103     set numLines 0
104
105     # Open the test file and start reading lines
106     set testFileId [ open $test r]
107     set runline ""
108     set PRNUMS ""
109     foreach line [split [read $testFileId] \n] {
110
111       # if its the END. line then stop parsing (optimization for big files)
112       if {[regexp {END.[ *]$} $line match endofscript]} {
113         break
114
115       # if the line is continued, concatenate and continue the loop
116       } elseif {[regexp {RUN: *(.+)(\\)$} $line match oneline suffix]} {
117         set runline "$runline$oneline "
118
119       # if its a terminating RUN: line then do substitution on the whole line
120       # and then save the line.
121       } elseif {[regexp {RUN: *([^&]+)(&&)?} $line match oneline suffix]} {
122         set runline "$runline$oneline"
123         set runline [ substitute $runline $test $tmpFile ]
124         set lines($numLines) $runline
125         set numLines [expr $numLines + 1]
126         set runline ""
127
128       # if its an PR line, save the problem report number
129       } elseif {[regexp {PR([0-9]+)} $line match prnum]} {
130         if {$PRNUMS == ""} {
131           set PRNUMS "PR$prnum"
132         } else {
133           set PRNUMS "$PRNUMS,$prnum"
134         }
135       # if its an XFAIL line, see if we should be XFAILing or not.
136       } elseif {[regexp {XFAIL:[ *](.+)} $line match targets]} {
137         set targets
138
139         #split up target if more then 1 specified
140         foreach target [split $targets ,] {
141           if { [regexp {\*} $target match] } {
142               set outcome XFAIL
143           } elseif { [regexp $target $target_triplet match] } {
144               set outcome XFAIL
145           } elseif { [regexp {llvmgcc(([0-9]+)|([0-9]+[.][0-9]+))} $target match submatch submatch2]  } {
146             if { [regexp ^($submatch)$|^(($submatch)(\.)) $llvmgcc_version match] } {
147               set outcome XFAIL
148             }
149           }
150         }
151       }
152     }
153     
154     # Done reading the script
155     close $testFileId
156     
157     
158     if { $numLines == 0 } {
159       fail "$test: \nDoes not have a RUN line\n"
160     } else {
161       set failed 0
162       for { set i 0 } { $i < $numLines } { set i [ expr $i + 1 ] } {
163         regsub ^.*RUN:(.*) $lines($i) \1 theLine
164         set resultmsg [execOneLine $test $PRNUMS $outcome $i $theLine ]
165         if { $resultmsg != "" } {
166           if { $outcome == "XFAIL" } {
167             xfail "$resultmsg"
168           } else {
169             fail "$resultmsg"
170           }
171           set failed 1
172           break
173         }
174       }
175       if { $failed } {
176         continue
177       } else {
178         if { $PRNUMS != "" } {
179           set PRNUMS " for $PRNUMS"
180         }
181         if { $outcome == "XFAIL" } {
182           xpass "$test$PRNUMS"
183         } else {
184           pass "$test$PRNUMS"
185         }
186       }
187     }
188   }
189 }
190
191 proc llvm_gcc_supports_objc { } {
192     global llvmgcc 
193     catch { set file_h [ open "/tmp/llvm_obj_check.m" w] }
194     set R [ catch { exec $llvmgcc -c "/tmp/llvm_obj_check.m"  -o /dev/null >& /tmp/llvm_obj_check.out } ]
195     set RESULT [ file size "/tmp/llvm_obj_check.out" ]
196     catch { file delete "/tmp/llvm_obj_check.m" }
197     if { $RESULT == 0 } {
198         return 1
199     } else {
200         return 0
201     }
202 }
203