Use correct compiler component names for objc
[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         # Any other error such as stderr output of a program, or syntax error in
35         # the RUN line.
36         set resultmsg "$test$PRS\nFailed with unknown error (or has stderr output)$errmsg" 
37       }
38       default {
39         set resultmsg "$test$PRS\nFailed with unknown error$errmsg" 
40       }
41     }
42   } 
43   return $resultmsg
44 }
45
46 # This procedure performs variable substitutions on the RUN: lines of a test
47 # cases.
48 proc substitute { line test tmpFile } {
49   global srcroot objroot srcdir objdir subdir target_triplet prcontext 
50   global llvmgcc llvmgxx llvmgcc_version llvmgccmajvers ocamlc
51   global gccpath gxxpath compile_c compile_cxx link shlibext llvmlibsdir
52   set path [file join $srcdir $subdir]
53
54   # Substitute all Tcl variables.
55   set new_line [subst $line ]
56
57   #replace %prcontext with prcontext.tcl (Must replace before %p)
58   regsub -all {%prcontext} $new_line $prcontext new_line
59   #replace %llvmgcc with actual path to llvmgcc
60   regsub -all {%llvmgcc} $new_line "$llvmgcc -emit-llvm -w" new_line
61   #replace %llvmgxx with actual path to llvmg++
62   regsub -all {%llvmgxx} $new_line "$llvmgxx -emit-llvm -w" new_line
63   #replace %compile_cxx with C++ compilation command
64   regsub -all {%compile_cxx} $new_line "$compile_cxx" new_line
65   #replace %compile_c with C compilation command
66   regsub -all {%compile_c} $new_line "$compile_c" new_line
67   #replace %link with C++ link command
68   regsub -all {%link} $new_line "$link" new_line
69   #replace %shlibext with shared library extension
70   regsub -all {%shlibext} $new_line "$shlibext" new_line
71   #replace %ocamlc with ocaml compiler command
72   regsub -all {%ocamlc} $new_line "$ocamlc" new_line
73   #replace %llvmlibsdir with configure library directory
74   regsub -all {%llvmlibsdir} $new_line "$llvmlibsdir" new_line
75   #replace %p with path to source, 
76   regsub -all {%p} $new_line [file join $srcdir $subdir] new_line
77   #replace %s with filename
78   regsub -all {%s} $new_line $test new_line
79   #replace %t with temp filenames
80   regsub -all {%t} $new_line $tmpFile new_line
81   #replace %% with %
82   regsub -all {%%} $new_line % new_line
83   return $new_line
84 }
85
86 # This procedure runs the set of tests for the test_source_files array.
87 proc RunLLVMTests { test_source_files } {
88   global srcroot objroot srcdir objdir subdir target_triplet llvmgcc_version
89   set timeout 60
90
91   set path [file join $objdir $subdir]
92   
93   #Make Output Directory if it does not exist already
94   if { [file exists path] } {
95     cd $path
96   } else {
97     file mkdir $path
98     cd $path
99   }
100   
101   file mkdir Output
102   cd Output
103
104   foreach test $test_source_files {
105     #Should figure out best way to set the timeout
106     #set timeout 40
107     
108     set filename [file tail $test]
109     verbose "ABOUT TO RUN: $filename" 2
110     set outcome PASS
111     set tmpFile "$filename.tmp"
112     
113     # Mark that it should not be XFAIL for this target.
114     set targetPASS 0
115     
116     #set hasRunline bool to check if testcase has a runline
117     set numLines 0
118
119     # Open the test file and start reading lines
120     set testFileId [ open $test r]
121     set runline ""
122     set PRNUMS ""
123     foreach line [split [read $testFileId] \n] {
124
125       # if its the END. line then stop parsing (optimization for big files)
126       if {[regexp {END.[[:space:]]*$} $line match endofscript]} {        
127         break
128
129       # if the line is continued, concatenate and continue the loop
130       } elseif {[regexp {RUN: *(.+)(\\)$} $line match oneline suffix]} {
131         set runline "$runline$oneline "
132
133       # if its a terminating RUN: line then do substitution on the whole line
134       # and then save the line. 
135       } elseif {[regexp {RUN: *(.+)$} $line match oneline suffix]} {
136         set runline "$runline$oneline"
137         set runline [ substitute $runline $test $tmpFile ]
138         set lines($numLines) $runline
139         set numLines [expr $numLines + 1]
140         set runline ""
141
142       # if its an PR line, save the problem report number
143       } elseif {[regexp {PR([0-9]+)} $line match prnum]} {
144         if {$PRNUMS == ""} {
145           set PRNUMS "PR$prnum"
146         } else {
147           set PRNUMS "$PRNUMS,$prnum"
148         }
149       # if its an XFAIL line, see if we should be XFAILing or not.
150       } elseif {[regexp {XFAIL:[ *](.+)} $line match targets]} {
151         set targets
152
153         #split up target if more then 1 specified
154         foreach target [split $targets ,] {
155           if { [regexp {\*} $target match] } {
156               if {$targetPASS != 1} {
157                  set outcome XFAIL
158               }
159           } elseif { [regexp $target $target_triplet match] } {
160               if {$targetPASS != 1} {
161                  set outcome XFAIL
162               }
163           } elseif { [regexp {llvmgcc(([0-9]+)|([0-9]+[.][0-9]+))} $target match submatch submatch2]  } {
164             if { [regexp ^($submatch)$|^(($submatch)(\.)) $llvmgcc_version match] } {
165               if {$targetPASS != 1} {
166                  set outcome XFAIL
167               }
168             }
169           }
170         }
171       } elseif {[regexp {XTARGET:[ *](.+)} $line match targets]} {
172         set targets
173
174         #split up target if more then 1 specified
175         foreach target [split $targets ,] {
176           if { [regexp {\*} $target match] } {
177               set targetPASS 1
178               set outcome PASS
179           } elseif { [regexp $target $target_triplet match] } {
180               set targetPASS 1
181               set outcome PASS
182           } elseif { [regexp {llvmgcc(([0-9]+)|([0-9]+[.][0-9]+))} $target match submatch submatch2]  } {
183             if { [regexp ^($submatch)$|^(($submatch)(\.)) $llvmgcc_version match] } {
184               set targetPASS 1
185               set outcome PASS
186             }
187           }
188         }
189       }
190     }
191     
192     # Done reading the script
193     close $testFileId
194     
195     
196     if { $numLines == 0 } {
197       fail "$test: \nDoes not have a RUN line\n"
198     } else {
199       set failed 0
200       for { set i 0 } { $i < $numLines } { set i [ expr $i + 1 ] } {
201         regsub ^.*RUN:(.*) $lines($i) \1 theLine
202         set resultmsg [execOneLine $test $PRNUMS $outcome $i $theLine ]
203         if { $resultmsg != "" } {
204           if { $outcome == "XFAIL" } {
205             xfail "$resultmsg"
206           } else {
207             fail "$resultmsg"
208           }
209           set failed 1
210           break
211         }
212       }
213       if { $failed } {
214         continue
215       } else {
216         if { $PRNUMS != "" } {
217           set PRNUMS " for $PRNUMS"
218         }
219         if { $outcome == "XFAIL" } {
220           xpass "$test$PRNUMS"
221         } else {
222           pass "$test$PRNUMS"
223         }
224       }
225     }
226   }
227 }
228
229 # This procedure provides an interface to check the LLVMGCC_LANGS makefile
230 # variable to see if llvm-gcc supports compilation of a particular language.
231 proc llvm_gcc_supports { lang } {
232   global llvmgcc llvmgcc_langs
233   # validate the language choices and determine the name of the compiler
234   # component responsible for determining if the compiler has been built.
235   switch "$lang" {
236     ada     { set file gnat1 }
237     c       { set file cc1 }
238     c++     { set file cc1plus }
239     objc    { set file cc1obj }
240     objc++  { set file cc1objplus }
241     fortran { set file f951 }
242     default { return 0 }
243   }
244   foreach supported_lang [split "$llvmgcc_langs" ,] {
245     if { "$lang" == "$supported_lang" } {
246       # FIXME: Knowing it is configured is not enough. We should do two more
247       # checks here. First, we need to run llvm-gcc -print-prog-name=$file to 
248       # get the path to the compiler. If we don't get a path, the language isn't
249       # properly configured or built. If we do get a path, we should check to 
250       # make sure that it is executable and perhaps even try executing it.
251       return 1;
252     }
253   }
254   return 0;
255 }
256
257 # This procedure provides an interface to check the TARGETS_TO_BUILD makefile
258 # variable to see if a particular target has been configured to build. This
259 # helps avoid running tests for targets that aren't available.
260 proc llvm_supports_target { tgtName } {
261   global TARGETS_TO_BUILD
262   foreach target [split $TARGETS_TO_BUILD] {
263     if { [regexp $tgtName $target match] } {
264       return 1
265     }
266   }
267   return 0
268 }