581c91f09b1f3e928d208b7311a8b76e9f83b2ab
[oota-llvm.git] / test / lib / llvm.exp
1 proc execOneLine { test 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     switch "$code" {
8       CHILDSTATUS {
9         set status [lindex $::errorCode 2]
10         if { $status ne 0 } {
11           set resultmsg "$test: exit($status)\nwhile running: $line\n$errmsg"
12         }
13       }
14       CHILDKILLED {
15         set signal [lindex $::errorCode 2]
16         set resultmsg "$test: signal($signal)\nwhile running: $line\n$errmsg"
17       }
18       CHILDSUSP {
19         set signal [lindex $::errorCode 2]
20         set resultmsg "$test: suspend($signal)\nwhile running: $line\n$errmsg"
21       }
22       POSIX {
23         set posixNum [lindex $::errorCode 1]
24         set posixMsg [lindex $::errorCode 2]
25         set resultmsg "$test: posix($posixNum)\n$posixMsg\nwhile running: $line\n$errmsg"
26       }
27       NONE {
28       }
29       default {
30       }
31     }
32   } 
33   return $resultmsg
34 }
35
36 proc substitute { line test tmpFile } {
37   global srcroot objroot srcdir objdir subdir target_triplet prcontext 
38   global llvmgcc llvmgxx global llvmgcc_version llvmgccmajvers 
39   global gccpath gxxpath compile_c compile_cxx link shlibext llvmlibsdir
40
41   set new_line $line
42   #replace %prcontext with prcontext.tcl (Must replace before %p)
43   regsub -all {%prcontext} $new_line $prcontext new_line
44   #replace %llvmgcc with actual path to llvmgcc
45   regsub -all {%llvmgcc} $new_line "$llvmgcc -emit-llvm" new_line
46   #replace %llvmgxx with actual path to llvmg++
47   regsub -all {%llvmgxx} $new_line "$llvmgxx -emit-llvm" new_line
48   #replace %compile_c with C compilation command
49   regsub -all {%compile_c} $new_line "$compile_c" new_line
50   #replace %compile_cxx with C++ compilation command
51   regsub -all {%compile_cxx} $new_line "$compile_cxx" new_line
52   #replace %link with C++ link command
53   regsub -all {%link} $new_line "$link" new_line
54   #replace %shlibext with shared library extension
55   regsub -all {%shlibext} $new_line "$shlibext" new_line
56   #replace %llvmlibsdir with configure library directory
57   regsub -all {%llvmlibsdir} $new_line "$llvmlibsdir" new_line
58   #replace %p with path to source, 
59   regsub -all {%p} $new_line [file join $srcdir $subdir] new_line
60   #replace %s with filename
61   regsub -all {%s} $new_line $test new_line
62   #replace %t with temp filenames
63   regsub -all {%t} $new_line [file join Output $tmpFile] new_line
64   return $new_line
65 }
66
67 proc llvm-runtest { programs } {
68   global srcroot objroot srcdir objdir subdir target_triplet
69   set timeout 60
70
71   set path [file join $objdir $subdir]
72   
73   #Make Output Directory if it does not exist already
74   if { [file exists path] } {
75     cd $path
76   } else {
77     file mkdir $path
78     cd $path
79   }
80   
81   file mkdir Output
82
83   foreach test $programs {
84     #Should figure out best way to set the timeout
85     #set timeout 40
86     
87     set filename [file tail $test]
88     set outcome PASS
89     set tmpFile "$filename.tmp"
90
91     #set hasRunline bool to check if testcase has a runline
92     set numLines 0
93
94     # Open the test file and start reading lines
95     set testFileId [ open $test r]
96     set runline ""
97     foreach line [split [read $testFileId] \n] {
98
99       #see if this is our run line
100       if {[regexp {END.[ *]$} $line match endofscript]} {
101         break
102       } elseif {[regexp {RUN: *([^\\]+)(\\)} $line match oneline suffix]} {
103         set runline "$runline$oneline "
104       } elseif {[regexp {RUN: *([^&]+)(&&)?} $line match oneline suffix]} {
105         set runline "$runline$oneline"
106         set runline [ substitute $runline $test $tmpFile ]
107         set lines($numLines) $runline
108         set numLines [expr $numLines + 1]
109         set runline ""
110       } elseif {[regexp {XFAIL:[ *](.+)} $line match targets]} {
111         set targets
112
113         #split up target if more then 1 specified
114         foreach target [split $targets ,] {
115           if { [regexp {\*} $target match] } {
116               set outcome XFAIL
117           } elseif { [regexp $target $target_triplet match] } {
118               set outcome XFAIL
119           } elseif { [regexp {llvmgcc(([0-9]+)|([0-9]+[.][0-9]+))} $target match submatch submatch2]  } {
120             if { [regexp ^($submatch)$|^(($submatch)(\.)) $llvmgcc_version match] } {
121               set outcome XFAIL
122             }
123           }
124         }
125       }
126     }
127     
128     # Done reading the script
129     close $testFileId
130     
131     
132     if { $numLines == 0 } {
133       fail "$test: \nDoes not have a RUN line\n"
134     } else {
135       set failed 0
136       for { set i 0 } { $i < $numLines } { set i [ expr $i + 1 ] } {
137         regsub ^.*RUN:(.*) $lines($i) \1 theLine
138         set theLine [subst $theLine ]
139         set resultmsg [execOneLine $test $outcome $i $theLine ]
140         if { $resultmsg != "" } {
141           if { $outcome == "XFAIL" } {
142             xfail "$resultmsg"
143           } else {
144             fail "$resultmsg"
145           }
146           set failed 1
147           break
148         }
149       }
150       if { !$failed } {
151         if { $outcome == "XFAIL" } {
152           xpass "$test"
153         } else {
154           pass "$resultmsg"
155         }
156       }
157     }
158   }
159 }