Preliminary version of the C unit tests run-time environment.
[wine] / programs / winetest / runtest
1 #!/usr/bin/perl
2 #
3 # Wrapper script to run tests from inside the Wine tree
4 #
5 # Usage: runtest [options] input_file [perl_args...]
6 #
7
8 use strict;
9
10 sub usage
11 {
12     print STDERR <<EOF;
13
14 Usage: $0 [options] input_file [perl_args...]
15
16 Options:
17     -q       quiet mode
18     -v       verbose mode (can be specified multiple times)
19     -p prog  name of the program to run for C tests
20     -I dir   prepend dir to Perl include path
21     -P name  set the current platform name
22     -M names set the module names to be tested
23     -T dir   set Wine tree top directory (autodetected if not specified)
24
25 EOF
26     exit 1;
27 }
28
29 # default values
30 my $platform = $ENV{WINETEST_PLATFORM};
31 $ENV{WINETEST_DEBUG} ||= 1;
32
33 my $topobjdir;
34 my $infile;
35 my $program;
36 my @include_dirs;
37 my @modules;
38
39 # parse command-line options
40 while ($#ARGV >= 0)
41 {
42     my $arg = shift @ARGV;
43     if ($arg eq "-h") { usage; }
44     if ($arg eq "-p") { $program = shift @ARGV; next; }
45     if ($arg eq "-q") { $ENV{WINETEST_DEBUG} = 0; next; }
46     if ($arg eq "-v") { $ENV{WINETEST_DEBUG}++; next; }
47     if ($arg eq "-P") { $platform = shift @ARGV; next; }
48     if ($arg eq "-M") { push @modules, split /,/, shift @ARGV; next; }
49     if ($arg eq "-I") { push @include_dirs, shift @ARGV; next; }
50     if ($arg eq "-T")
51     {
52         $topobjdir = shift @ARGV;
53         usage unless (-d $topobjdir);
54         next;
55     }
56     $infile = $arg;
57     last;
58 }
59
60 # we must have found an input file
61 usage unless defined($infile);
62
63 if ($infile =~ /\.c$/ && !defined($program))
64 {
65     # set program to the .c file base name if not specified otherwise
66     ($program = $infile) =~ s/\.c$//;
67 }
68
69 # check/detect topobjdir
70 if (defined($topobjdir))
71 {
72     unless (-f $topobjdir . "/server/wineserver")
73     {
74         printf STDERR "Wrong -T argument, %s/server/wineserver does not exist\n", $topobjdir;
75         usage;
76     }
77 }
78 else  # try to detect it automatically
79 {
80     if (-f "./server/wineserver") { $topobjdir = "."; }
81     elsif (-f "../server/wineserver") { $topobjdir = ".."; }
82     elsif (-f "../../server/wineserver") { $topobjdir = "../.."; }
83     elsif (-f "../../../server/wineserver") { $topobjdir = "../../.."; }
84 }
85
86 # check for include/ dir in script source directory and append it to search path
87 my $basedir = $0;
88 if ($basedir =~ /\//) { $basedir =~ s!/[^/]+$!!; }
89 else { $basedir = "."; }
90 if (-d $basedir . "/include") { push @include_dirs, $basedir . "/include"; }
91
92 $ENV{PERL5LIB} = join( ":", @include_dirs, split( ":", $ENV{PERL5LIB} ) );
93 if (@modules)
94 {
95     if (defined($ENV{WINEOPTIONS})) { $ENV{WINEOPTIONS} .= " "; }
96     $ENV{WINEOPTIONS} .= "--dll " . join(',',@modules) . "=b";
97 }
98
99 # set environment variables needed for Wine
100 if (defined($topobjdir))
101 {
102     chop($topobjdir = `cd $topobjdir && pwd`);
103     $ENV{LD_LIBRARY_PATH} = $topobjdir . "/dlls:" . $topobjdir . ":" . $ENV{LD_LIBRARY_PATH};
104     $ENV{WINESERVER} ||= $topobjdir . "/server/wineserver";
105     $ENV{WINELOADER} ||= $topobjdir . "/wine";
106     $ENV{WINETEST_PLATFORM} = $platform || "wine";
107     $ENV{WINEPRELOAD}=($program || ($topobjdir . "/programs/winetest/winetest")) . ".so";
108     # try to exec the wine loader directly; if it fails continue on to normal exec
109     exec $ENV{WINELOADER}, $infile, @ARGV;
110 }
111 else
112 {
113     $ENV{WINETEST_PLATFORM} = $platform || "windows";
114 }
115
116 # and now exec the program
117 $program ||= "winetest";
118 exec $program, $infile, @ARGV;
119 print STDERR "Could not exec $program\n";
120 exit 1;